-
Notifications
You must be signed in to change notification settings - Fork 2
/
vicmet2nc.py
executable file
·185 lines (164 loc) · 6.51 KB
/
vicmet2nc.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
#!/usr/bin/env python
"""Convert VIC meteorological ASCII data into a netcdf file
"""
import argparse
import sys
from netCDF4 import Dataset
from netCDF4 import default_fillvals
import numpy as np
import glob
import os
import datetime
import time as tm
def main():
(inpath, ncfile, domain, precision, start, end, ncformat) = get_args()
filelist = glob.glob(inpath + "*")
ndays = end - start + datetime.timedelta(1)
ndays = ndays.days
# set up matrices for writing to netcdf file
data = {}
data['prcp'] = np.ones((ndays, domain['ny'], domain['nx']),
dtype='float32') * default_fillvals['f4']
data['tmax'] = np.ones((ndays, domain['ny'], domain['nx']),
dtype='float32') * default_fillvals['f4']
data['tmin'] = np.ones((ndays, domain['ny'], domain['nx']),
dtype='float32') * default_fillvals['f4']
data['wind'] = np.ones((ndays, domain['ny'], domain['nx']),
dtype='float32') * default_fillvals['f4']
for file in filelist:
print "Ingesting: {}".format(file)
indata = np.loadtxt(file, dtype=None)
fields = file.rsplit('_')
lon = float(fields[-1])
lat = float(fields[-2])
x = int((lon-domain['west'])/domain['lonres'])
y = int((lat-domain['south'])/domain['latres'])
data['prcp'][:,y,x] = indata[:,0]
data['tmax'][:,y,x] = indata[:,1]
data['tmin'][:,y,x] = indata[:,2]
data['wind'][:,y,x] = indata[:,3]
write_netcdf(ncfile, ncformat, start, ndays, domain, data)
return
def get_args():
parser = argparse.ArgumentParser(description='Convert VIC ASCII met files '
'to NetCDF')
parser.add_argument('--netcdf', required=True, metavar='<netcdf file>',
help='netcdf file (output)')
parser.add_argument('--input', required=True, metavar='<input path>',
help='input path with VIC met files, including leading '
'part of filename (before the latitude)')
parser.add_argument('--domain', required=True, metavar='<domain>',
help='domain in following format: '
'west/east/resolution/south/north/resolution '
'all in degrees')
parser.add_argument('--precision', required=True, metavar='<precision>',
help='number of digits after the decimal in the '
'VIC file names')
parser.add_argument('--start', required=True, metavar='<date>',
help='start date in following format: YYYY/MM/DD')
parser.add_argument('--end', required=True, metavar='<date>',
help='end date in following format: YYYY/MM/DD')
parser.add_argument('--format', metavar='<NETCDF4|NETCDF4_CLASSIC|'
'NETCDF3_CLASSIC|NETCDF3_64BIT>',
help='NetCDF format. NETCDF4 is default')
args = parser.parse_args()
domain = parse_domain(args.domain)
ncformat = 'NETCDF4'
if (args.format):
ncformat = args.format
start = parse_date(args.start, '/')
end = parse_date(args.end, '/')
try:
precision = int(args.precision)
if (precision < 0):
raise ValueError
except:
print "Error: precision must be an integer >= 0: {}".format(args.precision)
return (args.input, args.netcdf, domain, precision, start, end, ncformat)
def parse_date(datestr, sep='-'):
try:
date = datetime.date(*[int(x) for x in datestr.split(sep)])
except (ValueError, TypeError):
sys.exit('Not a valid date: {}'.format(datestr))
except:
sys.exit('Unexpected error: {}'.format(sys.exc_info()[0]))
return date
def parse_domain(str):
domain = {}
fields = str.split('/')
if len(fields) != 6:
sys.exit('Error: Domain {} should have 6 fields'.format(str))
fields[:] = [float(field) for field in fields]
domain['west'] = fields[0]
domain['east'] = fields[1]
domain['lonres'] = fields[2]
domain['south'] = fields[3]
domain['north'] = fields[4]
domain['latres'] = fields[5]
domain['nx'] = int((domain['east']-domain['west'])/domain['lonres'])+1
domain['ny'] = int((domain['north']-domain['south'])/domain['latres'])+1
return domain
def write_netcdf(ncfile, format, start, ndays, domain, data):
print "Writing {}".format(ncfile)
nc = Dataset(ncfile, 'w', format=format)
nc.createDimension('time', None)
nc.createDimension('lon', domain['nx'])
nc.createDimension('lat', domain['ny'])
# coordinate variables
time = nc.createVariable('time', 'f4', 'time')
lon = nc.createVariable('lon', 'f4', 'lon')
lat = nc.createVariable('lat', 'f4', 'lat')
time[:] = np.arange(0, ndays, 1)
lon[:] = domain['west'] + np.arange(0, domain['nx']) * domain['lonres']
lat[:] = domain['south'] + np.arange(0, domain['ny']) * domain['latres']
for var in data.iterkeys():
ncvar = nc.createVariable(var, 'f4', ('time','lat','lon',),
fill_value=default_fillvals['f4'])
ncvar[:] = data[var]
attrtable = {
'time': {
'long_name': 'time',
'units': 'days since {}'.format(start),
'calendar': 'standard',
},
'lon': {
'long_name': 'longitude',
'units': 'degrees_east',
},
'lat': {
'long_name': 'latitude',
'units': 'degrees_north',
},
'prcp': {
'long_name': 'precipitation',
'units': 'mm/day',
'valid_min': 0.,
'valid_max': 500.,
},
'tmin': {
'long_name': 'daily minimum temperature',
'units': 'degrees C',
'valid_min': -60.,
'valid_max': 40.,
},
'tmax': {
'long_name': 'daily maximum temperature',
'units': 'degrees C',
'valid_min': -50.,
'valid_max': 50.,
},
'wind': {
'long_name': 'daily mean wind speed',
'units': 'm/s',
'valid_min': 0.,
'valid_max': 50.,
},
}
for var in attrtable.iterkeys():
for attr in attrtable[var].iterkeys():
nc.variables[var].setncattr(attr, attrtable[var][attr])
nc.history = 'Created: {}\n'.format(tm.ctime(tm.time()))
nc.history += ' '.join(sys.argv) + '\n'
nc.close()
if __name__ == "__main__":
main()