Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No public description #186

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions scripts/compute_derived_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def main(argv: list[str]) -> None:

source_dataset, source_chunks = xbeam.open_zarr(INPUT_PATH.value)

for var_name in PREEXISTING_VARIABLES_TO_REMOVE.value:
if var_name in source_dataset:
del source_dataset[var_name]
source_chunks = {
# Removing variables may remove some dims.
k: v for k, v in source_chunks.items() if k in source_dataset.dims
}

# Validate and clean-up the source datset.
if RENAME_RAW_TP_NAME.value:
source_dataset = source_dataset.rename(
Expand All @@ -168,10 +176,6 @@ def main(argv: list[str]) -> None:
rename_variables.get(k, k): v for k, v in source_chunks.items()
}

for var_name in PREEXISTING_VARIABLES_TO_REMOVE.value:
if var_name in source_dataset:
del source_dataset[var_name]

for var_name, dv in derived_variables.items():
if var_name in source_dataset:
raise ValueError(
Expand Down
7 changes: 5 additions & 2 deletions weatherbench2/derived_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,11 @@ def compute(self, dataset: xr.Dataset) -> xr.DataArray:
class PrecipitationAccumulation(DerivedVariable):
"""Compute precipitation accumulation from hourly accumulations.

Accumulation is computed for the time period leading up to the lead_time.
E.g. 24h accumulation at lead_time=24h indicates 0-24h accumulation.
Accumulation is computed for the time period leading up to and including the
lead_time. E.g. 24h accumulation at lead_time=24h indicates accumulation
from lead_time=0 to lead_time=24. This is equal to the values of
`total_precipitation_name` at 24, minus the value at 0.

Caution: Small negative values sometimes appear in model output.
Here, we set them to zero.

Expand Down
13 changes: 13 additions & 0 deletions weatherbench2/derived_variables_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ def testPrecipitationAccumulation6hr(self):
accumulation_hours=6,
)
result = derived_variable.compute(dataset)

# Test a few specific times for example's sake.
sel = lambda ds, hr: ds.sel(prediction_timedelta=f'{hr}hr')
np.testing.assert_array_equal(
(sel(dataset, 24) - sel(dataset, 24 - 6)).total_precipitation.data,
sel(result, 24),
)
np.testing.assert_array_equal(
(sel(dataset, 18) - sel(dataset, 18 - 6)).total_precipitation.data,
sel(result, 18),
)

# Test every timedelta.
expected = xr.DataArray(
[np.nan, 5, 10, 0, 6, 10, 0],
dims=['prediction_timedelta'],
Expand Down
Loading