forked from CohenBerkeleyLab/WRF-nco-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_wrf_output.sh
executable file
·219 lines (197 loc) · 6.53 KB
/
run_wrf_output.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
#!/bin/bash
#
# This script will run the necessary operations to process WRF-Chem output
# for use as BEHR a priori. There are 3 modes of processing, which should
# be set by modifying the "mode" variable below.
#
# "hourly" will just extract all profiles within the OMI overpass times
# of the continental US/CN, with the intention that the user will select the
# appropriate profile later.
# "daily" will average those profiles for each day based on the
# longitude and UTC time - profiles will be given more weight the closer
# they are to 1400 local standard time. Those more than an hour off will
# have a weight of 0.
# "monthly" will average with weights as in "daily" but over a month
# rather than a single day.
#
# "d02"(d03) will eclect wrfout_d02*(wrfout_d03*)
#
# This script does not directly perform any of those calculations, instead
# it collects the names of the WRF output files that belong
# to each group to pass to read_wrf_output.sh to do the actual calculation.
# This keeps the structure of this program the same as on the HPC cluster,
# where it needs to be done this way to launch multiple instances of
# read_wrf_output.sh in parallel.
# Josh Laughner <joshlaugh5@gmail.com> 2 Jul 2015
# modified Xin Zhang <xinzhang1215@gmail.com> May 2018
# Parse command arguments looking for two things: the averaging mode and which set of
# output quantities to copy/calculate. Credit to
# http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
# for the code outline.
while [[ $# > 0 ]]
do
keyin="$1"
# Ensure input is lower case
key=$(echo $keyin | awk '{print tolower($0)}')
case $key in
'monthly'|'daily'|'hourly')
mode=$key
shift # shift the input arguments left by one
;;
'behr'|'emis'|'avg')
varsout=$key
shift
;;
'd01'|'d02'|'d03')
domain=$key
shift
;;
'lnox'|'nolnox')
kind=$key
shift
;;
'keep'|'del')
choice=$key
shift
;;
*) # catch unrecognized arguments
echo "The argument \"$key\" is not recognized"
exit 1
;;
esac
done
# Set the defaults - averaging mode will default to "hourly"
# the outputs to "behr" and domain to "d01"
if [[ $mode == '' ]]
then
mode='hourly'
fi
if [[ $kind == '' ]] || [[ $kind != 'lnox' && $kind != 'nolnox' ]]
then
echo "Please input WRFKIND: lnox or nolnox"
exit 0
else
echo "kind set to $kind"
fi
if [[ $varsout == '' ]]
then
varsout='behr'
fi
if [[ $domain == '' ]]
then
domain='d01'
fi
if [[ $choice == '' ]]
then
choice='keep'
fi
# export the mode and the kind of wrfout*
# so that the child scripts can access it
export WRFPROCMODE=$mode
export WRFKIND=$kind
# Where the actual scripts (read_wrf_output.sh) are kept.
scriptdir='/data/home/wang/zx/BEHR/WRF-nco-tools'
export WRFSCRIPT_DIR=$scriptdir
# Where new wrfout* files are saved and check it
savedir='/data/home/wang/zx/data/history/processed'
if [[ $kind == "lnox" ]]
then
savedir=${savedir}"/lnox/"
else
savedir=${savedir}"/nolnox/"
fi
if [ ! -d "${savedir}" ]
then
mkdir -p $savedir
fi
export WRFSAVE_DIR=$savedir
# Check the mode selection
if [[ $mode != 'daily' && $mode != 'monthly' && $mode != 'hourly' ]]
then
echo "Input must be 'daily' or 'monthly'"
exit 1
else
echo "mode set to $mode"
fi
# Check the domain selection
if [ "$(echo wrfout_$domain*)" == "wrfout_$domain*" ]; then
echo "wrfout_$domain* not found. Please input correct domain like d02"
fi
# Find all unique dates - we'll need this to iterate over each day
# If we're doing monthly averages, then we just need to get the year and month
dates=''
olddate=''
for file in ./wrfout_$domain*
do
# Handle wrfout and wrfout_subset files
dtmp=$(awk -v a="$file" -v b="$domain" 'BEGIN{print index(a,b)}')
dstart=$((dtmp+3))
if [[ $mode == 'monthly' ]]
then
newdate=${file:$dstart:7}
else
newdate=${file:$dstart:10}
fi
if [[ $olddate != $newdate ]]
then
dates=$(echo $dates $newdate)
fi
olddate=$newdate
done
for day in $dates
do
echo ""
echo "Files on $day"
echo ""
# Firstly, we need to ensure wrfout* files are enough to process.
# For example, history_interval = 30 needs 48 files each day.
# Then, we should check if all of these are finished by wrf_write.
# Xin Zhang <xinzhang1215@gmail.com> Jul 2018
wrf_files=$(echo wrfout*_"$domain"_${day}_*)
len_wrf="$(wc -w <<< ${wrf_files})"
if [[ ${len_wrf} == 48 ]]
then
# Ensure that size of the last wrfout file is as same as others.
size_1=$(stat -c %s wrfout_"$domain"_${day}_00:00:00)
size_2=$(stat -c %s wrfout_"$domain"_${day}_23:30:00)
if [[ $size_1 == $size_2 ]]
then
# WRF file names include output time in UTC. We'll look for the output
# in the range of UTC times when OMI will be passing over domain
# for this day
# North America: {18,19,20,21,22}; add 16,17 for Gulf of Mexico #Xin 16 June 2018
# Southeast China: {06,07}
# If there are no files for this day or month, then it will try to iterate
# over the wildcard patterns themselves. Since those contain *, we
# can avoid doing anything in that case by requiring that the file
# name does not include a *
filepattern=$(echo wrfout*_"$domain"_${day}_{16,17,18,19,20,21,22}*)
if [[ $filepattern != *'*'* ]]
then
echo -e "Select these wrfout* files:\n $filepattern"
echo "$filepattern" > read_wrf.conf
# Choose which command to execute based on the command arguments
if [[ $varsout == 'behr' ]]
then
echo "Calling read_wrf_output"
echo "Save to $savedir"
$scriptdir/read_wrf_output.sh read_wrf.conf
elif [[ $varsout == 'emis' ]]
then
$scriptdir/read_wrf_emis.sh read_wrf.conf
elif [[ $varsout == 'avg' ]]
then
$scriptdir/avg_wrf_output.sh read_wrf.conf
else
echo "Error at $LINENO in slurmrun_wrf_output.sh: \"$varsout\" is not a recognized operation"
exit 1
fi
fi
# remove wrfout* files
if [[ $choice == 'del' ]]
then
rm wrfout_"$domain"_${day}_*
fi
fi
fi
done