根据不同的参考日期计算朱利安日。

huangapple go评论116阅读模式
英文:

Calculate Julian Day based on a different reference day

问题

我的目的是基于一个新的参考日期来计算儒略日,也就是说,现在儒略日1对应6月1日,儒略日366对应1月1日。

大家好,

我一直在尝试计算儒略日,其中参考日期不再是1月1日。事实上,我想从我的数据中提取儒略日,其中6月1日对应1,而1月1日对应366。

我尝试将以1月1日为参考的儒略日加上152,但我的数据超过了366,我认为这不是精确的解决方法。

我真的很困惑,知道这涉及到数学序列,但我不太熟悉。

如果你能帮助我,我将非常感激 根据不同的参考日期计算朱利安日。

英文:

My purpose is to calculate Julian Day based on a new reference, meaning that Julian Day 1 now will correspond to June 1st and Julian Day 366 to January 1st.

Hello everyone,

I've been struggling to calculate Julian Day where the reference day is not January 1st anymore.
Indeed, I want to extract from my data Julian Day where June 1st correspond to 1 and January 1st to 366.

I tried to add 152 to Julian Day calculated with January 1st as a reference however my data than exceed 366 and I don't think it's the exact solution.

I'm really confused and know it's about mathematical suite but I'm not comfortable with it.

If you could help me I would be very grateful 根据不同的参考日期计算朱利安日。

答案1

得分: 1

在假设您指的是5月31日而不是1月1日的情况下,可以使用以下可能有点复杂的方法开始:

set.seed(42)
dates <- sort(as.Date("2020-06-01") + c(-1, 0, sample(1000, size=5)))
dates
# [1] "2020-05-31" "2020-06-01" "2020-08-14" "2020-11-01" "2021-04-18" "2021-12-14" "2023-02-23"
june1s <- as.POSIXlt(paste0(substring(dates, 1, 4), "-06-01"))
june1s
# [1] "2020-06-01 EDT" "2020-06-01 EDT" "2020-06-01 EDT" "2020-06-01 EDT" "2021-06-01 EDT" "2021-06-01 EDT"
# [7] "2023-06-01 EDT"
june1s$year <- june1s$year - (as.Date(june1s) > dates)
dates - as.Date(june1s) + 1L
# Time differences in days
# [1] 366   1  75 154 322 197 268
as.numeric(dates - as.Date(june1s) + 1L, units = "days")
# [1] 366   1  75 154 322 197 268

我包括了05-3106-01,以演示以6月1日为基础的儒略年的两个极端。最后一步(as.numeric)主要是我的一个防御性习惯:difftime和类似的函数经常会根据差异的规模自动更改单位,这可能会有问题;使用units="days"可以强制指定单位。(也有其他处理方式。)

您可以使用以下函数化这个过程:

myjulian <- function(dates, origin = "-01-01") {
  if (!inherits(dates, "Date")) dates <- as.Date(dates)
  origins <- as.POSIXlt(paste0(substring(dates, 1, 4), origin))
  origins$year <- origins$year - (as.Date(origins) > dates)
  as.numeric(dates - as.Date(origins) + 1L, units = "days")
}

并进行简单的验证:

myjulian("2023-06-01") # 以1月1日为基准
# [1] 152
format(as.Date("2023-06-01"), format = "%j")
# [1] "152"

myjulian("2023-06-01", origin = "-06-01")
# [1] 1
英文:

Under the assumption that you meant May 31 instead of Jan 1, then a perhaps-convoluted method could start with:

set.seed(42)
dates &lt;- sort(as.Date(&quot;2020-06-01&quot;) + c(-1, 0, sample(1000, size=5)))
dates
# [1] &quot;2020-05-31&quot; &quot;2020-06-01&quot; &quot;2020-08-14&quot; &quot;2020-11-01&quot; &quot;2021-04-18&quot; &quot;2021-12-14&quot; &quot;2023-02-23&quot;
june1s &lt;- as.POSIXlt(paste0(substring(dates, 1, 4), &quot;-06-01&quot;))
june1s
# [1] &quot;2020-06-01 EDT&quot; &quot;2020-06-01 EDT&quot; &quot;2020-06-01 EDT&quot; &quot;2020-06-01 EDT&quot; &quot;2021-06-01 EDT&quot; &quot;2021-06-01 EDT&quot;
# [7] &quot;2023-06-01 EDT&quot;
june1s$year &lt;- june1s$year - (as.Date(june1s) &gt; dates)
dates - as.Date(june1s) + 1L
# Time differences in days
# [1] 366   1  75 154 322 197 268
as.numeric(dates - as.Date(june1s) + 1L, units = &quot;days&quot;)
# [1] 366   1  75 154 322 197 268

I included 05-31 and 06-01 to demonstrate the two extremes of the June-1-based-julian-year. The last step (as.numeric) is mostly a defensive habit of mine: difftime and similar functions often auto-change the units based on the scale of the difference, and that might be problematic; using units=&quot;days&quot; forces this. (There are other ways to deal with this, as well.)

One can functionize this with:

myjulian &lt;- function(dates, origin = &quot;-01-01&quot;) {
  if (!inherits(dates, &quot;Date&quot;)) dates &lt;- as.Date(dates)
  origins &lt;- as.POSIXlt(paste0(substring(dates, 1, 4), origin))
  origins$year &lt;- origins$year - (as.Date(origins) &gt; dates)
  as.numeric(dates - as.Date(origins) + 1L, units = &quot;days&quot;)
}

With brief validation:

myjulian(&quot;2023-06-01&quot;) # jan-1
# [1] 152
format(as.Date(&quot;2023-06-01&quot;), format = &quot;%j&quot;)
# [1] &quot;152&quot;

myjulian(&quot;2023-06-01&quot;, origin = &quot;-06-01&quot;)
# [1] 1

huangapple
  • 本文由 发表于 2023年6月2日 01:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定