VeruGHub / easyclimate

Easy access to high-resolution daily climate data for Europe
https://verughub.github.io/easyclimate/
GNU General Public License v3.0
46 stars 1 forks source link

Specifying a different period for each site/row #56

Open Pakillo opened 3 weeks ago

Pakillo commented 3 weeks ago

A student came to me with a problem where he had about a hundred sites and wanted to get climate for a specific period for each site. Currently, if you provide a 'period` vector of the same length as the number of sites (i.e. nrow(coords)), the periods will be recycled, so that each site gets the climate for the whole period vector.

I think it would be nice to be able to provide a data frame with sites/coords and a same-length vector of 'period' desired for each site. But I don't know how easy/feasible that is given current use of 'period'.

Reprex:

library(easyclimate)

df <- data.frame(lon = c(-5, -5), 
                 lat = c(37, 38), 
                 period = c("2010-01-01",
                            "2020-01-01"))
df
#>   lon lat     period
#> 1  -5  37 2010-01-01
#> 2  -5  38 2020-01-01

out <- get_daily_climate(df, period = df$period, check_connection = FALSE)
#> 
#> Downloading Prcp data... This process might take several minutes
out  
#>   ID_coords     period lon lat       date Prcp
#> 1         1 2010-01-01  -5  37 2010-01-01    0
#> 2         1 2010-01-01  -5  37 2020-01-01    0
#> 3         2 2020-01-01  -5  38 2010-01-01    0
#> 4         2 2020-01-01  -5  38 2020-01-01    0

Created on 2024-09-25 with reprex v2.1.1

Pakillo commented 3 weeks ago

By now this could be a quick solution to get climate for a specific period for each site:

df <- data.frame(lon = c(-5, -5), 
                 lat = c(37, 38), 
                 period = c("2010-01-01",
                            "2020-01-01"))

df.list <- split(df, 1:nrow(df))

clim_by_row <- function(row, climvar = "Prcp") {
  easyclimate::get_daily_climate(row, period = row$period, 
                                 climatic_var = climvar, 
                                 check_connection = FALSE)
}

output <- lapply(df.list, clim_by_row, climvar = "Prcp")
#> 
#> Downloading Prcp data... This process might take several minutes
#> 
#> Downloading Prcp data... This process might take several minutes
do.call("rbind", output)
#>   ID_coords     period lon lat       date Prcp
#> 1         1 2010-01-01  -5  37 2010-01-01    0
#> 2         1 2020-01-01  -5  38 2020-01-01    0

Created on 2024-09-25 with reprex v2.1.1