forked from NCAR/wrf-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce_file.py
More file actions
34 lines (24 loc) · 1.07 KB
/
reduce_file.py
File metadata and controls
34 lines (24 loc) · 1.07 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
from netCDF4 import Dataset as NetCDF
f = "/Users/ladwig/Documents/wrf_files/wrfout_d01_2016-02-25_18_00_00"
outfilename = "/Users/ladwig/Documents/wrf_files/rotated_pole_test.nc"
in_nc = NetCDF(f, mode='r', format="NETCDF3_CLASSIC")
out_nc = NetCDF(outfilename, mode='w', format="NETCDF3_CLASSIC")
# Copy Global Attributes
for att_name in in_nc.ncattrs():
setattr(out_nc, att_name, getattr(in_nc, att_name))
# Copy Dimensions, but modify the vertical dimensions
for dimname, dim in in_nc.dimensions.iteritems():
out_nc.createDimension(dimname, len(dim))
# Copy Variables and their Attributes, using the reduced vertical dimension
for varname, var in in_nc.variables.iteritems():
if varname in ("T2", "XLAT", "XLONG", "XTIME"):
datatype = var.datatype
dimensions = var.dimensions
shape = var.shape
new_shape = shape
new_var = out_nc.createVariable(varname, datatype, dimensions)
new_var[:] = var[:]
for att_name in var.ncattrs():
setattr(new_var, att_name, getattr(var, att_name))
in_nc.close()
out_nc.close()