forked from kodamail/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutdata.gs
253 lines (232 loc) · 6.72 KB
/
cutdata.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
*
* Help is in the end of this script
*
function cutdata( args )
_version = '0.01r1'
rc = gsfallow( 'on' )
if( args = '' )
help()
return
endif
***** Default value *****
xmin = -9999
xmax = -9999
ymin = -9999
ymax = -9999
zmin = -9999
zmax = -9999
tmin = -9999
tmax = -9999
timemin = ''
timemax = ''
ctl_in = ''
grd_out = ''
vnames = ''
undef = ''
***** Arguement *****
i = 1
while( 1 )
arg = subwrd( args, i )
i = i + 1;
if( arg = '' ); break; endif
while( 1 )
*** option
if( arg = '-t' )
timemin = ''
timemax = ''
tmp = subwrd( args, i ); i = i + 1
* -t t1 t2
if( find( tmp, '-' ) <= 0 )
tmin = tmp
tmax = subwrd( args, i ); i = i + 1
if( valnum(tmax) != 1 )
say 'error: invalid time range (-t ' % tmin % ' ' % tmax % ')'
exit
endif
* -t [t1-t2]
else
tmin = rgnwrd( tmp, 1, 1, '-' )
tmin = substr( tmin, 2, math_strlen(tmin)-1 )
tmax = rgnwrd( tmp, 2, 2, '-' )
tmax = substr( tmax, 1, math_strlen(tmax)-1 )
flag_tmin = substr( tmp, 1, 1 )
if( flag_tmin = '(' ) ; tmin = tmin + 1 ; endif
flag_tmax = substr( tmp, math_strlen(tmp), 1 )
if( flag_tmax = ')' ) ; tmax = tmax - 1 ; endif
endif
break
endif
if( arg = '-time' )
tmin = -9999
tmax = -9999
tmp = subwrd( args, i ); i = i + 1
* -time time1 time2
if( find( tmp, '-' ) <= 0 )
timemin = tmp
timemax = subwrd( args, i ); i = i + 1
* -time [time1-time2]
else
timemin = rgnwrd( tmp, 1, 1, '-' )
timemin = substr( timemin, 2, math_strlen(timemin)-1 )
timemax = rgnwrd( tmp, 2, 2, '-' )
timemax = substr( timemax, 1, math_strlen(timemax)-1 )
flag_timemin = substr( tmp, 1, 1 )
flag_timemax = substr( tmp, math_strlen(tmp), 1 )
endif
break
endif
if( arg = '-x' )
xmin = subwrd( args, i ); i = i + 1
xmax = subwrd( args, i ); i = i + 1
break
endif
if( arg = '-y' )
ymin = subwrd( args, i ); i = i + 1
ymax = subwrd( args, i ); i = i + 1
break
endif
if( arg = '-z' )
zmin = subwrd( args, i ); i = i + 1
zmax = subwrd( args, i ); i = i + 1
break
endif
if( arg = '-undef' ) ; undef = subwrd( args, i ); i = i + 1; break; endif
if( arg = '-var' ) ; vnames = subwrd( args, i ); i = i + 1; break; endif
*** ctl_in, grd_out
if( ctl_in = '' ); ctl_in = arg; break; endif
if( grd_out = '' ); grd_out = arg; break; endif
say 'syntax error : 'arg
return
endwhile
endwhile
***** check arguments *****
if( ctl_in = '' )
say 'error: input control file name is not specified.'
exit
endif
if( grd_out = '' )
say 'error: output file name is not specified.'
exit
endif
ret = read( grd_out )
line = sublin( ret, 1 )
stat = subwrd( line, 1 )
if( stat = 0 )
say 'output file (' % grd_out % ') exists.'
exit
endif
***** open control file *****
'xopen 'ctl_in
f = last()
'set dfile 'f
if( vnames = '' )
vnames = qctlinfo( f, 'vlist', 0 )
vnames = strrep( vnames, ' ', ',' )
say vnames
endif
v = 1
while( 1 = 1 )
vname.v = rgnwrd( vnames, v, v, ',' )
if( vname.v = '' | vname.v = vnames ) ; break ; endif
v = v + 1
endwhile
if( vnames = vname.1 ) ; vmax = 1
else ; vmax = v - 1 ; endif
'set gxout fwrite'
'set fwrite -be 'grd_out
if( undef = '' )
'set undef dfile'
else
'set undef 'undef
endif
if( timemin != '' )
tmin = time2t( timemin )
tmax = time2t( timemax )
if( flag_timemin = '(' ) ; tmin = tmin + 1 ; endif
if( flag_timemax = ')' ) ; tmax = tmax - 1 ; endif
endif
xdef = qctlinfo( f, 'xdef', 1 )
ydef = qctlinfo( f, 'ydef', 1 )
zdef = qctlinfo( f, 'zdef', 1 )
tdef = qctlinfo( f, 'tdef', 1 )
if( xmin = -9999 ) ; xmin = 1 ; endif
if( xmax = -9999 ) ; xmax = xdef ; endif
if( ymin = -9999 ) ; ymin = 1 ; endif
if( ymax = -9999 ) ; ymax = ydef ; endif
if( zmin = -9999 ) ; zmin = 1 ; endif
if( zmax = -9999 ) ; zmax = zdef ; endif
if( tmin = -9999 ) ; tmin = 1 ; endif
if( tmax = -9999 ) ; tmax = tdef ; endif
***** cut data *****
prex( 'set x 'xmin' 'xmax )
prex( 'set y 'ymin' 'ymax )
say 'zmin=' % zmin % ', zmax=' % zmax
t = tmin
while( t <= tmax )
prompt ' t=' % t % ': '
'set t 't
v = 1
while( v <= vmax )
if( v >= 2 ) ; prompt ', ' ; endif
prompt vname.v
z = zmin
while( z <= zmax )
'set z 'z
'd 'vname.v
z = z + 1
endwhile
v = v + 1
endwhile
say ''
t = t + 1
endwhile
'disable fwrite'
'close 'f
'set gxout contour'
return
*
* help
*
function help()
say ' Name:'
say ' cutdata '_version' - cut data'
say ' '
say ' Usage:'
say ' cutdata input-ctl output-grd '
say ' [-x xmin xmax]'
say ' [-y ymin ymax]'
say ' [-z zmin zmax]'
say ' [-t tmin tmax]'
say ' [-t timestep-range | -time timemin tmimeax | -time time-range]'
say ' [-var vname1,vname2,...]'
say ' [-undef undefined-value]'
say ''
say ' input-ctl : Control file name for input.'
say ' output-grd : Output flat-binary file name'
say ' cutdata.gs stops if the file already exists.'
say ' -x xmin xmax : X range to cut.'
say ' -y ymin ymax : Y range to cut.'
say ' -z zmin zmax : Z range to cut.'
say ' -t tmin tmax : Time step range to cut.'
say ' -t timestep-range: Time step range to cut.'
say ' e.g. [1-3]: 1<=t<=3 is stored'
say ' e.g. [1-3): 1<=t<3 is stored'
say ' -time timemin timemax '
say ' : Time range to cut.'
say ' -time time-range: Time range to cut.'
say ' e.g. [01jan2000-02jan2000]: 01jan2000<=time<=02jan2000 is stored'
say ' e.g. [01jan2000-02jan2000): 01jan2000<=t<02jan2000 is stored'
say ' -var vname1,vname2,...'
say ' : List of variable names to cut'
say ' -undef undefined-value'
say ' : Undefined value for output data.'
say ''
say ' Note:'
say ' [arg-name] : specify if needed'
say ' (arg1 | arg2) : arg1 or arg2 must be specified'
say ' cutdata.gs depends on xopen.gs, last.gsf, qctlinfo.gsf, and rgnwrd.gsf.'
say ''
say ' Copyright (C) 2014-2014 Chihiro Kodama'
say ' Distributed under GNU GPL (http://www.gnu.org/licenses/gpl.html)'
say ''
return