Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying a different period for each site/row #56

Open
Pakillo opened this issue Sep 25, 2024 · 1 comment
Open

Specifying a different period for each site/row #56

Pakillo opened this issue Sep 25, 2024 · 1 comment

Comments

@Pakillo
Copy link
Collaborator

Pakillo commented Sep 25, 2024

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
Copy link
Collaborator Author

Pakillo commented Sep 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant