Rutils/maybe-not-useful/gridt.r

#==========================================================================================#
#==========================================================================================#
# Function gridt                                                                           #
# Developed by Marcos Longo - EPS/Harvard University                                       #
#                                                                                          #
#   This function determine the times available.                                           #
#------------------------------------------------------------------------------------------#
gridt <<- function(ntimes,time0,dtime,century=2000){
   #----- Find position of each part ------------------------------------------------------#
   time0 = tolower(time0)
   colon = regexpr(":",time0)[1]
   zed   = max(regexpr("z",time0)[1])
   utc   = regexpr("z",time0)[1]
   size  = nchar(time0)
   #---------------------------------------------------------------------------------------#


   #------ Find the month position. -------------------------------------------------------#
   mloc  = max(sapply(X=tolower(month.abb),FUN=regexpr,text=time0))
   #---------------------------------------------------------------------------------------#


   #------ Grab times based on whether there is a colon or a Z. ---------------------------#
   if (colon > 0){nn = as.numeric(substr(time0,colon+1,colon+2))}else{nn=0}
   if (zed   > 0){hh = as.numeric(substr(time0,1,2))            }else{hh=0}
   dd = as.numeric(substr(time0,utc+1,mloc-1))
   mm = match(substr(time0,mloc,mloc+2),tolower(month.abb))
   yy = as.numeric(substr(time0,mloc+3,size))
   if (yy < 100) {yyyy = yy+century}else{yyyy = yy}
   #---------------------------------------------------------------------------------------#


   #----- Make the initial time. ----------------------------------------------------------#
   t0 = chron(paste(mm,dd,yyyy,sep="/"),paste(hh,nn,0,sep=":"))
   #---------------------------------------------------------------------------------------#


   #----- Find the time step. -------------------------------------------------------------#
   dtime  = tolower(dtime)
   size   = nchar(dtime)
   units  = substr(dtime,size-1,size)
   amount = as.numeric(substr(dtime,1,size-2))
   #---------------------------------------------------------------------------------------#


   #---------------------------------------------------------------------------------------#
   #      Select the appropriate time step.                                                #
   #---------------------------------------------------------------------------------------#
   if (units %in% "mn"){
      #----- Minutes. ---------------------------------------------------------------------#
      dtime = as.double(amount) / day.min
      gridt = t0 + (sequence(ntimes)-1) * dtime
      #------------------------------------------------------------------------------------#

   }else if(units == "hr"){
      #----- Hours. -----------------------------------------------------------------------#
      dtime = as.double(amount)/day.hr
      gridt = t0 + (sequence(ntimes) - 1) * dtime
      #------------------------------------------------------------------------------------#

   }else if (units == "dy"){
      #----- Days. ------------------------------------------------------------------------#
      dtime = as.double(amount)
      gridt = t0 + (sequence(ntimes) - 1) * dtime
      #------------------------------------------------------------------------------------#

   }else if (units == "mo"){
      #----- Months. ----------------------------------------------------------------------#
      mms   = mm + amount * (sequence(ntimes)-1)
      dy    = floor( ( mms - 1 )  / 12 )
      yrs   = yy + dy
      mms   =  1 + ( ( mms - 1 ) %% 12 )
      gridt = chron(paste(mms,dd,yrs,sep="/"),paste(hh,nn,00,sep=":"))
      #------------------------------------------------------------------------------------#

   }else if (units == "yr"){
      #----- Years. -----------------------------------------------------------------------#
      yrs   = yy + amount * (sequence(ntimes)-1)
      gridt = chron(paste(mm,dd,yrs,sep="/"),paste(hh,nn,00,sep=":"))
      #------------------------------------------------------------------------------------#
   }else{
      #----- Give the bad news. -----------------------------------------------------------#
      stop(paste("CTL doesn't have valid time units! (Found ",units,")",sep=""))
      #------------------------------------------------------------------------------------#
   }#end if
   #---------------------------------------------------------------------------------------#

   return(gridt)
}#end function gridt
#==========================================================================================#
#==========================================================================================#
manfredo89/ED2io documentation built on May 21, 2019, 11:24 a.m.