-
Notifications
You must be signed in to change notification settings - Fork 114
/
gridt.r
91 lines (76 loc) · 4.87 KB
/
gridt.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#==========================================================================================#
#==========================================================================================#
# 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
#==========================================================================================#
#==========================================================================================#