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

Adds support for forecast reference metadata in CF checks. #779

Merged
merged 2 commits into from
Mar 25, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compliance_checker/cfutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def is_geophysical(ds, variable):
if variable in get_auxiliary_coordinate_variables(ds):
return False

if variable in get_forecast_metadata_variables(ds):
return False

# Is it dimensionless and unitless?
if len(ncvar.shape) == 0 and unitless:
return False
Expand Down Expand Up @@ -296,6 +299,26 @@ def get_auxiliary_coordinate_variables(ds):
return ret_val


def get_forecast_metadata_variables(ds):
'''
Returns a list of variables that represent forecast reference time
metadata.

:param netCDF4.Dataset ds: An open netCDF4 Dataset.
:rtype: list
'''
forecast_metadata_standard_names = [
'forecast_period',
'forecast_reference_time',
]
forecast_metadata_variables = []
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually meant forecast_metadata_variables for the list -> set conversion, apologies if this wasn't explicit. This likely to be fairly minor, so I'll just keep as is and merge, possibly revisiting this for #727 and other similar places in code which use lists.

for varname in ds.variables:
standard_name = getattr(ds.variables[varname], 'standard_name', None)
if standard_name in forecast_metadata_standard_names:
forecast_metadata_variables.append(varname)
return forecast_metadata_variables


def get_cell_boundary_map(ds):
'''
Returns a dictionary mapping a variable to its boundary variable. The
Expand Down
32 changes: 32 additions & 0 deletions compliance_checker/tests/data/forecast_reference.cdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
netcdf forecast_reference.cdl {
dimensions:
time = 2 ;
lat = 4 ;
lon = 4 ;
variables:
float time(time) ;
time:units = "seconds since 1970-01-01" ;
time:standard_name = "time" ;
time:calendar = "gregorian" ;
time:axis = "T" ;
float lat(lat) ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
lat:axis = "Y" ;
float lon(lon) ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
lon:axis = "X" ;
float air_temp(time, lon, lat) ;
air_temp:units = "deg_C" ;
air_temp:standard_name = "air_temperature";
float forecast_reference_time(time) ;
forecast_reference_time:units = "seconds since 1970-01-01" ;
forecast_reference_time:standard_name = "forecast_reference_time" ;
float forecast_hour(time) ;
forecast_hour:units = "hours" ;
forecast_hour:standard_name = "forecast_period" ;

:featureType = "grid" ;

}
1 change: 1 addition & 0 deletions compliance_checker/tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def generate_dataset(cdl_path, nc_path):
'dimension_order' : get_filename('tests/data/dimension_order.cdl'),
'example-grid' : get_filename('tests/data/example-grid.cdl'),
'featureType' : get_filename('tests/data/example-grid.cdl'),
'forecast_reference' : get_filename('tests/data/forecast_reference.cdl'),
'fvcom' : get_filename('tests/data/examples/fvcom.cdl'),
'ghrsst' : get_filename('tests/data/20160919092000-ABOM-L3S_GHRSST-SSTfnd-AVHRR_D-1d_dn_truncate.cdl'),
'glcfs' : get_filename('tests/data/examples/glcfs.cdl'),
Expand Down
14 changes: 14 additions & 0 deletions compliance_checker/tests/test_feature_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ def test_auxiliary_coordinates(self):
aux_coord_vards = util.get_auxiliary_coordinate_variables(nc)
assert set(['lat', 'lon']) == set(aux_coord_vards)

def test_forecast_reference_metadata(self):
'''
Tests variables used for forecast reference metadata to ensure they are
not misclassified as geophysical variables.
'''
with Dataset(resources.STATIC_FILES['forecast_reference']) as nc:
assert util.is_geophysical(nc, 'forecast_reference_time') is False
assert util.is_geophysical(nc, 'forecast_hour') is False
assert util.is_geophysical(nc, 'air_temp') is True
assert util.is_geophysical(nc, 'time') is False

assert len(util.get_coordinate_variables(nc)) == 3
assert len(util.get_geophysical_variables(nc)) == 1

def test_rotated_pole_grid(self):
with Dataset(resources.STATIC_FILES['rotated_pole_grid']) as nc:
latitudes = util.get_latitude_variables(nc)
Expand Down