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 GRIB reader for greater flexibility. #1586

Merged
merged 3 commits into from
Mar 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 21 additions & 13 deletions satpy/readers/grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,25 +271,33 @@ def get_metadata(self, msg, ds_info):
center_description = msg['centreDescription']
except (RuntimeError, KeyError):
center_description = None

key_dicts = {
'shortName': 'shortName',
'long_name': 'name',
'pressureUnits': 'pressureUnits',
'typeOfLevel': 'typeOfLevel',
'standard_name': 'cfName',
'units': 'units',
'modelName': 'modelName',
'valid_min': 'minimum',
'valid_max': 'maximum',
'sensor': 'modelName'}

ds_info.update({
'filename': self.filename,
'shortName': msg['shortName'],
'long_name': msg['name'],
'pressureUnits': msg['pressureUnits'],
'typeOfLevel': msg['typeOfLevel'],
'standard_name': msg['cfName'],
'units': msg['units'],
'modelName': msg['modelName'],
'model_time': model_time,
'centreDescription': center_description,
'valid_min': msg['minimum'],
'valid_max': msg['maximum'],
'start_time': start_time,
'end_time': end_time,
'sensor': msg['modelName'],
# National Weather Prediction
'platform_name': 'unknown',
})
'platform_name': 'unknown'})

for key in key_dicts:
if key_dicts[key] in msg.keys():
ds_info[key] = msg[key_dicts[key]]
else:
ds_info[key] = 'unknown'

return ds_info

def get_dataset(self, dataset_id, ds_info):
Expand Down
49 changes: 42 additions & 7 deletions satpy/tests/reader_tests/test_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
)


def fake_gribdata():
"""Return some faked data for use as grib values."""
return np.arange(25.).reshape((5, 5))


def _round_trip_projection_lonlat_check(area):
"""Check that X/Y coordinates can be transformed multiple times.

Expand Down Expand Up @@ -77,6 +82,10 @@ def __init__(self, values, proj_params=None, latlons=None, **attrs):
self.projparams = proj_params
self._latlons = latlons

def keys(self):
"""Get message keys."""
return self.attrs.keys()

def latlons(self):
"""Get coordinates."""
return self._latlons
Expand All @@ -101,7 +110,7 @@ def __init__(self, messages=None, proj_params=None, latlons=None):
else:
self._messages = [
FakeMessage(
values=np.arange(25.).reshape((5, 5)),
values=fake_gribdata(),
name='TEST',
shortName='t',
level=100,
Expand All @@ -115,7 +124,7 @@ def __init__(self, messages=None, proj_params=None, latlons=None):
distinctLongitudes=np.arange(5.),
distinctLatitudes=np.arange(5.),
missingValue=9999,
modelName='unknown',
modelName='notknown',
minimum=100.,
maximum=200.,
typeOfLevel='isobaricInhPa',
Expand All @@ -124,7 +133,7 @@ def __init__(self, messages=None, proj_params=None, latlons=None):
latlons=latlons,
),
FakeMessage(
values=np.arange(25.).reshape((5, 5)),
values=fake_gribdata(),
name='TEST',
shortName='t',
level=200,
Expand All @@ -138,16 +147,16 @@ def __init__(self, messages=None, proj_params=None, latlons=None):
distinctLongitudes=np.arange(5.),
distinctLatitudes=np.arange(5.),
missingValue=9999,
modelName='unknown',
modelName='notknown',
minimum=100.,
maximum=200.,
typeOfLevel='isobaricInhPa',
jScansPositively=0,
jScansPositively=1,
proj_params=proj_params,
latlons=latlons,
),
FakeMessage(
values=np.arange(25.).reshape((5, 5)),
values=fake_gribdata(),
name='TEST',
shortName='t',
level=300,
Expand All @@ -161,7 +170,6 @@ def __init__(self, messages=None, proj_params=None, latlons=None):
distinctLongitudes=np.arange(5.),
distinctLatitudes=np.arange(5.),
missingValue=9999,
modelName='unknown',
minimum=100.,
maximum=200.,
typeOfLevel='isobaricInhPa',
Expand Down Expand Up @@ -307,3 +315,30 @@ def test_area_def_crs(self, proj_params, lon_corners, lat_corners):
if not hasattr(area, 'crs'):
pytest.skip("Can't test with pyproj < 2.0")
_round_trip_projection_lonlat_check(area)

@pytest.mark.parametrize(TEST_ARGS, TEST_PARAMS)
def test_missing_attributes(self, proj_params, lon_corners, lat_corners):
"""Check that the grib reader handles missing attributes in the grib file."""
fake_pygrib = self._get_fake_pygrib(proj_params, lon_corners, lat_corners)

# This has modelName
query_contains = DataQuery(name='t', level=100, modifiers=tuple())
# This does not have modelName
query_not_contains = DataQuery(name='t', level=300, modifiers=tuple())
dataset = self._get_test_datasets([query_contains, query_not_contains], fake_pygrib)
assert dataset[query_contains].attrs['modelName'] == 'notknown'
assert dataset[query_not_contains].attrs['modelName'] == 'unknown'

@pytest.mark.parametrize(TEST_ARGS, TEST_PARAMS)
def test_jscanspositively(self, proj_params, lon_corners, lat_corners):
"""Check that data is flipped if the jScansPositively is present."""
fake_pygrib = self._get_fake_pygrib(proj_params, lon_corners, lat_corners)

# This has no jScansPositively
query_not_contains = DataQuery(name='t', level=100, modifiers=tuple())
# This contains jScansPositively
query_contains = DataQuery(name='t', level=200, modifiers=tuple())
dataset = self._get_test_datasets([query_contains, query_not_contains], fake_pygrib)

np.testing.assert_allclose(fake_gribdata(), dataset[query_not_contains].values)
np.testing.assert_allclose(fake_gribdata(), dataset[query_contains].values[::-1])