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

Update wdf correction #158

Merged
merged 4 commits into from
Dec 20, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Changed
- Update wet day frequency correction to include small negative values in correction and to limit the correction range to the threshold * 10 ^ -2. (PR #158, @dgergel)
- Update package setup, README, HISTORY/CHANGELOG to new system. (PR #154, @brews)

## [0.13.0] - 2021-12-17
Expand Down
19 changes: 14 additions & 5 deletions dodola/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,13 @@ def apply_wet_day_frequency_correction(ds, process):
28, Issue 7, pp. 6938-6959.
"""
threshold = 0.05 # mm/day
low = 1e-16
# adjusted "low" value from the original epsilon in Cannon et al 2015 to
# avoid having some values get extremely large
low = threshold * pow(10, -2)

if process == "pre":
ds_corrected = ds.where(ds != 0.0, np.random.uniform(low=low, high=threshold))
# includes very small values that are negative in CMIP6 output
ds_corrected = ds.where(ds > 0.0, np.random.uniform(low=low, high=threshold))
elif process == "post":
ds_corrected = ds.where(ds >= threshold, 0.0)
else:
Expand Down Expand Up @@ -761,14 +765,19 @@ def _test_negative_values(ds, var):
Tests for presence of negative values
"""
# this is not set to 0 to deal with floating point error
assert ds[var].where(ds[var] < -0.001).count() == 0, "there are negative values!"
neg_values = ds[var].where(ds[var] < -0.001).count()
assert neg_values == 0, "there are {} negative values!".format(neg_values)


def _test_maximum_precip(ds, var):
"""
Tests that max precip is reasonable
"""
threshold = 2000 # in mm, max observed is 1.825m --> maximum occurs between 0.5-0.8
max_precip = ds[var].max()
num_precip_values_over_threshold = ds[var].where(ds[var] > threshold).count()
assert (
ds[var].where(ds[var] > threshold).count() == 0
), "maximum precip exceeds 2000mm"
num_precip_values_over_threshold == 0
), "maximum precip is {} mm and there are {} values over 2000mm".format(
max_precip, num_precip_values_over_threshold
)
17 changes: 5 additions & 12 deletions dodola/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,19 +747,12 @@ def test_correct_wet_day_frequency(process):
ds_precip_corrected = repository.read(out_url)

if process == "pre":
# all 0 values should have been set to a random uniform value below 0.05
assert (
ds_precip_corrected["fakevariable"].where(
ds_precip["fakevariable"] == 0, drop=True
)
!= 0.0
)
assert (
ds_precip_corrected["fakevariable"].where(
ds_precip["fakevariable"] == 0, drop=True
)
< threshold
# all 0s and very small negative values should have been set to a random uniform value below 0.05
corrected_values = ds_precip_corrected["fakevariable"].where(
ds_precip["fakevariable"] <= 0, drop=True
)
assert corrected_values > 0.0
assert corrected_values < threshold
elif process == "post":
# all values below 0.05 should be reset to 0
assert (
Expand Down