def test_Field_squeeze_transpose_insert_dimension(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = self.f g = f.transpose() self.assertTrue(g.data.shape == f.data.shape[::-1]) self.assertTrue(g.get_data_axes() == f.get_data_axes()[::-1]) g = f.squeeze() self.assertTrue(g.data.shape == f.data.shape[1:]) self.assertTrue(g.get_data_axes() == f.get_data_axes()[1:], (g.get_data_axes(), f.get_data_axes())) f = f.copy() g = f.copy() key = g.set_construct(cfdm.DomainAxis(1)) h = g.insert_dimension(axis=key) self.assertTrue(h.data.ndim == f.data.ndim + 1) self.assertTrue(h.get_data_axes()[1:] == f.get_data_axes()) key = g.set_construct(cfdm.DomainAxis(1)) h = g.insert_dimension(position=g.data.ndim, axis=key) self.assertTrue(h.data.ndim == f.data.ndim + 1) self.assertTrue(h.get_data_axes()[:-1] == f.get_data_axes())
def test_Field_squeeze_transpose_insert_dimension(self): """Test squeeze, transpose and `insert_dimension` methods.""" f = self.f1 g = f.transpose() self.assertEqual(g.data.shape, f.data.shape[::-1]) self.assertEqual(g.get_data_axes(), f.get_data_axes()[::-1]) g = f.squeeze() self.assertEqual(g.data.shape, f.data.shape[1:]) self.assertEqual( g.get_data_axes(), f.get_data_axes()[1:], (g.get_data_axes(), f.get_data_axes()), ) g = f.copy() key = g.set_construct(cfdm.DomainAxis(1)) h = g.insert_dimension(axis=key) self.assertEqual(h.data.ndim, f.data.ndim + 1) self.assertEqual(h.get_data_axes()[1:], f.get_data_axes()) key = g.set_construct(cfdm.DomainAxis(1)) h = g.insert_dimension(position=g.data.ndim, axis=key) self.assertEqual(h.data.ndim, f.data.ndim + 1) self.assertEqual(h.get_data_axes()[:-1], f.get_data_axes())
def test_GATHERING_create(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # Define the gathered values gathered_array = numpy.array([[280, 282.5, 281], [279, 278, 277.5]], dtype='float32') # Define the list array values list_array = [1, 4, 5] # Initialise the list variable list_variable = cfdm.List(data=cfdm.Data(list_array)) # Initialise the gathered array object array = cfdm.GatheredArray( compressed_array=cfdm.Data(gathered_array), compressed_dimension=1, shape=(2, 3, 2), size=12, ndim=3, list_variable=list_variable ) # Create the field construct with the domain axes and the # gathered array tas = cfdm.Field(properties={'standard_name': 'air_temperature', 'units': 'K'}) # Create the domain axis constructs for the uncompressed array T = tas.set_construct(cfdm.DomainAxis(2)) Y = tas.set_construct(cfdm.DomainAxis(3)) X = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[T, Y, X]) self.assertTrue((tas.data.array == numpy.ma.masked_array( data=[[[1, 280.0], [1, 1], [282.5, 281.0]], [[1, 279.0], [1, 1], [278.0, 277.5]]], mask=[[[True, False], [True, True], [False, False]], [[True, False], [True, True], [False, False]]], fill_value=1e+20, dtype='float32')).all()) self.assertEqual(tas.data.get_compression_type(), 'gathered') self.assertTrue((tas.data.compressed_array == numpy.array( [[280., 282.5, 281.], [279., 278., 277.5]], dtype='float32')).all()) self.assertTrue((tas.data.get_list().data.array == numpy.array( [1, 4, 5])).all())
def test_DomainAxis_source(self): """Test the source keyword argument to DomainAxis.""" d = self.d self.assertTrue(d.equals(cfdm.DomainAxis(source=d))) self.assertIsInstance(cfdm.DomainAxis(source="QWERTY"), cfdm.DomainAxis)
def test_DomainAxis_source(self): """TODO DOCS.""" d = self.d self.assertTrue(d.equals(cfdm.DomainAxis(source=d))) self.assertIsInstance(cfdm.DomainAxis(source="QWERTY"), cfdm.DomainAxis)
def test_DSG_contiguous(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = self.c.copy() self.assertEqual(len(f), 2) # Select the specific humidity field q = [g for g in f if g.get_property('standard_name') == 'specific_humidity'][0] self.assertTrue(q._equals(self.a, q.data.array)) cfdm.write(f, tempfile) g = cfdm.read(tempfile) self.assertEqual(len(g), len(f)) for i in range(len(f)): self.assertTrue(g[i].equals(f[i], verbose=3)) # ------------------------------------------------------------ # Test creation # ------------------------------------------------------------ # Define the ragged array values ragged_array = numpy.array([280, 282.5, 281, 279, 278, 279.5], dtype='float32') # Define the count array values count_array = [2, 4] # Create the count variable count_variable = cfdm.Count(data=cfdm.Data(count_array)) count_variable.set_property('long_name', 'number of obs for this timeseries') # Create the contiguous ragged array object array = cfdm.RaggedContiguousArray( compressed_array=cfdm.NumpyArray(ragged_array), shape=(2, 4), size=8, ndim=2, count_variable=count_variable) # Create the field construct with the domain axes and the ragged # array tas = cfdm.Field() tas.set_properties({'standard_name': 'air_temperature', 'units': 'K', 'featureType': 'timeSeries'}) # Create the domain axis constructs for the uncompressed array X = tas.set_construct(cfdm.DomainAxis(4)) Y = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[Y, X]) cfdm.write(tas, tempfile)
def test_GATHERING_create(self): """Test the creation of a construct with a gathered array.""" # Define the gathered values gathered_array = numpy.array([[280, 282.5, 281], [279, 278, 277.5]], dtype="float32") # Define the list array values list_array = [1, 4, 5] # Initialise the list variable list_variable = cfdm.List(data=cfdm.Data(list_array)) # Initialise the gathered array object array = cfdm.GatheredArray( compressed_array=cfdm.Data(gathered_array), compressed_dimension=1, shape=(2, 3, 2), size=12, ndim=3, list_variable=list_variable, ) # Create the field construct with the domain axes and the # gathered array tas = cfdm.Field(properties={ "standard_name": "air_temperature", "units": "K" }) # Create the domain axis constructs for the uncompressed array T = tas.set_construct(cfdm.DomainAxis(2)) Y = tas.set_construct(cfdm.DomainAxis(3)) X = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[T, Y, X]) self.assertTrue((tas.data.array == numpy.ma.masked_array( data=[ [[1, 280.0], [1, 1], [282.5, 281.0]], [[1, 279.0], [1, 1], [278.0, 277.5]], ], mask=[ [[True, False], [True, True], [False, False]], [[True, False], [True, True], [False, False]], ], fill_value=1e20, dtype="float32", )).all()) self.assertEqual(tas.data.get_compression_type(), "gathered") self.assertTrue((tas.data.compressed_array == numpy.array( [[280.0, 282.5, 281.0], [279.0, 278.0, 277.5]], dtype="float32", )).all()) self.assertTrue( (tas.data.get_list().data.array == numpy.array([1, 4, 5])).all())
def test_DomainAxis_equals(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = self.f d = f.construct('key%domainaxis0') self.assertIsInstance(cfdm.DomainAxis(source=d), cfdm.DomainAxis) self.assertIsInstance(cfdm.DomainAxis(source=f), cfdm.DomainAxis)
def test_netCDF_dimension_groups(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return d = cfdm.DomainAxis() d.nc_set_dimension('ncdim') with self.assertRaises(ValueError): d.nc_set_dimension_groups(['/forecast']) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) attrs = d.nc_clear_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) d.nc_set_dimension_groups(['forecast', 'model']) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ('forecast', 'model')) self.assertEqual(d.nc_get_dimension(), '/forecast/model/ncdim') attrs = d.nc_clear_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ('forecast', 'model')) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) self.assertEqual(d.nc_get_dimension(), 'ncdim') d.nc_set_dimension('ncdim') attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ()) d.nc_set_dimension('/ncdim') attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) d.nc_set_dimension('/forecast/model/ncdim') attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ('forecast', 'model')) d.nc_del_dimension() attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs)
def test_netCDF_dimension_groups(self): """Test dimension groups access and (un)setting methods.""" d = cfdm.DomainAxis() d.nc_set_dimension("ncdim") with self.assertRaises(ValueError): d.nc_set_dimension_groups(["/forecast"]) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) attrs = d.nc_clear_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) d.nc_set_dimension_groups(["forecast", "model"]) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ("forecast", "model")) self.assertEqual(d.nc_get_dimension(), "/forecast/model/ncdim") attrs = d.nc_clear_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ("forecast", "model")) attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) self.assertEqual(d.nc_get_dimension(), "ncdim") d.nc_set_dimension("ncdim") attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ()) d.nc_set_dimension("/ncdim") attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs) d.nc_set_dimension("/forecast/model/ncdim") attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertEqual(attrs, ("forecast", "model")) d.nc_del_dimension() attrs = d.nc_dimension_groups() self.assertIsInstance(attrs, tuple) self.assertFalse(attrs)
def test_Field_has_construct(self): """Test the `has_construct` Field method.""" f = self.f1.copy() self.assertTrue(f.has_construct("latitude")) self.assertTrue(f.has_construct("auxiliarycoordinate1")) self.assertFalse(f.has_construct("QWERTY")) # Test edge case whereby constructs have Falsy values as key names: f.set_construct(cfdm.DomainAxis(0), key="") self.assertTrue(f.has_construct(""))
def test_DomainAxis_unlimited(self): """TODO DOCS.""" d = cfdm.DomainAxis() d.set_size(99) self.assertFalse(d.nc_is_unlimited()) d.nc_set_unlimited(False) self.assertFalse(d.nc_is_unlimited()) d.nc_set_unlimited(True) self.assertTrue(d.nc_is_unlimited()) d.nc_set_unlimited(False) self.assertFalse(d.nc_is_unlimited())
def test_DomainAxis_unlimited(self): """Test the netCDF unlimited DomainAxis methods.""" d = cfdm.DomainAxis() d.set_size(99) self.assertFalse(d.nc_is_unlimited()) d.nc_set_unlimited(False) self.assertFalse(d.nc_is_unlimited()) d.nc_set_unlimited(True) self.assertTrue(d.nc_is_unlimited()) d.nc_set_unlimited(False) self.assertFalse(d.nc_is_unlimited())
def test_Field_has_construct(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = self.f.copy() b = f.has_construct('auxiliarycoordinate1') self.assertTrue(b) f.del_construct('auxiliarycoordinate1') c = f.has_construct('auxiliarycoordinate1') self.assertTrue(not c) # now expect False as we deleted the construct # Test edge case whereby constructs have Falsy values as key names: f.set_construct(cfdm.DomainAxis(0), key='') d = f.has_construct('') self.assertTrue(d)
def _dim_coord(self, label, data, properties={}, bounds=None, formula_data=None): bnds = False if bounds != None: if type(bounds) in [type(x) for x in [[], set()]]: bnds = 'data' elif bounds.__class__ == Ldp: bnds = 'full' else: assert False, 'bounds value invalid type' ## if formula_data != None: cfdata = cfdm.Data(data) if not bounds: dc = cfdm.DimensionCoordinate(data=cfdata, properties=properties) else: if bnds == 'full': bnds_construct = cfdm.Bounds(data=cfdm.Data(bounds.data), properties=bounds.properties) bnds_construct.nc_set_variable(bounds.label) else: bdat = cfdm.Data(bounds) bnds_construct = cfdm.Bounds(data=bdat) dc = cfdm.DimensionCoordinate(data=cfdata, properties=properties, bounds=bnds_construct) axis = cfdm.DomainAxis(dc.data.size) ##axis.nc_set_dimension( label ) dc.nc_set_variable(label) axis_tag = self.f.set_construct(axis) dim = self.f.set_construct(dc, axes=axis_tag) return (dc, axis_tag, axis, dim)
def test_netCDF_variable_dimension(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = cfdm.Field() f.nc_set_variable('qwerty') self.assertTrue(f.nc_has_variable()) self.assertTrue(f.nc_get_variable() == 'qwerty') self.assertTrue(f.nc_get_variable(default=None) == 'qwerty') self.assertTrue(f.nc_del_variable() == 'qwerty') self.assertFalse(f.nc_has_variable()) self.assertTrue(f.nc_get_variable(default=None) == None) self.assertTrue(f.nc_del_variable(default=None) == None) d = cfdm.DomainAxis() d.nc_set_dimension('qwerty') self.assertTrue(d.nc_has_dimension()) self.assertTrue(d.nc_get_dimension() == 'qwerty') self.assertTrue(d.nc_get_dimension(default=None) == 'qwerty') self.assertTrue(d.nc_del_dimension() == 'qwerty') self.assertFalse(d.nc_has_dimension()) self.assertTrue(d.nc_get_dimension(default=None) == None) self.assertTrue(d.nc_del_dimension(default=None) == None) d = cfdm.Count() d.nc_set_sample_dimension('qwerty') self.assertTrue(d.nc_has_sample_dimension()) self.assertTrue(d.nc_get_sample_dimension() == 'qwerty') self.assertTrue(d.nc_get_sample_dimension(default=None) == 'qwerty') self.assertTrue(d.nc_del_sample_dimension() == 'qwerty') self.assertFalse(d.nc_has_sample_dimension()) self.assertTrue(d.nc_get_sample_dimension(default=None) == None) self.assertTrue(d.nc_del_sample_dimension(default=None) == None)
def test_DSG_contiguous(self): """TODO DOCS.""" f = self.c.copy() self.assertEqual(len(f), 2) # Select the specific humidity field q = [ g for g in f if g.get_property("standard_name") == "specific_humidity" ][0] self.assertTrue(q._equals(self.a, q.data.array)) cfdm.write(f, tempfile) g = cfdm.read(tempfile) self.assertEqual(len(g), len(f)) for i in range(len(f)): self.assertTrue(g[i].equals(f[i], verbose=3)) # ------------------------------------------------------------ # Test creation # ------------------------------------------------------------ # Define the ragged array values ragged_array = numpy.array([280, 282.5, 281, 279, 278, 279.5], dtype="float32") # Define the count array values count_array = [2, 4] # Create the count variable count_variable = cfdm.Count(data=cfdm.Data(count_array)) count_variable.set_property("long_name", "number of obs for this timeseries") # Create the contiguous ragged array object array = cfdm.RaggedContiguousArray( compressed_array=cfdm.NumpyArray(ragged_array), shape=(2, 4), size=8, ndim=2, count_variable=count_variable, ) # Create the field construct with the domain axes and the ragged # array tas = cfdm.Field() tas.set_properties({ "standard_name": "air_temperature", "units": "K", "featureType": "timeSeries", }) # Create the domain axis constructs for the uncompressed array X = tas.set_construct(cfdm.DomainAxis(4)) Y = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[Y, X]) cfdm.write(tas, tempfile)
def test_STRING(self): """Test constructs with underlying string type arrays.""" for array in ( numpy.ma.array(list("abcdefghij"), dtype="S"), numpy.ma.array( ["a", "b1", "c12", "d123", "e1234", "f", "g", "h", "i", "j"], dtype="S", ), ): # Initialize the field tas = cfdm.Field( properties={ "project": "research", "standard_name": "air_temperature", "units": "K", } ) # Create and set domain axes tas.set_construct(cfdm.DomainAxis(1)) tas.set_construct(cfdm.DomainAxis(1)) axis_Y = tas.set_construct(cfdm.DomainAxis(10)) axis_X = tas.set_construct(cfdm.DomainAxis(9)) # Set the field data tas.set_data( cfdm.Data(numpy.arange(90.0).reshape(10, 9)), axes=[axis_Y, axis_X], ) # Create and set the dimension coordinates dimension_coordinate_Y = cfdm.DimensionCoordinate( properties={ "standard_name": "grid_latitude", "units": "degrees", }, data=cfdm.Data(numpy.arange(10.0)), bounds=cfdm.Bounds( data=cfdm.Data(numpy.arange(20).reshape(10, 2)) ), ) dimension_coordinate_X = cfdm.DimensionCoordinate( properties={ "standard_name": "grid_longitude", "units": "degrees", }, data=cfdm.Data(numpy.arange(9.0)), bounds=cfdm.Bounds( data=cfdm.Data(numpy.arange(18).reshape(9, 2)) ), ) tas.set_construct(dimension_coordinate_Y, axes=[axis_Y]) tas.set_construct(dimension_coordinate_X, axes=[axis_X]) # Create and set the auxiliary coordinates array[0] = numpy.ma.masked aux0 = cfdm.AuxiliaryCoordinate( properties={"long_name": "Grid latitude name"}, data=cfdm.Data(array), ) tas.set_construct(aux0, axes=[axis_Y]) cfdm.write(tas, tempfile) tas1 = cfdm.read(tempfile)[0] aux1 = tas1.constructs.filter_by_identity( "long_name=Grid latitude name" ).value() self.assertEqual(aux0.data.shape, array.shape, aux0.data.shape) self.assertEqual(aux1.data.shape, array.shape, aux1.data.shape)
def test_create_field_2(self): """Test ab initio creation of a second variation of field.""" # Dimension coordinates dim1 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.arange(10.0))) dim1.set_property("standard_name", "projection_y_coordinate") dim1.set_property("units", "m") data = numpy.arange(9.0) + 20 data[-1] = 34 dim0 = cfdm.DimensionCoordinate(data=cfdm.Data(data)) dim0.set_property("standard_name", "projection_x_coordinate") dim0.set_property("units", "m") array = dim0.data.array array = numpy.array([array - 0.5, array + 0.5]).transpose((1, 0)) array[-2, 1] = 30 array[-1, :] = [30, 36] dim0.set_bounds(cfdm.Bounds(data=cfdm.Data(array))) dim2 = cfdm.DimensionCoordinate( data=cfdm.Data([1.5]), bounds=cfdm.Bounds(data=cfdm.Data([[1, 2.0]])), ) dim2.set_property( "standard_name", "atmosphere_hybrid_height_coordinate" ) # Auxiliary coordinates aux2 = cfdm.AuxiliaryCoordinate( data=cfdm.Data(numpy.arange(-45, 45, dtype="int32").reshape(10, 9)) ) aux2.set_property("units", "degree_N") aux2.set_property("standard_name", "latitude") aux3 = cfdm.AuxiliaryCoordinate( data=cfdm.Data(numpy.arange(60, 150, dtype="int32").reshape(9, 10)) ) aux3.set_property("standard_name", "longitude") aux3.set_property("units", "degreeE") array = numpy.ma.array( [ "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", ], dtype="S", ) array[0] = numpy.ma.masked aux4 = cfdm.AuxiliaryCoordinate(data=cfdm.Data(array)) aux4.set_property("standard_name", "greek_letters") # Domain ancillaries ak = cfdm.DomainAncillary(data=cfdm.Data([10.0])) ak.set_property("units", "m") ak.set_bounds(cfdm.Bounds(data=cfdm.Data([[5, 15.0]]))) bk = cfdm.DomainAncillary(data=cfdm.Data([20.0])) bk.set_bounds(cfdm.Bounds(data=cfdm.Data([[14, 26.0]]))) # Cell measures msr0 = cfdm.CellMeasure( data=cfdm.Data(1 + numpy.arange(90.0).reshape(9, 10) * 1234) ) msr0.set_measure("area") msr0.set_property("units", "km2") # Data data = cfdm.Data(numpy.arange(90.0).reshape(10, 9)) properties = {"units": "m s-1"} f = cfdm.Field(properties=properties) f.set_property("standard_name", "eastward_wind") axisX = f.set_construct(cfdm.DomainAxis(9)) axisY = f.set_construct(cfdm.DomainAxis(10)) axisZ = f.set_construct(cfdm.DomainAxis(1)) f.set_data(data, axes=[axisY, axisX]) x = f.set_construct(dim0, axes=[axisX]) y = f.set_construct(dim1, axes=[axisY]) z = f.set_construct(dim2, axes=[axisZ]) lat = f.set_construct(aux2, axes=[axisY, axisX]) lon = f.set_construct(aux3, axes=[axisX, axisY]) f.set_construct(aux4, axes=[axisY]) ak = f.set_construct(ak, axes=[axisZ]) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references coordinate_conversion = cfdm.CoordinateConversion( parameters={ "grid_mapping_name": "transverse_mercator", "latitude_of_projection_origin": 49.0, "longitude_of_central_meridian": -2.0, "scale_factor_at_central_meridian": 0.9996012717, "false_easting": 400000.0, "false_northing": -100000.0, "unit": "metre", } ) datum0 = cfdm.Datum( parameters={ "inverse_flattening": 299.3249646, "longitude_of_prime_meridian": 0.0, "semi_major_axis": 6377563.396, } ) ref0 = cfdm.CoordinateReference( coordinates=[x, y], datum=datum0, coordinate_conversion=coordinate_conversion, ) coordinate_conversion = cfdm.CoordinateConversion( parameters={"grid_mapping_name": "latitude_longitude"} ) datum2 = cfdm.Datum( parameters={ "longitude_of_prime_meridian": 0.0, "semi_major_axis": 6378137.0, "inverse_flattening": 298.257223563, } ) ref2 = cfdm.CoordinateReference( coordinates=[lat, lon], datum=datum2, coordinate_conversion=coordinate_conversion, ) f.set_construct(msr0, axes=[axisX, axisY]) f.set_construct(ref0) f.set_construct(ref2) orog = cfdm.DomainAncillary(data=f.get_data()) orog.set_property("standard_name", "surface_altitude") orog.set_property("units", "m") orog = f.set_construct(orog, axes=[axisY, axisX]) coordinate_conversion = cfdm.CoordinateConversion( parameters={ "standard_name": "atmosphere_hybrid_height_coordinate" }, domain_ancillaries={"orog": orog, "a": ak, "b": bk}, ) ref1 = cfdm.CoordinateReference( coordinates=[z], datum=datum0, coordinate_conversion=coordinate_conversion, ) f.set_construct(ref1) # Field ancillary variables g = f.copy() anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = "ancillaryA" f.set_construct(anc, axes=[axisY, axisX]) g = f[0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = "ancillaryB" f.set_construct(anc, axes=[axisX]) g = f[..., 0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = "ancillaryC" f.set_construct(anc, axes=[axisY]) f.set_property("flag_values", numpy.array([1, 2, 4], "int32")) f.set_property("flag_meanings", "a bb ccc") f.set_property("flag_masks", [2, 1, 0]) cm0 = cfdm.CellMethod( axes=[axisX], method="mean", qualifiers={"interval": [cfdm.Data(1, "day")], "comment": "ok"}, ) cm1 = cfdm.CellMethod( axes=[axisY], method="maximum", qualifiers={"where": "sea"} ) f.set_construct(cm0) f.set_construct(cm1) self.assertTrue( f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself", ) for fmt in ( "NETCDF3_CLASSIC", "NETCDF3_64BIT", "NETCDF4", "NETCDF4_CLASSIC", ): cfdm.write(f, self.filename, fmt=fmt, verbose=verbose) g = cfdm.read(self.filename, verbose=verbose) self.assertEqual(len(g), 1, f"{len(g)} != 1") g = g[0].squeeze() self.assertEqual( sorted(f.constructs), sorted(g.constructs), f"\n\nf (created in memory)" f"\n{f.constructs}" f"\n\n{f.constructs.items()}" f"\n\ng (read from disk)" f"\n{g.constructs}" f"\n\n{g.constructs.items()}", ) self.assertTrue( g.equals(g.copy(), verbose=verbose), "Field g not equal to a copy of itself", ) self.assertTrue( g.equals(f, verbose=verbose), "Field not equal to itself read back in", ) x = g.dump(display=False) x = f.dump(display=False) g = cfdm.read( self.filename, verbose=verbose, extra=["domain_ancillary"], warnings=warnings, )
def test_create_field_3(self): """Test ab initio creation of a third variation of field.""" # Dimension coordinates data = numpy.arange(9.0) + 20 data[-1] = 34 dim0 = cfdm.DimensionCoordinate(data=cfdm.Data(data)) dim0.set_property("standard_name", "grid_longitude") dim0.set_property("units", "degrees") array = dim0.data.array array = numpy.array([array - 0.5, array + 0.5]).transpose((1, 0)) array[-2, 1] = 30 array[-1, :] = [30, 36] dim0.set_bounds(cfdm.Bounds(data=cfdm.Data(array))) dim1 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.arange(10.0))) dim1.set_property("standard_name", "grid_latitude") dim1.set_property("units", "degrees") dim2 = cfdm.DimensionCoordinate( data=cfdm.Data([1.5]), bounds=cfdm.Bounds(data=cfdm.Data([[1, 2.0]])), ) dim2.set_property("standard_name", "atmosphere_hybrid_height_coordinate") dim2.set_property("computed_standard_name", "altitude") dim3 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.array([15.0]))) dim3.set_property("standard_name", "time") dim3.set_property("units", "days since 2004-06-01") dim3.set_bounds(cfdm.Bounds(data=cfdm.Data([[0, 30.0]]))) # dim3.set_geometry('climatology') # Auxiliary coordinates ak = cfdm.DomainAncillary(data=cfdm.Data([10.0])) ak.set_property("units", "m") ak.set_bounds(cfdm.Bounds(data=cfdm.Data([[5, 15.0]]))) bk = cfdm.DomainAncillary(data=cfdm.Data([20.0])) bk.set_bounds(cfdm.Bounds(data=cfdm.Data([[14, 26.0]]))) aux2 = cfdm.AuxiliaryCoordinate(data=cfdm.Data( numpy.arange(-45, 45, dtype="int32").reshape(10, 9))) aux2.set_property("units", "degree_N") aux2.set_property("standard_name", "latitude") aux3 = cfdm.AuxiliaryCoordinate(data=cfdm.Data( numpy.arange(60, 150, dtype="int32").reshape(9, 10))) aux3.set_property("standard_name", "longitude") aux3.set_property("units", "degreeE") array = numpy.ma.array( [ "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", ], dtype="S", ) array[0] = numpy.ma.masked aux4 = cfdm.AuxiliaryCoordinate(data=cfdm.Data(array)) aux4.set_property("standard_name", "greek_letters") # Cell measures msr0 = cfdm.CellMeasure( data=cfdm.Data(1 + numpy.arange(90.0).reshape(9, 10) * 1234)) msr0.set_measure("area") msr0.set_property("units", "km2") # Data data = cfdm.Data(numpy.arange(90.0).reshape(10, 9)) properties = {"units": "m s-1"} f = cfdm.Field(properties=properties) f.set_property("standard_name", "eastward_wind") axisX = f.set_construct(cfdm.DomainAxis(9)) axisY = f.set_construct(cfdm.DomainAxis(10)) axisZ = f.set_construct(cfdm.DomainAxis(1)) axisT = f.set_construct(cfdm.DomainAxis(1)) f.set_data(data, axes=[axisY, axisX]) x = f.set_construct(dim0, axes=[axisX]) y = f.set_construct(dim1, axes=[axisY]) z = f.set_construct(dim2, axes=[axisZ]) f.set_construct(dim3, axes=[axisT]) lat = f.set_construct(aux2, axes=[axisY, axisX]) lon = f.set_construct(aux3, axes=[axisX, axisY]) f.set_construct(aux4, axes=[axisY]) ak = f.set_construct(ak, axes=[axisZ]) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references # ref0 = cfdm.CoordinateReference( # parameters={'grid_mapping_name': 'rotated_latitude_longitude', # 'grid_north_pole_latitude': 38.0, # 'grid_north_pole_longitude': 190.0, # 'earth_radius': 6371007,}, # coordinates=[x, y, lat, lon] # ) coordinate_conversion = cfdm.CoordinateConversion( parameters={ "grid_mapping_name": "rotated_latitude_longitude", "grid_north_pole_latitude": 38.0, "grid_north_pole_longitude": 190.0, }) datum = cfdm.Datum(parameters={"earth_radius": 6371007}) ref0 = cfdm.CoordinateReference( coordinate_conversion=coordinate_conversion, datum=datum, coordinates=[x, y, lat, lon], ) f.set_construct(msr0, axes=[axisX, axisY]) f.set_construct(ref0) orog = cfdm.DomainAncillary(data=f.get_data()) orog.set_property("standard_name", "surface_altitude") orog.set_property("units", "m") orog = f.set_construct(orog, axes=[axisY, axisX]) datum1 = cfdm.Datum({"earth_radius": 6371007}) coordinate_conversion1 = cfdm.CoordinateConversion( parameters={ "standard_name": "atmosphere_hybrid_height_coordinate", "computed_standard_name": "altitude", }, domain_ancillaries={ "orog": orog, "a": ak, "b": bk }, ) ref1 = cfdm.CoordinateReference( datum=datum1, coordinate_conversion=coordinate_conversion1, coordinates=[z], ) ref1 = f.set_construct(ref1) # Field ancillary variables # g = f.transpose([1, 0]) g = f.copy() # g.standard_name = 'ancillary0' # g *= 0.01 anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = "ancillaryA" f.set_construct(anc, axes=[axisY, axisX]) g = f[0] g = g.squeeze() # g.standard_name = 'ancillary2' # g *= 0.001 anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = "ancillaryB" f.set_construct(anc, axes=[axisX]) g = f[..., 0] g = g.squeeze() # g.standard_name = 'ancillary3' # g *= 0.001 anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = "ancillaryC" f.set_construct(anc, axes=[axisY]) f.set_property("flag_values", numpy.array([1, 2, 4], "int32")) f.set_property("flag_meanings", "a bb ccc") f.set_property("flag_masks", [2, 1, 0]) cm0 = cfdm.CellMethod( axes=[axisX], method="mean", qualifiers={ "interval": [cfdm.Data(1, "day")], "comment": "ok" }, ) cm1 = cfdm.CellMethod(axes=[axisY], method="maximum", qualifiers={"where": "sea"}) cm2 = cfdm.CellMethod(axes=[axisT], method="maximum", qualifiers={"within": "years"}) cm3 = cfdm.CellMethod(axes=[axisT], method="minimum", qualifiers={"over": "years"}) f.set_construct(cm0) f.set_construct(cm1) f.set_construct(cm2) f.set_construct(cm3) cfdm.write(f, self.filename, fmt="NETCDF3_CLASSIC", verbose=verbose) g = cfdm.read(self.filename, verbose=verbose) self.assertEqual(len(g), 1, f"Read produced too many fields: {len(g)} != 1") g = g[0].squeeze() self.assertEqual( sorted(f.constructs), sorted(g.constructs), f"\n\nf (created in memory)" f"\n{f.constructs}" f"\n\n{f.constructs.items()}" f"\n\ng (read from disk)" f"\n{g.constructs}" f"\n\n{g.constructs.items()}", ) self.assertTrue( f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself", ) self.assertTrue( g.equals(g.copy(), verbose=verbose), "Field g not equal to a copy of itself", ) self.assertTrue( g.equals(f, verbose=verbose), "Field not equal to itself read back in", ) x = g.dump(display=False) x = f.dump(display=False) g = cfdm.read( self.filename, verbose=verbose, extra=["domain_ancillary"], warnings=warnings, )
import netCDF4 import cfdm import numpy f = cfdm.Field(properties={'standard_name': 'precipitation_flux'}) dc = cfdm.DimensionCoordinate(properties={'long_name': 'Longitude'}, data=cfdm.Data([0, 1, 2.])) fa = cfdm.FieldAncillary( properties={'standard_name': 'precipitation_flux status_flag'}, data=cfdm.Data(numpy.array([0, 0, 2], dtype='int8'))) longitude_axis = f.set_construct(cfdm.DomainAxis(3)) key = f.set_construct(dc, axes=longitude_axis) cm = cfdm.CellMethod(axes=longitude_axis, method='minimum') f.set_construct(cm)
def test_create_field_3(self): # Dimension coordinates data = numpy.arange(9.) + 20 data[-1] = 34 dim0 = cfdm.DimensionCoordinate(data=cfdm.Data(data)) dim0.set_property('standard_name', 'grid_longitude') dim0.set_property('units', 'degrees') array = dim0.data.array array = numpy.array([array-0.5, array+0.5]).transpose((1,0)) array[-2, 1] = 30 array[-1, :] = [30, 36] dim0.set_bounds(cfdm.Bounds(data=cfdm.Data(array))) dim1 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.arange(10.))) dim1.set_property('standard_name', 'grid_latitude') dim1.set_property('units', 'degrees') dim2 = cfdm.DimensionCoordinate(data=cfdm.Data([1.5]), bounds=cfdm.Bounds(data=cfdm.Data([[1, 2.]]))) dim2.set_property('standard_name' , 'atmosphere_hybrid_height_coordinate') dim2.set_property('computed_standard_name', 'altitude') dim3 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.array([15.0]))) dim3.set_property('standard_name', 'time') dim3.set_property('units', 'days since 2004-06-01') dim3.set_bounds(cfdm.Bounds(data=cfdm.Data([[0, 30.]]))) # dim3.set_geometry('climatology') # Auxiliary coordinates ak = cfdm.DomainAncillary(data=cfdm.Data([10.])) ak.set_property('units', 'm') ak.set_bounds(cfdm.Bounds(data=cfdm.Data([[5, 15.]]))) bk = cfdm.DomainAncillary(data=cfdm.Data([20.])) bk.set_bounds(cfdm.Bounds(data=cfdm.Data([[14, 26.]]))) aux2 = cfdm.AuxiliaryCoordinate( data=cfdm.Data(numpy.arange(-45, 45, dtype='int32').reshape(10, 9))) aux2.set_property('units', 'degree_N') aux2.set_property('standard_name', 'latitude') aux3 = cfdm.AuxiliaryCoordinate( data=cfdm.Data(numpy.arange(60, 150, dtype='int32').reshape(9, 10))) aux3.set_property('standard_name', 'longitude') aux3.set_property('units', 'degreeE') array = numpy.ma.array(['alpha','beta','gamma','delta','epsilon', 'zeta','eta','theta','iota','kappa'], dtype='S') array[0] = numpy.ma.masked aux4 = cfdm.AuxiliaryCoordinate(data=cfdm.Data(array)) aux4.set_property('standard_name', 'greek_letters') # Cell measures msr0 = cfdm.CellMeasure( data=cfdm.Data(1+numpy.arange(90.).reshape(9, 10)*1234)) msr0.set_measure('area') msr0.set_property('units', 'km2') # Data data = cfdm.Data(numpy.arange(90.).reshape(10, 9)) properties = {'units': 'm s-1'} f = cfdm.Field(properties=properties) f.set_property('standard_name', 'eastward_wind') axisX = f.set_construct(cfdm.DomainAxis(9)) axisY = f.set_construct(cfdm.DomainAxis(10)) axisZ = f.set_construct(cfdm.DomainAxis(1)) axisT = f.set_construct(cfdm.DomainAxis(1)) f.set_data(data, axes=[axisY, axisX]) x = f.set_construct(dim0, axes=[axisX]) y = f.set_construct(dim1, axes=[axisY]) z = f.set_construct(dim2, axes=[axisZ]) t = f.set_construct(dim3, axes=[axisT]) lat = f.set_construct(aux2, axes=[axisY, axisX]) lon = f.set_construct(aux3, axes=[axisX, axisY]) greek = f.set_construct(aux4, axes=[axisY]) ak = f.set_construct(ak, axes=[axisZ]) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references # ref0 = cfdm.CoordinateReference( # parameters={'grid_mapping_name': 'rotated_latitude_longitude', # 'grid_north_pole_latitude': 38.0, # 'grid_north_pole_longitude': 190.0, # 'earth_radius': 6371007,}, # coordinates=[x, y, lat, lon] # ) coordinate_conversion = cfdm.CoordinateConversion( parameters={'grid_mapping_name': 'rotated_latitude_longitude', 'grid_north_pole_latitude': 38.0, 'grid_north_pole_longitude': 190.0}) datum = cfdm.Datum(parameters={'earth_radius': 6371007}) ref0 = cfdm.CoordinateReference( coordinate_conversion=coordinate_conversion, datum=datum, coordinates=[x, y, lat, lon] ) f.set_construct(msr0, axes=[axisX, axisY]) f.set_construct(ref0) orog = cfdm.DomainAncillary(data=f.get_data()) orog.set_property('standard_name', 'surface_altitude') orog.set_property('units', 'm') orog = f.set_construct(orog, axes=[axisY, axisX]) datum1 = cfdm.Datum({'earth_radius' : 6371007}) coordinate_conversion1 = cfdm.CoordinateConversion( parameters={'standard_name': 'atmosphere_hybrid_height_coordinate', 'computed_standard_name': 'altitude'}, domain_ancillaries={'orog': orog, 'a' : ak, 'b' : bk}) ref1 = cfdm.CoordinateReference( datum=datum1, coordinate_conversion=coordinate_conversion1, coordinates=[z] ) ref1 = f.set_construct(ref1) # Field ancillary variables # g = f.transpose([1, 0]) g = f.copy() # g.standard_name = 'ancillary0' # g *= 0.01 anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = 'ancillaryA' f.set_construct(anc, axes=[axisY, axisX]) g = f[0] g = g.squeeze() # g.standard_name = 'ancillary2' # g *= 0.001 anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = 'ancillaryB' f.set_construct(anc, axes=[axisX]) g = f[..., 0] g = g.squeeze() # g.standard_name = 'ancillary3' # g *= 0.001 anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = 'ancillaryC' f.set_construct(anc, axes=[axisY]) f.set_property('flag_values', numpy.array([1, 2, 4], 'int32')) f.set_property('flag_meanings', 'a bb ccc') f.set_property('flag_masks', [2, 1, 0]) cm0 = cfdm.CellMethod(axes=[axisX], method='mean', qualifiers={'interval': [cfdm.Data(1, 'day')], 'comment' : 'ok'}) cm1 = cfdm.CellMethod(axes=[axisY], method='maximum', qualifiers={'where' : 'sea'}) cm2 = cfdm.CellMethod(axes=[axisT], method='maximum', qualifiers={'within' : 'years'}) cm3 = cfdm.CellMethod(axes=[axisT], method='minimum', qualifiers={'over' : 'years'}) f.set_construct(cm0) f.set_construct(cm1) f.set_construct(cm2) f.set_construct(cm3) if verbose: print(repr(f)) print(f) print(f.constructs) print(f.construct_data_axes()) f.dump() # sys.exit(0) cfdm.write(f, self.filename, fmt='NETCDF3_CLASSIC', verbose=verbose) g = cfdm.read(self.filename, verbose=verbose) #, squeeze=True) if verbose: # g[0].dump() # sys.exit(0) for x in g: x.print_read_report() self.assertTrue(len(g) == 1, 'Read produced too many fields: {} != 1'.format(len(g))) g = g[0].squeeze() # print g self.assertTrue(sorted(f.constructs) == sorted(g.constructs), '\n\nf (created in memory)\n{}\n\n{}\n\ng (read from disk)\n{}\n\n{}'.format( sorted(f.constructs), sorted(f.constructs.items()), sorted(g.constructs), sorted(g.constructs.items()))) self.assertTrue(f.equals(f.copy(), verbose=True), "Field f not equal to a copy of itself") self.assertTrue(g.equals(g.copy(), verbose=True), "Field g not equal to a copy of itself") # print f.dump() # print'f' # print f # print 'g' # print g # f.dump() # g.dump() if verbose: f.dump() g.dump() # sys.exit(0) self.assertTrue(g.equals(f, verbose=True), "Field not equal to itself read back in") # sys.exit(0) x = g.dump(display=False) x = f.dump(display=False) g = cfdm.read(self.filename, verbose=verbose, extra=['domain_ancillary'], warnings=warnings) if verbose: for x in g: x.print_read_report() print(g) g[0].dump()
def test_netCDF_global_unlimited(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # ------------------------------------------------------------ # Unlimited dimensions # ------------------------------------------------------------ f = cfdm.Field() self.assertTrue(f.nc_clear_unlimited_dimensions() == set()) f = cfdm.Field() f.nc_set_unlimited_dimensions(()) f = cfdm.Field() self.assertTrue(f.nc_unlimited_dimensions() == set()) f.nc_set_unlimited_dimensions(['qwerty', 'asdf']) self.assertTrue(f.nc_unlimited_dimensions() == set(['qwerty', 'asdf'])) f.nc_set_unlimited_dimensions(['zxc']) self.assertTrue( f.nc_unlimited_dimensions() == set(['qwerty', 'asdf', 'zxc'])) self.assertTrue(f.nc_clear_unlimited_dimensions() == set( ['qwerty', 'asdf', 'zxc'])) self.assertTrue(f.nc_unlimited_dimensions() == set()) # ------------------------------------------------------------ # Global attributes # ------------------------------------------------------------ f = cfdm.Field() self.assertTrue(f.nc_clear_global_attributes() == {}) f.nc_set_global_attribute('Conventions') f.nc_set_global_attribute('project', 'X') self.assertTrue(f.nc_global_attributes() == { 'Conventions': None, 'project': 'X' }) f.nc_set_global_attribute('project') f.nc_set_global_attribute('comment', None) self.assertTrue(f.nc_global_attributes() == { 'Conventions': None, 'project': None, 'comment': None }) self.assertTrue(f.nc_clear_global_attributes() == { 'Conventions': None, 'project': None, 'comment': None }) self.assertTrue(f.nc_global_attributes() == {}) f.nc_set_global_attribute('Conventions') f.nc_set_global_attribute('project') self.assertTrue(f.nc_global_attributes() == { 'Conventions': None, 'project': None }) f = cfdm.Field() f.set_properties({'foo': 'bar', 'comment': 'variable comment'}) f.nc_set_variable('tas') d = f.set_construct(cfdm.DomainAxis(2)) f.set_data(cfdm.Data([8, 9]), axes=[d]) f2 = f.copy() f2.nc_set_variable('ua') cfdm.write([f, f2], 'tempfilename.nc', file_descriptors={ 'comment': 'global comment', 'qwerty': 'asdf' }) g = cfdm.read('tempfilename.nc', verbose=False) self.assertTrue(len(g) == 2) for x in g: self.assertTrue( x.properties() == { 'comment': 'variable comment', 'foo': 'bar', 'qwerty': 'asdf', 'Conventions': 'CF-1.7' }) self.assertTrue( x.nc_global_attributes() == { 'comment': 'global comment', 'qwerty': None, 'Conventions': None }, x.nc_global_attributes()) cfdm.write(g, 'tempfilename2.nc') h = cfdm.read('tempfilename2.nc') for x, y in zip(h, g): self.assertTrue(x.properties() == y.properties()) self.assertTrue( x.nc_global_attributes() == y.nc_global_attributes()) self.assertTrue(x.equals(y, verbose=True)) self.assertTrue(y.equals(x, verbose=True)) g[1].nc_set_global_attribute('comment', 'different comment') cfdm.write(g, 'tempfilename3.nc') h = cfdm.read('tempfilename3.nc') for x, y in zip(h, g): self.assertTrue(x.properties() == y.properties()) self.assertTrue(x.nc_global_attributes() == { 'comment': None, 'qwerty': None, 'Conventions': None }) self.assertTrue(x.equals(y, verbose=True)) self.assertTrue(y.equals(x, verbose=True))
def test_create_field_2(self): # Dimension coordinates dim1 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.arange(10.))) dim1.set_property('standard_name', 'projection_y_coordinate') dim1.set_property('units', 'm') data = numpy.arange(9.) + 20 data[-1] = 34 dim0 = cfdm.DimensionCoordinate(data=cfdm.Data(data)) dim0.set_property('standard_name', 'projection_x_coordinate') dim0.set_property('units', 'm') array = dim0.data.array array = numpy.array([array-0.5, array+0.5]).transpose((1, 0)) array[-2, 1] = 30 array[-1, :] = [30, 36] dim0.set_bounds(cfdm.Bounds(data=cfdm.Data(array))) dim2 = cfdm.DimensionCoordinate( data=cfdm.Data([1.5]), bounds=cfdm.Bounds(data=cfdm.Data([[1, 2.]])) ) dim2.set_property( 'standard_name', 'atmosphere_hybrid_height_coordinate') # Auxiliary coordinates aux2 = cfdm.AuxiliaryCoordinate( data=cfdm.Data( numpy.arange(-45, 45, dtype='int32').reshape(10, 9))) aux2.set_property('units', 'degree_N') aux2.set_property('standard_name', 'latitude') aux3 = cfdm.AuxiliaryCoordinate( data=cfdm.Data(numpy.arange( 60, 150, dtype='int32').reshape(9, 10)) ) aux3.set_property('standard_name', 'longitude') aux3.set_property('units', 'degreeE') array = numpy.ma.array( ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa'], dtype='S' ) array[0] = numpy.ma.masked aux4 = cfdm.AuxiliaryCoordinate(data=cfdm.Data(array)) aux4.set_property('standard_name', 'greek_letters') # Domain ancillaries ak = cfdm.DomainAncillary(data=cfdm.Data([10.])) ak.set_property('units', 'm') ak.set_bounds(cfdm.Bounds(data=cfdm.Data([[5, 15.]]))) bk = cfdm.DomainAncillary(data=cfdm.Data([20.])) bk.set_bounds(cfdm.Bounds(data=cfdm.Data([[14, 26.]]))) # Cell measures msr0 = cfdm.CellMeasure( data=cfdm.Data(1+numpy.arange(90.).reshape(9, 10)*1234)) msr0.set_measure('area') msr0.set_property('units', 'km2') # Data data = cfdm.Data(numpy.arange(90.).reshape(10, 9)) properties = {'units': 'm s-1'} f = cfdm.Field(properties=properties) f.set_property('standard_name', 'eastward_wind') axisX = f.set_construct(cfdm.DomainAxis(9)) axisY = f.set_construct(cfdm.DomainAxis(10)) axisZ = f.set_construct(cfdm.DomainAxis(1)) f.set_data(data, axes=[axisY, axisX]) x = f.set_construct(dim0, axes=[axisX]) y = f.set_construct(dim1, axes=[axisY]) z = f.set_construct(dim2, axes=[axisZ]) lat = f.set_construct(aux2, axes=[axisY, axisX]) lon = f.set_construct(aux3, axes=[axisX, axisY]) greek = f.set_construct(aux4, axes=[axisY]) ak = f.set_construct(ak, axes=[axisZ]) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references coordinate_conversion = cfdm.CoordinateConversion( parameters={'grid_mapping_name': "transverse_mercator", 'latitude_of_projection_origin': 49.0, 'longitude_of_central_meridian': -2.0, 'scale_factor_at_central_meridian': 0.9996012717, 'false_easting': 400000.0, 'false_northing': -100000.0, 'unit': "metre"}) datum0 = cfdm.Datum(parameters={'inverse_flattening': 299.3249646, 'longitude_of_prime_meridian': 0.0, 'semi_major_axis': 6377563.396}) ref0 = cfdm.CoordinateReference( coordinates=[x, y], datum=datum0, coordinate_conversion=coordinate_conversion ) coordinate_conversion = cfdm.CoordinateConversion( parameters={'grid_mapping_name': "latitude_longitude"}) datum2 = cfdm.Datum(parameters={'longitude_of_prime_meridian': 0.0, 'semi_major_axis': 6378137.0, 'inverse_flattening': 298.257223563}) ref2 = cfdm.CoordinateReference( coordinates=[lat, lon], datum=datum2, coordinate_conversion=coordinate_conversion) f.set_construct(msr0, axes=[axisX, axisY]) f.set_construct(ref0) f.set_construct(ref2) orog = cfdm.DomainAncillary(data=f.get_data()) orog.set_property('standard_name', 'surface_altitude') orog.set_property('units', 'm') orog = f.set_construct(orog, axes=[axisY, axisX]) coordinate_conversion = cfdm.CoordinateConversion( parameters={ 'standard_name': 'atmosphere_hybrid_height_coordinate' }, domain_ancillaries={ 'orog': orog, 'a': ak, 'b': bk } ) ref1 = cfdm.CoordinateReference( coordinates=[z], datum=datum0, coordinate_conversion=coordinate_conversion ) f.set_construct(ref1) # Field ancillary variables g = f.copy() anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = 'ancillaryA' f.set_construct(anc, axes=[axisY, axisX]) g = f[0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = 'ancillaryB' f.set_construct(anc, axes=[axisX]) g = f[..., 0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.standard_name = 'ancillaryC' f.set_construct(anc, axes=[axisY]) f.set_property('flag_values', numpy.array([1, 2, 4], 'int32')) f.set_property('flag_meanings', 'a bb ccc') f.set_property('flag_masks', [2, 1, 0]) cm0 = cfdm.CellMethod(axes=[axisX], method='mean', qualifiers={'interval': [cfdm.Data(1, 'day')], 'comment': 'ok'}) cm1 = cfdm.CellMethod(axes=[axisY], method='maximum', qualifiers={'where': 'sea'}) f.set_construct(cm0) f.set_construct(cm1) self.assertTrue(f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself") for fmt in ('NETCDF3_CLASSIC', 'NETCDF3_64BIT', 'NETCDF4', 'NETCDF4_CLASSIC'): cfdm.write(f, self.filename, fmt=fmt, verbose=verbose) g = cfdm.read(self.filename, verbose=verbose) self.assertEqual(len(g), 1, '{} != 1'.format(len(g))) g = g[0].squeeze() self.assertEqual(sorted(f.constructs), sorted(g.constructs), '\n\nf\n{}\n\n{}\n\ng\n{}\n\n{}'.format( sorted(f.constructs), sorted(f.constructs.items()), sorted(g.constructs), sorted(g.constructs.items()))) self.assertTrue(g.equals(g.copy(), verbose=verbose), "Field g not equal to a copy of itself") self.assertTrue(g.equals(f, verbose=verbose), "Field not equal to itself read back in") # --- End: for x = g.dump(display=False) x = f.dump(display=False) g = cfdm.read(self.filename, verbose=verbose, extra=['domain_ancillary'], warnings=warnings)
import numpy import cfdm # Initialize the field construct tas = cfdm.Field(properties={ 'project': 'research', 'standard_name': 'air_temperature', 'units': 'K' }) # Create and set domain axis constructs axis_T = tas.set_construct(cfdm.DomainAxis(1)) axis_Z = tas.set_construct(cfdm.DomainAxis(1)) axis_Y = tas.set_construct(cfdm.DomainAxis(10)) axis_X = tas.set_construct(cfdm.DomainAxis(9)) # Set the field construct data tas.set_data(cfdm.Data(numpy.arange(90.).reshape(10, 9)), axes=[axis_Y, axis_X]) # Create and set the cell method constructs cell_method1 = cfdm.CellMethod(axes=[axis_Y, axis_X], method='mean', qualifiers={ 'where': 'land', 'interval': [cfdm.Data(0.1, units='degrees')] }) cell_method2 = cfdm.CellMethod(axes=axis_T, method='maximum')
def test_create_field(self): # Dimension coordinates dim1 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.arange(10.))) dim1.set_property('standard_name', 'grid_latitude') dim1.set_property('units', 'degrees') data = numpy.arange(9.) + 20 data[-1] = 34 dim0 = cfdm.DimensionCoordinate(data=cfdm.Data(data)) dim0.set_property('standard_name', 'grid_longitude') dim0.set_property('units', 'degrees') dim0.nc_set_variable('x') array = dim0.data.array array = numpy.array([array - 0.5, array + 0.5]).transpose((1, 0)) array[-2, 1] = 30 array[-1, :] = [30, 36] dim0.set_bounds(cfdm.Bounds(data=cfdm.Data(array))) dim2 = cfdm.DimensionCoordinate( data=cfdm.Data([1.5]), bounds=cfdm.Bounds(data=cfdm.Data([[1, 2.]]))) dim2.set_property('standard_name', 'atmosphere_hybrid_height_coordinate') dim2.set_property('computed_standard_name', 'altitude') # Auxiliary coordinates ak = cfdm.DomainAncillary(data=cfdm.Data([10.])) ak.set_property('units', 'm') ak.id = 'atmosphere_hybrid_height_coordinate_ak' ak.set_bounds(cfdm.Bounds(data=cfdm.Data([[5, 15.]]))) bk = cfdm.DomainAncillary(data=cfdm.Data([20.])) bk.id = 'atmosphere_hybrid_height_coordinate_bk' bk.set_bounds(cfdm.Bounds(data=cfdm.Data([[14, 26.]]))) aux2 = cfdm.AuxiliaryCoordinate(data=cfdm.Data( numpy.arange(-45, 45, dtype='int32').reshape(10, 9))) aux2.set_property('units', 'degree_N') aux2.set_property('standard_name', 'latitude') aux3 = cfdm.AuxiliaryCoordinate(data=cfdm.Data( numpy.arange(60, 150, dtype='int32').reshape(9, 10))) aux3.set_property('standard_name', 'longitude') aux3.set_property('units', 'degreeE') array = numpy.ma.array([ 'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa' ], dtype='S') array[0] = numpy.ma.masked aux4 = cfdm.AuxiliaryCoordinate(data=cfdm.Data(array)) aux4.set_property('long_name', 'greek_letters') # Cell measures msr0 = cfdm.CellMeasure( data=cfdm.Data(1 + numpy.arange(90.).reshape(9, 10) * 1234)) msr0.set_measure('area') msr0.set_property('units', 'km2') msr0.nc_set_variable('areacella') # Data data = cfdm.Data(numpy.arange(90.).reshape(10, 9)) properties = {'units': 'm s-1'} f = cfdm.Field(properties=properties) f.set_property('standard_name', 'eastward_wind') da = cfdm.DomainAxis(9) da.nc_set_dimension('grid_longitude') axisX = f.set_construct(da) axisY = f.set_construct(cfdm.DomainAxis(10)) axisZ = f.set_construct(cfdm.DomainAxis(1)) f.set_data(data, axes=[axisY, axisX]) x = f.set_construct(dim0, axes=axisX) y = f.set_construct(dim1, axes=axisY) z = f.set_construct(dim2, axes=[axisZ]) lat = f.set_construct(aux2, axes=[axisY, axisX]) lon = f.set_construct(aux3, axes=[axisX, axisY]) greek = f.set_construct(aux4, axes=[axisY]) ak = f.set_construct(ak, axes=axisZ) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references coordinate_conversion = cfdm.CoordinateConversion( parameters={ 'grid_mapping_name': 'rotated_latitude_longitude', 'grid_north_pole_latitude': 38.0, 'grid_north_pole_longitude': 190.0 }) datum = cfdm.Datum(parameters={'earth_radius': 6371007}) ref0 = cfdm.CoordinateReference( coordinate_conversion=coordinate_conversion, datum=datum, coordinates=[x, y, lat, lon]) f.set_construct(msr0, axes=[axisX, axisY]) f.set_construct(ref0) orog = cfdm.DomainAncillary(data=f.get_data()) orog.set_property('standard_name', 'surface_altitude') orog.set_property('units', 'm') orog = f.set_construct(orog, axes=[axisY, axisX]) coordinate_conversion = cfdm.CoordinateConversion(parameters={ 'standard_name': 'atmosphere_hybrid_height_coordinate', 'computed_standard_name': 'altitude' }, domain_ancillaries={ 'orog': orog, 'a': ak, 'b': bk }) ref1 = cfdm.CoordinateReference( coordinates=[z], datum=datum, coordinate_conversion=coordinate_conversion) ref1 = f.set_construct(ref1) # Field ancillary variables g = f.copy() anc = cfdm.FieldAncillary(data=g.get_data()) anc.set_property('standard_name', 'ancillaryA') f.set_construct(anc, axes=[axisY, axisX]) g = f[0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.set_property('long_name', 'ancillaryB') f.set_construct(anc, axes=axisX) g = f[..., 0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.set_property('foo', 'bar') f.set_construct(anc, axes=[axisY]) f.set_property('flag_values', numpy.array([1, 2, 4], 'int32')) f.set_property('flag_meanings', 'a bb ccc') f.set_property('flag_masks', [2, 1, 0]) cm0 = cfdm.CellMethod(axes=axisX, method='mean', qualifiers={ 'interval': [cfdm.Data(1, 'day')], 'comment': 'ok' }) cm1 = cfdm.CellMethod(axes=[axisY], method='maximum', qualifiers={'where': 'sea'}) f.set_construct(cm0) f.set_construct(cm1) self.assertTrue(f.equals(f, verbose=verbose), "Field f not equal to itself") self.assertTrue(f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself") cfdm.write(f, self.filename, fmt='NETCDF3_CLASSIC', verbose=verbose) g = cfdm.read(self.filename, verbose=1) array = g[0].constructs.filter_by_identity( 'long_name=greek_letters').value().data.array self.assertEqual(array[1], b'beta', 'greek_letters = {!r}'.format(array)) self.assertEqual( len(g), 1, 'Read produced the wrong number of fields: {} != 1'.format(len(g))) g = g[0].squeeze() self.assertEqual( sorted(f.constructs), sorted(g.constructs), '\n\nf (created in memory)\n{}\n\n{}\n\ng ' '(read from disk)\n{}\n\n{}'.format(sorted(f.constructs), sorted(f.constructs.items()), sorted(g.constructs), sorted(g.constructs.items()))) self.assertTrue( f.equals(f, verbose=verbose), "Field f not equal to itself after having been written to disk") self.assertTrue( f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself after having been " "written to disk") self.assertTrue(g.equals(g.copy(), verbose=verbose), "Field g not equal to a copy of itself") self.assertTrue(g.equals(f, verbose=verbose), "Field (f) not equal to itself read back in (g)") x = g.dump(display=False) x = f.dump(display=False) g = cfdm.read(self.filename, verbose=verbose, extra='domain_ancillary', warnings=warnings)
fa p = cfdm.Field() p p.set_property('standard_name', 'precipitation_flux') p dc = cfdm.DimensionCoordinate() dc dc.set_property('long_name', 'Longitude') dc.set_data(cfdm.Data([1, 2, 3.])) dc fa = cfdm.FieldAncillary( data=cfdm.Data(numpy.array([0, 0, 2], dtype='int8'))) fa fa.set_property('standard_name', 'precipitation_flux status_flag') fa longitude_axis = p.set_construct(cfdm.DomainAxis(3)) longitude_axis key = p.set_construct(dc, axes=longitude_axis) key cm = cfdm.CellMethod(axes=longitude_axis, method='minimum') p.set_construct(cm) import numpy import cfdm # Initialise the field construct with properties Q = cfdm.Field( properties={'project': 'research', 'standard_name': 'specific_humidity', 'units': '1'})
def test_create_field(self): """TODO DOCS.""" # Dimension coordinates dim1 = cfdm.DimensionCoordinate(data=cfdm.Data(numpy.arange(10.0))) dim1.set_property("standard_name", "grid_latitude") dim1.set_property("units", "degrees") data = numpy.arange(9.0) + 20 data[-1] = 34 dim0 = cfdm.DimensionCoordinate(data=cfdm.Data(data)) dim0.set_property("standard_name", "grid_longitude") dim0.set_property("units", "degrees") dim0.nc_set_variable("x") array = dim0.data.array array = numpy.array([array - 0.5, array + 0.5]).transpose((1, 0)) array[-2, 1] = 30 array[-1, :] = [30, 36] dim0.set_bounds(cfdm.Bounds(data=cfdm.Data(array))) dim2 = cfdm.DimensionCoordinate( data=cfdm.Data([1.5]), bounds=cfdm.Bounds(data=cfdm.Data([[1, 2.0]])), ) dim2.set_property("standard_name", "atmosphere_hybrid_height_coordinate") dim2.set_property("computed_standard_name", "altitude") # Auxiliary coordinates ak = cfdm.DomainAncillary(data=cfdm.Data([10.0])) ak.set_property("units", "m") ak.id = "atmosphere_hybrid_height_coordinate_ak" ak.set_bounds(cfdm.Bounds(data=cfdm.Data([[5, 15.0]]))) bk = cfdm.DomainAncillary(data=cfdm.Data([20.0])) bk.id = "atmosphere_hybrid_height_coordinate_bk" bk.set_bounds(cfdm.Bounds(data=cfdm.Data([[14, 26.0]]))) aux2 = cfdm.AuxiliaryCoordinate(data=cfdm.Data( numpy.arange(-45, 45, dtype="int32").reshape(10, 9))) aux2.set_property("units", "degree_N") aux2.set_property("standard_name", "latitude") aux3 = cfdm.AuxiliaryCoordinate(data=cfdm.Data( numpy.arange(60, 150, dtype="int32").reshape(9, 10))) aux3.set_property("standard_name", "longitude") aux3.set_property("units", "degreeE") array = numpy.ma.array( [ "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", ], dtype="S", ) array[0] = numpy.ma.masked aux4 = cfdm.AuxiliaryCoordinate(data=cfdm.Data(array)) aux4.set_property("long_name", "greek_letters") # Cell measures msr0 = cfdm.CellMeasure( data=cfdm.Data(1 + numpy.arange(90.0).reshape(9, 10) * 1234)) msr0.set_measure("area") msr0.set_property("units", "km2") msr0.nc_set_variable("areacella") # Data data = cfdm.Data(numpy.arange(90.0).reshape(10, 9)) properties = {"units": "m s-1"} f = cfdm.Field(properties=properties) f.set_property("standard_name", "eastward_wind") da = cfdm.DomainAxis(9) da.nc_set_dimension("grid_longitude") axisX = f.set_construct(da) axisY = f.set_construct(cfdm.DomainAxis(10)) axisZ = f.set_construct(cfdm.DomainAxis(1)) f.set_data(data, axes=[axisY, axisX]) x = f.set_construct(dim0, axes=axisX) y = f.set_construct(dim1, axes=axisY) z = f.set_construct(dim2, axes=[axisZ]) lat = f.set_construct(aux2, axes=[axisY, axisX]) lon = f.set_construct(aux3, axes=[axisX, axisY]) f.set_construct(aux4, axes=[axisY]) ak = f.set_construct(ak, axes=axisZ) bk = f.set_construct(bk, axes=[axisZ]) # Coordinate references coordinate_conversion = cfdm.CoordinateConversion( parameters={ "grid_mapping_name": "rotated_latitude_longitude", "grid_north_pole_latitude": 38.0, "grid_north_pole_longitude": 190.0, }) datum = cfdm.Datum(parameters={"earth_radius": 6371007}) ref0 = cfdm.CoordinateReference( coordinate_conversion=coordinate_conversion, datum=datum, coordinates=[x, y, lat, lon], ) f.set_construct(msr0, axes=[axisX, axisY]) f.set_construct(ref0) orog = cfdm.DomainAncillary(data=f.get_data()) orog.set_property("standard_name", "surface_altitude") orog.set_property("units", "m") orog = f.set_construct(orog, axes=[axisY, axisX]) coordinate_conversion = cfdm.CoordinateConversion( parameters={ "standard_name": "atmosphere_hybrid_height_coordinate", "computed_standard_name": "altitude", }, domain_ancillaries={ "orog": orog, "a": ak, "b": bk }, ) ref1 = cfdm.CoordinateReference( coordinates=[z], datum=datum, coordinate_conversion=coordinate_conversion, ) ref1 = f.set_construct(ref1) # Field ancillary variables g = f.copy() anc = cfdm.FieldAncillary(data=g.get_data()) anc.set_property("standard_name", "ancillaryA") f.set_construct(anc, axes=[axisY, axisX]) g = f[0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.set_property("long_name", "ancillaryB") f.set_construct(anc, axes=axisX) g = f[..., 0] g = g.squeeze() anc = cfdm.FieldAncillary(data=g.get_data()) anc.set_property("foo", "bar") f.set_construct(anc, axes=[axisY]) f.set_property("flag_values", numpy.array([1, 2, 4], "int32")) f.set_property("flag_meanings", "a bb ccc") f.set_property("flag_masks", [2, 1, 0]) cm0 = cfdm.CellMethod( axes=axisX, method="mean", qualifiers={ "interval": [cfdm.Data(1, "day")], "comment": "ok" }, ) cm1 = cfdm.CellMethod(axes=[axisY], method="maximum", qualifiers={"where": "sea"}) f.set_construct(cm0) f.set_construct(cm1) self.assertTrue(f.equals(f, verbose=verbose), "Field f not equal to itself") self.assertTrue( f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself", ) cfdm.write(f, self.filename, fmt="NETCDF3_CLASSIC", verbose=verbose) g = cfdm.read(self.filename, verbose=1) array = (g[0].constructs.filter_by_identity( "long_name=greek_letters").value().data.array) self.assertEqual(array[1], b"beta") self.assertEqual(len(g), 1) g = g[0].squeeze() self.assertEqual( sorted(f.constructs), sorted(g.constructs), "\n\nf (created in memory)\n{}\n\n{}\n\ng " "(read from disk)\n{}\n\n{}".format( sorted(f.constructs), sorted(f.constructs.items()), sorted(g.constructs), sorted(g.constructs.items()), ), ) self.assertTrue( f.equals(f, verbose=verbose), "Field f not equal to itself after having been written to disk", ) self.assertTrue( f.equals(f.copy(), verbose=verbose), "Field f not equal to a copy of itself after having been " "written to disk", ) self.assertTrue( g.equals(g.copy(), verbose=verbose), "Field g not equal to a copy of itself", ) self.assertTrue( g.equals(f, verbose=verbose), "Field (f) not equal to itself read back in (g)", ) x = g.dump(display=False) x = f.dump(display=False) g = cfdm.read( self.filename, verbose=verbose, extra="domain_ancillary", warnings=warnings, )
def test_netCDF_variable_dimension(self): """Test variable and dimension access NetCDF methods.""" f = cfdm.Field() f.nc_set_variable("qwerty") self.assertTrue(f.nc_has_variable()) self.assertEqual(f.nc_get_variable(), "qwerty") self.assertEqual(f.nc_get_variable(default=None), "qwerty") self.assertEqual(f.nc_del_variable(), "qwerty") self.assertFalse(f.nc_has_variable()) self.assertIsNone(f.nc_get_variable(default=None)) self.assertIsNone(f.nc_del_variable(default=None)) f.nc_set_variable("/ncvar") self.assertEqual(f.nc_get_variable(), "ncvar") f.nc_set_variable("/ncvar/qwerty") self.assertEqual(f.nc_get_variable(), "/ncvar/qwerty") for nc_var_name in self.nc_grouped_variable_names: with self.assertRaises(ValueError): f.nc_set_variable(nc_var_name) d = cfdm.DomainAxis() d.nc_set_dimension("qwerty") self.assertTrue(d.nc_has_dimension()) self.assertEqual(d.nc_get_dimension(), "qwerty") self.assertEqual(d.nc_get_dimension(default=None), "qwerty") self.assertEqual(d.nc_del_dimension(), "qwerty") self.assertFalse(d.nc_has_dimension()) self.assertIsNone(d.nc_get_dimension(default=None)) self.assertIsNone(d.nc_del_dimension(default=None)) d.nc_set_dimension("/ncdim") self.assertEqual(d.nc_get_dimension(), "ncdim") d.nc_set_dimension("/ncdim/qwerty") self.assertEqual(d.nc_get_dimension(), "/ncdim/qwerty") for nc_dim_name in self.nc_grouped_dimension_names: with self.assertRaises(ValueError): d.nc_set_dimension(nc_dim_name) d = cfdm.Count() d.nc_set_sample_dimension("qwerty") self.assertTrue(d.nc_has_sample_dimension()) self.assertEqual(d.nc_get_sample_dimension(), "qwerty") self.assertEqual(d.nc_get_sample_dimension(default=None), "qwerty") self.assertEqual(d.nc_del_sample_dimension(), "qwerty") self.assertFalse(d.nc_has_sample_dimension()) self.assertIsNone(d.nc_get_sample_dimension(default=None)) self.assertIsNone(d.nc_del_sample_dimension(default=None)) d.nc_set_sample_dimension("/ncdim") self.assertEqual(d.nc_get_sample_dimension(), "ncdim") d.nc_set_sample_dimension("/ncdim/qwerty") self.assertEqual(d.nc_get_sample_dimension(), "/ncdim/qwerty") for nc_dim_name in self.nc_grouped_dimension_names: with self.assertRaises(ValueError): d.nc_set_sample_dimension(nc_dim_name) # ------------------------------------------------------------ # Global attributes # ------------------------------------------------------------ # values keyword f = cfdm.Field() f.nc_set_global_attribute("Conventions", "CF-1.8") f.nc_set_global_attribute("project") f.nc_set_global_attribute("foo") f.set_property("Conventions", "Y") f.set_property("project", "X") self.assertEqual( f.nc_global_attributes(values=True), {"Conventions": "CF-1.8", "project": "X", "foo": None}, ) f = cfdm.Field() self.assertEqual(f.nc_clear_global_attributes(), {}) f.nc_set_global_attribute("Conventions") f.nc_set_global_attribute("project", "X") self.assertEqual( f.nc_global_attributes(), {"Conventions": None, "project": "X"} ) f.nc_set_global_attribute("project") f.nc_set_global_attribute("comment", None) self.assertEqual( f.nc_global_attributes(), {"Conventions": None, "project": None, "comment": None}, ) self.assertEqual( f.nc_clear_global_attributes(), {"Conventions": None, "project": None, "comment": None}, ) self.assertEqual(f.nc_global_attributes(), {}) f.nc_set_global_attribute("Conventions") f.nc_set_global_attribute("project") self.assertEqual( f.nc_global_attributes(), {"Conventions": None, "project": None} ) _ = f.nc_clear_global_attributes() f.nc_set_global_attributes({}) self.assertEqual(f.nc_global_attributes(), {}) f.nc_set_global_attributes({"comment": 123}, copy=False) self.assertEqual(f.nc_global_attributes(), {"comment": 123}) f.nc_set_global_attributes({"comment": None, "foo": "bar"}) self.assertEqual( f.nc_global_attributes(), {"comment": None, "foo": "bar"} ) f = cfdm.Field() f.set_properties({"foo": "bar", "comment": "variable comment"}) f.nc_set_variable("tas") d = f.set_construct(cfdm.DomainAxis(2)) f.set_data(cfdm.Data([8, 9]), axes=[d]) f2 = f.copy() f2.nc_set_variable("ua") cfdm.write( [f, f2], tempfile1, file_descriptors={"comment": "global comment", "qwerty": "asdf"}, ) g = cfdm.read(tempfile1) self.assertEqual(len(g), 2) for x in g: self.assertEqual( x.properties(), { "comment": "variable comment", "foo": "bar", "qwerty": "asdf", "Conventions": "CF-" + cfdm.CF(), }, ) self.assertEqual( x.nc_global_attributes(), { "comment": "global comment", "qwerty": None, "Conventions": None, }, ) cfdm.write(g, tempfile2) h = cfdm.read(tempfile2) for x, y in zip(h, g): self.assertEqual(x.properties(), y.properties()) self.assertEqual( x.nc_global_attributes(), y.nc_global_attributes() ) self.assertTrue(x.equals(y, verbose=3)) self.assertTrue(y.equals(x, verbose=3)) g[1].nc_set_global_attribute("comment", "different comment") cfdm.write(g, tempfile3) h = cfdm.read(tempfile3) for x, y in zip(h, g): self.assertEqual(x.properties(), y.properties()) self.assertEqual( x.nc_global_attributes(), {"comment": None, "qwerty": None, "Conventions": None}, ) self.assertTrue(x.equals(y, verbose=3)) self.assertTrue(y.equals(x, verbose=3))
area.set_data(cfdm.Data(data)) orog = f.get_construct('surface_altitude').data[-1, -1] = 79.8 t = cfdm.DimensionCoordinate(properties={'standard_name': 'time', 'units': 'days since 2018-12-01'}) t.set_data(cfdm.Data([31.0])) a=cfdm.FieldAncillary(properties={'standard_name': 'air_temperature standard_error', 'units': 'K'}) data = numpy.random.uniform(0.1, 0.9, 90).reshape(10, 9) data = numpy.around(data, 2) a.set_data(cfdm.Data(data)) tda = f.set_domain_axis(cfdm.DomainAxis(1)) f.set_dimension_coordinate(t, axes=[tda]) f.set_field_ancillary(a, axes=['domainaxis1', 'domainaxis2']) f.del_construct('ncvar%ancillary_data') f.del_construct('ncvar%ancillary_data_1') f.del_construct('ncvar%ancillary_data_2') f.get_construct('long_name:greek_letters').set_property('long_name', 'Grid latitude name') f.cell_methods()['cellmethod0'].del_property('comment') f.cell_methods()['cellmethod0'].set_property('intervals', [cfdm.Data(0.1 , 'degrees')]) f.cell_methods()['cellmethod0'].set_property('where', 'land') f.cell_methods()['cellmethod0'].set_axes(['domainaxis1', 'domainaxis2'])