Skip to content

Commit 956c55f

Browse files
authored
Merge pull request #37 from charlie-becker/HRRR_Zarr
Hrrr zarr
2 parents 817a570 + eeca18f commit 956c55f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

hagelslag/data/ModelOutput.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .HRRRModelGrid import HRRRModelGrid
77
from .HRRREModelGrid import HRRREModelGrid
88
from .HREFv2ModelGrid import HREFv2ModelGrid
9+
from .HRRRZarrModelGrid import HRRRZarrModelGrid
910
from .NCARStormEventModelGrid import NCARStormEventModelGrid
1011
from hagelslag.util.make_proj_grids import make_proj_grids, read_arps_map_file, read_ncar_map_file, get_proj_obj
1112
from hagelslag.util.derived_vars import relative_humidity_pressure_level, melting_layer_height

hagelslag/data/ZarrModelGrid.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class ZarrModelGrid(object):
2222
freqency (str): spacing between model time steps.
2323
valid_dates: DatetimeIndex of all model timesteps
2424
forecast_hours: array of all hours in the forecast
25-
file_objects (list): List of the file objects for each model time step
2625
"""
2726
def __init__(self,
2827
path,
@@ -37,29 +36,31 @@ def __init__(self,
3736
self.start_date = pd.to_datetime(start_date)
3837
self.end_date = pd.to_datetime(end_date)
3938
self.frequency = frequency
40-
self.valid_dates = date_range(start=self.start_date,
41-
end=self.end_date,
42-
freq=self.frequency)
43-
print(self.run_date)
44-
print(type(self.run_date))
39+
self.valid_dates = date_range(start=self.start_date, end=self.end_date, freq=self.frequency)
4540
self.forecast_hours = (self.valid_dates - self.run_date).astype("timedelta64[h]").astype(int)
4641

47-
4842
def load_data(self):
4943

5044
units = ""
5145
level = self.variable.split('-')[1]
5246
self.variable = self.variable.split('-')[0]
5347
fs = s3fs.S3FileSystem(anon=True)
54-
files = []
5548
run_date_str = self.run_date.strftime("%Y%m%d")
56-
forecast_hour = self.run_date.strftime("%H")
57-
path = join(self.path, run_date_str, f'{run_date_str}_{forecast_hour}z_fcst.zarr', level, self.variable, level)
49+
run_hour = self.run_date.strftime("%H")
50+
path = join(self.path, run_date_str, f'{run_date_str}_{run_hour}z_fcst.zarr', level, self.variable, level)
5851
f = s3fs.S3Map(root=path, s3=fs, check=False)
59-
files.append(f)
52+
ds = xr.open_mfdataset([f], engine='zarr', parallel=True).load()
6053

61-
ds = xr.open_mfdataset(files, engine='zarr').load()
62-
array = ds[self.variable].values.astype('float32')
54+
if self.run_date in self.valid_dates:
55+
arr = ds[self.variable].values[self.forecast_hours[0]:self.forecast_hours[-1] + 1].astype('float32')
56+
forecast_hour_00_path = join(self.path, run_date_str, f'{run_date_str}_{run_hour}z_anl.zarr', level,
57+
self.variable.replace('1hr_', ''), level)
58+
fh_0_file = s3fs.S3Map(root=forecast_hour_00_path, s3=fs, check=False)
59+
fh_0_ds = xr.open_mfdataset([fh_0_file], engine='zarr', parallel=True).expand_dims('time')
60+
fh_0_arr = fh_0_ds[self.variable.replace('1hr_', '')].values
61+
array = np.concatenate([fh_0_arr, arr])[self.forecast_hours[0]:self.forecast_hours[-1] + 1, :, :]
62+
else:
63+
array = ds[self.variable].values[self.forecast_hours[0] - 1:self.forecast_hours[-1]].astype('float32')
6364

6465
if hasattr(ds[self.variable], 'units'):
6566
units = ds[self.variable].attrs['units']

0 commit comments

Comments
 (0)