4 from scipy
import ndimage
7 from datetime
import datetime
8 from shutil
import copyfile
12 G. Fireman May 2017; adapted from Wayne Robinson's met_reanal_full.pro
21 'press' : [850, 1084],
25 params = PARAM_RANGES.keys()
30 limits = PARAM_RANGES[param]
31 np.clip(values, limits[0], limits[1], out=values)
32 except Exception
as e:
34 print(
'Unknown parameter referenced in enforce_range: ', param)
39 new_arr = ndimage.uniform_filter1d(old_arr, width, axis=0, mode=
'nearest')
41 new_arr = ndimage.uniform_filter1d(new_arr, width, axis=1, mode=
'wrap')
48 zoom = np.divide(old_dat.shape, new_dat.shape)
49 new_dat2 = ndimage.interpolation.zoom(new_dat, zoom, order=1, mode=
'nearest')
61 h4_upd =
SD(file_upd, SDC.READ)
62 h4_nrt =
SD(file_nrt, SDC.READ)
66 start_time = getattr(h4_upd,
'Start Time')[:9]
67 data_source = getattr(h4_upd,
'Data Source')
68 p = re.compile(
'NCEP Reanalysis (\d)')
69 reanalysis_type = p.search(data_source).
group(1)
71 print(
'Reanalysis file has non-standard Data Source: "'
77 file_out =
"N" + start_time +
"_MET_NCEPR" + reanalysis_type +
"_6h.hdf"
80 nx = getattr(h4_nrt,
'Number of Columns')
81 ny = getattr(h4_nrt,
'Number of Rows')
83 if (nx == NX_OUT)
and (ny == NY_OUT):
84 print(
'Calling apply_reanalysis_bias to make file: ', file_out)
85 copyfile(file_nrt, file_out)
86 h4_out =
SD(file_out, SDC.WRITE)
89 infiles = file_nrt+
' '+file_upd
90 copy_atts = (
'Title',
'Data Source',
'Data Source Desc')
94 print(
'Calling expand_reanalysis_file to make file: ', file_out)
95 is_modern = (
int(start_time[:4]) > 1995)
97 copyfile(file_tpl, file_out)
99 copyfile(file_nrt, file_out)
100 h4_out =
SD(file_out, SDC.WRITE)
103 copy_atts = (
'Title',
'Data Source',
'Data Source Desc',
104 'Start Time',
'Start Year',
'Start Day',
'Start Millisec',
105 'End Time' ,
'End Year' ,
'End Day' ,
'End Millisec' )
108 setattr(h4_out,
'Product Name', file_out)
109 setattr(h4_out,
'Data Center',
'NASA/GSFC Ocean Biology Processing Group')
110 setattr(h4_out,
'Mission',
'General')
111 setattr(h4_out,
'Satellite Platform',
' ')
112 setattr(h4_out,
'Software ID',
'merge_met_reanalysis')
113 setattr(h4_out,
'Processing Time',
114 datetime.utcnow().strftime(
'%Y%j%H%M%S%f'))
115 setattr(h4_out,
'Input Files', infiles)
116 setattr(h4_out,
'Processing Control',
' ')
117 for att
in copy_atts:
118 setattr(h4_out, att, getattr(h4_upd, att))
122 except Exception
as e:
141 if (sds ==
'z_wind')
or (sds ==
'm_wind'):
142 data_out = h4_nrt.select(sds).get()
146 data_upd = h4_upd.select(sds).get()
147 data_nrt = h4_nrt.select(sds).get()
152 data_out =
fix_bias(data_nrt, data_upd, width=width)
156 h4_out.select(sds).
set(data_out)
159 except Exception
as e:
170 if (sds ==
'z_wind')
or (sds ==
'm_wind'):
171 data_upd = h4_nrt.select(sds).get()
175 data_upd = h4_upd.select(sds).get()
179 zoom = (NY_OUT/data_upd.shape[0], NX_OUT/data_upd.shape[1])
180 data_out = ndimage.interpolation.zoom(
181 data_upd, zoom, order=1, mode=
'nearest')
187 h4_out.select(sds).
set(data_out)
190 except Exception
as e:
195 if __name__ ==
"__main__":
197 parser = argparse.ArgumentParser(description=
'Adjust predicted met files with bias derived from NCEP Reanalysis.')
198 parser.add_argument(
'file_upd', metavar=
'prer2file', help=
'predicted file')
199 parser.add_argument(
'file_nrt', metavar=
'metfile', help=
'updated file')
200 parser.add_argument(
'file_tpl', metavar=
'r2_template', help=
'file template', nargs=
'?')
201 parser.add_argument(
'file_out', metavar=
'ofile', help=
'output file', nargs=
'?')
203 args = parser.parse_args()
208 dict_args[
'file_nrt'],
209 dict_args[
'file_tpl'],
210 dict_args[
'file_out'])