Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard coded exception in 'sw_SiteClimate_Ambient' for year 1942 #17

Closed
dschlaep opened this issue Aug 31, 2016 · 4 comments
Closed

Hard coded exception in 'sw_SiteClimate_Ambient' for year 1942 #17

dschlaep opened this issue Aug 31, 2016 · 4 comments
Assignees
Labels

Comments

@dschlaep
Copy link
Member

The function 'sw_SiteClimate_Ambient' has a hard-coded exception for the year 1942:
- If the year is 1942, then an additional day is added to the sequence 'month_forEachDoy'

I don't see the reason why that this is done. It appears that the code believes that the year 1942 is a leap year and that the input argument 'weatherList' would treat it as non-leap year? In any case, 1942 was not a leap year.

Please explain code in a comment or remove.

Thanks, Daniel

@gopherchucks
Copy link
Contributor

Interesting, I'm not sure why it's there. I don't think it's my code. Maybe someone was writing a test case, or something? Anyway, I'll delete it since it doesn't look like it has any effect on the Wrapper.

gopherchucks pushed a commit that referenced this issue Sep 7, 2016
We could not determine the original purpose of the piece of code and so
it has been removed
@CaitlinA
Copy link
Member

In retrospect, I think I am the one who hardcoded the 1942 exception into the sw_SiteClimate_Ambient function. I ran into the 1942 'problem' again today. For some reason, in 1942, the month_forEachDoy statement, only creates a sequence that is a length of 364, and ends on 12-30-1942 (excludes 12-31-1942). The hardcoded exception added an additional day-month to account for 12-31-1942. Do you have a more elegant solution to fix this? I will send along weather files in a separate file so you can see for yourself.

@dschlaep

@dschlaep
Copy link
Member Author

Replicate problem described by Caitlin for year 1942

sessionInfo()

R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin14.5.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] tools_3.3.0 Rsoilwat31_1.1.1

## Load weather data in question
weath <- Rsoilwat31::getWeatherData_folders(
  LookupWeatherFolder = ".", weatherDirName = "Weather_SheepStation",
  filebasename = "weath", startYear = 1926, endYear = 2015)

# Check structure
str(weath[["1942"]])
wdims <- sapply(weath, function(x) dim(x@data))

isLeapYear <- function(y) {
  #from package: tis
  y %% 4 == 0 & (y %% 100 != 0 | y %% 400 == 0)
}

identical(as.vector(wdims[1, ]), ifelse(isLeapYear(1926:2015), 366L, 365L))

Sys.timezone()
## Formal class 'swWeatherData' [package "Rsoilwat31"] with 2 slots
##   ..@ data: num [1:365, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : NULL
##   .. .. ..$ : chr [1:4] "DOY" "Tmax_C" "Tmin_C" "PPT_cm"
##   ..@ year: int 1942
## [1] TRUE
## [1] "Europe/Zurich"

I tested this for "Europe/Berlin" and "Europe/Zurich". I copy-pasted results below for "Europe/Berlin":

# Code from 'sw_SiteClimate_Ambient'
weatherList <- weath
year.start <- 1926
year.end <- 2015
do.C4vars <- FALSE

  sw.weather.suffix <- as.numeric(names(weatherList))
  itemp <- year.start <= sw.weather.suffix & year.end >= sw.weather.suffix
  years <- sw.weather.suffix[itemp]

  no.yrs <- length(years)
  y <- which(years == 1942)

      x <- Rsoilwat31::get_swWeatherData(weatherList, years[y])@data[, c("Tmax_C", "Tmin_C", "PPT_cm"), drop = FALSE]
      temp.dailyTempMean <- rowMeans(x[, c("Tmax_C", "Tmin_C")])

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


print(data.frame(temp_seq, month_forEachDoy)[c(305:308, 360:365), ])
##       temp_seq month_forEachDoy
## 305 1942-11-01               11
## 306 1942-11-02               11
## 307 1942-11-03               11
## 308 1942-11-04               11
## 360 1942-12-26               12
## 361 1942-12-27               12
## 362 1942-12-28               12
## 363 1942-12-29               12
## 364 1942-12-30               12
## 365 1942-12-31               12

For timezone "Europe/Berlin", I get

305 1942-11-01 00:00:00 11
306 1942-11-02 00:00:00 11
307 1942-11-02 23:00:00 11
308 1942-11-03 23:00:00 11
360 1942-12-25 23:00:00 12
361 1942-12-26 23:00:00 12
362 1942-12-27 23:00:00 12
363 1942-12-28 23:00:00 12
364 1942-12-29 23:00:00 12
365 1942-12-30 23:00:00 12

==> there is no date for Dec 31, 1942 even though there are 365 entries

print(temp_seq[305:308])
## [1] "1942-11-01 CET" "1942-11-02 CET" "1942-11-03 CET" "1942-11-04 CET"

For timezone "Europe/Berlin", I get:

[1] "1942-11-01 00:00:00 CEST" "1942-11-02 00:00:00 CEST" "1942-11-02 23:00:00 CET" "1942-11-03 23:00:00 CET"

==> date Nov 2, 1942 occurs twice due to switch from CEST to CET

Explanation of issue:

Note: see Regression in strptime:
"In R-3.1.3 that returned "1942-01-01 CEST" which, paradoxically, is correct as they
evidently did strange things in Germany during the war period. Java also returns the
same. However, R-3.2.4 returns "1942-01-01 CET"."

See History of Daylight Saving Time — DST:
"Year-round DST, also called “War Time”, was in force during World War II,
from February 9, 1942, to September 30, 1945"

==> If I run the code on my system where Sys.timezone() returns "Europe/Zurich",
then I don't experience issue #17.

==> Thus, issue 17 should appear for US, Canadian, and German/Hitler time zones
during WW I and II which had DST all year round, but not for time zones which didn't
have DST year round in 1942 (such as Europe/Zurich).

Suggested solution: fix time zone to UTC during time sequence calculation

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

print(data.frame(temp_seq2, month_forEachDoy2)[c(305:308, 360:365), ])

print(temp_seq2[305:308])
##               temp_seq2 month_forEachDoy2
## 305 1942-11-01 12:00:00                11
## 306 1942-11-02 12:00:00                11
## 307 1942-11-03 12:00:00                11
## 308 1942-11-04 12:00:00                11
## 360 1942-12-26 12:00:00                12
## 361 1942-12-27 12:00:00                12
## 362 1942-12-28 12:00:00                12
## 363 1942-12-29 12:00:00                12
## 364 1942-12-30 12:00:00                12
## 365 1942-12-31 12:00:00                12
## [1] "1942-11-01 12:00:00 UTC" "1942-11-02 12:00:00 UTC"
## [3] "1942-11-03 12:00:00 UTC" "1942-11-04 12:00:00 UTC"

@CaitlinA
Copy link
Member

How interesting! Who knew that programming could lead to a quick history
lesson? Thanks for looking into this.

On Wed, Sep 28, 2016 at 2:52 AM, daniel [email protected] wrote:

Closed #17
#17 via
eab32aa
eab32aa
.


You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
#17 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AM0e_3_Kfyj8iFjjS5OsacQ1PAdiAikOks5qujjZgaJpZM4JxaLA
.

Caitlin Andrews
Ecologist
Colorado Plateau Research Station
Southwest Biological Science Center
U.S. Geological Survey
Mobile: 802-922-3494

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants