Skip to content

Commit

Permalink
implement simplified function for dstime_handling_check
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmigueles committed Dec 17, 2024
1 parent d18cf72 commit 82a0f4c
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions R/g.sib.det.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,18 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12),
# So, out of convenience I keep the object name SPTE.
dstime_handling_check = function(tmpTIME = tmpTIME, spt_estimate = spt_estimate, tz = c(),
calc_SPTE_end = c(), calc_SPTE_start = c()) {
time_SPTE_start = iso8601chartime2POSIX(tmpTIME[spt_estimate$SPTE_start], tz = tz)
time_SPTE_end = iso8601chartime2POSIX(tmpTIME[spt_estimate$SPTE_end], tz = tz)
time_SPTE_end_hr = as.numeric(format(time_SPTE_end, format = "%H"))
time_SPTE_start_hr = as.numeric(format(time_SPTE_start, format = "%H"))
t0 = as.numeric(format(iso8601chartime2POSIX(tmpTIME[1], tz = tz), format = "%H"))
# redefinition of t1 in case window ends some epochs before 12:00:00
t1hms = format(iso8601chartime2POSIX(tmpTIME[length(tmpTIME)], tz = tz), format = "%H:%M:%S")
t1hms = as.numeric(unlist(strsplit(t1hms, ":", fixed = T)))
t1 = ceiling(sum(c(t1hms[1], t1hms[2]/60, t1hms[3]/3600)))
if (is.na(t1) == TRUE | is.na(t0) == TRUE) {
# if timestamp is stored in POSIX format (older versions of GGIR prior 2017)
# for example when milestone data generated by old packages is now processed
# then this part of the code is to facilitate consistency
time_SPTE_start = as.POSIXlt(tmpTIME[spt_estimate$SPTE_start], origin = "1970-01-01", tz = tz)
time_SPTE_end = as.POSIXlt(tmpTIME[spt_estimate$SPTE_end], origin = "1970-01-01", tz = tz)
time_SPTE_end_hr = as.numeric(format(time_SPTE_end, format = "%H"))
time_SPTE_start_hr = as.numeric(format(time_SPTE_start, format = "%H"))
t0 = as.numeric(format(as.POSIXlt(tmpTIME[1],origin = "1970-01-01", tz = tz), format = "%H"))
t1 = as.numeric(format(as.POSIXlt(tmpTIME[length(tmpTIME)], origin = "1970-01-01", tz = tz), format = "%H"))
}
Nhoursdata = floor(length(tmpTIME) / (3600/ws3))
delta_t1_t0 = (t1 + 24) - t0
if (length(time_SPTE_end_hr) > 0 & length(time_SPTE_start_hr) > 0) {
if (Nhoursdata > (delta_t1_t0 + 0.1) & # time has moved backward (autumn) so SPTE_end also needs to move backward
(time_SPTE_end_hr > 1 | time_SPTE_end_hr < 12) &
(time_SPTE_start_hr < 1 | time_SPTE_start_hr > 12)) { #extra DST hour not recognized
calc_SPTE_end = calc_SPTE_end - 1
} else if (Nhoursdata + 0.1 < delta_t1_t0 & # time has moved forward (spring) so SPTE_end also needs to move forward
(time_SPTE_end_hr > 1 | time_SPTE_end_hr < 12) &
(time_SPTE_start_hr < 1 | time_SPTE_start_hr > 12)) { #missing DST hour not recognized
calc_SPTE_end = calc_SPTE_end + 1
}
t = tmpTIME[c(1, spt_estimate$SPTE_start, spt_estimate$SPTE_end)]
timezone = format(GGIR::iso8601chartime2POSIX(t, tz = tz), "%z")
if (length(unique(timezone)) == 1) {
return(c(calc_SPTE_start, calc_SPTE_end))
} else {
sign = ifelse(substr(timezone, 1, 1) == "-", -1, 1)
hours = as.numeric(substr(timezone, 2, 3))
minutes = as.numeric(substr(timezone, 4, 5))
offset = sign * (hours + minutes / 60)
offset = offset[2:3] - offset[1]
return(c(calc_SPTE_start + offset[1], calc_SPTE_end + offset[2]))
}
return(calc_SPTE_end)
}
#==================================================
# get variables
Expand Down Expand Up @@ -365,9 +343,12 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12),
}
SPTE_end[sptei] = (spt_estimate$SPTE_end / (3600 / ws3)) + daysleep_offset
SPTE_start[sptei] = (spt_estimate$SPTE_start / (3600 / ws3)) + daysleep_offset
SPTE_end[sptei] = dstime_handling_check(tmpTIME = tmpTIME, spt_estimate = spt_estimate,
tz = desiredtz, calc_SPTE_end = SPTE_end[sptei],
calc_SPTE_start = SPTE_start[sptei])
SPTE_dst = dstime_handling_check(tmpTIME = tmpTIME, spt_estimate = spt_estimate,
tz = desiredtz,
calc_SPTE_end = SPTE_end[sptei],
calc_SPTE_start = SPTE_start[sptei])
SPTE_start[sptei] = SPTE_dst[1]
SPTE_end[sptei] = SPTE_dst[2]
tib.threshold[sptei] = spt_estimate$tib.threshold
part3_guider[sptei] = spt_estimate$part3_guider
}
Expand Down

0 comments on commit 82a0f4c

Please sign in to comment.