R/rts.R

# Author: Babak Naimi, naimi.b@gmail.com
# Date :  July 2012
# Last Update :  Feb. 2024
# Version 1.2
# Licence GPL v3
#---------------------

if (!isGeneric("rts")) {
  setGeneric("rts", function(x, time,...)
    standardGeneric("rts"))
}  

setMethod('rts', signature(x='character'), 
          function(x, time){
            
            x <- rast(x)
            
            if (missing(time)) {
              if (any(is.na(time(x)))) stop('time is missing...!')
              else time <- unique(time(x))
            }
            
            if (!(class(time) %in% c("POSIXct","POSIXt","Date","yearmon","yearqtr"))[1])
              stop("time must be one of time-based classes including: 'POSIXct', 'POSIXt', 'Date', 'yearmon','yearqtr'")
            
            
            if (nlyr(x) != length(time))
              stop("time must have the same length as the number of layers in the Raster object")
            
            time=xts(1:nlyr(x),time)
            o <- new("SpatRasterTS")
            o@raster <- x
            o@time <- time
            o
          }
          )

setMethod('rts', signature(x='RasterStack'), 
          function(x, time) {
            if (missing(time)) stop('time is missing...!')
            if (!(class(time) %in% c("POSIXct","POSIXt","Date","timeDate","yearmon","yearqtr"))[1])
              stop("time must be one of time-based classes including: 'POSIXct', 'POSIXt', 'Date', 'timeDate', 'yearmon','yearqtr'")
            
            if (raster::nlayers(x) != length(time))
              stop("time must have the same length as the number of layers in RasterStack")
            
            time <- xts(1:raster::nlayers(x),time)
            o <- new("RasterStackTS")
            o@raster <- x
            o@time <- time
            o
          }
          )


setMethod('rts', signature(x='RasterBrick'), 
          function(x, time) {
            
            if (missing(time)) stop('time is missing...!')
            
            if (!(class(time) %in% c("POSIXct","POSIXt","Date","timeDate","yearmon","yearqtr"))[1])
              stop("time must be one of time-based classes including: 'POSIXct', 'POSIXt', 'Date', 'timeDate', 'yearmon','yearqtr'")
            
            if (raster::nlayers(x) != length(time))
              stop("time must have the same length as the number of layers in RasterBrick")
            
            time=xts(1:raster::nlayers(x),time)
            o <- new("RasterBrickTS")
            o@raster <- x
            o@time <- time
            o
          }
          )



setMethod('rts', signature(x='SpatRaster'), 
          function(x, time) {
            
            if (missing(time)) {
              if (any(is.na(time(x)))) stop('time is missing...!')
              else time <- unique(time(x))
            }
            
            if (!(class(time) %in% c("POSIXct","POSIXt","Date","timeDate","yearmon","yearqtr"))[1])
              stop("time must be one of time-based classes including: 'POSIXct', 'POSIXt', 'Date', 'timeDate', 'yearmon','yearqtr'")
            
            if (nlyr(x) != length(time)) stop("time must have the same length as the number of layers in SpatRaster")
            
            time=xts(1:nlyr(x),time)
            o <- new("SpatRasterTS")
            o@raster <- x
            o@time <- time
            o
          }
)

setMethod('rts', signature(x='xts'), 
          function(x){
            structure(x,class=c('rts','xts','zoo'))
          }
          )

setMethod('rts', signature(x='character',time='missing'), 
          function(x,cls=NULL){
            if (missing(cls)) cls <- NULL
            if (length(x) > 1) warning("input filename is a vector with more than 1 element, only the first element is considered!")
            x <- x[1]
            read.rts(x,cls=cls)
          }
          )
babaknaimi/rts documentation built on June 18, 2024, 11:31 p.m.