Skip to content

Commit

Permalink
Merge pull request #164 from NREL/bnb/dual_dh_patch
Browse files Browse the repository at this point in the history
Bnb/dual dh patch
  • Loading branch information
bnb32 authored Sep 14, 2023
2 parents 791ab5d + 5ec2206 commit 63ae740
Show file tree
Hide file tree
Showing 13 changed files with 1,429 additions and 1,705 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ netCDF4==1.5.8
dask
sphinx
pandas
numpy==1.22
101 changes: 45 additions & 56 deletions sup3r/bias/bias_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,18 @@ def get_spatial_bc_factors(lat_lon, feature_name, bias_fp, threshold=0.1):
slice_x = slice(idx[0], idx[0] + lat_lon.shape[1])

if diff.min() > threshold:
msg = (
'The DataHandler top left coordinate of {} '
'appears to be {} away from the nearest '
'bias correction coordinate of {} from {}. '
'Cannot apply bias correction.'.format(
lat_lon,
diff.min(),
lat_lon_bc[idy, idx],
os.path.basename(bias_fp),
)
)
msg = ('The DataHandler top left coordinate of {} '
'appears to be {} away from the nearest '
'bias correction coordinate of {} from {}. '
'Cannot apply bias correction.'.format(
lat_lon, diff.min(), lat_lon_bc[idy, idx],
os.path.basename(bias_fp),
))
logger.error(msg)
raise RuntimeError(msg)

assert dset_scalar in res.dsets and dset_adder in res.dsets
msg = (f'Either {dset_scalar} or {dset_adder} not found in {bias_fp}.')
assert dset_scalar in res.dsets and dset_adder in res.dsets, msg
scalar = res[dset_scalar, slice_y, slice_x]
adder = res[dset_adder, slice_y, slice_x]
return scalar, adder
Expand Down Expand Up @@ -94,15 +91,14 @@ def global_linear_bc(input, scalar, adder, out_range=None):
return out


def local_linear_bc(
input,
lat_lon,
feature_name,
bias_fp,
lr_padded_slice,
out_range=None,
smoothing=0,
):
def local_linear_bc(input,
lat_lon,
feature_name,
bias_fp,
lr_padded_slice,
out_range=None,
smoothing=0,
):
"""Bias correct data using a simple annual (or multi-year) *scalar +adder
method on a site-by-site basis.
Expand Down Expand Up @@ -156,10 +152,8 @@ def local_linear_bc(
adder = adder[spatial_slice]

if np.isnan(scalar).any() or np.isnan(adder).any():
msg = (
'Bias correction scalar/adder values had NaNs for '
f'"{feature_name}" from: {bias_fp}'
)
msg = ('Bias correction scalar/adder values had NaNs for '
f'"{feature_name}" from: {bias_fp}')
logger.warning(msg)
warn(msg)

Expand All @@ -171,12 +165,12 @@ def local_linear_bc(

if smoothing > 0:
for idt in range(scalar.shape[-1]):
scalar[..., idt] = gaussian_filter(
scalar[..., idt], smoothing, mode='nearest'
)
adder[..., idt] = gaussian_filter(
adder[..., idt], smoothing, mode='nearest'
)
scalar[..., idt] = gaussian_filter(scalar[..., idt],
smoothing,
mode='nearest')
adder[..., idt] = gaussian_filter(adder[..., idt],
smoothing,
mode='nearest')

out = input * scalar + adder
if out_range is not None:
Expand All @@ -186,17 +180,16 @@ def local_linear_bc(
return out


def monthly_local_linear_bc(
input,
lat_lon,
feature_name,
bias_fp,
lr_padded_slice,
time_index,
temporal_avg=True,
out_range=None,
smoothing=0,
):
def monthly_local_linear_bc(input,
lat_lon,
feature_name,
bias_fp,
lr_padded_slice,
time_index,
temporal_avg=True,
out_range=None,
smoothing=0,
):
"""Bias correct data using a simple monthly *scalar +adder method on a
site-by-site basis.
Expand Down Expand Up @@ -269,29 +262,25 @@ def monthly_local_linear_bc(
scalar = np.repeat(scalar, input.shape[-1], axis=-1)
adder = np.repeat(adder, input.shape[-1], axis=-1)
if len(time_index.month.unique()) > 2:
msg = (
'Bias correction method "monthly_local_linear_bc" was used '
'with temporal averaging over a time index with >2 months.'
)
msg = ('Bias correction method "monthly_local_linear_bc" was used '
'with temporal averaging over a time index with >2 months.')
warn(msg)
logger.warning(msg)

if np.isnan(scalar).any() or np.isnan(adder).any():
msg = (
'Bias correction scalar/adder values had NaNs for '
f'"{feature_name}" from: {bias_fp}'
)
msg = ('Bias correction scalar/adder values had NaNs for '
f'"{feature_name}" from: {bias_fp}')
logger.warning(msg)
warn(msg)

if smoothing > 0:
for idt in range(scalar.shape[-1]):
scalar[..., idt] = gaussian_filter(
scalar[..., idt], smoothing, mode='nearest'
)
adder[..., idt] = gaussian_filter(
adder[..., idt], smoothing, mode='nearest'
)
scalar[..., idt] = gaussian_filter(scalar[..., idt],
smoothing,
mode='nearest')
adder[..., idt] = gaussian_filter(adder[..., idt],
smoothing,
mode='nearest')

out = input * scalar + adder
if out_range is not None:
Expand Down
Loading

0 comments on commit 63ae740

Please sign in to comment.