Skip to content

Commit

Permalink
daystoyears() with better error message and better format handling (t…
Browse files Browse the repository at this point in the history
…hanks to Leanoard Mada)
  • Loading branch information
phgrosjean committed Aug 8, 2021
1 parent 33cd501 commit 5d425d0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pastecs
Type: Package
Version: 1.4.0
Version: 1.4.1
Title: Package for Analysis of Space-Time Ecological Series
Description: Regularisation, decomposition and analysis of space-time series.
The pastecs R package is a PNEC-Art4 and IFREMER (Benoit Beliaeff
Expand Down
108 changes: 55 additions & 53 deletions R/daystoyears.R
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
"daystoyears" <-
function (x, datemin=NULL, dateformat="m/d/Y") {
x <- x
datemin <- datemin
defyearorig <- 1970
# In R, we use POSIXt
if (length(datemin) > 0 && !any(class(datemin) == "POSIXt")) { # We must convert
# To be compatible with chron() and with Splus, we accept format as "d/m/y"
# which is converted into %d/%m/%y. Warning! Still must make difference between
# "y" that corresponds to year in "89" and "Y" for full-spelled years in "1989"!!!
# make necessary conversions to accept old formats
dateformat <- sub("month", "%B", dateformat) # Full spelled month
dateformat <- sub("mon", "%b", dateformat) # Abbreviated month
dateformat <- sub("m", "%m", dateformat) # month - numeric
dateformat <- sub("d", "%d", dateformat) # day
dateformat <- sub("y", "%y", dateformat) # year (two digits)
dateformat <- sub("Y", "%Y", dateformat) # Year (four digits)
datemin <- strptime(as.character(datemin), format=dateformat)
}
# Julian is adapted from julian.default in lib chron 2.2-19 (this way we don't require chron!)
"Julian" <- function(x, d, y) {
if(is.null(origin. <- getOption("chron.origin")))
origin. <- c(month = 1, day = 1, year = 1970) # Default origin in R
m <- c(origin.[1], x) # prepend month of new origin
d <- c(origin.[2], d) # prepend day of new origin
y <- c(origin.[3], y) # prepend year of new origin
# code from julian date in the S book (p.269)
y <- y + ifelse(m > 2, 0, -1)
m <- m + ifelse(m > 2, -3, 9)
c <- y %/% 100
ya <- y - 100 * c
out <- ((146097 * c) %/% 4 + (1461 * ya) %/% 4 + (153 * m + 2) %/% 5 + d + 1721119)
## now subtract the new origin from all dates
if(all(origin. == 0))
out <- out[-1]
else
out <- out[-1] - out[1]
# orig according to S algorithm
out
}

if (length(datemin) > 0) {
dateminval <- Julian(datemin$mon+1, datemin$mday, datemin$year+1900)
# now we shift the whole x series so as the minimal day matches dateminval
x <- x - trunc(min(x, na.rm=TRUE)) + dateminval
}
# We have days as units. We want years with a "linear scale", i.e.: 1 year = 365.25 days, 1 month = 1/12 years
# We want also the integer value reflect exactly the current year, i.e.: 1997.xxx for dates in the year 1997
if(is.null(yearorig <- options("chron.origin")$year))
yearorig <- defyearorig
x <- x/365.25 + yearorig
x
}
"daystoyears" <-
function (x, datemin=NULL, dateformat="m/d/Y") {
x <- x
datemin <- datemin
defyearorig <- 1970
# In R, we use POSIXt
if (length(datemin) > 0 && !any(class(datemin) == "POSIXt")) { # We must convert
# To be compatible with chron() and with Splus, we accept format as "d/m/y"
# which is converted into %d/%m/%y. Warning! Still must make difference between
# "y" that corresponds to year in "89" and "Y" for full-spelled years in "1989"!!!
# make necessary conversions to accept old formats
dateformat <- sub("month", "%B", dateformat) # Full spelled month
dateformat <- sub("mon", "%b", dateformat) # Abbreviated month
dateformat <- sub("[%]?m", "%m", dateformat) # month - numeric
dateformat <- sub("[%]?d", "%d", dateformat) # day
dateformat <- sub("[%]?y", "%y", dateformat) # year (two digits)
dateformat <- sub("[%]?Y", "%Y", dateformat) # Year (four digits)
datemin <- strptime(as.character(datemin), format = dateformat)
if (is.na(datemin))
stop("Invalid date or date format!")
}
# Julian is adapted from julian.default in lib chron 2.2-19 (this way we don't require chron!)
"Julian" <- function(x, d, y) {
if(is.null(origin. <- getOption("chron.origin")))
origin. <- c(month = 1, day = 1, year = 1970) # Default origin in R
m <- c(origin.[1], x) # prepend month of new origin
d <- c(origin.[2], d) # prepend day of new origin
y <- c(origin.[3], y) # prepend year of new origin
# code from julian date in the S book (p.269)
y <- y + ifelse(m > 2, 0, -1)
m <- m + ifelse(m > 2, -3, 9)
c <- y %/% 100
ya <- y - 100 * c
out <- ((146097 * c) %/% 4 + (1461 * ya) %/% 4 + (153 * m + 2) %/% 5 + d + 1721119)
## now subtract the new origin from all dates
if(all(origin. == 0))
out <- out[-1]
else
out <- out[-1] - out[1]
# orig according to S algorithm
out
}

if (length(datemin) > 0) {
dateminval <- Julian(datemin$mon+1, datemin$mday, datemin$year+1900)
# now we shift the whole x series so as the minimal day matches dateminval
x <- x - trunc(min(x, na.rm=TRUE)) + dateminval
}
# We have days as units. We want years with a "linear scale", i.e.: 1 year = 365.25 days, 1 month = 1/12 years
# We want also the integer value reflect exactly the current year, i.e.: 1997.xxx for dates in the year 1997
if(is.null(yearorig <- options("chron.origin")$year))
yearorig <- defyearorig
x <- x/365.25 + yearorig
x
}
Binary file added manual/pastecs_manuel_utilisateur.docx
Binary file not shown.

0 comments on commit 5d425d0

Please sign in to comment.