示例#1
0
 def test_get_dimensioned_variables_one_variable_in_target_dataset(self):
     uri = self.test_data.get_uri('cancm4_tas')
     rd = RequestDataset(uri=uri)
     driver = DriverNetcdf(rd)
     ret = driver.get_dimensioned_variables()
     self.assertEqual(ret, ['tas'])
     self.assertEqual(rd._variable, ('tas',))
示例#2
0
    def test_get_name_bounds_dimension(self):
        rd = self.test_data.get_rd('cancm4_tas')
        source_metadata = rd.source_metadata
        res = DriverNetcdf._get_name_bounds_dimension_(source_metadata)
        self.assertEqual(res, 'bnds')

        # remove any mention of bounds from the dimension map and try again
        for value in source_metadata['dim_map'].itervalues():
            try:
                value['bounds'] = None
            except TypeError:
                # likely a nonetype
                if value is not None:
                    raise
        res = DriverNetcdf._get_name_bounds_dimension_(source_metadata)
        self.assertIsNone(res)

        # now remove the bounds key completely
        for value in source_metadata['dim_map'].itervalues():
            try:
                value.pop('bounds')
            except AttributeError:
                if value is not None:
                    raise
        res = DriverNetcdf._get_name_bounds_dimension_(source_metadata)
        self.assertIsNone(res)
示例#3
0
    def test_get_field_different_dimension_names_and_values(self):
        """Test dimension names and dimension values are correctly read from netCDF."""

        path = os.path.join(self.current_dir_output, 'foo.nc')
        with nc_scope(path, 'w') as ds:
            ds.createDimension('lat', 1)
            ds.createDimension('lon', 1)
            ds.createDimension('tme', 1)
            ds.createDimension('the_bounds', 2)
            latitude = ds.createVariable('latitude', int, dimensions=('lat',))
            longitude = ds.createVariable('longitude', int, dimensions=('lon',))
            time = ds.createVariable('time', int, dimensions=('tme',))
            time_bounds = ds.createVariable('long_live_the_bounds', int, dimensions=('tme', 'the_bounds'))
            time.units = 'days since 0000-01-01'
            time.bounds = 'long_live_the_bounds'
            value = ds.createVariable('value', int, dimensions=('tme', 'lat', 'lon'))

            latitude[:] = 5
            longitude[:] = 6
            time[:] = 6
            value[:] = np.array([7]).reshape(1, 1, 1)

        rd = RequestDataset(path)
        driver = DriverNetcdf(rd)
        field = driver._get_field_()
        self.assertEqual(field.temporal.name, 'tme')
        self.assertEqual(field.temporal.name_value, 'time')
        self.assertEqual(field.spatial.grid.row.name, 'lat')
        self.assertEqual(field.spatial.grid.row.name_value, 'latitude')
        self.assertEqual(field.spatial.grid.col.name, 'lon')
        self.assertEqual(field.spatial.grid.col.name_value, 'longitude')
        self.assertEqual(field.temporal.name_bounds, 'long_live_the_bounds')
示例#4
0
 def test_open(self):
     # test a multifile dataset where the variable does not appear in all datasets
     uri1 = self.test_data.get_uri('cancm4_tas')
     uri2 = self.test_data.get_uri('cancm4_tasmax_2001')
     uri = [uri1, uri2]
     rd = RequestDataset(uri=uri, variable='tas')
     driver = DriverNetcdf(rd)
     with self.assertRaises(KeyError):
         driver.open()
     with self.assertRaises(KeyError):
         rd.source_metadata
示例#5
0
 def test_get_vector_dimension(self):
     # test exception raised with no row and column
     path = self.get_netcdf_path_no_row_column()
     rd = RequestDataset(path)
     driver = DriverNetcdf(rd)
     k = 'row'
     v = {'name_uid': 'yc_id', 'axis': 'Y', 'adds': {'interpolate_bounds': False}, 'name': 'yc',
          'cls': VectorDimension}
     source_metadata = rd.source_metadata
     res = driver._get_vector_dimension_(k, v, source_metadata)
     self.assertEqual(res['name'], 'yc')
示例#6
0
    def test_get_source_metadata(self):
        dimension_map = {'X': {'variable': 'lon', 'dimension': 'x', 'pos': 2, 'bounds': 'lon_bnds'},
                         'Y': {'variable': 'lat', 'dimension': 'y', 'pos': 1, 'bounds': 'lat_bounds'},
                         'T': {'variable': 'time', 'dimension': 'time', 'pos': 0, 'bounds': 'time_bounds'}}
        uri = self.test_data.get_uri('cancm4_tas')
        rd = RequestDataset(uri=uri, dimension_map=dimension_map)
        driver = DriverNetcdf(rd)
        meta = driver.get_source_metadata()
        self.assertEqual(meta['dim_map'], dimension_map)

        # test with no dimensioned variables
        uri = self.get_netcdf_path_no_dimensioned_variables()
        rd = RequestDataset(uri=uri)
        driver = DriverNetcdf(rd)
        meta = driver.get_source_metadata()
        self.assertEqual(meta['dimensions']['dim']['len'], 0)
        with self.assertRaises(KeyError):
            meta['dim_map']
示例#7
0
    def test_get_field_without_row_column_vectors(self):
        """Test loading a field object without row and column vectors."""

        path = self.get_netcdf_path_no_row_column()

        rd = RequestDataset(path)
        driver = DriverNetcdf(rd)
        new_field = driver.get_field()
        self.assertIsNotNone(new_field.crs)
        grid = new_field.spatial.grid
        self.assertIsNone(grid.row)
        self.assertIsNone(grid.col)
        self.assertEqual(grid.name_row, 'yc')
        self.assertEqual(grid.name_col, 'xc')
        self.assertIsNone(grid._value)
        actual = np.ma.array([[[4.0, 4.0], [5.0, 5.0]], [[40.0, 50.0], [40.0, 50.0]]])
        self.assertNumpyAll(grid.value, actual)
        var = new_field.variables.first()
        self.assertEqual(var.shape, (1, 2, 1, 2, 2))
        self.assertEqual(var.value.shape, (1, 2, 1, 2, 2))

        new_field = driver.get_field()
        grid = new_field.spatial.grid
        self.assertEqual(grid.shape, (2, 2))
        self.assertIsNone(grid._value)
        sub = new_field[:, :, :, 0, 1]
        sub_grid = sub.spatial.grid
        self.assertIsInstance(sub_grid, NcSpatialGridDimension)
        self.assertEqual(sub.shape, (1, 2, 1, 1, 1))
        self.assertIsNone(sub_grid._value)
        self.assertEqual(sub_grid.shape, (1, 1))
        actual = np.ma.array([[[4.0]], [[50.0]]])
        self.assertNumpyAll(actual, sub_grid.value)
        sub_var = sub.variables.first()
        self.assertEqual(sub_var.shape, (1, 2, 1, 1, 1))
        self.assertEqual(sub_var.value.shape, (1, 2, 1, 1, 1))

        path2 = os.path.join(self.current_dir_output, 'foo2.nc')
        with self.nc_scope(path2, 'w') as ds:
            new_field.write_netcdf(ds)
        self.assertNcEqual(path, path2, ignore_attributes={'foo': ['grid_mapping']},
                           ignore_variables=['latitude_longitude'])
示例#8
0
    def test_get_field(self):
        # test updating of regrid source flag
        rd = self.test_data.get_rd('cancm4_tas')
        driver = DriverNetcdf(rd)
        field = driver.get_field()
        self.assertTrue(field._should_regrid)
        rd.regrid_source = False
        driver = DriverNetcdf(rd)
        field = driver.get_field()
        self.assertFalse(field._should_regrid)

        # test flag with an assigned coordinate system
        rd = self.test_data.get_rd('cancm4_tas')
        driver = DriverNetcdf(rd)
        field = driver.get_field()
        self.assertFalse(field._has_assigned_coordinate_system)
        rd = self.test_data.get_rd('cancm4_tas', kwds={'crs': CFWGS84()})
        driver = DriverNetcdf(rd)
        field = driver.get_field()
        self.assertTrue(field._has_assigned_coordinate_system)
示例#9
0
    def test_inspect(self):
        rd = self.test_data.get_rd('cancm4_tas')
        driver = DriverNetcdf(rd)
        with self.print_scope() as ps:
            driver.inspect()
        self.assertTrue(len(ps.storage) >= 1)

        # test with a request dataset having no dimensioned variables
        path = self.get_temporary_file_path('bad.nc')
        with self.nc_scope(path, 'w') as ds:
            ds.createDimension('foo')
            var = ds.createVariable('foovar', int, dimensions=('foo',))
            var.a_name = 'a name'
        rd = RequestDataset(uri=path)
        driver = DriverNetcdf(rd)
        with self.print_scope() as ps:
            driver.inspect()
        self.assertTrue(len(ps.storage) >= 1)
示例#10
0
 def test_get_dump_report(self):
     rd = self.test_data.get_rd('cancm4_tas')
     driver = DriverNetcdf(rd)
     self.assertTrue(len(driver.get_dump_report()) > 15)
示例#11
0
 def test_source_metadata_as_json(self):
     rd = self.test_data.get_rd('cancm4_tas')
     driver = DriverNetcdf(rd)
     js = driver.get_source_metadata_as_json()
     self.assertIsInstance(js, basestring)
     self.assertTrue(len(js) > 100)
示例#12
0
 def test_inspect(self):
     rd = self.test_data.get_rd('cancm4_tas')
     driver = DriverNetcdf(rd)
     with self.print_scope() as ps:
         driver.inspect()
     self.assertTrue(len(ps.storage) > 1)