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

Gb/bc #96

Merged
merged 29 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
efcf50e
added bias module with a place for bias transformation functions and …
grantbuster Sep 20, 2022
115e9c2
feature specific bias correction
grantbuster Sep 20, 2022
c184917
removed warnings about excessive padding - not a bad thing
grantbuster Sep 20, 2022
1d823e3
added a site-by-site linear bias correction calculation method
grantbuster Sep 21, 2022
8b7f25b
bug fix and logging
grantbuster Sep 21, 2022
56148db
bias calc mods and new functions
grantbuster Sep 22, 2022
50c3692
added bias calc cli
grantbuster Sep 22, 2022
2f7d4f7
added bias calc to main cli
grantbuster Sep 22, 2022
c132b81
make bias out dir
grantbuster Sep 22, 2022
915edd5
bug fixes and minor refactor to run on eagle
grantbuster Sep 22, 2022
7b28943
added local linear bias correct to forward pass bc options
grantbuster Sep 22, 2022
0cee671
added option to smooth spatial bias correction factors outside of the…
grantbuster Sep 23, 2022
aa0a040
better enumerated progress logging for fwp
grantbuster Sep 23, 2022
240a0d6
added bias correction option to QA
grantbuster Sep 23, 2022
9ac0905
minor refactor to bias correct u and v instead of windspeed and direc…
grantbuster Sep 23, 2022
d2fb1e2
fixed up the u/v QA with bias correction
grantbuster Sep 27, 2022
01fbeda
added meta data to bc h5 output attrs
grantbuster Sep 27, 2022
b370b9f
more bc convenience functions
grantbuster Sep 28, 2022
24c40b6
added monthly bias correction
grantbuster Sep 28, 2022
0deab95
added montly bias correction data transformation method and integrate…
grantbuster Sep 29, 2022
1f28ccc
fixed collection logic for undefined mask meta variable when file is …
grantbuster Oct 2, 2022
5d93283
added bias correction calc tests
grantbuster Oct 3, 2022
0638d7b
added bias transform calcs
grantbuster Oct 3, 2022
6cc6ced
added fwp+bc integration test
grantbuster Oct 3, 2022
ca24793
added qa+bc integration test
grantbuster Oct 3, 2022
b75b3fc
added version record to bias calc output files and incremented versio…
grantbuster Oct 4, 2022
b0a2c49
simplify qa test and pylint issue
grantbuster Oct 4, 2022
7b9c88f
fixed test on h5 meta attrs dtype and docstrings
grantbuster Oct 4, 2022
2ea15e3
serial data handling for QA+BC bug
grantbuster Oct 4, 2022
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
Prev Previous commit
Next Next commit
minor refactor to bias correct u and v instead of windspeed and direc…
…tion
  • Loading branch information
grantbuster committed Oct 4, 2022
commit 9ac090537e27026acac3ffef4d41b0bf35b1e0cb
69 changes: 33 additions & 36 deletions sup3r/bias/bias_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DataRetrievalBase:
baseline data
"""

def __init__(self, base_fps, bias_fps, base_dset, bias_features,
def __init__(self, base_fps, bias_fps, base_dset, bias_feature,
target, shape,
base_handler='Resource', bias_handler='DataHandlerNCforCC',
bias_handler_kwargs=None):
Expand All @@ -43,12 +43,12 @@ def __init__(self, base_fps, bias_fps, base_dset, bias_features,
data to be corrected based on the baseline data. This is typically
several years of GCM .nc files.
base_dset : str
A single dataset from the base_fps to retrieve.
bias_features : str | list
This is the biased features from bias_fps to retrieve. This should
be a single feature name, or in the case of windspeed this will be
the two north/east components (e.g. bias_features=[U_100m, V_100m]
corresponding to base_dset=windspeed_100m)
A single dataset from the base_fps to retrieve. In the case of wind
components, this can be U_100m or V_100m which will retrieve
windspeed and winddirection and derive the U/V component.
bias_feature : str
This is the biased feature from bias_fps to retrieve. This should
be a single feature name corresponding to base_dset
target : tuple
(lat, lon) lower left corner of raster to retrieve from bias_fps.
shape : tuple
Expand All @@ -63,11 +63,11 @@ def __init__(self, base_fps, bias_fps, base_dset, bias_features,

logger.info('Initializing DataRetrievalBase for base dset "{}" '
'correcting biased dataset(s): {}'
.format(base_dset, bias_features))
.format(base_dset, bias_feature))
self.base_fps = base_fps
self.bias_fps = bias_fps
self.base_dset = base_dset
self.bias_features = bias_features
self.bias_feature = bias_feature
self.target = target
self.shape = shape
bias_handler_kwargs = bias_handler_kwargs or {}
Expand All @@ -76,24 +76,6 @@ def __init__(self, base_fps, bias_fps, base_dset, bias_features,
self.base_fps = [self.base_fps]
if isinstance(self.bias_fps, str):
self.bias_fps = [self.bias_fps]
if isinstance(self.bias_features, str):
self.bias_features = [self.bias_features]

if 'windspeed' in self.base_dset and len(self.bias_features) != 2:
msg = ('Base windspeed dataset of "{}" needs exactly two features '
'for the bias data corresponding to the U and V '
'components, but received: {}'
.format(self.base_dset, self.bias_features))
logger.error(msg)
raise RuntimeError(msg)

elif ('windspeed' not in self.base_dset
and len(self.bias_features) != 1):
msg = ('If base dataset is not windspeed, cannot handle more than '
'one feature from the bias data, but received: {}'
.format(self.bias_features))
logger.error(msg)
raise RuntimeError(msg)

self.base_handler = getattr(rex, base_handler)
self.bias_handler = getattr(sup3r.preprocessing.data_handling,
Expand All @@ -103,7 +85,7 @@ def __init__(self, base_fps, bias_fps, base_dset, bias_features,
self.base_meta = res.meta
self.base_tree = KDTree(self.base_meta[['latitude', 'longitude']])

self.bias_dh = self.bias_handler(self.bias_fps, self.bias_features,
self.bias_dh = self.bias_handler(self.bias_fps, [self.bias_feature],
target=self.target, shape=self.shape,
val_split=0.0, **bias_handler_kwargs)

Expand Down Expand Up @@ -276,13 +258,13 @@ def get_bias_data(self, bias_gid):
"""
idx = np.where(self.bias_gid_raster == bias_gid)
bias_data = self.bias_dh.data[idx][0]
if bias_data.shape[-1] > 1 and 'windspeed' in self.base_dset:
bias_data = np.hypot(bias_data[:, 0], bias_data[:, 1])
elif bias_data.shape[-1] == 1:

if bias_data.shape[-1] == 1:
bias_data = bias_data[:, 0]
else:
msg = ('Found a weird number of feature channels for the bias '
'data retrieval: {}'.format(bias_data.shape))
'data retrieval: {}. Need just one channel'
.format(bias_data.shape))
logger.error(msg)
raise RuntimeError(msg)

Expand Down Expand Up @@ -316,11 +298,27 @@ def get_base_data(base_fps, base_dset, base_gid, base_handler,
1D array of base data spatially averaged across the base_gid input
and possibly daily-averaged as well.
"""

out = []
for fp in base_fps:
with base_handler(fp) as res:
base_ti = res.time_index
base_data = res[base_dset, :, base_gid]

if base_dset.startswith(('U_', 'V_')):
dset_ws = base_dset.replace('U_', 'windspeed_')
dset_ws = dset_ws.replace('V_', 'windspeed_')
dset_wd = dset_ws.replace('speed', 'direction')
base_ws = res[dset_ws, :, base_gid]
base_wd = res[dset_wd, :, base_gid]

if base_dset.startswith('U_'):
base_data = -base_ws * np.sin(np.radians(base_wd))
else:
base_data = -base_ws * np.cos(np.radians(base_wd))

else:
base_data = res[base_dset, :, base_gid]

if len(base_data.shape) == 2:
base_data = base_data.mean(axis=1)

Expand Down Expand Up @@ -384,9 +382,8 @@ def write_outputs(self, fp_out, scalar, adder):
lon = self.bias_dh.lat_lon[..., 1]
f.create_dataset('latitude', data=lat)
f.create_dataset('longitude', data=lon)
for name in self.bias_features:
f.create_dataset(f'{name}_scalar', data=scalar)
f.create_dataset(f'{name}_adder', data=adder)
f.create_dataset(f'{self.bias_feature}_scalar', data=scalar)
f.create_dataset(f'{self.bias_feature}_adder', data=adder)

logger.info('Wrote scalar adder factors to file: {}'
.format(fp_out))
Expand Down
4 changes: 2 additions & 2 deletions sup3r/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def bias_calc(ctx, verbose):
{
"base_fps" : ["/datasets/WIND/HRRR/HRRR_2015.h5"],
"bias_fps": ["./ta_day_EC-Earth3-Veg_ssp585.nc"],
"base_dset": "windspeed_100m",
"bias_features": ["U_100m", "V_100m"],
"base_dset": "U_100m",
"bias_feature": "U_100m",
"target": [20, -130],
"shape": [48, 95]
}
Expand Down