-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrftailor.sh
executable file
·338 lines (320 loc) · 15.4 KB
/
wrftailor.sh
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/bash
# Contact person: Amirhossein Nikfal <https://github.com/anikfal>
export app_dir=$(pwd)
awk_read_onoff() {
awk -v pat=$1 '$0~pat {print $3}' namelist.tailor
}
is_numeric() {
if [[ $1 =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
return 0 # Return success (true) if it's numeric
else
return 1 # Return failure (false) if it's not numeric
fi
}
shapeonoff=$(awk_read_onoff shapefile_ON_OFF)
boundonoff=$(awk_read_onoff bounding_box_ON_OFF)
pointsonoff=$(awk_read_onoff points_list_ON_OFF)
wholeonoff=$(awk_read_onoff whole_domain_ON_OFF)
geotiffonoff=$(awk_read_onoff geotiff_replace_ON_OFF)
sumopts=$((shapeonoff + boundonoff + pointsonoff + wholeonoff + geotiffonoff))
if [[ $sumopts -gt 1 ]]; then
echo " Warning: more than one task is enabled"
echo " Select only one task in namelist.tailor and run again"
exit
fi
if [[ $sumopts -eq 0 ]]; then
echo " Warning: No section is activated"
echo " Select one task or section in namelist.wrf and run again"
exit
fi
function countline() {
numlinevars=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | awk -F',' '{ print NF }')
ifendcomma=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | awk -F "," '{print $NF}' | tr -d " ")
if [[ $ifendcomma == "" ]]; then
numlinevars=$((numlinevars - 1))
fi
}
export number_of_domains=$(sed -n "/number_of_domains/s/.*=//p" namelist.tailor | tr -d " ")
if [[ $number_of_domains -gt 5 ]]; then
echo Warning!
echo number_of_domains cannot be more than 5
echo set the correct number_of_domains and run again
exit
fi
export domain_1=$(sed -n "/domain_1/s/.*=//p" namelist.tailor | tr -d " ")
export domain_2=$(sed -n "/domain_2/s/.*=//p" namelist.tailor | tr -d " ")
export domain_3=$(sed -n "/domain_3/s/.*=//p" namelist.tailor | tr -d " ")
export domain_4=$(sed -n "/domain_4/s/.*=//p" namelist.tailor | tr -d " ")
export domain_5=$(sed -n "/domain_5/s/.*=//p" namelist.tailor | tr -d " ")
if [[ $pointsonoff == 1 ]]; then
export wrf_variable=$(sed -n "/target_variable3/s/.*=//p" namelist.tailor | tr -d " ")
export variable_level=$(sed -n "/target_var_level3/s/.*=//p" namelist.tailor | tr -d " ")
myvar="point_values"
countline
export nclpoints=$numlinevars #Zero (0) is included in the line numbers
#Extracting Variables into array
varcount=0
while [ $varcount -lt $nclpoints ]; do
locpoints[$varcount]=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | cut -d, -f$((varcount + 1)))
locpoints[$varcount]=$(echo ${locpoints[$varcount]}) #Remove spaces
varcount=$((varcount + 1))
done
unset varcount
varcount=0
while [ $varcount -lt $nclpoints ]; do
declare ncllocpoints$varcount=${locpoints[$varcount]}
export ncllocpoints$varcount
varcount=$((varcount + 1))
done
unset myvar
myvar="latitudes_list"
countline
export ncllats=$numlinevars #Zero (0) is included in the line numbers
#Extracting Variables into array
varcount=0
while [ $varcount -lt $ncllats ]; do
loclats[$varcount]=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | cut -d, -f$((varcount + 1)))
loclats[$varcount]=$(echo ${loclats[$varcount]}) #Remove spaces
varcount=$((varcount + 1))
done
unset varcount
varcount=0
while [ $varcount -lt $ncllats ]; do
declare nclloclats$varcount=${loclats[$varcount]}
export nclloclats$varcount
varcount=$((varcount + 1))
done
unset myvar
myvar="longitudes_list"
countline
ncllons=$numlinevars #Zero (0) is included in the line numbers
if [[ $ncllats -ne $ncllons || $ncllons -ne $nclpoints ]]; then
echo "Warning: Number of elements for latitudes_list, longitudes_list, and point_values are $ncllats, $ncllons, and $nclpoints. But they must be equal."
echo Exiting ..
exit
fi
#Extracting Vairables into array
varcount=0
while [ $varcount -lt $ncllons ]; do
loclons[$varcount]=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | cut -d, -f$((varcount + 1)))
loclons[$varcount]=$(echo ${loclons[$varcount]}) #Remove spaces
varcount=$((varcount + 1))
done
unset varcount
varcount=0
while [ $varcount -lt $ncllons ]; do
declare nclloclons$varcount=${loclons[$varcount]}
export nclloclons$varcount
varcount=$((varcount + 1))
done
unset myvar
echo $wrf_new_variable >$app_dir"/modules/totalequation.txt"
cd $app_dir/modules
ncl separation.ncl >/dev/null
filename="bounding.ncl"
filename_copy=$filename"_copy"
sed '/added_new_line_by_sed/ d' $filename >$filename_copy #cleaning previous vars added by sed
mv $filename_copy $filename #recycling the code to its prestine condition
count=$(cat variables.txt | wc -l)
mm=0
while [ $mm -lt $count ]; do
onevar[$mm]=$(sed -n "$((mm + 1)) p" variables.txt)
sed '/shell script/ a '${onevar[$mm]}' := varlist['$mm'] ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
mm=$((mm + 1))
done
equation=$(cat totalequation.txt)
sed '/equation from namelist.wrf/ a polynomial := '$equation' ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
ncl -Qn points.ncl
fi
if [[ $wholeonoff == 1 ]]; then
export wrf_variable=$(sed -n "/target_variable4/s/.*=//p" namelist.tailor | tr -d " ")
export variable_level=$(sed -n "/target_var_level4/s/.*=//p" namelist.tailor | tr -d " ")
export wrf_new_variable=$(sed -n "/substitute_variable4/s/.*=//p" namelist.tailor | tr -d " ")
if is_numeric $wrf_new_variable; then
wrf_new_variable="$wrf_variable*0+$wrf_new_variable"
fi
echo $wrf_new_variable >$app_dir"/modules/totalequation.txt"
myvar="substitute_var_levels4"
countline
export substitutenumber=$numlinevars #Zero (0) is included in the line numbers
#Extracting Variables into array
varcount=0
while [ $varcount -lt $substitutenumber ]; do
sublevels[$varcount]=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | cut -d, -f$((varcount + 1)))
sublevels[$varcount]=$(echo ${sublevels[$varcount]}) #Remove spaces
varcount=$((varcount + 1))
done
varcount=0
while [ $varcount -lt $substitutenumber ]; do
declare sublevels$varcount=${sublevels[$varcount]}
export sublevels$varcount
varcount=$((varcount + 1))
done
cd $app_dir/modules
ncl separation.ncl >/dev/null
filename="whole_domain.ncl"
filename_copy=$filename"_copy"
sed '/added_new_line_by_sed/ d' $filename >$filename_copy #cleaning previous vars added by sed
mv $filename_copy $filename #recycling the code to its prestine condition
count=$(cat variables.txt | wc -l)
mm=0
while [ $mm -lt $count ]; do
onevar[$mm]=$(sed -n "$((mm + 1)) p" variables.txt)
sed '/shell script/a \
'${onevar[$mm]}' := varlist['$mm'] ;;;added_new_line_by_sed \
vardim := dimsizes('${onevar[$mm]}') ;;;added_new_line_by_sed \
if (dimsizes(vardim) .eq. 4) then ;;;added_new_line_by_sed \
dimnames = getvardims('${onevar[$mm]}') ;;;added_new_line_by_sed \
if ((sublevels('$mm') .gt. (vardim(1)-1)) .or. (sublevels('$mm') .lt. 0)) then ;;;added_new_line_by_sed \
selected_sublevel = sublevels('$mm')+1 ;;;added_new_line_by_sed \
print("Warning: " + "substitute_var_levels2 for " + NCLvarnames('$mm') + \\ ;;;added_new_line_by_sed \
" (" + '${onevar[$mm]}'@description + ") in namelist.tailor is " + selected_sublevel + \\ ;;;added_new_line_by_sed \
". It should be between 1 to " + vardim(1) + " (maximum number of " + dimnames(1) + ").") ;;;added_new_line_by_sed \
print("Exiting ..") ;;;added_new_line_by_sed \
exit() ;;;added_new_line_by_sed \
end if ;;;added_new_line_by_sed \
'${onevar[$mm]}' := '${onevar[$mm]}'(:, sublevels('$mm'), :, :) ;;;added_new_line_by_sed \
end if ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
mm=$((mm + 1))
done
equation=$(cat totalequation.txt)
sed '/equation from namelist.wrf/ a polynomial := '$equation' ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
ncl -Qn whole_domain.ncl
fi
if [[ $shapeonoff == 1 ]]; then
export shape_path=$(sed -n "/path_to_shapefile/s/.*=//p" namelist.tailor | tr -d " ")
export wrf_variable=$(sed -n "/target_variable1/s/.*=//p" namelist.tailor | tr -d " ")
export variable_level=$(sed -n "/target_var_level1/s/.*=//p" namelist.tailor | tr -d " ")
wrf_new_variable=$(sed -n "/substitute_variable1/s/.*=//p" namelist.tailor | tr -d " ")
export inverse_mask_on_off=$(awk_read_onoff inverse_mask_on_off)
myvar="substitute_var_levels1"
countline
export substitutenumber=$numlinevars #Zero (0) is included in the line numbers
#Extracting Variables into array
varcount=0
while [ $varcount -lt $substitutenumber ]; do
sublevels[$varcount]=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | cut -d, -f$((varcount + 1)))
sublevels[$varcount]=$(echo ${sublevels[$varcount]}) #Remove spaces
varcount=$((varcount + 1))
done
varcount=0
while [ $varcount -lt $substitutenumber ]; do
declare sublevels$varcount=${sublevels[$varcount]}
export sublevels$varcount
varcount=$((varcount + 1))
done
echo $wrf_new_variable >$app_dir"/modules/totalequation.txt"
cd $app_dir/modules
ncl separation.ncl >/dev/null
filename="shapefile.ncl"
filename_copy=$filename"_copy"
sed '/added_new_line_by_sed/ d' $filename >$filename_copy #cleaning previous vars added by sed
mv $filename_copy $filename #recycling the code to its prestine condition
count=$(cat variables.txt | wc -l)
mm=0
while [ $mm -lt $count ]; do
onevar[$mm]=$(sed -n "$((mm + 1)) p" variables.txt)
sed '/shell script/a \
'${onevar[$mm]}' := varlist['$mm'] ;;;added_new_line_by_sed \
vardim := dimsizes('${onevar[$mm]}') ;;;added_new_line_by_sed \
if (dimsizes(vardim) .eq. 4) then ;;;added_new_line_by_sed \
dimnames = getvardims('${onevar[$mm]}') ;;;added_new_line_by_sed \
if ((sublevels('$mm') .gt. (vardim(1)-1)) .or. (sublevels('$mm') .lt. 0)) then ;;;added_new_line_by_sed \
selected_sublevel = sublevels('$mm')+1 ;;;added_new_line_by_sed \
print("Warning: " + "substitute_var_levels1 for " + NCLvarnames('$mm') + \\ ;;;added_new_line_by_sed \
" (" + '${onevar[$mm]}'@description + ") in namelist.tailor is " + selected_sublevel + \\ ;;;added_new_line_by_sed \
". It should be between 1 to " + vardim(1) + " (maximum number of " + dimnames(1) + ").") ;;;added_new_line_by_sed \
print("Exiting ..") ;;;added_new_line_by_sed \
exit() ;;;added_new_line_by_sed \
end if ;;;added_new_line_by_sed \
'${onevar[$mm]}' := '${onevar[$mm]}'(:, sublevels('$mm'), :, :) ;;;added_new_line_by_sed \
end if ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
mm=$((mm + 1))
done
equation=$(cat totalequation.txt)
sed '/equation from namelist.wrf/ a polynomial := '$equation' ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
ncl -Qn shapefile.ncl
fi
if [[ $boundonoff == 1 ]]; then
export north_lat=$(sed -n "/north_lat/s/.*=//p" namelist.tailor | tr -d " ")
export south_lat=$(sed -n "/south_lat/s/.*=//p" namelist.tailor | tr -d " ")
export west_long=$(sed -n "/west_long/s/.*=//p" namelist.tailor | tr -d " ")
export east_long=$(sed -n "/east_long/s/.*=//p" namelist.tailor | tr -d " ")
export wrf_new_variable=$(sed -n "/substitute_variable2/s/.*=//p" namelist.tailor | tr -d " ")
export wrf_variable=$(sed -n "/target_variable2/s/.*=//p" namelist.tailor | tr -d " ")
export variable_level=$(sed -n "/target_var_level2/s/.*=//p" namelist.tailor | tr -d " ")
myvar="substitute_var_levels2"
countline
export substitutenumber=$numlinevars #Zero (0) is included in the line numbers
#Extracting Variables into array
varcount=0
while [ $varcount -lt $substitutenumber ]; do
sublevels[$varcount]=$(sed -n "/$myvar/p" namelist.tailor | awk -F"=" '{print $NF}' | cut -d, -f$((varcount + 1)))
sublevels[$varcount]=$(echo ${sublevels[$varcount]}) #Remove spaces
varcount=$((varcount + 1))
done
varcount=0
while [ $varcount -lt $substitutenumber ]; do
declare sublevels$varcount=${sublevels[$varcount]}
export sublevels$varcount
varcount=$((varcount + 1))
done
echo $wrf_new_variable >$app_dir"/modules/totalequation.txt"
cd $app_dir/modules
ncl separation.ncl >/dev/null
filename="bounding.ncl"
filename_copy=$filename"_copy"
sed '/added_new_line_by_sed/ d' $filename >$filename_copy #cleaning previous vars added by sed
mv $filename_copy $filename #recycling the code to its prestine condition
count=$(cat variables.txt | wc -l)
mm=0
while [ $mm -lt $count ]; do
onevar[$mm]=$(sed -n "$((mm + 1)) p" variables.txt)
sed '/shell script/a \
'${onevar[$mm]}' := varlist['$mm'] ;;;added_new_line_by_sed \
vardim := dimsizes('${onevar[$mm]}') ;;;added_new_line_by_sed \
if (dimsizes(vardim) .eq. 4) then ;;;added_new_line_by_sed \
dimnames = getvardims('${onevar[$mm]}') ;;;added_new_line_by_sed \
if ((sublevels('$mm') .gt. (vardim(1)-1)) .or. (sublevels('$mm') .lt. 0)) then ;;;added_new_line_by_sed \
selected_sublevel = sublevels('$mm')+1 ;;;added_new_line_by_sed \
print("Warning: " + "substitute_var_levels2 for " + NCLvarnames('$mm') + \\ ;;;added_new_line_by_sed \
" (" + '${onevar[$mm]}'@description + ") in namelist.tailor is " + selected_sublevel + \\ ;;;added_new_line_by_sed \
". It should be between 1 to " + vardim(1) + " (maximum number of " + dimnames(1) + ").") ;;;added_new_line_by_sed \
print("Exiting ..") ;;;added_new_line_by_sed \
exit() ;;;added_new_line_by_sed \
end if ;;;added_new_line_by_sed \
'${onevar[$mm]}' := '${onevar[$mm]}'(:, sublevels('$mm'), :, :) ;;;added_new_line_by_sed \
end if ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
mm=$((mm + 1))
done
equation=$(cat totalequation.txt)
sed '/equation from namelist.wrf/ a polynomial := '$equation' ;;;added_new_line_by_sed' $filename >$filename_copy
mv $filename_copy $filename
# exit
ncl -Qn bounding.ncl
fi
if [[ $geotiffonoff == 1 ]]; then
gdal_translate --version >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Warning: gdal_translate must be installed on the system."
echo "Exiting ..."
exit
fi
export geotiff_file=$(sed -n "/geotiff_file/s/.*=//p" namelist.tailor | tr -d " ")
export wrf_variable=$(sed -n "/target_variable5/s/.*=//p" namelist.tailor | tr -d " ")
export variable_level=$(sed -n "/target_var_level5/s/.*=//p" namelist.tailor | tr -d " ")
cd $app_dir/modules
filename=$(basename $geotiff_file)
export tiff2nc=$filename".nc"
echo "Converting GeoTIFF to NetCDF ..."
gdal_translate -of NetCDF $geotiff_file $tiff2nc 1>/dev/null
ncl -Q geotiff.ncl
fi
#sed -n '/whole_domain_ON_OFF/,${/target_variable4/{p;q}}' namelist.tailor