-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_snowmodel_ak.sh
executable file
·399 lines (349 loc) · 16.2 KB
/
run_snowmodel_ak.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#! /bin/bash
#This script will execute the complete assimilative snowmodel run for the western WY
#domain. See inline comments for further explanation. This version of the code does a
#complete run from Oct 1 of current water year to today - (3days). Only the last time
#slice is extracted, saved, and uploaded.
#Note, we need to do a number of things that are related to the date of the model run.
#this includes a reset the time axis to start 1 Oct of present water year. Get today's
#date info and figure out year of current water year. Let's do that up front in
#this script. We do this since the model run takes a long time (12+ hours) and may
#conclude TOMORROW and not today. We want today's date.
day=$(date '+%d')
month=$(date '+%b')
monthnum=$(date '+%m')
year=$(date '+%Y')
if [ $((10#$monthnum)) -lt 10 ]
then
year=$(($year - 1))
elif [ $((10#$monthnum)) -eq 10 ]
then
if [ $day -lt 3 ]
then
year=$(($year - 1))
fi
fi
#also, we are ultimately going to extract just the 'last' time slice, so let's figure
#out the time stamp for that. Get date string from three days ago
d=$(date --date="2 days ago" '+%d')
m=$(date --date="2 days ago" '+%m')
y=$(date --date="2 days ago" '+%Y')
STAMP="${y}_${m}_${d}"
################################
#run the query for met data
cd /nfs/depot/cce_u1/hill/dfh/op_snowmodel/op_snowmodel_python_scripts
source /nfs/attic/dfh/miniconda/bin/activate ee
ipython met_data_ak.py
conda deactivate
################################
#run assim snowmodel...(review that script closely for details, there is a lot going on)
cd /nfs/depot/cce_u1/hill/dfh/op_snowmodel/op_snowmodel_python_scripts
source /nfs/attic/dfh/miniconda/bin/activate snowmodelcal
ipython assim_ak.py
conda deactivate
echo
echo "snowmodel has finished"
echo
################################
#time to clean up a bit...delete ssmt and sspr grads files, both in
#the wo_assim and the wi_assim folders. We just don't need them.
smpath="/nfs/depot/cce_u1/hill/dfh/op_snowmodel/ak_snowmodel/"
#define output path on scratch
outpath="/scratch/op_snowmodel_outputs/AK/"
rm "${smpath}outputs/wi_assim/ssmt.gdat"
rm "${smpath}outputs/wi_assim/sspr.gdat"
rm "${smpath}outputs/wo_assim/ssmt.gdat"
rm "${smpath}outputs/wo_assim/sspr.gdat"
################################
#Next, a TON of data repackaging.
#convert the grads output to .nc
/scratch/cdo/bin/cdo -f nc import_binary "${smpath}ctl_files/wo_assim/swed.ctl" "${smpath}ctl_files/wo_assim/swed.nc"
/scratch/cdo/bin/cdo -f nc import_binary "${smpath}ctl_files/wi_assim/swed.ctl" "${smpath}ctl_files/wi_assim/swed.nc"
/scratch/cdo/bin/cdo -f nc import_binary "${smpath}ctl_files/wo_assim/snod.ctl" "${smpath}ctl_files/wo_assim/snod.nc"
/scratch/cdo/bin/cdo -f nc import_binary "${smpath}ctl_files/wi_assim/snod.ctl" "${smpath}ctl_files/wi_assim/snod.nc"
echo
echo "done converting to nc"
echo
#clean up more...we can deled snod and swed gdats (huge files) since we have the .nc now
rm "${smpath}outputs/wi_assim/swed.gdat"
rm "${smpath}outputs/wi_assim/snod.gdat"
rm "${smpath}outputs/wo_assim/swed.gdat"
rm "${smpath}outputs/wo_assim/snod.gdat"
#use cdo to reset the time axis.
/scratch/cdo/bin/cdo settaxis,$year-10-01,00:00:00,1days "${smpath}ctl_files/wo_assim/swed.nc" "${smpath}ctl_files/wo_assim/swed2.nc"
/scratch/cdo/bin/cdo settaxis,$year-10-01,00:00:00,1days "${smpath}ctl_files/wi_assim/swed.nc" "${smpath}ctl_files/wi_assim/swed2.nc"
/scratch/cdo/bin/cdo settaxis,$year-10-01,00:00:00,1days "${smpath}ctl_files/wo_assim/snod.nc" "${smpath}ctl_files/wo_assim/snod2.nc"
/scratch/cdo/bin/cdo settaxis,$year-10-01,00:00:00,1days "${smpath}ctl_files/wi_assim/snod.nc" "${smpath}ctl_files/wi_assim/snod2.nc"
echo
echo " done resetting time axis"
echo
#next, we are going to mess with the .nc metadata. CDO treats files as lon / lat, whereas
#we have projected coords out of SnowModel.
# DO SWED (WO_ASSIM) FIRST
################################
#First, change variable and dimension names
infile1="${smpath}ctl_files/wo_assim/swed2.nc"
outfile1="${smpath}ctl_files/wo_assim/swed3.nc"
ncrename -O -v .lat,projection_y_coordinate -d .lat,projection_y_coordinate -v .lon,projection_x_coordinate -d .lon,projection_x_coordinate "${infile1}" "${outfile1}"
#Next, change attributes
ncatted -O -a long_name,projection_y_coordinate,o,c,y "${outfile1}"
ncatted -O -a standard_name,projection_y_coordinate,o,c,y "${outfile1}"
ncatted -O -a long_name,projection_x_coordinate,o,c,x "${outfile1}"
ncatted -O -a standard_name,projection_x_coordinate,o,c,x "${outfile1}"
#finally, to change units requires that we know something from the .par file. See
#my writeup on code changes regarding the grid options for the ctl files. Currently,
#line 151 of the par file has the flag that controls the grid (index, m, km, etc.)
parfile="${smpath}snowmodel.par"
unitchoice=$(sed -n '151p' "${parfile}")
unitchoice="${unitchoice:0:1}"
#check on unitchoice and change .nc file appropriately
if [ $unitchoice -eq 1 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,index "${outfile1}"
ncatted -O -a units,projection_x_coordinate,o,c,index "${outfile1}"
elif [ $unitchoice -eq 2 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile1}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile1}"
elif [ $unitchoice -eq 3 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile1}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile1}"
elif [ $unitchoice -eq 4 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile1}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile1}"
elif [ $unitchoice -eq 5 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile1}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile1}"
fi
# DO SWED (WI_ASSIM) next
################################
#First, change variable and dimension names
infile2="${smpath}ctl_files/wi_assim/swed2.nc"
outfile2="${smpath}ctl_files/wi_assim/swed3.nc"
ncrename -O -v .lat,projection_y_coordinate -d .lat,projection_y_coordinate -v .lon,projection_x_coordinate -d .lon,projection_x_coordinate "${infile2}" "${outfile2}"
#Next, change attributes
ncatted -O -a long_name,projection_y_coordinate,o,c,y "${outfile2}"
ncatted -O -a standard_name,projection_y_coordinate,o,c,y "${outfile2}"
ncatted -O -a long_name,projection_x_coordinate,o,c,x "${outfile2}"
ncatted -O -a standard_name,projection_x_coordinate,o,c,x "${outfile2}"
#check on unitchoice and change .nc file appropriately
if [ $unitchoice -eq 1 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,index "${outfile2}"
ncatted -O -a units,projection_x_coordinate,o,c,index "${outfile2}"
elif [ $unitchoice -eq 2 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile2}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile2}"
elif [ $unitchoice -eq 3 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile2}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile2}"
elif [ $unitchoice -eq 4 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile2}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile2}"
elif [ $unitchoice -eq 5 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile2}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile2}"
fi
# DO SNOD (WO_ASSIM) next
################################
#First, change variable and dimension names
infile3="${smpath}ctl_files/wo_assim/snod2.nc"
outfile3="${smpath}ctl_files/wo_assim/snod3.nc"
ncrename -O -v .lat,projection_y_coordinate -d .lat,projection_y_coordinate -v .lon,projection_x_coordinate -d .lon,projection_x_coordinate "${infile3}" "${outfile3}"
#Next, change attributes
ncatted -O -a long_name,projection_y_coordinate,o,c,y "${outfile3}"
ncatted -O -a standard_name,projection_y_coordinate,o,c,y "${outfile3}"
ncatted -O -a long_name,projection_x_coordinate,o,c,x "${outfile3}"
ncatted -O -a standard_name,projection_x_coordinate,o,c,x "${outfile3}"
#check on unitchoice and change .nc file appropriately
if [ $unitchoice -eq 1 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,index "${outfile3}"
ncatted -O -a units,projection_x_coordinate,o,c,index "${outfile3}"
elif [ $unitchoice -eq 2 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile3}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile3}"
elif [ $unitchoice -eq 3 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile3}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile3}"
elif [ $unitchoice -eq 4 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile3}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile3}"
elif [ $unitchoice -eq 5 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile3}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile3}"
fi
# DO SNOD (WI_ASSIM) last
################################
#First, change variable and dimension names
infile4="${smpath}ctl_files/wi_assim/snod2.nc"
outfile4="${smpath}ctl_files/wi_assim/snod3.nc"
ncrename -O -v .lat,projection_y_coordinate -d .lat,projection_y_coordinate -v .lon,projection_x_coordinate -d .lon,projection_x_coordinate "${infile4}" "${outfile4}"
#Next, change attributes
ncatted -O -a long_name,projection_y_coordinate,o,c,y "${outfile4}"
ncatted -O -a standard_name,projection_y_coordinate,o,c,y "${outfile4}"
ncatted -O -a long_name,projection_x_coordinate,o,c,x "${outfile4}"
ncatted -O -a standard_name,projection_x_coordinate,o,c,x "${outfile4}"
#check on unitchoice and change .nc file appropriately
if [ $unitchoice -eq 1 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,index "${outfile4}"
ncatted -O -a units,projection_x_coordinate,o,c,index "${outfile4}"
elif [ $unitchoice -eq 2 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile4}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile4}"
elif [ $unitchoice -eq 3 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile4}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile4}"
elif [ $unitchoice -eq 4 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,meters "${outfile4}"
ncatted -O -a units,projection_x_coordinate,o,c,meters "${outfile4}"
elif [ $unitchoice -eq 5 ]
then
ncatted -O -a units,projection_y_coordinate,o,c,kilometers "${outfile4}"
ncatted -O -a units,projection_x_coordinate,o,c,kilometers "${outfile4}"
fi
echo
echo "done editing metadata"
echo
################################
#cool, so our entire water year of SWED and SNOD is now converted over properly to .nc.
#Let us now extract just the final day and rename it appropriately. First, figure out
#number of time steps in the file.
numsteps=$(/scratch/cdo/bin/cdo -ntime "${outfile4}")
echo $numsteps
#do swed wo_assim
singleday="${smpath}ctl_files/wo_assim/${STAMP}_swed_wo_assim.nc"
/scratch/cdo/bin/cdo -seltimestep,$numsteps $outfile1 $singleday
#do swed wi_assim
singleday="${smpath}ctl_files/wi_assim/${STAMP}_swed_wi_assim.nc"
/scratch/cdo/bin/cdo -seltimestep,$numsteps $outfile2 $singleday
#do snod wo_assim
singleday="${smpath}ctl_files/wo_assim/${STAMP}_snod_wo_assim.nc"
/scratch/cdo/bin/cdo -seltimestep,$numsteps $outfile3 $singleday
#do snod wi_assim
singleday="${smpath}ctl_files/wi_assim/${STAMP}_snod_wi_assim.nc"
/scratch/cdo/bin/cdo -seltimestep,$numsteps $outfile4 $singleday
#clean up a bit
rm "${outfile1}"
rm "${infile1}"
rm "${outfile2}"
rm "${infile2}"
rm "${outfile3}"
rm "${infile3}"
rm "${outfile4}"
rm "${infile4}"
################################
#next, we want to convert this .nc to a geotiff. We can do this with gdal. The synatx:
#>>gdal_translate -of GTiff -a_srs EPSG:xxxx file.nc file.tif
#the -of GTiff requests tiff as output format. The -a_srs EPSG:xxxx sets the projection
#the -a_ullr fixes the weird 'shift' issue we have been having!
fin="${smpath}ctl_files/wo_assim/${STAMP}_swed_wo_assim.nc"
fout="${smpath}ctl_files/wo_assim/${STAMP}_swed_wo_assim.tif"
gdal_translate -q -of GTiff -a_srs EPSG:3338 -a_ullr 230450 1252650 294850 1181350 $fin $fout
#clean up
rm "${fin}"
fin="${smpath}ctl_files/wi_assim/${STAMP}_swed_wi_assim.nc"
fout="${smpath}ctl_files/wi_assim/${STAMP}_swed_wi_assim.tif"
gdal_translate -q -of GTiff -a_srs EPSG:3338 -a_ullr 230450 1252650 294850 1181350 $fin $fout
#clean up
rm "${fin}"
fin="${smpath}ctl_files/wo_assim/${STAMP}_snod_wo_assim.nc"
fout="${smpath}ctl_files/wo_assim/${STAMP}_snod_wo_assim.tif"
gdal_translate -q -of GTiff -a_srs EPSG:3338 -a_ullr 230450 1252650 294850 1181350 $fin $fout
#clean up
rm "${fin}"
fin="${smpath}ctl_files/wi_assim/${STAMP}_snod_wi_assim.nc"
fout="${smpath}ctl_files/wi_assim/${STAMP}_snod_wi_assim.tif"
gdal_translate -q -of GTiff -a_srs EPSG:3338 -a_ullr 230450 1252650 294850 1181350 $fin $fout
#clean up
rm "${fin}"
echo
echo "done converting to geotiff"
echo
################################
#next, let's set values of swe less than 0.001 m to be 'nodata' values. In this way,
#when we plot the tif, those cells will be transparent. I tested this in QGIS and they do show up
#transparent. Need to deactivate conda and then reactivate it, in order to access
#gdal_calc. Do same for hs less than 0.01 m
source /nfs/attic/dfh/miniconda/bin/activate cso
fin="${smpath}ctl_files/wo_assim/${STAMP}_swed_wo_assim.tif"
fout="${smpath}ctl_files/wo_assim/${STAMP}_mask_swed_wo_assim.tif"
python /nfs/attic/dfh/miniconda/envs/cso/bin/gdal_calc.py -A $fin --outfile=$fout --calc="A*(A>0.001)" --NoDataValue=0 --quiet
#clean up
rm "${fin}"
fin="${smpath}ctl_files/wi_assim/${STAMP}_swed_wi_assim.tif"
fout="${smpath}ctl_files/wi_assim/${STAMP}_mask_swed_wi_assim.tif"
python /nfs/attic/dfh/miniconda/envs/cso/bin/gdal_calc.py -A $fin --outfile=$fout --calc="A*(A>0.001)" --NoDataValue=0 --quiet
#clean up
rm "${fin}"
fin="${smpath}ctl_files/wo_assim/${STAMP}_snod_wo_assim.tif"
fout="${smpath}ctl_files/wo_assim/${STAMP}_mask_snod_wo_assim.tif"
python /nfs/attic/dfh/miniconda/envs/cso/bin/gdal_calc.py -A $fin --outfile=$fout --calc="A*(A>0.01)" --NoDataValue=0 --quiet
rm "${fin}"
fin="${smpath}ctl_files/wi_assim/${STAMP}_snod_wi_assim.tif"
fout="${smpath}ctl_files/wi_assim/${STAMP}_mask_snod_wi_assim.tif"
python /nfs/attic/dfh/miniconda/envs/cso/bin/gdal_calc.py -A $fin --outfile=$fout --calc="A*(A>0.01)" --NoDataValue=0 --quiet
rm "${fin}"
conda deactivate
echo
echo "done masking"
echo
################################
#cloud optimized geotiff...
fin="${smpath}ctl_files/wo_assim/${STAMP}_mask_swed_wo_assim.tif"
fout="${smpath}ctl_files/wo_assim/${STAMP}_mask_cog_swed_wo_assim.tif"
gdal_translate -q $fin $fout -co TILED=YES -co COPY_SRC_OVERVIEWS=YES -co COMPRESS=DEFLATE
rm -f $fin
fin="${smpath}ctl_files/wo_assim/${STAMP}_swed_wo_assim.tif"
mv $fout $fin
rm -f $fout
gsutil cp $fin gs://cso_test_upload/ak_domain/swed_wo_assim/
#let's move it to /scratch and get it off of depot
mv $fin "${outpath}${STAMP}_swed_wo_assim.tif"
fin="${smpath}ctl_files/wi_assim/${STAMP}_mask_swed_wi_assim.tif"
fout="${smpath}ctl_files/wi_assim/${STAMP}_mask_cog_swed_wi_assim.tif"
gdal_translate -q $fin $fout -co TILED=YES -co COPY_SRC_OVERVIEWS=YES -co COMPRESS=DEFLATE
rm -f $fin
fin="${smpath}ctl_files/wi_assim/${STAMP}_swed_wi_assim.tif"
mv $fout $fin
rm -f $fout
gsutil cp $fin gs://cso_test_upload/ak_domain/swed_wi_assim/
#let's move it to /scratch and get it off of depot
mv $fin "${outpath}${STAMP}_swed_wi_assim.tif"
fin="${smpath}ctl_files/wo_assim/${STAMP}_mask_snod_wo_assim.tif"
fout="${smpath}ctl_files/wo_assim/${STAMP}_mask_cog_snod_wo_assim.tif"
gdal_translate -q $fin $fout -co TILED=YES -co COPY_SRC_OVERVIEWS=YES -co COMPRESS=DEFLATE
rm -f $fin
fin="${smpath}ctl_files/wo_assim/${STAMP}_snod_wo_assim.tif"
mv $fout $fin
rm -f $fout
gsutil cp $fin gs://cso_test_upload/ak_domain/snod_wo_assim/
#let's move it to /scratch and get it off of depot
mv $fin "${outpath}${STAMP}_snod_wo_assim.tif"
fin="${smpath}ctl_files/wi_assim/${STAMP}_mask_snod_wi_assim.tif"
fout="${smpath}ctl_files/wi_assim/${STAMP}_mask_cog_snod_wi_assim.tif"
gdal_translate -q $fin $fout -co TILED=YES -co COPY_SRC_OVERVIEWS=YES -co COMPRESS=DEFLATE
rm -f $fin
fin="${smpath}ctl_files/wi_assim/${STAMP}_snod_wi_assim.tif"
mv $fout $fin
rm -f $fout
gsutil cp $fin gs://cso_test_upload/ak_domain/snod_wi_assim/
#let's move it to /scratch and get it off of depot
mv $fin "${outpath}${STAMP}_snod_wi_assim.tif"
echo
echo "done uploading to GCS"
echo