使用非公历从纪元转换
Converting from epochs using non-Gregorian calendars
我正在处理一系列气候模型输出(具体来说,CMIP5 models)。这些是带有时间戳的温度、风等的 netcdfs。
他们都使用 days since YYYY-mm-dd 00:00:00
UTC 约定。我一直在使用 lubridate
:
转换为更简单的日期(不是日期时间)对象
library(tidyverse)
input$date.utc =
ymd_hms('0001-01-01 00:00:00', tz = 'UTC') +
days(floor(input$time))
我遇到了两个问题。一是每个模型都有不同的时代。这很容易修复。另一个更棘手的问题是并非所有模型都使用公历。有些使用 365 天变化,没有闰年。
我看不到任何在 lubridate
函数中指定非公历的方法。这可能吗?
我在lubridate
中找不到这个功能,所以我写了一个函数来至少计算日期向量的每个元素与给定纪元之间的闰日数:
# count_leap_days: returns an integer for the number of leap days between a
# supplied vector of dates and a fixed epoch
count_leap_days <- function(dates, epoch = ymd('1850-01-01'), proleptic = FALSE)
{
require(lubridate)
# check input
if (!is(epoch, 'Date') | !is(dates, 'Date'))
{
stop('count_leap_days: both arguments must be Date objects.')
}
if (any(dates <= epoch))
{
stop('count_leap_days: dates should all be later than epoch.')
}
if (proleptic = FALSE & epoch < ymd('1582-10-15'))
{
message('count_leap_days: ',
'no leap days before 1582-10-15 unless proleptic = TRUE.')
epoch = ymd('1582-10-15')
}
# get the year range
# exclude start (end) years if they begin after (start before) feb 29
y.epoch = year(epoch) +
ifelse(epoch >= ymd(paste0(year(epoch), '-03-01')), 1, 0)
y.dates = year(dates) -
ifelse(dates <= ymd(paste0(year(dates), '-02-28')), 1, 0)
span = y.dates - y.epoch + 1
# a year is a leap year if it's:
# - divisble by 4. but
# - NOT if it's also divisible by 100, unless
# - it's also divisible by 400.
# all years div. by 4 are also div. by 100, and
# all years div. by 100 are also div. by 400.
# hence, total days = (div. by 4) - (div. by 100) + (div. by 400)
div4 = span %/% 4 +
ifelse(
(y.epoch %% 4) %in% c(0, (4 - (span %% 4) - 1):(4 - 1)) &
(y.dates %% 4) %in% 0:(span %% 4 - 1),
1, 0)
div100 = span %/% 100 +
ifelse(
(y.epoch %% 100) %in% c(0, (100 - (span %% 100) - 1):(100 - 1)) &
(y.dates %% 100) %in% 0:(span %% 100 - 1),
1, 0)
div400 = span %/% 400 +
ifelse(
(y.epoch %% 400) %in% c(0, (400 - (span %% 400) - 1):(400 - 1)) &
(y.dates %% 400) %in% 0:(span %% 400 - 1),
1, 0)
return(div4 - div100 + div400)
}
有了这个,我只需添加缺少的闰日数,就可以将 365 天日历转换为公历:
input$date.utc = input$date.utc +
count_leap_days(input$date.utc, epoch = epoch)
我正在处理一系列气候模型输出(具体来说,CMIP5 models)。这些是带有时间戳的温度、风等的 netcdfs。
他们都使用 days since YYYY-mm-dd 00:00:00
UTC 约定。我一直在使用 lubridate
:
library(tidyverse)
input$date.utc =
ymd_hms('0001-01-01 00:00:00', tz = 'UTC') +
days(floor(input$time))
我遇到了两个问题。一是每个模型都有不同的时代。这很容易修复。另一个更棘手的问题是并非所有模型都使用公历。有些使用 365 天变化,没有闰年。
我看不到任何在 lubridate
函数中指定非公历的方法。这可能吗?
我在lubridate
中找不到这个功能,所以我写了一个函数来至少计算日期向量的每个元素与给定纪元之间的闰日数:
# count_leap_days: returns an integer for the number of leap days between a
# supplied vector of dates and a fixed epoch
count_leap_days <- function(dates, epoch = ymd('1850-01-01'), proleptic = FALSE)
{
require(lubridate)
# check input
if (!is(epoch, 'Date') | !is(dates, 'Date'))
{
stop('count_leap_days: both arguments must be Date objects.')
}
if (any(dates <= epoch))
{
stop('count_leap_days: dates should all be later than epoch.')
}
if (proleptic = FALSE & epoch < ymd('1582-10-15'))
{
message('count_leap_days: ',
'no leap days before 1582-10-15 unless proleptic = TRUE.')
epoch = ymd('1582-10-15')
}
# get the year range
# exclude start (end) years if they begin after (start before) feb 29
y.epoch = year(epoch) +
ifelse(epoch >= ymd(paste0(year(epoch), '-03-01')), 1, 0)
y.dates = year(dates) -
ifelse(dates <= ymd(paste0(year(dates), '-02-28')), 1, 0)
span = y.dates - y.epoch + 1
# a year is a leap year if it's:
# - divisble by 4. but
# - NOT if it's also divisible by 100, unless
# - it's also divisible by 400.
# all years div. by 4 are also div. by 100, and
# all years div. by 100 are also div. by 400.
# hence, total days = (div. by 4) - (div. by 100) + (div. by 400)
div4 = span %/% 4 +
ifelse(
(y.epoch %% 4) %in% c(0, (4 - (span %% 4) - 1):(4 - 1)) &
(y.dates %% 4) %in% 0:(span %% 4 - 1),
1, 0)
div100 = span %/% 100 +
ifelse(
(y.epoch %% 100) %in% c(0, (100 - (span %% 100) - 1):(100 - 1)) &
(y.dates %% 100) %in% 0:(span %% 100 - 1),
1, 0)
div400 = span %/% 400 +
ifelse(
(y.epoch %% 400) %in% c(0, (400 - (span %% 400) - 1):(400 - 1)) &
(y.dates %% 400) %in% 0:(span %% 400 - 1),
1, 0)
return(div4 - div100 + div400)
}
有了这个,我只需添加缺少的闰日数,就可以将 365 天日历转换为公历:
input$date.utc = input$date.utc +
count_leap_days(input$date.utc, epoch = epoch)