forked from NCAR/wrf-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_test_file.py
More file actions
181 lines (130 loc) · 6.32 KB
/
make_test_file.py
File metadata and controls
181 lines (130 loc) · 6.32 KB
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
from __future__ import print_function, division
import os
import argparse
import numpy as np
from netCDF4 import Dataset
from wrf import (getvar, interplevel, interpline, vertcross, vinterp, py2round,
CoordPair, ll_to_xy, xy_to_ll, to_np)
VARS_TO_KEEP = ("Times", "XLAT", "XLONG", "XLAT_U", "XLAT_V", "XLONG_U",
"XLONG_V", "U", "V", "W", "PH", "PHB", "T", "P", "PB", "Q2",
"T2", "PSFC", "U10", "V10", "XTIME", "QVAPOR", "QCLOUD",
"QGRAUP", "QRAIN", "QSNOW", "QICE", "MAPFAC_M", "MAPFAC_U",
"MAPFAC_V", "F", "HGT", "RAINC", "RAINSH", "RAINNC")
DIMS_TO_TRIM = ("west_east", "south_north", "bottom_top", "bottom_top_stag",
"west_east_stag", "south_north_stag")
WRF_DIAGS = ["avo", "eth", "cape_2d", "cape_3d", "ctt", "dbz", "mdbz",
"geopt", "helicity", "lat", "lon", "omg", "p", "pressure",
"pvo", "pw", "rh2", "rh", "slp", "ter", "td2", "td", "tc",
"theta", "tk", "tv", "twb", "updraft_helicity", "ua", "va",
"wa", "uvmet10", "uvmet", "z", "cfrac", "zstag"]
INTERP_METHS = ["interplevel", "vertcross", "interpline", "vinterp"]
LATLON_METHS = ["xy", "ll"]
def copy_and_reduce(opts):
infilename = opts.filename
outfilename = os.path.expanduser(
os.path.join(opts.outdir, "ci_test_file.nc"))
with Dataset(infilename) as infile, Dataset(outfilename, "w") as outfile:
# Copy the global attributes
outfile.setncatts(infile.__dict__)
# Copy Dimensions
for name, dimension in infile.dimensions.iteritems():
if name in DIMS_TO_TRIM:
if name.find("_stag") > 0:
dimsize = (len(dimension) / 2
if len(dimension) % 2 == 0
else (len(dimension) + 1) / 2)
else:
dimsize = (len(dimension) / 2
if len(dimension) % 2 == 0
else (len(dimension) - 1) / 2)
else:
dimsize = len(dimension)
outfile.createDimension(name, dimsize)
# Copy Variables
for name, variable in infile.variables.iteritems():
if name not in VARS_TO_KEEP:
continue
outvar = outfile.createVariable(name, variable.datatype,
variable.dimensions, zlib=True)
in_slices = tuple(slice(0, dimsize) for dimsize in outvar.shape)
outvar[:] = variable[in_slices]
outvar.setncatts(infile.variables[name].__dict__)
def add_to_ncfile(outfile, var, varname):
dim_d = dict(zip(var.dims, var.shape))
for dimname, dimsize in dim_d.items():
if dimname not in outfile.dimensions:
outfile.createDimension(dimname, dimsize)
fill_value = None
if "_FillValue" in var.attrs:
fill_value = var.attrs["_FillValue"]
ncvar = outfile.createVariable(varname, var.dtype, var.dims,
zlib=True, fill_value=fill_value)
var_vals = to_np(var)
ncvar[:] = var_vals[:]
def make_result_file(opts):
infilename = os.path.expanduser(
os.path.join(opts.outdir, "ci_test_file.nc"))
outfilename = os.path.expanduser(
os.path.join(opts.outdir, "ci_result_file.nc"))
with Dataset(infilename) as infile, Dataset(outfilename, "w") as outfile:
for varname in WRF_DIAGS:
var = getvar(infile, varname)
add_to_ncfile(outfile, var, varname)
for interptype in INTERP_METHS:
if interptype == "interpline":
hts = getvar(infile, "z")
p = getvar(infile, "pressure")
hts_850 = interplevel(hts, p, 850.)
add_to_ncfile(outfile, hts_850, "interplevel")
if interptype == "vertcross":
hts = getvar(infile, "z")
p = getvar(infile, "pressure")
pivot_point = CoordPair(hts.shape[-1] // 2, hts.shape[-2] // 2)
ht_cross = vertcross(hts, p, pivot_point=pivot_point,
angle=90.)
add_to_ncfile(outfile, ht_cross, "vertcross")
if interptype == "interpline":
t2 = getvar(infile, "T2")
pivot_point = CoordPair(t2.shape[-1] // 2, t2.shape[-2] // 2)
t2_line = interpline(t2, pivot_point=pivot_point, angle=90.0)
add_to_ncfile(outfile, t2_line, "interpline")
if interptype == "vinterp":
tk = getvar(infile, "temp", units="k")
interp_levels = [200, 300, 500, 1000]
field = vinterp(infile,
field=tk,
vert_coord="theta",
interp_levels=interp_levels,
extrapolate=True,
field_type="tk",
log_p=True)
add_to_ncfile(outfile, field, "vinterp")
for latlonmeth in LATLON_METHS:
if latlonmeth == "xy":
# Hardcoded values from other unit tests
lats = [22.0, 25.0, 27.0]
lons = [-90.0, -87.5, -83.75]
xy = ll_to_xy(infile, lats[0], lons[0])
add_to_ncfile(outfile, xy, "xy")
else:
# Hardcoded from other unit tests
x_s = np.asarray([10, 50, 90], int)
y_s = np.asarray([10, 50, 90], int)
ll = xy_to_ll(infile, x_s[0], y_s[0])
add_to_ncfile(outfile, ll, "ll")
def main(opts):
copy_and_reduce(opts)
make_result_file(opts)
if __name__ == "__main__":
DEFAULT_FILE = ("/Users/ladwig/Documents/wrf_files/"
"wrf_vortex_multi/moving_nest/"
"wrfout_d02_2005-08-28_12:00:00")
parser = argparse.ArgumentParser(description="Generate conda test files "
"for unit testing.")
parser.add_argument("-f", "--filename", required=False,
default=DEFAULT_FILE,
help="the WRF test file")
parser.add_argument("-o", "--outdir", required=False, default="./",
help="the location for the output files")
opts = parser.parse_args()
main(opts)