-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathungrib_princeton.py
executable file
·166 lines (150 loc) · 6.62 KB
/
ungrib_princeton.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# convert Princeton Meteorological Forcing Dataset from NetCDF format
# to WPS intermediate file format.
# author: Hui ZHENG
# email: [email protected]
import os
import os.path
import struct
import argparse
import dateutil.parser
import numpy as np
import netCDF4 as nc
def wps_write_latlon_field(f,
hdate, xfcst, map_src,
field, units, desc,
xlvl, nlat, nlon,
startloc,
startlat, startlon,
deltalat, deltalon,
is_wind_grid_rel, data):
"""
Note: For WPS intermediate file,
x-dimension is the longitude, y-dimension is the latitude,
data in the dimension of (nlat, nlon) (C-order)
"""
version = 5
iproj = 0
earth_radius = 6371.229004
recsize = struct.calcsize('>i')
f.write(struct.pack('>I', recsize))
f.write(struct.pack('>i', version))
f.write(struct.pack('>I', recsize))
recsize = struct.calcsize('>24s f 32s 9s 25s 46s f i i i')
f.write(struct.pack('>I', recsize))
f.write(struct.pack('>24s', hdate.strftime('%Y:%m:%d_%H:%M:%S').encode('ascii')))
f.write(struct.pack('>f', xfcst))
f.write(struct.pack('>32s', map_src.ljust(32, ' ').encode('ascii')))
f.write(struct.pack('>9s', field.ljust(9, ' ').encode('ascii')))
f.write(struct.pack('>25s', units.ljust(25, ' ').encode('ascii')))
f.write(struct.pack('>46s', desc.ljust(46, ' ').encode('ascii')))
f.write(struct.pack('>f', xlvl))
f.write(struct.pack('>i', nlon))
f.write(struct.pack('>i', nlat))
f.write(struct.pack('>i', iproj))
f.write(struct.pack('>I', recsize))
recsize = struct.calcsize('>8s f f f f f')
f.write(struct.pack('>I', recsize))
f.write(struct.pack('>8s', startloc.ljust(8, ' ').encode('ascii')))
f.write(struct.pack('>ff', startlat, startlon))
f.write(struct.pack('>ff', deltalat, deltalon))
f.write(struct.pack('>f', earth_radius))
f.write(struct.pack('>I', recsize))
recsize = struct.calcsize('>I')
f.write(struct.pack('>I', recsize))
f.write(struct.pack('>I', is_wind_grid_rel))
f.write(struct.pack('>I', recsize))
bdata = np.array(data, data.dtype.newbyteorder('>'))
recsize = bdata.nbytes
f.write(struct.pack('>I', recsize))
f.write(bdata.tobytes('C'))
f.write(struct.pack('>I', recsize))
return
def main(files=None, prefix='FILE', append=False, begtime=None, endtime=None):
VARS = set(['dswrf', 'dlwrf', 'wind', 'tas', 'shum', 'pres', 'prcp'])
if files is None:
return
# possible outputs
dates = set()
for flnm in files:
if not os.path.isfile(flnm):
print('Error: no such file ', flnm)
return
with nc.Dataset(flnm, 'r') as f:
dates.update(nc.num2date(f.variables['time'][:],
f.variables['time'].units))
# delete exsiting intermediate files
for dd in dates:
if ((begtime is not None and dd < begtime)
or (endtime is not None and dd >= endtime)):
continue
oflnm = ''.join([prefix, ':', dd.strftime('%Y-%m-%d_%H')])
if not append and os.path.isfile(oflnm):
os.remove(oflnm)
del dates
# process & write output
for flnm in files:
with nc.Dataset(flnm, 'r') as f:
dates = nc.num2date(f.variables['time'][:],
f.variables['time'].units)
varname = VARS.intersection(set(f.variables.keys())).pop()
var = f.variables[varname]
for ii, dd in enumerate(dates):
if ((begtime is not None and dd < begtime)
or (endtime is not None and dd >= endtime)):
continue
oflnm = ''.join([prefix, ':', dd.strftime('%Y-%m-%d_%H')])
omod = 'ab' if os.path.isfile(oflnm) else 'wb'
print(oflnm)
with open(oflnm, omod) as of:
xfcst = 0.0
map_src = var.source
field = varname.upper()
units = var.units
desc = var.title
nlat = len(f.dimensions['latitude'])
nlon = len(f.dimensions['longitude'])
startloc = 'SWCORNER'
startlat = f.variables['latitude'][0]
startlon = f.variables['longitude'][0]
deltalat = f.variables['latitude'][1] - f.variables['latitude'][0]
deltalon = f.variables['longitude'][1] - f.variables['longitude'][0]
is_wind_grid_rel = False
for iz in range(len(f.dimensions['z'])):
xlvl = f.variables['z'][iz]
data = var[ii,iz]
wps_write_latlon_field(of,
dd, xfcst, map_src,
field, units, desc,
xlvl, nlat, nlon,
startloc,
startlat, startlon,
deltalat, deltalon,
is_wind_grid_rel,
data)
return
if __name__ == '__main__':
# prefix, append switch
parser = argparse.ArgumentParser(
description='Convert Princeton Meteorological Forcing Dataset from NetCDF format to WPS intermediate file format')
parser.add_argument('file', nargs='+',
help='input files')
parser.add_argument('-p', '--prefix', type=str,
help='prefix of resulting intermediate file names',
default='FILE')
parser.add_argument('-a', '--append',
help='append data to existing files (other than create new files).',
default=False, action='store_true')
parser.add_argument('-b', '--begtime',
help='begin date & time',
default=None, type=str)
parser.add_argument('-e', '--endtime',
help='end date & time (exclusive)',
default=None, type=str)
args = parser.parse_args()
main(files=args.file,
prefix=args.prefix,
append=args.append,
begtime=dateutil.parser.parse(args.begtime) if args.begtime is not None else None,
endtime=dateutil.parser.parse(args.endtime) if args.endtime is not None else None)