Skip to content

Commit

Permalink
fixing noleap treamtent
Browse files Browse the repository at this point in the history
  • Loading branch information
stineb committed Jun 17, 2024
1 parent 2bed225 commit 3130142
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"){
Expand All @@ -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) |>
Expand Down

0 comments on commit 3130142

Please sign in to comment.