Skip to content

Commit

Permalink
✨ support more template in dset. support multi line zdef. add valid_t…
Browse files Browse the repository at this point in the history
…ime in load_field_from_file for grads.
  • Loading branch information
perillaroc committed Oct 26, 2024
1 parent f57c694 commit 2a03173
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions reki/format/grads/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def load_field_from_file(
level: Union[int, float, list] = None,
level_dim: Optional[str] = None,
latitude_direction: Literal["degree_north", "degree_south"] = "degree_north",
valid_time: Union[str, pd.Timestamp] = None,
forecast_time: Union[str, pd.Timedelta] = None,
**kwargs
) -> Optional[xr.DataArray]:
Expand All @@ -37,6 +38,7 @@ def load_field_from_file(
latitude_direction
* degree_north
* degree_south
valid_time
forecast_time
kwargs
Expand Down Expand Up @@ -81,6 +83,8 @@ def load_field_from_file(
forecast_time = pd.to_timedelta(forecast_time)
if isinstance(file_path, str):
file_path = Path(file_path)
if isinstance(valid_time, str):
valid_time = pd.to_datetime(valid_time)

ctl_parser = GradsCtlParser()
ctl_parser.parse(file_path)
Expand Down Expand Up @@ -118,6 +122,7 @@ def load_field_from_file(
parameter=parameter,
level=level,
level_type=grads_level_type,
valid_time=valid_time,
forecast_time=forecast_time,
):
continue
Expand Down
20 changes: 17 additions & 3 deletions reki/format/grads/grads_ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ def _parse_levels_dimension(self, dim_name, tokens):
700.0000
...
zdef 22 levels
1000 925 850 700 600 500 400 300 250 200 150 100 70 50 30 20 10 7 5 3 2 1
"""
levels = list()
count = int(tokens[1])
Expand All @@ -207,8 +210,10 @@ def _parse_levels_dimension(self, dim_name, tokens):
while i < count:
self.cur_no += 1
cur_line = self.ctl_file_lines[self.cur_no]
levels.append(float(cur_line))
i += 1
current_line_tokens = cur_line.split()
current_levels = [float(l) for l in current_line_tokens]
levels.extend(current_levels)
i += len(current_line_tokens)

setattr(self.grads_ctl, dim_name, {
'type': 'levels',
Expand Down Expand Up @@ -374,7 +379,16 @@ def get_data_file_path(cls, grads_ctl, record) -> Path:

tokens = str(grads_ctl.dset).split("%")
token_mapper = {
"n2": lambda x: f"{x['forecast_time'].seconds // 60 % 60:02d}",
"y2": lambda x: f"{x['valid_time'].year % 100:02d}",
"y4": lambda x: f"{x['valid_time'].year:04d}",
"m1": lambda x: f"{x['valid_time'].month}",
"m2": lambda x: f"{x['valid_time'].month:02d}",
"d1": lambda x: f"{x['valid_time'].day}",
"d2": lambda x: f"{x['valid_time'].day:02d}",
"h1": lambda x: f"{x['valid_time'].hour}",
"h2": lambda x: f"{x['valid_time'].hour:02d}",
"h3": lambda x: f"{x['valid_time'].hour:03d}",
"n2": lambda x: f"{x['forecast_time'].seconds // 60 % 60:02d}", # TODO: check n2
"f2": lambda x: f"{int(x['forecast_time'] / pd.Timedelta(hours=1)):02d}",
"f3": lambda x: f"{int(x['forecast_time'] / pd.Timedelta(hours=1)):03d}",
"fn2": lambda x: f"{int(x['forecast_time'] / pd.Timedelta(minutes=1)):02d}",
Expand Down

0 comments on commit 2a03173

Please sign in to comment.