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/bias nc #181

Merged
merged 15 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
defensive coding, better error messages
  • Loading branch information
grantbuster committed Dec 11, 2023
commit c9ed54abb7507dcefb56a19c40844a1a4f36ebf6
4 changes: 3 additions & 1 deletion sup3r/preprocessing/data_handling/h5_data_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def extract_feature(cls,
try:
fdata = handle[(feature, time_slice, *(raster_index.flatten(),))]
except ValueError as e:
msg = f'{feature} cannot be extracted from source data'
msg = (f'Requested feature "{feature}" cannot be extracted from '
f'source data that has available variables: '
f'{handle.dsets}.')
logger.exception(msg)
raise ValueError(msg) from e

Expand Down
4 changes: 3 additions & 1 deletion sup3r/preprocessing/data_handling/nc_data_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ def extract_feature(cls,
time_slice)

else:
msg = f'{feature} cannot be extracted from source data.'
available = [str(var) for var in handle.variables]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use cls.get_handle_features here, for both nc and h5

msg = (f'Requested feature "{feature}" cannot be extracted from '
f'source data that has available variables: {available}.')
logger.exception(msg)
raise ValueError(msg)

Expand Down
52 changes: 29 additions & 23 deletions sup3r/preprocessing/feature_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,11 +1223,13 @@ def get_height(feature):
height to use for interpolation
in meters
"""
height = re.search(r'\d+m', feature)
if height:
height = height.group(0).strip('m')
if not height.isdigit():
height = None
height = None
if isinstance(feature, str):
height = re.search(r'\d+m', feature)
if height:
height = height.group(0).strip('m')
if not height.isdigit():
height = None
return height

@staticmethod
Expand All @@ -1244,11 +1246,13 @@ def get_pressure(feature):
float | None
pressure to use for interpolation in pascals
"""
pressure = re.search(r'\d+pa', feature)
if pressure:
pressure = pressure.group(0).strip('pa')
if not pressure.isdigit():
pressure = None
pressure = None
if isinstance(feature, str):
pressure = re.search(r'\d+pa', feature)
if pressure:
pressure = pressure.group(0).strip('pa')
if not pressure.isdigit():
pressure = None
return pressure


Expand Down Expand Up @@ -1752,10 +1756,11 @@ def _exact_lookup(cls, feature):
Matching feature registry entry.
"""
out = None
for k, v in cls.FEATURE_REGISTRY.items():
if k.lower() == feature.lower():
out = v
break
if isinstance(feature, str):
for k, v in cls.FEATURE_REGISTRY.items():
if k.lower() == feature.lower():
out = v
break
return out

@classmethod
Expand All @@ -1774,10 +1779,11 @@ def _pattern_lookup(cls, feature):
Matching feature registry entry.
"""
out = None
for k, v in cls.FEATURE_REGISTRY.items():
if re.match(k.lower(), feature.lower()):
out = v
break
if isinstance(feature, str):
for k, v in cls.FEATURE_REGISTRY.items():
if re.match(k.lower(), feature.lower()):
out = v
break
return out

@classmethod
Expand Down Expand Up @@ -1816,7 +1822,7 @@ def _lookup(cls, out, feature, handle_features=None):
if pressure is not None:
out = out.split('(.*)')[0] + f'{pressure}pa'

return lambda x: [out]
return lambda x: [out] if isinstance(out, str) else out

@classmethod
def lookup(cls, feature, attr_name, handle_features=None):
Expand Down Expand Up @@ -1851,7 +1857,6 @@ def lookup(cls, feature, attr_name, handle_features=None):
return getattr(out, attr_name, None)

elif attr_name == 'inputs':

return cls._lookup(out, feature, handle_features)

@classmethod
Expand All @@ -1873,11 +1878,11 @@ def get_inputs_recursive(cls, feature, handle_features):
"""
raw_features = []
method = cls.lookup(feature, 'inputs', handle_features=handle_features)
lower_handle_features = [f.lower() for f in handle_features]
low_handle_features = [f.lower() for f in handle_features]
vhf = cls.valid_handle_features([feature.lower()], low_handle_features)

check1 = feature not in raw_features
check2 = (cls.valid_handle_features(
[feature.lower()], lower_handle_features) or method is None)
check2 = (vhf or method is None)

if check1 and check2:
raw_features.append(feature)
Expand Down Expand Up @@ -1924,6 +1929,7 @@ def get_raw_feature_list(cls, features, handle_features):
f'Requested features: {req}')
logger.error(msg)
raise ValueError(msg)

return raw_features

@classmethod
Expand Down