示例#1
0
    def __init__(self, **kwargs):

        ##
        ## tried making this a sub-class of Field ... but it messed up something in the cfdm.write() methods.
        ## Something must be keyed on "Field..." .. perhaps on copy of construct ...
        ##
        self.f = cfdm.Field(kwargs)
示例#2
0
    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())
示例#3
0
    def test_netCDF_geometry_variable(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.Field()

        f.nc_set_geometry_variable('qwerty')
        self.assertTrue(f.nc_has_geometry_variable())
        self.assertEqual(f.nc_get_geometry_variable(), 'qwerty')
        self.assertEqual(f.nc_get_geometry_variable(default=None), 'qwerty')
        self.assertEqual(f.nc_del_geometry_variable(), 'qwerty')
        self.assertFalse(f.nc_has_geometry_variable())
        self.assertIsNone(f.nc_get_geometry_variable(default=None))
        self.assertIsNone(f.nc_del_geometry_variable(default=None))

        f.nc_set_geometry_variable('/ncvar')
        self.assertEqual(f.nc_get_geometry_variable(), 'ncvar')

        f.nc_set_geometry_variable('/ncvar/qwerty')
        self.assertEqual(f.nc_get_geometry_variable(), '/ncvar/qwerty')

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable('group/ncvar')

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable('group/')

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable('group/ncvar/')

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable('/group/ncvar/')

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable('/')
示例#4
0
    def test_netCDF_variable_groups(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.Field()

        f.nc_set_variable('ncdim')

        with self.assertRaises(ValueError):
            f.nc_set_variable_groups(['/forecast'])

        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        attrs = f.nc_clear_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        f.nc_set_variable_groups(['forecast', 'model'])
        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ('forecast', 'model'))
        self.assertEqual(f.nc_get_variable(), '/forecast/model/ncdim')

        attrs = f.nc_clear_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ('forecast', 'model'))

        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)
        self.assertEqual(f.nc_get_variable(), 'ncdim')

        f.nc_set_variable('ncdim')
        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ())

        f.nc_set_variable('/ncdim')
        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        f.nc_set_variable('/forecast/model/ncdim')
        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ('forecast', 'model'))

        f.nc_del_variable()
        attrs = f.nc_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        with self.assertRaises(ValueError):
            f.nc_set_variable_groups(['forecast', 'model'])
示例#5
0
    def test_netCDF_geometry_variable_groups(self):
        """Test geometry variable groups access NetCDF methods."""
        f = cfdm.Field()

        f.nc_set_geometry_variable("ncvar")

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable_groups(["/forecast"])

        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        attrs = f.nc_clear_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        f.nc_set_geometry_variable_groups(["forecast", "model"])
        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ("forecast", "model"))
        self.assertEqual(f.nc_get_geometry_variable(), "/forecast/model/ncvar")

        attrs = f.nc_clear_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ("forecast", "model"))

        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)
        self.assertEqual(f.nc_get_geometry_variable(), "ncvar")

        f.nc_set_geometry_variable("ncvar")
        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ())

        f.nc_set_geometry_variable("/ncvar")
        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        f.nc_set_geometry_variable("/forecast/model/ncvar")
        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertEqual(attrs, ("forecast", "model"))

        f.nc_del_geometry_variable()
        attrs = f.nc_geometry_variable_groups()
        self.assertIsInstance(attrs, tuple)
        self.assertFalse(attrs)

        with self.assertRaises(ValueError):
            f.nc_set_geometry_variable_groups(["forecast", "model"])
示例#6
0
文件: test_dsg.py 项目: bradyrx/cfdm
    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)
示例#7
0
    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())
示例#8
0
    def test_netCDF_group_attributes(self):
        """Test group attributes access and (un)setting methods."""
        f = cfdm.Field()

        attrs = f.nc_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        attrs = f.nc_clear_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        attrs = f.nc_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        f.nc_set_group_attributes({"comment": "somthing"})
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {"comment": "somthing"})

        attrs = f.nc_clear_group_attributes()
        self.assertEqual(attrs, {"comment": "somthing"})

        attrs = f.nc_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        f.nc_set_group_attributes({"comment": "something"})
        f.nc_set_group_attributes({"foo": "bar"})
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {"comment": "something", "foo": "bar"})

        f.nc_clear_group_attributes()
        f.nc_set_group_attribute("foo", "bar")
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {"foo": "bar"})
        f.nc_set_group_attribute("foo", "bar2")
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {"foo": "bar2"})

        f.set_properties({"prop1": "value1", "comment": "variable comment"})
        f.nc_clear_group_attributes()
        f.nc_set_group_attributes({"comment": None, "foo": "bar"})
        self.assertEqual(
            f.nc_group_attributes(values=True),
            {
                "comment": "variable comment",
                "foo": "bar"
            },
        )
示例#9
0
    def test_netCDF_group_attributes(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.Field()

        attrs = f.nc_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        attrs = f.nc_clear_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        attrs = f.nc_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        f.nc_set_group_attributes({'comment': 'somthing'})
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {'comment': 'somthing'})

        attrs = f.nc_clear_group_attributes()
        self.assertEqual(attrs, {'comment': 'somthing'})

        attrs = f.nc_group_attributes()
        self.assertIsInstance(attrs, dict)
        self.assertFalse(attrs)

        f.nc_set_group_attributes({'comment': 'something'})
        f.nc_set_group_attributes({'foo': 'bar'})
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {'comment': 'something', 'foo': 'bar'})

        f.nc_clear_group_attributes()
        f.nc_set_group_attribute('foo', 'bar')
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {'foo': 'bar'})
        f.nc_set_group_attribute('foo', 'bar2')
        attrs = f.nc_group_attributes()
        self.assertEqual(attrs, {'foo': 'bar2'})

        f.set_properties({'prop1': 'value1',
                          'comment': 'variable comment'})
        f.nc_clear_group_attributes()
        f.nc_set_group_attributes({'comment': None,
                                   'foo': 'bar'})
        self.assertEqual(f.nc_group_attributes(values=True),
                         {'comment': 'variable comment',
                          'foo': 'bar'})
示例#10
0
    def test_netCDF_geometry_variable(self):
        """Test geometry variable access and (un)setting methods."""
        f = cfdm.Field()

        f.nc_set_geometry_variable("qwerty")
        self.assertTrue(f.nc_has_geometry_variable())
        self.assertEqual(f.nc_get_geometry_variable(), "qwerty")
        self.assertEqual(f.nc_get_geometry_variable(default=None), "qwerty")
        self.assertEqual(f.nc_del_geometry_variable(), "qwerty")
        self.assertFalse(f.nc_has_geometry_variable())
        self.assertIsNone(f.nc_get_geometry_variable(default=None))
        self.assertIsNone(f.nc_del_geometry_variable(default=None))

        f.nc_set_geometry_variable("/ncvar")
        self.assertEqual(f.nc_get_geometry_variable(), "ncvar")

        f.nc_set_geometry_variable("/ncvar/qwerty")
        self.assertEqual(f.nc_get_geometry_variable(), "/ncvar/qwerty")

        for nc_var_name in self.nc_grouped_variable_names:
            with self.assertRaises(ValueError):
                f.nc_set_geometry_variable(nc_var_name)
示例#11
0
    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)
示例#12
0
文件: ex02.py 项目: cmip6dr/Cookbook
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)
示例#13
0
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')
示例#14
0
    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.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')

        with self.assertRaises(ValueError):
            f.nc_set_variable(None)

        with self.assertRaises(ValueError):
            f.nc_set_variable('/')

        with self.assertRaises(ValueError):
            f.nc_set_variable('group/ncvar')

        with self.assertRaises(ValueError):
            f.nc_set_variable('group/')

        with self.assertRaises(ValueError):
            f.nc_set_variable('group/ncvar/')

        with self.assertRaises(ValueError):
            f.nc_set_variable('/group/ncvar/')

        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')

        with self.assertRaises(ValueError):
            d.nc_set_dimension(None)

        with self.assertRaises(ValueError):
            d.nc_set_dimension('/')

        with self.assertRaises(ValueError):
            d.nc_set_dimension('group/ncdim')

        with self.assertRaises(ValueError):
            d.nc_set_dimension('group/')

        with self.assertRaises(ValueError):
            d.nc_set_dimension('group/ncdim/')

        with self.assertRaises(ValueError):
            d.nc_set_dimension('/group/ncdim/')

        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')

        with self.assertRaises(ValueError):
            d.nc_set_sample_dimension(None)

        with self.assertRaises(ValueError):
            d.nc_set_sample_dimension('/')

        with self.assertRaises(ValueError):
            d.nc_set_sample_dimension('group/ncdim')

        with self.assertRaises(ValueError):
            d.nc_set_sample_dimension('group/')

        with self.assertRaises(ValueError):
            d.nc_set_sample_dimension('group/ncdim/')

        with self.assertRaises(ValueError):
            d.nc_set_sample_dimension('/group/ncdim/')

        # ------------------------------------------------------------
        # 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))
示例#15
0
    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)
示例#16
0
    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)
示例#17
0
print("\n**Field ancillaries**\n")

a = t.get_construct("fieldancillary0")
a
a.properties()
a.data

print("\n**Field creation**\n")

print("\n**Stage 1:** The field construct is created without metadata\n")

print("\n**Stage 2:** Metadata constructs are created independently.\n")

print("\n**Stage 3:** The metadata constructs are inserted into the field\n")

p = cfdm.Field(properties={"standard_name": "precipitation_flux"})
p
dc = cfdm.DimensionCoordinate(properties={"long_name": "Longitude"},
                              data=cfdm.Data([0, 1, 2.0]))
dc
fa = cfdm.FieldAncillary(
    properties={"standard_name": "precipitation_flux status_flag"},
    data=cfdm.Data(numpy.array([0, 0, 2], dtype="int8")),
)
fa
p = cfdm.Field()
p
p.set_property("standard_name", "precipitation_flux")
p
dc = cfdm.DimensionCoordinate()
dc
示例#18
0
    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,
        )
示例#19
0
    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,
        )
示例#20
0
 def test_Field__init__(self):
     """TODO DOCS."""
     cfdm.Field(source="qwerty")
示例#21
0
    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)
示例#22
0
    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)
示例#23
0
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'])

f.cell_methods()['cellmethod1'].set_property('method', 'maximum')
f.cell_methods()['cellmethod1'].del_property('where')
f.cell_methods()['cellmethod1'].set_axes([tda])


f.dump()

print(f)



h = cfdm.Field()
h.properties({'standard_name': 'specific_humidity',
              'units': '1',
              'project': 'research',
              })
h.nc_global_attributes(['project'])
h.nc_set_variable('q')

y = h.set_domain_axis(cfdm.DomainAxis(5))
x = h.set_domain_axis(cfdm.DomainAxis(8))
t = h.set_domain_axis(cfdm.DomainAxis(1))

data = numpy.random.uniform(0, 0.15, 40).reshape(5, 8)
data[[1, 3]] /= 2.
data[[0, 4]] /= 4.
data = numpy.around(data, 3)
示例#24
0
    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))
示例#25
0
文件: tutorial.py 项目: bradyrx/cfdm
a
a.properties()
a.data

print("\n**Field creation**\n")


print("\n**Stage 1:** The field construct is created without metadata\n")


print("\n**Stage 2:** Metadata constructs are created independently.\n")


print("\n**Stage 3:** The metadata constructs are inserted into the field\n")

p = cfdm.Field(properties={'standard_name': 'precipitation_flux'})
p
dc = cfdm.DimensionCoordinate(properties={'long_name': 'Longitude'},
                               data=cfdm.Data([0, 1, 2.]))
dc
fa = cfdm.FieldAncillary(
        properties={'standard_name': 'precipitation_flux status_flag'},
        data=cfdm.Data(numpy.array([0, 0, 2], dtype='int8')))
fa
p = cfdm.Field()
p
p.set_property('standard_name', 'precipitation_flux')
p
dc = cfdm.DimensionCoordinate()
dc
dc.set_property('long_name', 'Longitude')
示例#26
0
    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,
        )
示例#27
0
    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()
示例#28
0
 def test_Field__init__(self):
     """Test the Field constructor and source keyword."""
     cfdm.Field(source="qwerty")
示例#29
0
    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))
示例#30
0
    def test_Field__init__(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.Field(source='qwerty')