generated from geco-bern/R_proj_template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from geco-bern/update-for-map2tidy-v2
Update map2tidy v2.0.0
- Loading branch information
Showing
12 changed files
with
307 additions
and
351 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,43 @@ | ||
cwd_annmax_byilon <- function( | ||
ilon, | ||
indir, | ||
cwd_annmax_byLON <- function( | ||
filnam, | ||
outdir, | ||
fileprefix | ||
overwrite | ||
){ | ||
|
||
# function to get annual maximum | ||
get_annmax <- function(df){ | ||
df |> | ||
mutate(year = lubridate::year(time)) |> | ||
group_by(year) |> | ||
summarise(evspsbl_cum = max(evspsbl_cum)) | ||
} | ||
# # write (complemented) data to file. Give it some meaningful name and the index counter | ||
outpath <- file.path(outdir, basename(filnam) %>% gsub(".rds","_ANNMAX.rds", .)) | ||
if (file.exists(outpath) && !overwrite){ | ||
|
||
# read evapotranspiration file tidy | ||
filnam <- list.files( | ||
indir, | ||
pattern = paste0(fileprefix, "_", ilon, ".rds"), | ||
full.names = TRUE | ||
) | ||
df <- readr::read_rds(filnam) | ||
|
||
# apply annual maximum function | ||
out <- df |> | ||
mutate(data = purrr::map( | ||
data, | ||
~get_annmax(.) | ||
)) | ||
|
||
# write (complemented) data to file. Give it some meaningful name and the index counter | ||
path <- paste0(outdir, "/", fileprefix, "_", ilon, "_ANNMAX.rds") | ||
message( | ||
paste0( | ||
"Writing file ", path, " ..." | ||
) | ||
) | ||
readr::write_rds( | ||
out, | ||
path | ||
# don't do anything | ||
return(paste0("File exists already: ", outpath)) | ||
|
||
} else { | ||
# read from file that contains tidy data for a single longitudinal band | ||
|
||
# read evapotranspiration file tidy | ||
df_evap <- readr::read_rds(filnam) | ||
|
||
# function to apply to get annual maximum: | ||
get_annmax <- function(df_of_one_coordinate){ | ||
df_of_one_coordinate |> | ||
mutate(year = lubridate::year(datetime)) |> | ||
group_by(year) |> | ||
summarise(evspsbl_cum = max(evspsbl_cum)) | ||
} | ||
|
||
# apply annual maximum function | ||
out <- df_evap |> | ||
# apply the custom function on the time series data frame separately for each gridcell. | ||
dplyr::mutate(data = purrr::map(data, ~get_annmax(.))) | ||
|
||
message( | ||
paste0( | ||
"Writing file ", outpath, " ..." | ||
) | ||
) | ||
readr::write_rds(out, outpath) | ||
|
||
# don't return data - it's written to file | ||
# don't return data - it's written to file | ||
return(paste0("Written results to: ", outpath)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,57 @@ | ||
cwd_byilon <- function( | ||
ilon, | ||
indir, | ||
cwd_byLON <- function( | ||
filnam, | ||
outdir, | ||
fileprefix | ||
){ | ||
|
||
# load function that will be applied to time series | ||
source(paste0(here::here(), "/R/my_cwd.R")) | ||
|
||
# read from file that contains tidy data for a single longitudinal band | ||
|
||
# read evapotranspiration file tidy | ||
filnam <- list.files( | ||
indir, | ||
pattern = paste0("evspsbl_mon_CESM2_historical_r1i1p1f1_native_ilon_", ilon, ".rds"), | ||
full.names = TRUE | ||
) | ||
df_evap <- readr::read_rds(filnam) | ||
|
||
# read other required files (precipitation, temperature, ... | ||
|
||
# # merge all such that monthly data is repeated for each day within month | ||
# df <- df_prec |> # one of the daily data frames | ||
# tidyr::unnest(data) |> # must unnest to join by date | ||
# left_join( | ||
# df_evap |> # one of the monthly data frames | ||
# tidyr::unnest(data), | ||
# by = join_by(year, month) | ||
# ) | ||
|
||
# for demo only | ||
df <- df_evap |> | ||
tidyr::unnest(data) | ||
|
||
out <- df |> | ||
|
||
# Uncomment code below to nest data by gridcell, if not already nested. | ||
# group data by gridcells and wrap time series for each gridcell into a new | ||
# column, by default called 'data'. | ||
dplyr::group_by(lon, lat) |> | ||
tidyr::nest() |> | ||
|
||
# apply the custom function on the time series data frame separately for | ||
# each gridcell. | ||
dplyr::mutate(data = purrr::map(data, ~my_cwd(.))) | ||
overwrite | ||
){ | ||
|
||
# prepare output | ||
# write (complemented) data to file. Give it some meaningful name and the index counter | ||
path <- paste0(outdir, "/", fileprefix, "_", ilon, ".rds") | ||
message( | ||
paste0( | ||
"Writing file ", path, " ..." | ||
) | ||
) | ||
readr::write_rds( | ||
out, | ||
path | ||
fileprefix <- "CWD_result" | ||
LON_string <- gsub("^.*?(LON_[0-9.+-]*).rds$", "\\1", basename(filnam)) | ||
outpath <- file.path(outdir, paste0(fileprefix, "_", LON_string, ".rds")) | ||
if (file.exists(outpath) && !overwrite){ | ||
|
||
# don't do anything | ||
return(paste0("File exists already: ", outpath)) | ||
|
||
} else { | ||
# read from file that contains tidy data for a single longitudinal band | ||
|
||
# read evapotranspiration file tidy | ||
df_evap <- readr::read_rds(filnam) | ||
|
||
# read other required files (precipitation, temperature, ... | ||
|
||
# # merge all such that monthly data is repeated for each day within month | ||
# df <- df_prec |> # one of the daily data frames | ||
# tidyr::unnest(data) |> # must unnest to join by date | ||
# left_join( | ||
# df_evap |> # one of the monthly data frames | ||
# tidyr::unnest(data), | ||
# by = join_by(year, month) | ||
# ) | ||
|
||
# function to apply: | ||
## my_cwd: NOTE this is defined in outer scope and accessible by closure | ||
|
||
out <- df_evap |> | ||
# # Uncomment code below to nest data by gridcell, if not already nested. | ||
# # group data by gridcells and wrap time series for each gridcell into a new | ||
# # column, by default called 'data'. | ||
# dplyr::group_by(lon, lat) |> tidyr::nest() |> | ||
|
||
# apply the custom function on the time series data frame separately for each gridcell. | ||
dplyr::mutate(data = purrr::map(data, ~my_cwd(.))) # NOTE: this uses the closure | ||
|
||
# write (complemented) data to file. | ||
message( | ||
paste0( | ||
"Writing file ", outpath, " ..." | ||
) | ||
) | ||
readr::write_rds(out, outpath) | ||
|
||
# don't return data - it's written to file | ||
# don't return data - it's written to file | ||
return(paste0("Written results to: ", outpath)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.