Skip to content

Commit

Permalink
Set all date/time objects to UTC time zone
Browse files Browse the repository at this point in the history
- closes #17 and fixed bug which was expressed for some years and
certain time zones (e.g., ‘Europe/Berlin’ and year 1942, but not for
‘Europe/Zurich’ and year 1942)
- bump version number


Former-commit-id: c2503e611f0ef0fd0aba7f14c8942ed98054d796 [formerly c2f89c228a9d9112e459fbbeab7a72526c242211]
Former-commit-id: a6fef13
  • Loading branch information
dschlaep committed Sep 28, 2016
1 parent 18f6eaf commit b980d70
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
26 changes: 13 additions & 13 deletions 2_SWSF_p3of5_ExternalDataExtractions_v51.R
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
dailyPPTceiling <- 1.5 * max(sapply(obs.hist.daily, FUN=function(obs) max(obs@data[,4]))) #Hamlet et al. 2010: "an arbitrary ceiling of 150% of the observed maximum precipitation value for each cell is also imposed by “spreading out” very large daily precipitation values into one or more adjacent days"

res <- try(lapply(obs.hist.daily, FUN=function(obs) {
month <- as.POSIXlt(paste(obs@year, obs@data[, "DOY"], sep="-"), format="%Y-%j")$mon + 1
month <- as.POSIXlt(paste(obs@year, obs@data[, "DOY"], sep="-"), format="%Y-%j", tz = "UTC")$mon + 1
ydelta <- delta_ts[delta_ts[, "Year"] == obs@year, -(1:2)]
tmax <- obs@data[, "Tmax_C"] + ydelta[month, "Tmax_C"]

Expand All @@ -871,7 +871,7 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
if (any(i_rainyMYears <- obs.hist.monthly[obs.hist.monthly[, "Month"] == m, "PPT_cm"] > 0)) { #sample from the same historic month in an other with rainy days instead
#Locate data of same month in other year
i_newYear <- which(i_rainyMYears)[which.min(abs(obs.hist.monthly[obs.hist.monthly[, "Month"] == m, "PPT_cm"][i_rainyMYears] - m_ydelta))]
newMonth <- as.POSIXlt(paste((newObs <- obs.hist.daily[i_newYear][[1]])@year, newObs@data[, "DOY"], sep="-"), format="%Y-%j")$mon + 1
newMonth <- as.POSIXlt(paste((newObs <- obs.hist.daily[i_newYear][[1]])@year, newObs@data[, "DOY"], sep="-"), format="%Y-%j", tz = "UTC")$mon + 1
newMonthData <- newObs@data[, "PPT_cm"][newMonth == m]
#Adjust data
newMonthData <- fix_PPTdata_length(data=newMonthData, targetLength=sum(im_month)) #adjust number of days in case we got a leap year February issue
Expand All @@ -880,7 +880,7 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
#Locate data of any month in any year
i_newMYear <- which(i_rainyMonth)[which.min(abs(obs.hist.monthly[i_rainyMonth, "PPT_cm"] - m_ydelta))]
i_newYear <- which(obs.hist.monthly[i_newMYear, "Year"] == sort(unique(obs.hist.monthly[, "Year"])))
newMonth <- as.POSIXlt(paste((newObs <- obs.hist.daily[i_newYear][[1]])@year, newObs@data[, "DOY"], sep="-"), format="%Y-%j")$mon + 1
newMonth <- as.POSIXlt(paste((newObs <- obs.hist.daily[i_newYear][[1]])@year, newObs@data[, "DOY"], sep="-"), format="%Y-%j", tz = "UTC")$mon + 1
newMonthData <- newObs@data[, "PPT_cm"][newMonth == obs.hist.monthly[i_newMYear, "Month"]]
#Adjust data
newMonthData <- fix_PPTdata_length(data=newMonthData, targetLength=sum(im_month)) #adjust number of days in case we got a month with a different number of days
Expand Down Expand Up @@ -1037,7 +1037,8 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
applyDelta_oneYear <- compiler::cmpfun(function(obs, delta_ts, ppt_fun, daily, monthly, ppt_type = c("simple", "detailed"), dailyPPTceiling, sigmaN, do_checks) {
ppt_type <- match.arg(ppt_type)

month <- 1 + as.POSIXlt(seq(ISOdate(obs@year, 1, 1), ISOdate(obs@year, 12, 31), by = "day"))$mon
month <- 1 + as.POSIXlt(seq(ISOdate(obs@year, 1, 1, tz = "UTC"),
ISOdate(obs@year, 12, 31, tz = "UTC"), by = "day"))$mon
ydeltas <- delta_ts[delta_ts[, "Year"] == obs@year, -(1:2)]
add_days <- ppt_fun[month] == "+"
mult_days <- !add_days
Expand Down Expand Up @@ -1083,7 +1084,6 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U

applyDeltas2 <- compiler::cmpfun(function(daily, monthly, years, delta_ts, ppt_fun, ppt_type = c("simple", "detailed"), dailyPPTceiling, sigmaN, do_checks = FALSE) {
ppt_type <- match.arg(ppt_type)
# daily_months <- 1 + as.POSIXlt(seq(ISOdate(years[1], 1, 1), ISOdate(years[length(years)], 12, 31), by = "day"))$mon

sw_list <- list()
totalPPT_to_remove <- 0
Expand Down Expand Up @@ -1606,7 +1606,7 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
ftemp <- textConnection(success)
} else if (service == "opendap") {
ftemp <- textConnection((temp <- strsplit(success, split="\n\n", fixed=TRUE))[[1]][3])
ttemp <- as.POSIXlt("1950-01-01") + 86400 * as.numeric(scan(text=sub("\n", ",", temp[[1]][4], fixed=TRUE), what="character", sep=",", quiet=TRUE)[-1])
ttemp <- as.POSIXlt("1950-01-01", tz = "UTC") + 86400 * as.numeric(scan(text=sub("\n", ",", temp[[1]][4], fixed=TRUE), what="character", sep=",", quiet=TRUE)[-1])
}
success <- 0
}
Expand All @@ -1623,7 +1623,7 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
if (service == "ncss") {
temp <- read.csv(ftemp, colClasses=c("POSIXct", "NULL", "NULL", "numeric")) #colnames = Time, Lat, Long, Variable
vtemp <- temp[, 2]
ttemp <- as.POSIXlt(temp[, 1])
ttemp <- as.POSIXlt(temp[, 1], tz = "UTC")
} else if (service == "opendap") {
vtemp <- read.csv(ftemp, colClasses=c("NULL", "numeric"), header=FALSE)[-1, ] #columns = Index, Variable
}
Expand Down Expand Up @@ -1738,8 +1738,8 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
baseYear <- temp[sapply(temp, function(x) !is.na(x))][[1]]
stopifnot(length(baseYear) == 1)

firstDate <- as.POSIXlt(baseYear + tvals[1])
lastDate <- as.POSIXlt(baseYear + tvals[N])
firstDate <- as.POSIXlt(baseYear + tvals[1], tz = "UTC")
lastDate <- as.POSIXlt(baseYear + tvals[N], tz = "UTC")

startYear <- firstDate$year + 1900
startMonth <- firstDate$mon + 1
Expand Down Expand Up @@ -2459,11 +2459,11 @@ if (exinfo$GDODCPUCLLNL || exinfo$ExtractClimateChangeScenarios_CMIP5_BCSD_NEX_U
getYears <- list(n_first = nrow(temp1), first = temp1, n_second = nrow(temp2), second = temp2)

#Monthly time-series
getYears$first_dates <- lapply(1:getYears$n_first, FUN=function(it) as.POSIXlt(seq(from=as.POSIXlt(paste0(getYears$first[it, 1], "-01-01")), to=as.POSIXlt(paste0(getYears$first[it, 2], "-12-31")), by="1 month")))
getYears$second_dates <- lapply(1:getYears$n_second, FUN=function(it) as.POSIXlt(seq(from=as.POSIXlt(paste0(getYears$second[it, 1], "-01-01")), to=as.POSIXlt(paste0(getYears$second[it, 2], "-12-31")), by="1 month")))
getYears$first_dates <- lapply(1:getYears$n_first, FUN=function(it) as.POSIXlt(seq(from=ISOdate(getYears$first[it, 1], 1, 1, tz = "UTC"), to=ISOdate(getYears$first[it, 2], 12, 31, tz = "UTC"), by="1 month")))
getYears$second_dates <- lapply(1:getYears$n_second, FUN=function(it) as.POSIXlt(seq(from=ISOdate(getYears$second[it, 1], 1, 1, tz = "UTC"), to=ISOdate(getYears$second[it, 2], 12, 31, tz = "UTC")), by="1 month")))
#Days per month
getYears$first_dpm <- lapply(1:getYears$n_first, FUN=function(it) rle(as.POSIXlt(seq(from=as.POSIXlt(paste0(getYears$first[it, 1], "-01-01")), to=as.POSIXlt(paste0(getYears$first[it, 2], "-12-31")), by="1 day"))$mon)$lengths)
getYears$second_dpm <- lapply(1:getYears$n_second, FUN=function(it) rle(as.POSIXlt(seq(from=as.POSIXlt(paste0(getYears$second[it, 1], "-01-01")), to=as.POSIXlt(paste0(getYears$second[it, 2], "-12-31")), by="1 day"))$mon)$lengths)
getYears$first_dpm <- lapply(1:getYears$n_first, FUN=function(it) rle(as.POSIXlt(seq(from=ISOdate(getYears$first[it, 1], 1, 1, tz = "UTC"), to=ISOdate(getYears$first[it, 2], 12, 31, tz = "UTC")), by="1 day"))$mon)$lengths)
getYears$second_dpm <- lapply(1:getYears$n_second, FUN=function(it) rle(as.POSIXlt(seq(from=ISOdate(getYears$second[it, 1], 1, 1, tz = "UTC"), to=ISOdate(getYears$second[it, 2], 12, 31, tz = "UTC")), by="1 day"))$mon)$lengths)

#Logical on how to select from getYears
assocYears <- vector("list", length = 1 + length(reqRCPs) * nrow(future_yrs))
Expand Down
4 changes: 2 additions & 2 deletions 2_SWSF_p4of5_Code_v51.R
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ if(do_weather_source){
# - Grids: Geographic Coordinate Reference: WGS_1984; Projection: Lambert Conformal Conic
# - Cells size: 1000 x 1000 m
# - All Daymet years, including leap years, have 1 - 365 days. For leap years, the Daymet database includes leap day. Values for December 31 are discarded from leap years to maintain a 365-day year.
there <- simstartyr >= 1980 && endyr <= as.POSIXlt(Sys.time())$year+1900 - 1
there <- simstartyr >= 1980 && endyr <= as.POSIXlt(Sys.time(), tz = "UTC")$year+1900 - 1
if (any(there)) {
there <- (SWRunInformation[runIDs_sites, "X_WGS84"] >= -179 &
SWRunInformation[runIDs_sites, "X_WGS84"] <= -5) &
Expand Down Expand Up @@ -4907,7 +4907,7 @@ do_OneSite <- function(i_sim, i_labels, i_SWRunInformation, i_sw_input_soillayer

#Regeneration year=RY: RYdoy=1 == start of seed dispersal = start of 'regeneration year'
Doy_SeedDispersalStart <- max(round(param$Doy_SeedDispersalStart0 + param$SeedDispersalStart_DependencyOnMeanTempJanuary * TmeanJan, 0) %% 365, 1)
moveByDays <- ifelse(Doy_SeedDispersalStart == 1, 1, max(as.numeric(as.POSIXlt(paste(simTime$useyrs[1] - 1, "-12-31", sep="")) - as.POSIXlt(paste(simTime$useyrs[1] - 1, "-01-01", sep=""))) + 1 - (Doy_SeedDispersalStart - 1) %% 365, 1))
moveByDays <- ifelse(Doy_SeedDispersalStart == 1, 1, max(as.numeric(ISOdate(simTime$useyrs[1] - 1, 12, 31, tz = "UTC") - ISOdate(simTime$useyrs[1] - 1, 1, 1, tz = "UTC")) + 1 - (Doy_SeedDispersalStart - 1) %% 365, 1))
#Calculate regeneration year dates
if (startyr > simstartyr) {
#start earlier to complete RY
Expand Down
28 changes: 14 additions & 14 deletions 2_SWSF_p5of5_Functions_v51.R
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,17 @@ simTiming <- compiler::cmpfun(function(startyr, simstartyr, endyr) {
res <- list()
#simyrs <- simstartyr:endyr
#no.simyr <- endyr - simstartyr + 1
temp <- as.POSIXlt(paste0(startyr, "-01-01"))
temp <- ISOdate(startyr, 1, 1, tz = "UTC")

res[["useyrs"]] <- startyr:endyr

res[["no.useyr"]] <- endyr - startyr + 1
res[["no.usemo"]] <- res[["no.useyr"]] * 12
res[["no.usedy"]] <- as.numeric(as.POSIXlt(paste0(endyr, "-12-31")) - temp) + 1
res[["no.usedy"]] <- as.numeric(ISOdate(endyr, 12, 31, tz = "UTC") - temp) + 1

res[["discardyr"]] <- startyr - simstartyr
res[["discardmo"]] <- res[["discardyr"]] * 12
res[["discarddy"]] <- as.numeric(temp - as.POSIXlt(paste0(simstartyr, "-01-01")))
res[["discarddy"]] <- as.numeric(temp - ISOdate(simstartyr, 1, 1, tz = "UTC"))

res[["index.useyr"]] <- res[["discardyr"]] + seq_len(res[["no.useyr"]])
res[["index.usemo"]] <- res[["discardmo"]] + seq_len(res[["no.usemo"]])
Expand All @@ -497,16 +497,16 @@ simTiming_ForEachUsedTimeUnit <- compiler::cmpfun(function(st, sim_tscales, lati
res <- list()

if (any(sim_tscales == "daily")) {
temp <- as.POSIXlt(seq(from = as.POSIXlt(paste0(min(st$useyrs), "-01-01")),
to = as.POSIXlt(paste0(max(st$useyrs), "-12-31")),
temp <- as.POSIXlt(seq(from = ISOdate(min(st$useyrs), 1, 1, tz = "UTC"),
to = ISOdate(max(st$useyrs), 12, 31, tz = "UTC"),
by = "1 day"))

res$doy_ForEachUsedDay <- res$doy_ForEachUsedDay_NSadj <- temp$yday + 1
res$month_ForEachUsedDay <- res$month_ForEachUsedDay_NSadj <- temp$mon + 1
res$year_ForEachUsedDay <- res$year_ForEachUsedDay_NSadj <- temp$year + 1900

if (latitude < 0 && account_NorthSouth) {
dshift <- as.POSIXlt(paste(st$useyrs, 6, 30, sep = "-"))$yday + 1 #new month either at end of year or in the middle because the two halfs (6+6 months) of a year are of unequal length (182 (183 if leap year) and 183 days): I chose to have a new month at end of year (i.e., 1 July -> 1 Jan & 30 June -> 31 Dec; but, 1 Jan -> July 3/4): and instead of a day with doy=366, there are two with doy=182
dshift <- as.POSIXlt(ISOdate(st$useyrs, 6, 30, tz = "UTC"))$yday + 1 #new month either at end of year or in the middle because the two halfs (6+6 months) of a year are of unequal length (182 (183 if leap year) and 183 days): I chose to have a new month at end of year (i.e., 1 July -> 1 Jan & 30 June -> 31 Dec; but, 1 Jan -> July 3/4): and instead of a day with doy=366, there are two with doy=182
res$doy_ForEachUsedDay_NSadj <- unlist(lapply(seq_along(st$useyrs), function(x) {
temp <- res$doy_ForEachUsedDay[st$useyrs[x] == res$year_ForEachUsedDay]
c(temp[-(1:dshift[x])], temp[1:dshift[x]])
Expand Down Expand Up @@ -596,8 +596,8 @@ sw_SiteClimate_Ambient <- compiler::cmpfun(function(weatherList, year.start, yea
dailyTempMean <- c(dailyTempMean, temp.dailyTempMean)
}

month_forEachDoy <- as.POSIXlt(seq(from = as.POSIXlt(paste0(years[y], "-01-01")),
to = as.POSIXlt(paste0(years[y], "-12-31")),
month_forEachDoy <- as.POSIXlt(seq(from = ISOdate(years[y], 1, 1, tz = "UTC"),
to = ISOdate(years[y], 12, 31, tz = "UTC"),
by = "1 day"))$mon + 1

tempMean <- tempMean + tapply(temp.dailyTempMean, month_forEachDoy, mean)
Expand Down Expand Up @@ -2031,13 +2031,13 @@ ExtractGriddedDailyWeatherFromMaurer2002_NorthAmerica <- compiler::cmpfun(functi
colnames(weath.data) <- c("year", "month", "day", "prcp_mm", "Tmax_C", "Tmin_C", "Wind_mPERs")

#times
date <- seq(from=as.Date(with(weath.data[1, ], paste(year, month, day, sep="-")), format="%Y-%m-%d"),
to=as.Date(with(weath.data[nrow(weath.data), ], paste(year, month, day, sep="-")), format="%Y-%m-%d"),
by="1 day")
doy <- 1 + as.POSIXlt(seq(from = with(weath.data[1, ], ISOdate(year, month, day, tz = "UTC")),
to = with(weath.data[nrow(weath.data), ], ISOdate(year, month, day, tz = "UTC")),
by = "1 day"))$yday

# conversion precipitation: mm/day -> cm/day
data_all <- with(weath.data, data.frame(doy=1 + as.POSIXlt(date)$yday, Tmax_C, Tmin_C, prcp_mm/10))
colnames(data_all) <- c("DOY", "Tmax_C", "Tmin_C", "PPT_cm")
data_all <- with(weath.data, data.frame(
DOY = doy, Tmax_C = Tmax_C, Tmin_C = Tmin_C, PPT_cm = prcp_mm / 10))

years <- startYear:endYear
n_years <- length(years)
Expand All @@ -2046,7 +2046,7 @@ ExtractGriddedDailyWeatherFromMaurer2002_NorthAmerica <- compiler::cmpfun(functi
for(y in seq_along(years)) {
data_sw <- data_all[weath.data$year == years[y], ]
data_sw[, -1] <- round(data_sw[, -1], 2) #weather.digits
weathDataList[[y]]<-new("swWeatherData",
weathDataList[[y]] <- new("swWeatherData",
year = years[y],
data = data.matrix(data_sw, rownames.force = FALSE)) #strip row.names, otherwise they consume about 60% of file size
}
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SWSF
Title: SOILWAT Simulation Framework
Version: 1.6.2
Date: 2016-09-24
Version: 1.6.3
Date: 2016-09-28
Authors@R: c(person("Daniel", "Schlaepfer", email = "daniel.schlaepfer@unibas.ch", role = c("aut", "cre")),
person("Caitlin", "Andrews", role = "ctb", email = "not@available.com"))
person("Ryan", "Murphy", role = "ctb", email = "not@available.com"))
Expand Down

0 comments on commit b980d70

Please sign in to comment.