I have tried to follow the code from [https://stackoverflow.com/questions/63237565/extracting-values-from-one-of-multiple-prism-rasters-in-r][1] but I keep running into multiple errors. I am also trying to extra daily precipitation rather than monthly temperature.
dates <- seq(as.Date("2019-01-01"),as.Date("2019-12-31"), by="days")
library(prism)
for (d in unique(dates)) {
dir.create(d, FALSE)
options(prism.path = d)
get_prism_dailys(type = "ppt", dates = d)
}
#Error in dir.create(d, FALSE) : invalid 'path' argument
locs <- foo.df[,5:4]
head(locs)
length(locs)
locs$Longitude<-as.numeric(locs$Longitude)
locs$Latitude<-as.numeric(locs$Latitude)
#Keep things simple and put the results in a list first
temps <- list()
for(i in 1:length(dates)) {
options(prism.path = dates[i])
climate_data <- prism_stack(ls_prism_data())
temps[[i]] <- extract(climate_data, locs)
}
#Error in list.files(getOption("prism.path")) : invalid 'path' argument
#In addition: Warning messages:
# 1: 'prism_stack' is deprecated.
#Use '`pd_stack()`' instead.
#See help("Deprecated")
#2: 'ls_prism_data' is deprecated.
#Use '`prism_archive_ls()`' instead.
#See help("Deprecated")
I'm not sure what I need to change to a) make this code work for days vs years and b) how to convert this script from the deprecated code to the newest code.
Thanks for your help
Let's go step by step.
library(prism)
#> Be sure to set the download folder using `prism_set_dl_dir()`.
dates <- seq(as.Date("2019-01-01"),as.Date("2019-01-02"), by="days")
unique(dates)
#> [1] "2019-01-01" "2019-01-02"
Till now everything is fine.
for (d in unique(dates)) {
dir.create(d, FALSE)
options(prism.path = d)
get_prism_dailys(type = "ppt", dates = d)
}
#Error in dir.create(d, FALSE) : invalid 'path' argument
Why?? Let's see what's wrong:
for (d in unique(dates)) {
print(d)
}
#> [1] 17897
#> [1] 17898
Ah, d in dates
is converted to integer
, and dir.create()
expects an character
. Let's change it providing the dates as.character
:
for (d in 1:length(dates)) {
dir.create(as.character(dates[d]), TRUE)
options(prism.path = as.character(dates[d]))
get_prism_dailys(type = "ppt", dates = dates[d])
}
#> | 0% |
|======================================================================| 100%
#> | 0% |
|======================================================================| 100%
Seems data was downloaded; to be sure we can check it right now:
list.files(as.character(dates[1]))
#> [1] "PRISM_ppt_stable_4kmD2_20190101_bil"
#> [2] "PRISM_ppt_stable_4kmD2_20190101_bil.zip"
As you haven's shared details of `foo.df`` I will skip this part:
locs <- foo.df[,5:4]
head(locs)
length(locs)
locs$Longitude<-as.numeric(locs$Longitude)
locs$Latitude<-as.numeric(locs$Latitude)
The rest is pretty simple: below error is the same as previous, path
has to be character
#Error in list.files(getOption("prism.path")) : invalid 'path' argument
So, let's supply as.character
as needed. The deprecated function should be replaced by the new ones, like:
#Keep things simple and put the results in a list first
temps <- list()
for(i in 1:length(dates)) {
options(prism.path = as.character(dates[i]))
climate_data <- pd_stack(prism_archive_ls())
temps[[i]] <- extract(climate_data, locs)
}
Regards, Grzegorz