This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r.qpe_rfc.py
425 lines (354 loc) · 16.7 KB
/
r.qpe_rfc.py
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env python
#
############################################################################
#
# MODULE: r.qpe_rfc
# AUTHOR(S): mweier 2016-02-19
#
#
# PURPOSE: Generate subbasin hyetograph file for DSS import from NWS River Forecast Center radar adjusted rainfall
# COPYRIGHT:
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#
#############################################################################
#%Module
#% description: This script extracts recent QPE grids from the NWS River Forcast center and retains them in .tiff format, averaging values over subbasins, outputs text file of averages for import in to HEC-DSS
#% keywords: rainfall, radar, qpe, rfc
#%End
#%Option
#% key: basinrast
#% type: string
#% required: yes
#% multiple: no
#% key_desc: name
#% description: Categorical subbasin raster
#% answer:
#% gisprompt: old,cell,raster
#%End
#%Option
#% key: rfc
#% type: string
#% required: yes
#% multiple: no
#% key_desc: name
#% description: River forecast center MB = missouri river basin NC = red
#% options: MB,NC
#%End
#%Option
#% key: res
#% type: string
#% required: no
#% multiple: no
#% key_desc: name
#% description: Region resolution
#% answer: 50
#%End
#%Option
#% key: start_date
#% type: string
#% required: yes
#% multiple: no
#% key_desc: name
#% description: Start date #yyyy_mm_dd# format, Begins in 1994
#% answer:
#%End
#%Option
#% key: end_date
#% type: string
#% required: yes
#% multiple: no
#% key_desc: name
#% description: End date #yyyy_mm_dd# format, Recent dates may not be available
#% answer:
#%End
from os.path import expanduser
import os, sys, glob, datetime
from datetime import date, timedelta
import time
import pprint as pp
import subprocess
import shutil
##==============================================================================
## #######For grass envrironment debugging
##==============================================================================
#print "startGRASS_"
#
#os.environ['GIS_LOCK'] = str(os.getpid())
#envHome = os.environ['HOME']
#os.environ['GISBASE'] = "/Applications/GRASS-6.4.app/Contents/MacOS"
#
## stuff that should be in PATH
#pathList = [os.path.join(os.environ['GISBASE'],'bin'), os.path.join(os.environ['GISBASE'],'scripts'), os.path.join(envHome,'Library','GRASS','6.4','Modules','bin'), os.path.join(envHome,'Library','GRASS','6.4','Modules','scripts'), os.path.join('/Library','Frameworks','GDAL.framework', 'Programs')]
#
## update PATH if needed
#for p in pathList:
# if p not in os.environ['PATH']:
# os.environ['PATH'] = os.environ['PATH'] + ':' + p
# print 'adding ' + p + ' to PATH'
#
##update sys.path if needed
#if os.path.join(os.environ['GISBASE'], "etc", "python") not in sys.path:
# sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
#
## update/create PYTHONPATH if needed
#if 'PYTHONPATH' not in os.environ:
# print 'create PYTHONPATH'
# os.environ['PYTHONPATH'] = os.path.join(os.environ['GISBASE'], "etc", "python")
#else:
# if os.path.join(os.environ['GISBASE'], "etc", "python") not in os.environ['PYTHONPATH']:
# print 'update PYTHONPATH'
# os.environ['PYTHONPATH'] = os.environ['PYTHONPATH'] + ':' + os.path.join(os.environ['GISBASE'], "etc", "python")
#
#os.environ['GISDBASE'] = os.path.join(envHome,'grassdata')
#
## check for database folder
#if not os.path.exists(os.environ['GISDBASE']):
# print 'no grassdata folder'
# sys.exit()
#
## check for current grass session
#gisrc = None
#for x in os.listdir('/tmp'):
# if x.startswith('grass6'):
# gisrc = os.path.join('/tmp',x,'gisrc')
# print 'found current grass session'
# break
#if not gisrc:
# # check for rc6 file
# gisrc = os.path.join(envHome,'.grassrc6')
# print 'found old grass session'
# if not os.path.isfile( gisrc ):
# print "gisrc not found"
# sys.exit()
#os.environ['GISRC'] = gisrc
#
#
#
#basinrast = 'basin@PERMANENT'
#res = 50
#rfc = 'MB'
#startdate = '2014_06_01'
#enddate = '2014_06_15'
##dirPath = os.path.join('/Volumes/GIS_Rasters/NWS_QPE_Data/renamed')
##==============================================================================
## #######end grass environment
##==============================================================================
import grass.script as grass
if not os.environ.has_key("GISBASE"):
grass.message( "You must be in GRASS GIS to run this program." )
sys.exit(1)
def main():
t0=time.time()
HOME = expanduser('~')
########this script extracts recent QPE grids from NMQ site and retains them in .tiff format, averaging values over subbasins, outputs text file of averages
##################################change these parameters
basinrast = options['basinrast'].split('@')[0]
res = options['res'].split('@')[0]
rfc = 'MB' ####river forecast center MB = missouri river basin NC = red river/devils lake/souris basins
startdate = options['start_date'].split('@')[0]
enddate = options['end_date'].split('@')[0]
dirPath = os.path.join('/Volumes/GIS_Rasters/NWS_QPE_Data/renamed')
#########################################
basinlist = []
filelist = []
if not os.environ.has_key("GISBASE"):
grass.message( "You must be in GRASS GIS to run this program." )
sys.exit(1)
# Save current region
grass.read_command('g.region', flags = 'p', save = 'original', overwrite = True)
# Change resolution to res
cmd = "g.region -g rast=%s res=%s" % (basinrast,res)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
#assign extents
ymax = (ff.split('\n')[0]).split('=')[1]
ymin = (ff.split('\n')[1]).split('=')[1]
xmin = (ff.split('\n')[2]).split('=')[1]
xmax = (ff.split('\n')[3]).split('=')[1]
#get basin names
info = grass.parse_command('r.category', map = basinrast)
for i in info:
basinlist.append(i.split('\t')[1])
basinlist.sort()
numbasins = len(basinlist)
# Read location
cmd = "g.gisenv"
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
loc = ff.split("NAME='")[1].split("'")[0]
temp_dir = os.path.join(HOME,'Desktop','temp_'+loc+'_'+startdate)
#Read projection
cmd = 'g.proj -j -f'
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
crs = ff
if os.path.isdir(temp_dir):
shutil.rmtree(temp_dir)
os.makedirs(temp_dir)
#change working directory
os.chdir(dirPath)
#Create list of available data files
filelist.extend(glob.glob('*.tar.gz'))
##convert dates to datetime objects
startdate_d = datetime.date(int(startdate.split('_')[0]),int(startdate.split('_')[1]),int(startdate.split('_')[2]))-datetime.timedelta(days = 1)
enddate_d = datetime.date(int(enddate.split('_')[0]),int(enddate.split('_')[1]),int(enddate.split('_')[2]))
#check if date range spans data format change
if startdate_d <= datetime.date(year=2005,month=9,day=30) <= enddate_d:
grass.message( 'Data formats change on Oct 1st 2005...Adjust date range and retry')
sys.exit()
#create list of years of data requested
yearlist = []
for i in range(int(enddate_d.year) - int(startdate_d.year)+1):
tempyear = int(startdate_d.year)+i
if enddate_d <= datetime.date(year=2005,month=9,day=30):
yearlist.append(str(int(startdate_d.year)+i)+'_'+rfc)
else:
yearlist.append(str(int(startdate_d.year)+i))
for i in yearlist:
grass.message( 'Expanding %s QPE files'%i)
cmd = 'tar -xf %s.tar.gz -C %s'%(i,temp_dir)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
os.chdir(temp_dir)
#create output file
out_table = 'precipavg_%s_%s.txt'%(basinrast,startdate)
fout = open(out_table,'w')
fout.write( 'datetimeUTC\tdatetimeCDT\tdatatype\t' )
for i in basinlist:
fout.write( '%s\t'%i )
fout.write('\n')
first = True
#enter main loop
for root,dir,files in os.walk(temp_dir):
for name in files:
#print name
if (name.endswith('.Z') or name.endswith('.gz')) and 'miss' not in name:
if len(name.split('.'))>2 and name.split('.')[2].endswith(('06h','24h')):
os.remove(os.path.join(root,name))
continue
else:
if name.startswith('ST4.'):
filename = os.path.join(root,name)
datatype = 'stage4'
#create datetime object from file name
y = int(name.split('.')[1][0:4])
m = int(name.split('.')[1][4:6])
d = int(name.split('.')[1][6:8])
h = int(name.split('.')[1][8:10])
ext=name.split('.')[-1]
filename = '.'.join(filename.split('.')[0:-1])
name = '.'.join(name.split('.')[0:-1])
else:
filename = os.path.join(root,name)
datatype = name.split('.')[0].split('_')[5]
#create datetime object from file name
y = int(name.split('_')[0])
m = int(name.split('_')[1])
d = int(name.split('_')[2])
h = int(name.split('_')[3][0:2])
ext=name.split('.')[-1]
filename = filename.split('.')[0]
name = name.split('.')[0]
dt_utc = datetime.datetime(y,m,d,h)
dt_cdt = dt_utc - datetime.timedelta(hours=5)#convert from UTC to CDT
#check if first iteration
if first:
dt_utc_old = dt_utc-datetime.timedelta(hours=1)
dt_cdt_old = dt_cdt - datetime.timedelta(hours=1)
first = False
#check of file is within data window
if startdate_d <= datetime.date(year = y, month = m, day = d) <= enddate_d:
####Check for missing data
timegap = dt_utc - dt_utc_old
if timegap > datetime.timedelta(hours=1):
grass.message( 'Missing data, there is a gap of %s' %timegap)
timegaplist_temp = range(int(timegap.days*24+timegap.seconds/60/60))
for i in timegaplist_temp[1:]:
fout.write('%s\t%s\tno data\t%s\n'%(dt_utc_old+datetime.timedelta(hours=(i)),dt_utc_old+datetime.timedelta(hours=(i-5)),'0\t'*len(basinlist)) )#write place holder for missing data
grass.message( 'Processing %s'%name)
# cmd = 'gunzip '+filename
# proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# ff, err = proc.communicate()
if ext == 'gz':
cmd = 'gunzip '+filename+'.'+ext
else:
cmd = 'uncompress '+filename
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
#print err
if y >=2013:
cmd = '''/Library/Frameworks/GDAL.framework/Programs/gdalwarp -t_srs %r -of GTiff -te %s %s %s %s %s/%s %s.tif'''%(crs,xmin,ymin,xmax,ymax,root, name,name)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
else:
#convert XMRG binary to .asc
cmd = os.path.join(dirPath,'xmrgtoasc')+' %s %s ster'%(filename,name)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
print err
#warp .asc from HRAP projection to requested prjection, crop to region
cmd = '''/Library/Frameworks/GDAL.framework/Programs/gdalwarp -s_srs '+proj=stere +lat_0=90 +lat_ts=60 +lon_0=-105 +k=1 +x_0=0 +y_0=0 +a=6371200 +b=6371200 +units=m +no_defs' -t_srs '%s' -te %s %s %s %s %s.asc %s.tif'''%(crs,xmin,ymin,xmax,ymax,name,name)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
print err
os.remove(name+'.asc')
cmd = '''/Library/Frameworks/GDAL.framework/Programs/gdalinfo -mm %s.tif'''%(name)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
#scan raster to see if precip data exists
# print [ff]
# print len(ff.split('Max')[1].split(',')[1])
# print type(ff.split('Max')[1].split(',')[1])
# print [ff.split('Max')[1]]
max_value = float(ff.split('Max=')[1].split(',')[1].split('\n')[0])
#remove zero precip raster
if max_value/25.4<0.01:
os.remove(name+'.tif')
fout.write('%s\t%s\t%s\t%s\n' %(dt_utc,dt_cdt,datatype,'0.00\t'*len(basinlist)))
else:
cmd = 'r.in.gdal input=%s.tif output=temp_rast --overwrite'%(name)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
#zonal stats
cmd = "r.univar -t map=temp_rast zones=%s" %(basinrast)
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
data1 = ff.split('\n')
fields = data1[0].split('|')
dict2={}
for x in data1[1:-1]:
rec1 = x.split('|')
dict1 = dict(zip(fields, rec1 ) )
basinname = rec1[1]
dict2[basinname]=dict1;
fout.write('%s\t%s\t%s\t'%(dt_utc, dt_cdt,datatype))
for x2 in basinlist:
fout.write('%.2f\t'%(float((dict2[x2])['mean'])/25.4))
fout.write('\n')
else:
os.remove(os.path.join(root,name+'.'+ext))
dt_utc_old = dt_utc
dt_cdt_old = dt_cdt
else:
continue
fout.close()
#clean up files
cmd = "g.remove temp_rast"
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
# Change resolution to original
cmd = "g.region region=original"
proc = subprocess.Popen( cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ff, err = proc.communicate()
for i in yearlist:
shutil.rmtree(i)
t1=time.time()
tdelta = (t1-t0)/60
grass.message( '\nFinished - Runtime was %.2f minutes\nData is located at:\n\n%s\n\n\n' % (tdelta, temp_dir))
if __name__ == "__main__":
options, flags = grass.parser()
main()