def test_missing_temporal_extent_but_start_and_end_provided_as_kwargs(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_attrs.nc') with self.assertRaises(AttributeError): nc2mmd.to_mmd(time_coverage_start='1850-01-01T00:00:00Z', time_coverage_end='1950-01-01T00:00:00Z') self.assertEqual(nc2mmd.metadata['temporal_extent']['start_date'], '1850-01-01T00:00:00Z') self.assertEqual(nc2mmd.metadata['temporal_extent']['end_date'], '1950-01-01T00:00:00Z')
def test_missing_temporal_extent(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_attrs.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_temporal_extents(mmd_yaml['temporal_extent'], ncin) self.assertEqual(value, []) self.assertEqual(nc2mmd.missing_attributes['errors'][0], 'time_coverage_start is a required ACDD attribute')
def test_temporal_extent(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_temporal_extents(mmd_yaml['temporal_extent'], ncin) self.assertEqual(value[0]['start_date'], '2020-11-27T13:40:02.019817Z') self.assertEqual(value[0]['end_date'], '2020-11-27T13:51:24.401505Z')
def test_last_metadata_update(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_metadata_updates(mmd_yaml['last_metadata_update'], ncin) self.assertEqual(value['update'][0]['datetime'], '2020-11-27T14:05:56Z')
def test__to_mmd__get_correct_id_from_ncfile(self): tested = tempfile.mkstemp()[1] # The id attribute is a uuid nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc', output_file=tested) nc2mmd.to_mmd() self.assertEqual(nc2mmd.missing_attributes['warnings'][0], 'Using default value Active for metadata_status') self.assertEqual(nc2mmd.missing_attributes['errors'], [])
def test_create_mmd_2(self): """ToDo: Add docstring""" self.maxDiff = None tested = tempfile.mkstemp()[1] md = Nc_to_mmd(self.fail_nc, output_file=tested) with self.assertRaises(AttributeError): md.to_mmd()
def test_keywords_vocabulary_missing(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_keywords_vocab.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_keywords(mmd_yaml['keywords'], ncin) self.assertEqual(nc2mmd.missing_attributes['errors'][0], 'keywords_vocabulary is a required ACDD attribute')
def test_metadata_status_is_active(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_acdd_metadata(mmd_yaml['metadata_status'], ncin, 'metadata_status') self.assertEqual(value, 'Active')
def test_personnel_acdd_roles_not_list(self): """Test that we can have multiple people in MMD personnel field""" mmd_yaml = yaml.load( resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader ) nc2mmd = Nc_to_mmd( 'tests/data/reference_nc_attrs_multiple_and_contributor.nc', check_only=True ) ncin = Dataset(nc2mmd.netcdf_product) mmd_yaml['personnel']['name']['acdd'].pop(-1) mmd_yaml['personnel']['name']['acdd'] = mmd_yaml['personnel']['name']['acdd'][0] mmd_yaml['personnel']['role']['acdd'].pop(-1) mmd_yaml['personnel']['role']['acdd'] = mmd_yaml['personnel']['role']['acdd'][0] mmd_yaml['personnel']['email'].pop('acdd_ext') mmd_yaml['personnel']['organisation'].pop('acdd_ext') value = nc2mmd.get_personnel(mmd_yaml['personnel'], ncin) self.assertEqual(value[0]['name'], 'Trygve') self.assertEqual(value[1]['name'], 'Nina') self.assertEqual(value[0]['role'], 'Investigator') self.assertEqual(value[1]['role'], 'Technical contact') self.assertEqual(value[0]['email'], '*****@*****.**') self.assertEqual(value[1]['email'], '*****@*****.**') self.assertEqual(value[0]['organisation'], 'Norwegian Meteorological Institute') self.assertEqual(value[1]['organisation'], 'Norwegian Meteorological Institute')
def test_create_mmd_1(self): """Test MMD creation from a valid netcdf file, validation with the mmd_strict.xsd, and that some fields are as expected. Please add new fields to test as needed.. """ tested = tempfile.mkstemp()[1] # md = Nc_to_mmd(self.reference_nc, output_file=tested) md = Nc_to_mmd('tests/data/reference_nc_with_altID_multiple.nc', output_file=tested) md.to_mmd() xsd_obj = etree.XMLSchema(etree.parse(self.reference_xsd)) xml_doc = etree.ElementTree(file=tested) valid = xsd_obj.validate(xml_doc) self.assertTrue(valid) """ Check content of the xml_doc """ # alternate_identifier self.assertEqual( xml_doc.getroot().find( "{http://www.met.no/schema/mmd}alternate_identifier[@type='dummy_type']" ).text, "dummy_id_no1" ) self.assertEqual( xml_doc.getroot().find( "{http://www.met.no/schema/mmd}alternate_identifier[@type='other_type']" ).text, "dummy_id_no2" )
def test_alternate_identifier(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_acdd_metadata(mmd_yaml['alternate_identifier'], ncin, 'alternate_identifier') self.assertEqual(value, [])
def test_spatial_repr(self): """ToDo: Add docstring""" tempfile.mkstemp()[1] nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc', check_only=True) nc2mmd.to_mmd() spatial_repr = nc2mmd.metadata['spatial_representation'] self.assertEqual(spatial_repr, 'grid')
def test_access_constraint(self): """ToDo: Add docstring""" tempfile.mkstemp()[1] nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc', check_only=True) nc2mmd.to_mmd() spatial_repr = nc2mmd.metadata['access_constraint'] self.assertEqual(spatial_repr, 'Open')
def test_missing_vocabulary_platform(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_keywords_vocab.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_platforms(mmd_yaml['platform'], ncin) self.assertEqual(value[0]['resource'], '')
def test_missing_geographic_extent(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_attrs.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_acdd_metadata(mmd_yaml['geographic_extent'], ncin, 'geographic_extent') self.assertEqual(value['rectangle']['north'], None) self.assertEqual(nc2mmd.missing_attributes['errors'][0], 'geospatial_lat_max is a required attribute')
def test_missing_id(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) # nc file is missing the id attribute nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_keywords_vocab.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_metadata_identifier(mmd_yaml['metadata_identifier'], ncin) self.assertTrue(Nc_to_mmd.is_valid_uuid(value))
def test__to_mmd__error_if_accd_id_is_invalid(self): """Test that metadata_identifier is set to an empty string, and that an exception is raised if the ACDD id is not a valid uuid. """ tested = tempfile.mkstemp()[1] # The id attribute is not a uuid nc2mmd = Nc_to_mmd('tests/data/reference_nc_id_not_uuid.nc', output_file=tested) with self.assertRaises(AttributeError): nc2mmd.to_mmd() self.assertEqual( nc2mmd.missing_attributes['errors'][0], ( 'naming_authority is a required attribute.' ) ) self.assertEqual( nc2mmd.missing_attributes['errors'][1], ( 'id ACDD attribute is not valid.' ) ) self.assertEqual(nc2mmd.missing_attributes['warnings'], []) self.assertFalse(Nc_to_mmd.is_valid_uuid(nc2mmd.metadata['metadata_identifier'])) ncin = Dataset(nc2mmd.netcdf_product) id = ncin.getncattr('id') self.assertNotEqual(id, nc2mmd.metadata['metadata_identifier']) self.assertEqual(':', nc2mmd.metadata['metadata_identifier'])
def test_related_dataset(self): nc2mmd = Nc_to_mmd('tests/data/reference_nc_id_missing.nc') ncin = Dataset('tests/data/reference_nc_id_missing.nc') mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) data = nc2mmd.get_related_dataset(mmd_yaml['related_dataset'], ncin) self.assertEqual(data[0]['id'], 'b7cb7934-77ca-4439-812e-f560df3fe7eb') self.assertEqual(data[0]['relation_type'], 'parent')
def test_get_data_access_dict(self): nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) nc2mmd.netcdf_product = 'https://thredds.met.no/thredds/dodsC/arcticdata/S2S_drift_TCD/SIDRIFT_S2S_SH/2019/07/31/' + nc2mmd.netcdf_product data = nc2mmd.get_data_access_dict(ncin) self.assertEqual(data[0]['type'], 'OPeNDAP') self.assertEqual(data[1]['type'], 'HTTP')
def test_dataset_citation_missing_attrs(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_keywords_vocab.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_dataset_citations(mmd_yaml['dataset_citation'], ncin) self.assertEqual(value[0]['url'], '') self.assertEqual(value[0]['other'], '')
def test_platforms(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_platforms(mmd_yaml['platform'], ncin) self.assertEqual(value[0]['long_name'], 'SNPP') self.assertEqual(value[0]['instrument']['long_name'], 'VIIRS')
def test_publication_date(self): format = '%Y-%m-%d' mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_dataset_citations(mmd_yaml['dataset_citation'], ncin) dt = datetime.datetime.strptime(value[0]['publication_date'], format) self.assertEqual(dt, datetime.datetime(2020, 11, 27, 0, 0))
def test_create_mmd_missing_update_times(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) md = Nc_to_mmd(self.fail_nc) ncin = Dataset(md.netcdf_product) value = md.get_metadata_updates(mmd_yaml['last_metadata_update'], ncin) self.assertEqual(md.missing_attributes['errors'][0], 'ACDD attribute date_created or date_metadata_modified is required')
def test_keywords_multiple(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc_attrs_multiple.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_keywords(mmd_yaml['keywords'], ncin) self.assertEqual(value[0]['vocabulary'], 'GCMD') self.assertEqual(value[0]['resource'], 'https://gcmdservices.gsfc.nasa.gov/static/kms/') self.assertEqual(value[0]['keyword'], 'Earth Science > Atmosphere > Atmospheric radiation')
def test_create_mmd_missing_abstract(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) md = Nc_to_mmd(self.fail_nc) ncin = Dataset(md.netcdf_product) value = md.get_abstracts(mmd_yaml['abstract'], ncin) self.assertEqual(md.missing_attributes['errors'][0], 'summary is a required ACDD attribute')
def test_iso_topic_category(self): mmd_yaml = yaml.load(resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc') ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_acdd_metadata(mmd_yaml['iso_topic_category'], ncin, 'iso_topic_category') self.assertEqual(value[0], 'climatologyMeteorologyAtmosphere') self.assertEqual(value[1], 'environment') self.assertEqual(value[2], 'oceans')
def test_missing_temporal_extent_but_start_provided_as_kwarg(self): """ToDo: Add docstring""" yaml.load( resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader ) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_attrs.nc', check_only=True) with self.assertRaises(AttributeError): nc2mmd.to_mmd(time_coverage_start='1850-01-01T00:00:00Z') self.assertEqual(nc2mmd.metadata['temporal_extent']['start_date'], '1850-01-01T00:00:00Z')
def test_projects(self): """Test getting project information from nc-file""" mmd_yaml = yaml.load( resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader ) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc', check_only=True) ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_projects(mmd_yaml['project'], ncin) self.assertEqual(value[0]['long_name'], 'MET Norway core services')
def test_personnel(self): """Test reading of personnel from nc file into MMD""" mmd_yaml = yaml.load( resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader ) nc2mmd = Nc_to_mmd('tests/data/reference_nc.nc', check_only=True) ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_personnel(mmd_yaml['personnel'], ncin) self.assertEqual(value[0]['email'], '*****@*****.**')
def test_missing_vocabulary_platform_instrument_short_name(self): """ToDo: Add docstring""" mmd_yaml = yaml.load( resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader ) nc2mmd = Nc_to_mmd('tests/data/reference_nc_missing_keywords_vocab.nc', check_only=True) ncin = Dataset(nc2mmd.netcdf_product) value = nc2mmd.get_platforms(mmd_yaml['platform'], ncin) self.assertEqual(value[0]['instrument']['short_name'], '')