def test_export2thredds_rmmetadata(self): n = Nansat(self.test_file_arctic, mapper=self.default_mapper, log_level=40) n.export2thredds(self.tmp_filename, {'Bristol': { 'type': '>i2' }}, time=datetime.datetime(2016, 1, 20), rm_metadata=['description'])
def test_example1(self): n = Nansat(self.tmp_ncfile) n.export2thredds(self.filename_exported, mask_name='mask', no_mask_value=1) self.assertTrue(os.path.exists(self.filename_exported)) ds = Dataset(self.filename_exported) self.assertEqual(ds.variables['x_wind_10m'][0, 0, 0].data, 9) self.assertTrue(np.isnan(ds.variables['x_wind_10m'][0, -1, 0].data))
def test_export2thredds_stere_one_band(self): # skip the test if anaconda is used if IS_CONDA: return n = Nansat(self.test_file_stere, logLevel=40) tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_1b.nc') n.export2thredds(tmpfilename, ['L_469']) self.assertTrue(os.path.exists(tmpfilename))
def test_export2thredds_rmmetadata(self): n = Nansat(self.test_file_arctic, mapper=self.default_mapper, log_level=40) with warnings.catch_warnings(record=True) as recorded_warnings: n.export2thredds(self.tmp_filename, {'Bristol': { 'type': '>i2' }}, time=datetime.datetime(2016, 1, 20), rmMetadata=['description']) self.assertEqual(recorded_warnings[0].category, NansatFutureWarning)
def test_export2thredds_longlat_list(self): d = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs", "-te 27 70 31 72 -ts 200 200") n = Nansat(domain=d) n.add_band(np.ones(d.shape(), np.float32), parameters={'name': 'L_469'}) n.set_metadata('time_coverage_start', '2016-01-19') tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_longlat.nc') n.export2thredds(tmpfilename, ['L_469']) ncI = netcdf_file(tmpfilename, 'r') ncIVar = ncI.variables['L_469'] self.assertTrue(ncIVar.grid_mapping in ncI.variables.keys())
def test_export2thredds_arctic_long_lat(self): n = Nansat(self.test_file_arctic, mapper=self.default_mapper, log_level=40) tmpfilename = os.path.join(self.tmp_data_path, 'nansat_export2thredds_arctic.nc') bands = { 'Bristol': { 'type': '>i2' }, 'Bootstrap': { 'type': '>i2' }, 'UMass_AES': { 'type': '>i2' }, } n.export2thredds(tmpfilename, bands, time=datetime.datetime(2016, 1, 20)) self.assertTrue(os.path.exists(tmpfilename)) g = gdal.Open(tmpfilename) metadata = g.GetMetadata_Dict() # GDAL behaves differently: # Windows: nc-attributes are accessible without 'NC_GLOBAL#' prefix # Linux: nc-attributes are accessible only with 'NC_GLOBAL#' prefix # OSX: ? # Therefore we have to add NC_GLOBAL# and test if such metadata exists nc_prefix = 'NC_GLOBAL#' if not nc_prefix + 'easternmost_longitude' in metadata: nc_prefix = '' self.assertIn(nc_prefix + 'easternmost_longitude', metadata) # Test that the long/lat values are set correctly test_metadata_keys = [ 'easternmost_longitude', 'westernmost_longitude', 'northernmost_latitude', 'southernmost_latitude' ] test_metadata_min = [179, -180, 89.9, 53] test_metadata_max = [180, -179, 90, 54] for i, test_metadata_key in enumerate(test_metadata_keys): medata_value = float(metadata[nc_prefix + test_metadata_key]) self.assertTrue( medata_value >= test_metadata_min[i], '%s is wrong: %f' % (test_metadata_key, medata_value)) self.assertTrue( medata_value <= test_metadata_max[i], '%s is wrong: %f' % (test_metadata_key, medata_value))
def test_export2thredds_stere_many_bands(self): # skip the test if anaconda is used if IS_CONDA: return n = Nansat(self.test_file_stere, logLevel=40) tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_3b.nc') bands = { 'L_645' : {'type': '>i1'}, 'L_555' : {'type': '>i1'}, 'L_469' : {'type': '>i1'}, } n.export2thredds(tmpfilename, bands) self.assertTrue(os.path.exists(tmpfilename))
def test_example2(self): n = Nansat(self.tmp_ncfile) res = n.export2thredds(self.filename_exported, {'x_wind_10m': { 'description': 'example' }}) self.assertEqual(res, None)
def test_export2thredds_arctic_long_lat(self): n = Nansat(self.test_file_arctic, logLevel=40) tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_arctic.nc') bands = { 'Bristol': { 'type': '>i2' }, 'Bootstrap': { 'type': '>i2' }, 'UMass_AES': { 'type': '>i2' }, } n.export2thredds(tmpfilename, bands, time=datetime.datetime(2016, 1, 20)) self.assertTrue(os.path.exists(tmpfilename)) g = gdal.Open(tmpfilename) metadata = g.GetMetadata_Dict() # Test that the long/lat values are set aproximately correct ncg = 'NC_GLOBAL#' easternmost_longitude = metadata.get(ncg + 'easternmost_longitude') self.assertTrue( float(easternmost_longitude) > 179, 'easternmost_longitude is wrong:' + easternmost_longitude) westernmost_longitude = metadata.get(ncg + 'westernmost_longitude') self.assertTrue( float(westernmost_longitude) < -179, 'westernmost_longitude is wrong:' + westernmost_longitude) northernmost_latitude = metadata.get(ncg + 'northernmost_latitude') self.assertTrue( float(northernmost_latitude) > 89.999, 'northernmost_latitude is wrong:' + northernmost_latitude) southernmost_latitude = metadata.get(ncg + 'southernmost_latitude') self.assertTrue( float(southernmost_latitude) < 54, 'southernmost_latitude is wrong:' + southernmost_latitude) self.assertTrue( float(southernmost_latitude) > 53, 'southernmost_latitude is wrong:' + southernmost_latitude)
def test_export2thredds_stere_many_bands(self): # skip the test if anaconda is used if IS_CONDA: return n = Nansat(self.test_file_stere, logLevel=40) tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_3b.nc') bands = { 'L_645': { 'type': '>i1' }, 'L_555': { 'type': '>i1' }, 'L_469': { 'type': '>i1' }, } n.export2thredds(tmpfilename, bands) self.assertTrue(os.path.exists(tmpfilename))
def test_export2thredds_arctic_long_lat(self): n = Nansat(self.test_file_arctic, logLevel=40) tmpfilename = os.path.join(ntd.tmp_data_path, 'nansat_export2thredds_arctic.nc') bands = { 'Bristol': {'type': '>i2'}, 'Bootstrap': {'type': '>i2'}, 'UMass_AES': {'type': '>i2'}, } n.export2thredds(tmpfilename, bands, time=datetime.datetime(2016, 1, 20)) self.assertTrue(os.path.exists(tmpfilename)) g = gdal.Open(tmpfilename) metadata = g.GetMetadata_Dict() # Test that the long/lat values are set aproximately correct ncg = 'NC_GLOBAL#' easternmost_longitude = metadata.get(ncg + 'easternmost_longitude') self.assertTrue(float(easternmost_longitude) > 179, 'easternmost_longitude is wrong:' + easternmost_longitude) westernmost_longitude = metadata.get(ncg + 'westernmost_longitude') self.assertTrue(float(westernmost_longitude) < -179, 'westernmost_longitude is wrong:' + westernmost_longitude) northernmost_latitude = metadata.get(ncg + 'northernmost_latitude') self.assertTrue(float(northernmost_latitude) > 89.999, 'northernmost_latitude is wrong:' + northernmost_latitude) southernmost_latitude = metadata.get(ncg + 'southernmost_latitude') self.assertTrue(float(southernmost_latitude) < 54, 'southernmost_latitude is wrong:' + southernmost_latitude) self.assertTrue(float(southernmost_latitude) > 53, 'southernmost_latitude is wrong:' + southernmost_latitude)
def test_export2thredds_arctic_long_lat(self): n = Nansat(self.test_file_arctic, mapper=self.default_mapper, log_level=40) tmpfilename = os.path.join(self.tmp_data_path, 'nansat_export2thredds_arctic.nc') bands = { 'Bristol': {'type': '>i2'}, 'Bootstrap': {'type': '>i2'}, 'UMass_AES': {'type': '>i2'}, } n.export2thredds(tmpfilename, bands, time=datetime.datetime(2016, 1, 20)) self.assertTrue(os.path.exists(tmpfilename)) g = gdal.Open(tmpfilename) metadata = g.GetMetadata_Dict() # GDAL behaves differently: # Windows: nc-attributes are accessible without 'NC_GLOBAL#' prefix # Linux: nc-attributes are accessible only with 'NC_GLOBAL#' prefix # OSX: ? # Therefore we have to add NC_GLOBAL# and test if such metadata exists nc_prefix = 'NC_GLOBAL#' if not nc_prefix + 'easternmost_longitude' in metadata: nc_prefix = '' self.assertIn(nc_prefix + 'easternmost_longitude', metadata) # Test that the long/lat values are set correctly test_metadata_keys = ['easternmost_longitude', 'westernmost_longitude', 'northernmost_latitude', 'southernmost_latitude'] test_metadata_min = [179, -180, 89.9, 53] test_metadata_max = [180, -179, 90, 54] for i, test_metadata_key in enumerate(test_metadata_keys): medata_value = float(metadata[nc_prefix + test_metadata_key]) self.assertTrue(medata_value >= test_metadata_min[i], '%s is wrong: %f'%(test_metadata_key, medata_value)) self.assertTrue(medata_value <= test_metadata_max[i], '%s is wrong: %f'%(test_metadata_key, medata_value))
def test_example3(self): n = Nansat(self.tmp_ncfile) res = n.export2thredds( self.filename_exported, { 'x_wind_10m': { 'type': '>i2', 'scale': 0.1, 'offset': 0 }, 'y_wind_10m': { 'type': '>i2', 'scale': 0.1, 'offset': 0 } }) # TODO: test that the type, scale and offset are actually modified according to the input self.assertEqual(res, None)
def test_export2thredds_rmmetadata(self): n = Nansat(self.test_file_arctic, mapper=self.default_mapper, log_level=40) n.export2thredds(self.tmp_filename, {'Bristol': {'type': '>i2'}}, time=datetime.datetime(2016, 1, 20), rm_metadata=['description'])
def test_example1(self): n = Nansat(self.tmp_ncfile) res = n.export2thredds(self.filename_exported) self.assertEqual(res, None)
def test_export2thredds_longlat_list(self): n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper) with self.assertRaises(ValueError): n.export2thredds('aa', ['L_469'])