From 3130142a06b956daa8550033a4cc28253260da68 Mon Sep 17 00:00:00 2001 From: Beni Stocker Date: Mon, 17 Jun 2024 14:00:00 +0200 Subject: [PATCH] fixing noleap treamtent --- R/helpers.R | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/R/helpers.R b/R/helpers.R index a9b6bd4..cff33c9 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -215,14 +215,14 @@ nclist_to_df_byfil <- function( if (noleap){ # if calendar of netcdf time dimension is no-leap, then do... # determine last year in file. - last_date <- basedate + lubridate::days(floor(time[length(time)])) - lubridate::days(1) + last_date <- basedate + lubridate::days(floor(df$time[length(df$time)])) - lubridate::days(1) last_year <- lubridate::year(last_date) # "manually" remove additional day in leap years df_noleap <- tibble( time = seq(from = basedate, to = lubridate::ymd(paste0(last_year , "-12-31")), by = "days") ) |> - dplyr::mutate(month = lubridate::month(date), mday = lubridate::mday(time)) |> + dplyr::mutate(month = lubridate::month(time), mday = lubridate::mday(time)) |> dplyr::filter(!(month == 2 & mday == 29)) if (res_time == "mon"){ @@ -231,6 +231,18 @@ nclist_to_df_byfil <- function( dplyr::filter(mday == 15) } + # repeat data frame with dates and without leap years and stack along rows + nlat <- length(unique(df$lat)) + df_noleap <- replicate( + nlat, + df_noleap, + simplify = FALSE) |> + dplyr::bind_rows() + + # re-organise df to vary fastest by time (not lat) + df <- df |> + dplyr::arrange(lat) + # at this stage, number of rows in df_noleap should correspond to length of the time dimension in netcdf file df <- df |> dplyr::select(-!!timedimnam) |>