@@ -57,15 +57,19 @@ def _resolve_dt(ctime, ref_date):
5757 return pd .to_datetime (ref_ns ) + pd .to_timedelta (float (c ), unit = 'h' )
5858
5959
60- def proc_time_step (ds_org , ctime , ref_date , output_dir : Path , case = "init" , ens_mean = True ):
60+ def proc_time_step (
61+ ds_org , ctime , ref_date ,
62+ output_dir : Path , case = "init" , ens_mean = True
63+ ):
6164
6265 FILL_VALUE = np .float32 (1.0e15 )
6366
6467 ds = ds_org .sel (time = ctime ).expand_dims ("time" )
6568
6669 # Time
6770 # dt = pd.to_datetime(ref_date + ctime)
68- dt = _resolve_dt (ctime , ref_date ) # instead of: pd.to_datetime(ref_date + ctime)
71+ # instead of: pd.to_datetime(ref_date + ctime)
72+ dt = _resolve_dt (ctime , ref_date )
6973 HH = dt .strftime ("%H" )
7074 YYYY = dt .strftime ("%Y" )
7175 MM = dt .strftime ("%m" )
@@ -139,8 +143,10 @@ def proc_time_step(ds_org, ctime, ref_date, output_dir: Path, case="init", ens_m
139143 "PRECTOT" : {"long_name" : "total_precipitation" , "units" : "m" },
140144 "U" : {"long_name" : "eastward_wind" , "units" : "m s-1" },
141145 "V" : {"long_name" : "northward_wind" , "units" : "m s-1" },
142- "OMEGA" : {"long_name" : "vertical_pressure_velocity" , "units" : "Pa s-1" },
143- "PHIS" : {"long_name" : "surface_geopotential_height" , "units" : "m+2 s-2" },
146+ "OMEGA" : {
147+ "long_name" : "vertical_pressure_velocity" , "units" : "Pa s-1" },
148+ "PHIS" : {
149+ "long_name" : "surface_geopotential_height" , "units" : "m+2 s-2" },
144150 }
145151
146152 valid_rename = {k : v for k , v in rename_dict .items () if k in ds .variables }
@@ -174,7 +180,9 @@ def proc_time_step(ds_org, ctime, ref_date, output_dir: Path, case="init", ens_m
174180 fname = f"FMGenCast-initial-geos_date-{ tstamp } _res-1.0_levels-13.nc"
175181 else :
176182 suffix = "_ens-mean.nc" if ens_mean else ".nc"
177- fname = f"FMGenCast-prediction-geos_date-{ tstamp } _res-1.0_levels-13{ suffix } "
183+ fname = \
184+ "FMGenCast-prediction-geos_date-" + \
185+ f"{ tstamp } _res-1.0_levels-13{ suffix } "
178186
179187 output_dir .mkdir (parents = True , exist_ok = True )
180188 ds .to_netcdf (output_dir / fname , encoding = encoding , engine = "netcdf4" )
@@ -210,7 +218,8 @@ def run_postprocess_day(
210218 geos_dir .glob (f"*source-geos*{ Y :04d} -{ M :02d} -{ D :02d} T{ H :02d} _*.nc" ))
211219
212220 if init_files :
213- # ds_init = xr.open_dataset(init_files[0]).drop_vars("land_sea_mask", errors="ignore")
221+ # ds_init = xr.open_dataset(
222+ # init_files[0]).drop_vars("land_sea_mask", errors="ignore")
214223 ds_init = _open_xr_cf_safe (init_files [0 ]).drop_vars (
215224 "land_sea_mask" , errors = "ignore" )
216225 # ref_init = np.datetime64(f"{Y}-{M}-{D}T00:00:00")
@@ -229,11 +238,13 @@ def run_postprocess_day(
229238 pred_files = sorted (
230239 pred_dir .glob (f"*geos_date-{ Y :04d} -{ M :02d} -{ D :02d} T{ H :02d} _*.nc" ))
231240 if pred_files :
232- # ds_pred = xr.open_dataset(pred_files[0]).drop_vars("land_sea_mask", errors="ignore")
241+ # ds_pred = xr.open_dataset(
242+ # pred_files[0]).drop_vars("land_sea_mask", errors="ignore")
233243 ds_pred = _open_xr_cf_safe (
234244 pred_files [0 ]).drop_vars ("land_sea_mask" , errors = "ignore" )
235245 # ref_pred = np.datetime64(f"{Y}-{M}-{D}T12:00:00")
236- ref_pred = date + pd .Timedelta (hours = 12 ) # pd.Timestamp(f"{Y}-{M}-{D}T12:00:00") # TODO: Modify to be +12
246+ # pd.Timestamp(f"{Y}-{M}-{D}T12:00:00") # TODO: Modify to be +12?
247+ ref_pred = date + pd .Timedelta (hours = 12 )
237248 for ctime in ds_pred .time .values :
238249 proc_time_step (
239250 ds_pred , ctime , ref_pred ,
@@ -255,12 +266,16 @@ def run_postprocess_multiday(
255266 post_out_dir : str ,
256267 ens_mean : bool = True ,
257268):
258- """Postprocess multiple days (inclusive) of GenCast outputs into CF-compliant NetCDFs.
269+ """
270+ Postprocess multiple days (inclusive) of
271+ GenCast outputs into CF-compliant NetCDFs.
259272 Calls run_postprocess_day for each day in [start_date, end_date].
260273 """
261274 # start_date = np.datetime64(start_date)
262275 # end_date = np.datetime64(end_date)
263- # date_range = np.arange(start_date, end_date + np.timedelta64(1, "D"), dtype="datetime64[D]")
276+ # date_range = np.arange(
277+ # start_date, end_date + np.timedelta64(1, "D"),
278+ # dtype="datetime64[D]")
264279 fmt = "%Y-%m-%d:%H"
265280
266281 # Parse exact hour from input
@@ -293,17 +308,25 @@ def run_postprocess_multiday(
293308 level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s"
294309 )
295310
296- parser = argparse .ArgumentParser (description = "Convert GenCast outputs to CF-compliant NetCDFs" )
297- parser .add_argument ("--start_date" , type = str , required = True , help = "Start date (YYYY-MM-DD)" )
298- parser .add_argument ("--end_date" , type = str , required = True , help = "End date (YYYY-MM-DD)" )
299- parser .add_argument ("--geos_dir" , type = str , required = True ,
300- help = "Directory with GEOS inputs (for initial conditions)" )
311+ parser = argparse .ArgumentParser (
312+ description = "Convert GenCast outputs to CF-compliant NetCDFs" )
313+ parser .add_argument (
314+ "--start_date" , type = str , required = True ,
315+ help = "Start date (YYYY-MM-DD:HH)" )
316+ parser .add_argument (
317+ "--end_date" , type = str , required = True ,
318+ help = "End date (YYYY-MM-DD:HH)" )
319+ parser .add_argument (
320+ "--geos_dir" , type = str , required = True ,
321+ help = "Directory with GEOS inputs (for initial conditions)" )
301322 parser .add_argument ("--pred_dir" , type = str , required = True ,
302323 help = "Directory with GenCast predictions" )
303- parser .add_argument ("--post_out_dir" , type = str , default = "./output/postprocess" ,
304- help = "Directory for CF-compliant NetCDF outputs" )
305- parser .add_argument ("--no_ens_mean" , action = "store_true" ,
306- help = "Disable ensemble mean (keep all ensemble members)" )
324+ parser .add_argument (
325+ "--post_out_dir" , type = str , default = "./output/postprocess" ,
326+ help = "Directory for CF-compliant NetCDF outputs" )
327+ parser .add_argument (
328+ "--no_ens_mean" , action = "store_true" ,
329+ help = "Disable ensemble mean (keep all ensemble members)" )
307330
308331 args = parser .parse_args ()
309332
0 commit comments