mikejohnson51 / climateR

An R 📦 for getting point and gridded climate data by AOI
https://mikejohnson51.github.io/climateR/
MIT License
168 stars 40 forks source link

Point based extraction #20

Closed mikejohnson51 closed 3 years ago

mikejohnson51 commented 3 years ago

This was sent as an email:


I am working on modeling recharge relative to our state groundwater level network, and it is nice to have the ability to pull gridded data such as the sources that you have used in this package.

Can you possibly send me an example for pulling time series data for a point location? Alternatively, if I retrieve for example data for the whole state as a raster with time series, then extract from that?

Here's what I caurrently have:

library(climateR)
library(AOI)
precip = aoi_get("41.59313330, -72.42642220") %>%  getPRISM(param = 'prcp', startDate = "2019-10-01","2020-09-30")

returns this Error in { : task 353 failed - "NetCDF: file not found"

If I retrieve for GridMET, I get Error in { : task 1 failed - "NetCDF: Access failure"

assuming I am missing a package dependency for NetCDF?

mikejohnson51 commented 3 years ago

A few things of note on this issue.

(1) Creating an sf point is the easiest way to define a point by lat long (for now) (2) Both PRISM and GRIDMET work for this type of request but GridMET is much faster. This is not because of the coarser grid (4km vs 800m) but because of the way the data is structured on each server.

To illustrate these points:

library(climateR)
library(AOI)
library(sf)
library(ggplot2)

pt = st_point(c(-72.42642220, 41.59313330)) %>% 
  st_sfc(crs = 4326) %>% 
  st_sf()

system.time({
prism = getPRISM(pt, 
                 param = 'prcp', 
                 startDate = "2019-10-01",
                 endDate = "2020-09-30")
})

#>    user  system elapsed 
#>   1.952   1.096  58.066

system.time({
gridmet = getGridMET(pt, 
                     param = 'prcp', 
                     startDate = "2019-10-01",
                     endDate = "2020-09-30")
})
#>    user  system elapsed 
#>   0.025   0.009   0.387

ggplot() + 
  geom_line(data = prism, aes(x = date, y = prcp), col = 'red', lwd = 1) + 
  geom_line(data = gridmet, aes(x = date, y = prcp), col = "blue") + 
  theme_linedraw() + 
  labs(title = "Red = PRISM; Blue = GRIDMET")

Created on 2020-10-19 by the reprex package (v0.3.0)

I hope it is useful.

jmusgs commented 3 years ago

Thank you very much, this is very useful!

mikejohnson51 commented 3 years ago

Awesome! I am glad it helped :)