def test_all_basic(self): """Test everything about the NetCDF4 class.""" from satpy.readers.netcdf_utils import NetCDF4FileHandler import xarray as xr file_handler = NetCDF4FileHandler('test.nc', {}, {}) self.assertEqual(file_handler['/dimension/rows'], 10) self.assertEqual(file_handler['/dimension/cols'], 100) for ds in ('test_group/ds1_f', 'test_group/ds1_i', 'ds2_f', 'ds2_i'): self.assertEqual(file_handler[ds].dtype, np.float32 if ds.endswith('f') else np.int32) self.assertTupleEqual(file_handler[ds + '/shape'], (10, 100)) self.assertEqual(file_handler[ds + '/attr/test_attr_str'], 'test_string') self.assertEqual(file_handler[ds + '/attr/test_attr_int'], 0) self.assertEqual(file_handler[ds + '/attr/test_attr_float'], 1.2) self.assertEqual(file_handler['/attr/test_attr_str'], 'test_string') self.assertEqual(file_handler['/attr/test_attr_str_arr'], 'test_string2') self.assertEqual(file_handler['/attr/test_attr_int'], 0) self.assertEqual(file_handler['/attr/test_attr_float'], 1.2) self.assertIsInstance(file_handler.get('ds2_f')[:], xr.DataArray) self.assertIsNone(file_handler.get('fake_ds')) self.assertEqual(file_handler.get('fake_ds', 'test'), 'test') self.assertTrue('ds2_f' in file_handler) self.assertFalse('fake_ds' in file_handler) self.assertIsNone(file_handler.file_handle) self.assertEqual(file_handler["ds2_sc"], 42)
def test_filenotfound(self): """Test that error is raised when file not found """ from satpy.readers.netcdf_utils import NetCDF4FileHandler with self.assertRaises(IOError): NetCDF4FileHandler("/thisfiledoesnotexist.nc", {}, {})
def test_caching(self): """Test that caching works as intended. """ from satpy.readers.netcdf_utils import NetCDF4FileHandler h = NetCDF4FileHandler("test.nc", {}, {}, cache_var_size=1000, cache_handle=True) self.assertIsNotNone(h.file_handle) self.assertTrue(h.file_handle.isopen()) self.assertEqual(sorted(h.cached_file_content.keys()), ["ds2_s", "ds2_sc"]) # with caching, these tests access different lines than without np.testing.assert_array_equal(h["ds2_s"], np.arange(10)) np.testing.assert_array_equal(h["test_group/ds1_i"], np.arange(10 * 100).reshape((10, 100))) h.__del__() self.assertFalse(h.file_handle.isopen())