-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclave.gs
executable file
·223 lines (190 loc) · 5.12 KB
/
clave.gs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
*
* help is in the end of this script
*
function clave(args)
_version='0.03r1'
rc = gsfallow("on")
if( args = '' )
help()
return
endif
varin = subwrd( args, 1 )
time1 = subwrd( args, 2 )
time2 = subwrd( args, 3 )
ystart = subwrd( args, 4 )
if( ystart = '-ylist' )
ystart = ''
yend = ''
ylist = subwrd( args, 5 )
else
yend = subwrd( args, 5 )
ylist = ''
endif
varout = subwrd( args, 6 )
if( varin = '' ) ; say 'error: no varin is specified!' ; return ; endif
if( time1 = '' ) ; say 'error: no time1 is specified!' ; return ; endif
if( time2 = '' ) ; say 'error: no time2 is specified!' ; return ; endif
if( ystart = '' & yend = '' & ylist = '' )
say 'error: no year range is specified!' ; return ; endif
if( ystart != '' & yend != '' & ystart > yend )
say 'error: no ystart > yend' ; return ; endif
* if( varin = '' | time1 = '' | time2 = '' | ystart = '' | yend = '' | ystart > yend )
* say 'Error: arguements are inscorrect!'
* say 'try "clave" for help'
* return
* endif
if( ystart != '' & y_end != '' )
pn = 0
y = ystart
while( y <= yend )
pn = pn + 1
year.pn = y
y = y + 1
endwhile
endif
if( ylist != '' )
pn = 1
year.pn = ''
len = math_strlen( ylist )
i = 1
while( i <= len )
c = substr( ylist, i, 1 )
if( c = ',' ) ; pn = pn + 1 ; year.pn = ''
else ; year.pn = year.pn % c ; endif
i = i + 1
endwhile
endif
***** Calculate *****
p = 1
* y = ystart
* while( y <= yend )
while( p <= pn )
y = year.p
timey1 = gettime( time1, y )
timey2 = gettime( time2, y )
say 'ave( 'varin', time='timey1', time='timey2 ')'
'clavetemp = ave( 'varin', time='timey1', time='timey2' )'
* if( y = ystart )
if( p = 1 )
'clavenum = const( clavetemp*0+1, 0, -u )'
'clave = clavetemp'
else
'clavenum = clavenum + const( clavetemp*0+1, 0, -u )'
'clavemask = const( clave*0+1, 0, -u ) + const( clavetemp*0+1, 0, -u ) - 0.5'
'clave = maskout( const( clave, 0, -u ) + const( clavetemp, 0, -u ), clavemask )'
endif
p = p + 1
* y = y + 1
endwhile
'clave = clave / clavenum'
***** Output *****
if( varout = '' )
'd clave'
else
varout'=clave'
endif
'undefine clave'
return
*
* get time for grads
*
* e.g. %y, %ypp -> 1980, 1981
* %end -> 18z31 (if Jan, 6 hourly )
function gettime( str, y )
time = ''
len = math_strlen( str )
pos = 1
while( pos <= len )
c = substr( str, pos, 1 )
if( c = '%' )
c1 = substr( str, pos+1, 1 )
c2 = substr( str, pos+2, 1 )
c3 = substr( str, pos+3, 1 )
while( 1 )
* %ypp
if( c1 = 'y' & c2 = 'p' & c3 = 'p' )
ypp = y + 1
time = time % ypp
pos = pos + 3
break
endif
* %y
if( c1 = 'y' )
time = time % y
pos = pos + 1
break
endif
time = time % '%'
break
endwhile
else
time = time % c
endif
pos = pos + 1
endwhile
return ( time )
* below TODO
* for %end
str = time
len = math_strlen( str )
pos = 1
while( pos <= len )
c = substr( str, pos, 1 )
if( c = '%' )
c1 = substr( str, pos+1, 1 )
c2 = substr( str, pos+2, 1 )
c3 = substr( str, pos+3, 1 )
while( 1 )
* %end
if( c1 = 'e' & c2 = 'n' & c3 = 'd' )
temp = substr( str, pos+4, len-(pos+3) )
* say t2time( time2t( temp )-1 )
say temp
* temp = time2t
exit
time = time % ypp
pos = pos + 3
break
endif
time = time % '%'
break
endwhile
else
time = time % c
endif
pos = pos + 1
endwhile
return ( time )
*
* help
*
function help()
say ' Name:'
say ' clave '_version' - make climatological mean '
say ''
say ' Usage:'
say ' 1. clave var_in time1 time2 ystart yend [var_out]'
say ' 2. clave var_in time1 time2 -ylist y1,y2,... [var_out]'
say ''
say ' var_in : variable'
say ' time1 time2 : (seasonal) time range'
say ' %y and %ypp should be included. '
say ' They will be replaced by a particular year '
say ' and the next year'
say ' e.g. time1 time2'
say ' 01jan%y 01feb%y : January-mean (including 00z01feb)'
say ' 01jun%y 01sep%y : JJA-mean (including 00z01sep)'
say ' 01dec%y 01mar%ypp: DJF-mean (including 00z01mar)'
say ' ystart yend : year range (ystart <= yend)'
say ' -ylist list : year used'
say ' var_out : variable in which climatological mean will be stored.'
say ''
say ' Note:'
say ' [arg-name] : specify if needed'
say ' (arg1 | arg2) : arg1 or arg2 must be specified'
say ' If var_out is not specified, climatological mean will be drawn.'
say ''
say ' Copyright (C) 2009 Chihiro Kodama'
say ' Distributed under GNU GPL (http://www.gnu.org/licenses/gpl.html)'
say ''
return