예제 #1
0
    def test_unique_constructs(self):
        """Test the `unique_constructs` function."""
        f = cfdm.example_field(0)
        g = cfdm.example_field(1)

        self.assertFalse(cfdm.unique_constructs([]))

        self.assertEqual(len(cfdm.unique_constructs([f])), 1)
        self.assertEqual(len(cfdm.unique_constructs([f, f])), 1)
        self.assertEqual(len(cfdm.unique_constructs([f, f.copy()])), 1)
        self.assertEqual(len(cfdm.unique_constructs([f, f.copy(), g])), 2)

        fields = [f, f, g]
        domains = [x.domain for x in (f, f, g)]

        self.assertEqual(len(cfdm.unique_constructs(domains)), 2)
        self.assertEqual(len(cfdm.unique_constructs(domains + fields)), 4)
        self.assertEqual(
            len(cfdm.unique_constructs(domains + fields + [f.domain])), 4
        )

        # Test generator
        domains = (x.domain for x in ())
        self.assertEqual(len(cfdm.unique_constructs(domains)), 0)

        domains = (x.domain for x in (f,))
        self.assertEqual(len(cfdm.unique_constructs(domains)), 1)

        domains = (x.domain for x in (f, f, g))
        self.assertEqual(len(cfdm.unique_constructs(domains)), 2)
예제 #2
0
    def test_Field_has_geometry(self):
        """TODO DOCS."""
        f = self.f1
        self.assertFalse(f.has_geometry())

        f = cfdm.example_field(6)
        self.assertTrue(f.has_geometry())
예제 #3
0
    def test_Field_equals(self):
        """Test the equality-testing Field method."""
        f = self.f1

        self.assertTrue(f.equals(f, verbose=3))

        g = f.copy()
        self.assertTrue(f.equals(g, verbose=3))
        self.assertTrue(g.equals(f, verbose=3))

        g = f[...]
        self.assertTrue(f.equals(g, verbose=3))
        self.assertTrue(g.equals(f, verbose=3))

        g = g.squeeze()
        self.assertFalse(f.equals(g))

        h = f.copy()
        h.data[...] = h.data.array[...] + 1
        self.assertFalse(f.equals(h))

        # Symmetry
        f = cfdm.example_field(2)
        g = f.copy()
        self.assertTrue(f.equals(g))
        self.assertTrue(g.equals(f))

        g.del_construct("dimensioncoordinate0")
        self.assertFalse(f.equals(g))
        self.assertFalse(g.equals(f))
예제 #4
0
    def test_Field_has_geometry(self):
        """Test the `creation_commands` Field method."""
        f = self.f1
        self.assertFalse(f.has_geometry())

        f = cfdm.example_field(6)
        self.assertTrue(f.has_geometry())
예제 #5
0
 def test_CoordinateReference_write(self):
     """Test write when vertical CRS has no coordinates."""
     f = cfdm.example_field(1)
     c = f.construct("standard_name:atmosphere_hybrid_height_coordinate")
     c.clear_coordinates()
     # This write should not fail ...
     cfdm.write(f, tempfile1)
예제 #6
0
파일: test_Field.py 프로젝트: bradyrx/cfdm
    def test_Field_get_filenames(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.example_field(0)

        cfdm.write(f, tmpfile)
        g = cfdm.read(tmpfile)[0]

        abspath_tmpfile = os.path.abspath(tmpfile)
        self.assertEqual(g.get_filenames(), set([abspath_tmpfile]))

        g.data[...] = -99
        self.assertEqual(g.get_filenames(), set([abspath_tmpfile]))

        for c in g.constructs.filter_by_data().values():
            c.data[...] = -99

        self.assertEqual(g.get_filenames(), set([abspath_tmpfile]))

        for c in g.constructs.filter_by_data().values():
            if c.has_bounds():
                c.bounds.data[...] = -99
        # --- End: for

        self.assertEqual(g.get_filenames(), set())

        os.remove(tmpfile)
예제 #7
0
    def test_read_mask(self):
        """Test reading and writing of netCDF with masked data."""
        f = cfdm.example_field(0)

        N = f.size

        f.data[1, 1] = cfdm.masked
        f.data[2, 2] = cfdm.masked

        f.del_property("_FillValue", None)
        f.del_property("missing_value", None)

        cfdm.write(f, tmpfile)

        g = cfdm.read(tmpfile)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)

        g = cfdm.read(tmpfile, mask=False)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N)

        g.apply_masking(inplace=True)
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)

        f.set_property("_FillValue", 999)
        f.set_property("missing_value", -111)
        cfdm.write(f, tmpfile)

        g = cfdm.read(tmpfile)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)

        g = cfdm.read(tmpfile, mask=False)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N)

        g.apply_masking(inplace=True)
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)
예제 #8
0
    def test_netCDF_to_memory(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.example_field(4)
        f.data.to_memory()  # on non-compressed array
        f.compress('indexed_contiguous', inplace=True)
        f.data.to_memory()  # on compressed array
예제 #9
0
파일: test_groups.py 프로젝트: bradyrx/cfdm
    def test_groups_compression(self):
        f = cfdm.example_field(4)

        ungrouped_file = ungrouped_file3
        grouped_file = grouped_file3

        f.compress('indexed_contiguous', inplace=True)
        f.data.get_count().nc_set_variable('count')
        f.data.get_index().nc_set_variable('index')

        cfdm.write(f, ungrouped_file, verbose=1)
        g = cfdm.read(ungrouped_file)[0]
        self.assertTrue(f.equals(g, verbose=2))

        # ------------------------------------------------------------
        # Move the field construct to the /forecast/model group
        # ------------------------------------------------------------
        g.nc_set_variable_groups(['forecast', 'model'])

        # ------------------------------------------------------------
        # Move the count variable to the /forecast group
        # ------------------------------------------------------------
        g.data.get_count().nc_set_variable_groups(['forecast'])

        # ------------------------------------------------------------
        # Move the index variable to the /forecast group
        # ------------------------------------------------------------
        g.data.get_index().nc_set_variable_groups(['forecast'])

        # ------------------------------------------------------------
        # Move the coordinates that span the element dimension to the
        # /forecast group
        # ------------------------------------------------------------
        name = 'altitude'
        g.construct(name).nc_set_variable_groups(['forecast'])

        # ------------------------------------------------------------
        # Move the sample dimension to the /forecast group
        # ------------------------------------------------------------
        g.data.get_count().nc_set_sample_dimension_groups(['forecast'])

        cfdm.write(g, grouped_file, verbose=1)

        nc = netCDF4.Dataset(grouped_file, 'r')
        self.assertIn(f.nc_get_variable(),
                      nc.groups['forecast'].groups['model'].variables)
        self.assertIn(f.data.get_count().nc_get_variable(),
                      nc.groups['forecast'].variables)
        self.assertIn(f.data.get_index().nc_get_variable(),
                      nc.groups['forecast'].variables)
        self.assertIn(
            f.construct('altitude').nc_get_variable(),
            nc.groups['forecast'].variables)
        nc.close()

        h = cfdm.read(grouped_file, verbose=1)
        self.assertEqual(len(h), 1, repr(h))
        self.assertTrue(f.equals(h[0], verbose=2))
예제 #10
0
    def test_write_coordinates(self):
        """TODO DOCS."""
        f = cfdm.example_field(0)

        cfdm.write(f, tmpfile, coordinates=True)
        g = cfdm.read(tmpfile)

        self.assertEqual(len(g), 1)
        self.assertTrue(g[0].equals(f, verbose=3))
예제 #11
0
파일: test_Field.py 프로젝트: bradyrx/cfdm
    def test_Field_has_geometry(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = self.f
        self.assertFalse(f.has_geometry())

        f = cfdm.example_field(6)
        self.assertTrue(f.has_geometry())
예제 #12
0
    def test_write_coordinates(self):
        """Test the `coordinates` keyword argument of `write`."""
        f = cfdm.example_field(0)

        cfdm.write(f, tmpfile, coordinates=True)
        g = cfdm.read(tmpfile)

        self.assertEqual(len(g), 1)
        self.assertTrue(g[0].equals(f, verbose=3))
예제 #13
0
    def test_write_coordinates(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.example_field(0)

        cfdm.write(f, tmpfile, coordinates=True)
        g = cfdm.read(tmpfile)

        self.assertEqual(len(g), 1)
        self.assertTrue(g[0].equals(f))
예제 #14
0
    def test_write_filename(self):
        """Test the writing of a named netCDF file."""
        f = cfdm.example_field(0)
        a = f.data.array

        cfdm.write(f, tmpfile)
        g = cfdm.read(tmpfile)

        with self.assertRaises(Exception):
            cfdm.write(g, tmpfile)

        self.assertTrue((a == g[0].data.array).all())
예제 #15
0
    def test_Domain_climatological_time_axes(self):
        """Test the `climatological_time_axes` method of Domain."""
        f = cfdm.example_field(7)
        d = f.domain

        self.assertEqual(d.climatological_time_axes(), set())

        cm = cfdm.CellMethod(axes="domainaxis0", method="mean")
        cm.set_qualifier("over", "years")
        f.set_construct(cm)

        self.assertEqual(d.climatological_time_axes(), set(("domainaxis0", )))
예제 #16
0
    def test_example_field(self):
        """Test the `example_field` function."""
        top = 8

        example_fields = cfdm.example_fields()
        self.assertEqual(len(example_fields), top)

        for f in example_fields:
            _ = f.data.array

            self.assertIsInstance(f.dump(display=False), str)

            cfdm.write(f, temp_file)
            g = cfdm.read(temp_file, verbose=1)

            self.assertEqual(len(g), 1)
            self.assertTrue(f.equals(g[0], verbose=3))

        with self.assertRaises(Exception):
            cfdm.example_field(top + 1)

        with self.assertRaises(ValueError):
            cfdm.example_field(1, 2)

        with self.assertRaises(TypeError):
            cfdm.example_field(1, 2, 3)

        self.assertEqual(len(cfdm.example_fields(0)), 1)
        self.assertEqual(len(cfdm.example_fields(0, 2)), 2)
        self.assertEqual(len(cfdm.example_fields(0, 2, 0)), 3)
예제 #17
0
    def test_example_field(self):
        """Test the `example_field` function."""
        top = 7

        for n in range(top + 1):
            f = cfdm.example_field(n)

            f.data.array

            self.assertIsInstance(f.dump(display=False), str)

            cfdm.write(f, temp_file)
            g = cfdm.read(temp_file, verbose=1)

            self.assertEqual(len(g), 1)
            self.assertTrue(f.equals(g[0], verbose=3), "n={}".format(n))

        with self.assertRaises(Exception):
            cfdm.example_field(top + 1)

        with self.assertRaises(ValueError):
            cfdm.example_field(1, 2)
예제 #18
0
    def test_write_scalar_domain_ancillary(self):
        """Test the writing of a file with a scalar domain ancillary."""
        f = cfdm.example_field(1)

        # Create scalar domain ancillary
        d = f.construct("ncvar%a")
        d.del_data()
        d.set_data(10)
        d.del_bounds()

        key = f.construct_key("ncvar%a")
        f.set_data_axes((), key=key)

        cfdm.write(f, tmpfile)
예제 #19
0
    def test_Domain_equals(self):
        """Test the equality-testing Domain method."""
        d = self.d
        e = d.copy()

        self.assertTrue(d.equals(d, verbose=3))
        self.assertTrue(d.equals(e, verbose=3))
        self.assertTrue(e.equals(d, verbose=3))

        e = cfdm.example_field(0).domain
        self.assertFalse(e.equals(d))

        e.set_property("foo", "bar")
        self.assertFalse(e.equals(d))
예제 #20
0
    def test_write_filename(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.example_field(0)
        a = f.data.array

        cfdm.write(f, tmpfile)
        g = cfdm.read(tmpfile)

        with self.assertRaises(Exception):
            cfdm.write(g, tmpfile)

        self.assertTrue((a == g[0].data.array).all())
예제 #21
0
파일: test_Field.py 프로젝트: bradyrx/cfdm
    def test_Field_apply_masking(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.example_field(0)

        for prop in ('missing_value', '_FillValue', 'valid_min', 'valid_max',
                     'valid_range'):
            f.del_property(prop, None)

        d = f.data.copy()
        g = f.copy()
        self.assertIsNone(f.apply_masking(inplace=True))
        self.assertTrue(f.equals(g, verbose=3))

        x = 0.11
        y = 0.1
        z = 0.2

        f.set_property('_FillValue', x)
        d = f.data.copy()

        g = f.apply_masking()
        e = d.apply_masking(fill_values=[x])
        self.assertTrue(e.equals(g.data, verbose=3))
        self.assertEqual(g.data.array.count(), g.data.size - 1)

        f.set_property('valid_range', [y, z])
        d = f.data.copy()
        g = f.apply_masking()
        e = d.apply_masking(fill_values=[x], valid_range=[y, z])
        self.assertTrue(e.equals(g.data, verbose=3))

        f.del_property('valid_range')
        f.set_property('valid_min', y)
        g = f.apply_masking()
        e = d.apply_masking(fill_values=[x], valid_min=y)
        self.assertTrue(e.equals(g.data, verbose=3))

        f.del_property('valid_min')
        f.set_property('valid_max', z)
        g = f.apply_masking()
        e = d.apply_masking(fill_values=[x], valid_max=z)
        self.assertTrue(e.equals(g.data, verbose=3))

        f.set_property('valid_min', y)
        g = f.apply_masking()
        e = d.apply_masking(fill_values=[x], valid_min=y, valid_max=z)
        self.assertTrue(e.equals(g.data, verbose=3))
예제 #22
0
    def test_example_field(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        top = 7

        for n in range(top + 1):
            f = cfdm.example_field(n)

            _ = f.data.array

            self.assertIsInstance(f.dump(display=False), str)

            cfdm.write(f, temp_file)
            g = cfdm.read(temp_file, verbose=1)

            self.assertEqual(len(g), 1)
            self.assertTrue(f.equals(g[0], verbose=3), 'n={}'.format(n))

        with self.assertRaises(Exception):
            _ = cfdm.example_field(top + 1)

        with self.assertRaises(ValueError):
            cfdm.example_field(1, 2)
예제 #23
0
    def test_Field_climatological_time_axes(self):
        """Test the `climatological_time_axes` method of Field."""
        f = cfdm.example_field(0)
        self.assertEqual(f.climatological_time_axes(), set())

        f.set_construct(
            cfdm.CellMethod("domainaxis2", "mean", {"within": "years"}))
        f.set_construct(
            cfdm.CellMethod("domainaxis2", "mean", {"over": "years"}))

        cta = f.climatological_time_axes()
        self.assertEqual(cta, set(["domainaxis2"]))

        d = f.get_domain()
        self.assertEqual(d.climatological_time_axes(), cta)
예제 #24
0
    def test_Field_creation_commands(self):
        """Test the `creation_commands` Field method."""
        for i in range(7):
            f = cfdm.example_field(i)

        f = self.f1

        for rd in (False, True):
            f.creation_commands(representative_data=rd)

        for indent in (0, 4):
            f.creation_commands(indent=indent)

        for s in (False, True):
            f.creation_commands(string=s)

        for ns in ("cfdm", ""):
            f.creation_commands(namespace=ns)
예제 #25
0
    def test_read_write_domain(self):
        """Test the reading and writing of domain constucts."""
        f = cfdm.example_field(1)
        d = f.domain.copy()

        # 1 domain
        cfdm.write(d, tmpfile)
        e = cfdm.read(tmpfile)
        self.assertTrue(len(e), 10)

        e = cfdm.read(tmpfile, domain=True, verbose=1)
        self.assertEqual(len(e), 1)
        e = e[0]
        self.assertIsInstance(e, cfdm.Domain)
        self.assertTrue(e.equals(e.copy(), verbose=3))
        self.assertTrue(d.equals(e, verbose=3))
        self.assertTrue(e.equals(d, verbose=3))

        # 1 field and 1 domain
        cfdm.write([f, d], tmpfile)
        g = cfdm.read(tmpfile)
        self.assertTrue(len(g), 1)
        g = g[0]
        self.assertIsInstance(g, cfdm.Field)
        self.assertTrue(g.equals(f, verbose=3))

        e = cfdm.read(tmpfile, domain=True, verbose=1)
        self.assertEqual(len(e), 1)
        e = e[0]
        self.assertIsInstance(e, cfdm.Domain)

        # 1 field and 2 domains
        cfdm.write([f, d, d], tmpfile)
        g = cfdm.read(tmpfile)
        self.assertTrue(len(g), 1)
        g = g[0]
        self.assertIsInstance(g, cfdm.Field)
        self.assertTrue(g.equals(f, verbose=3))

        e = cfdm.read(tmpfile, domain=True, verbose=1)
        self.assertEqual(len(e), 2)
        self.assertIsInstance(e[0], cfdm.Domain)
        self.assertIsInstance(e[1], cfdm.Domain)
        self.assertTrue(e[0].equals(e[1]))
예제 #26
0
파일: test_Field.py 프로젝트: bradyrx/cfdm
    def test_Field_creation_commands(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = self.f.copy()

        for rd in (False, True):
            for indent in (0, 4):
                for h in (False, True):
                    for s in (False, True):
                        for ns in (None, ''):
                            _ = f.creation_commands(representative_data=rd,
                                                    indent=indent,
                                                    namespace=ns,
                                                    string=s,
                                                    header=h)
                            for i in range(7):
                                f = cfdm.example_field(i)
                                _ = f.creation_commands(representative_data=rd,
                                                        indent=indent,
                                                        namespace=ns,
                                                        string=s,
                                                        header=h)
예제 #27
0
    def test_read_mask(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cfdm.example_field(0)

        N = f.size

        f.data[1, 1] = cfdm.masked
        f.data[2, 2] = cfdm.masked

        f.del_property('_FillValue', None)
        f.del_property('missing_value', None)

        cfdm.write(f, tmpfile)

        g = cfdm.read(tmpfile)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)

        g = cfdm.read(tmpfile, mask=False)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N)

        g.apply_masking(inplace=True)
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)

        f.set_property('_FillValue', 999)
        f.set_property('missing_value', -111)
        cfdm.write(f, tmpfile)

        g = cfdm.read(tmpfile)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)

        g = cfdm.read(tmpfile, mask=False)[0]
        self.assertEqual(numpy.ma.count(g.data.array), N)

        g.apply_masking(inplace=True)
        self.assertEqual(numpy.ma.count(g.data.array), N - 2)
예제 #28
0
    def test_groups_unlimited_dimension(self):
        """TODO DOCS."""
        f = cfdm.example_field(0)

        # Create an unlimited dimension in the root group
        key = f.domain_axis_key("time")
        domain_axis = f.constructs[key]
        domain_axis.nc_set_unlimited(True)

        f.insert_dimension(key, 0, inplace=True)

        key = f.domain_axis_key("latitude")
        domain_axis = f.constructs[key]
        domain_axis.nc_set_unlimited(True)
        domain_axis.nc_set_dimension_groups(["forecast"])

        # ------------------------------------------------------------
        # Move the latitude coordinate to the /forecast group. Note
        # that this will drag its netDF dimension along with it,
        # because it's a dimension coordinate variable
        # ------------------------------------------------------------
        lat = f.construct("latitude")
        lat.nc_set_variable_groups(["forecast"])

        # ------------------------------------------------------------
        # Move the field construct to the /forecast/model group
        # ------------------------------------------------------------
        f.nc_set_variable_groups(["forecast", "model"])

        grouped_file = grouped_file5
        cfdm.write(f, grouped_file5, verbose=1)

        h = cfdm.read(grouped_file, verbose=1)
        self.assertEqual(len(h), 1)
        h = h[0]
        self.assertTrue(f.equals(h, verbose=3))
예제 #29
0
class ConstructsTest(unittest.TestCase):
    """TODO DOCS."""

    f = cfdm.example_field(1)
    c = f.constructs

    def setUp(self):
        """TODO DOCS."""
        # Disable log messages to silence expected warnings
        cfdm.LOG_LEVEL("DISABLE")
        # Note: to enable all messages for given methods, lines or
        # calls (those without a 'verbose' option to do the same)
        # e.g. to debug them, wrap them (for methods, start-to-end
        # internally) as follows:
        #
        # cfdm.LOG_LEVEL('DEBUG')
        # < ... test code ... >
        # cfdm.log_level('DISABLE')

    def test_Constructs__repr__str__dump(self):
        """TODO DOCS."""
        c = self.c

        repr(c)
        str(c)

        c = cfdm.Constructs()
        str(c)

    def test_Constructs__len__(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c), 20)
        self.assertEqual(len(self.f.domain.constructs), 17)

    def test_Constructs_check_construct_type(self):
        """TODO DOCS."""
        c = self.c

        with self.assertRaises(ValueError):
            c._check_construct_type("bad type")

    def test_Constructs_construct_type(self):
        """TODO DOCS."""
        d = self.f.domain.constructs
        self.assertIsNone(d.construct_type("cell_method"))

    def test_Constructs_construct_types(self):
        """TODO DOCS."""
        d = self.f.domain.constructs
        self.assertEqual(len(d.construct_types()), len(d))

    def test_Constructs_items_key_value(self):
        """TODO DOCS."""
        c = self.c

        for i, (key, value) in enumerate(c.items()):
            x = c.filter_by_key(key)
            self.assertEqual(x.key(), key)
            self.assertIs(x.value(), value)

        self.assertEqual(i, 19)

    def test_Constructs_copy_shallow_copy(self):
        """TODO DOCS."""
        c = self.c

        d = c.filter_by_type("domain_axis")
        e = d.copy()
        self.assertTrue(d.equals(e, verbose=3))
        self.assertTrue(e.equals(d, verbose=3))

        d = c.shallow_copy()
        self.assertTrue(c.equals(d, verbose=3))
        self.assertTrue(d.equals(c, verbose=3))

    def test_Constructs_domain_axis_identity(self):
        """TODO DOCS."""
        c = self.c

        with self.assertRaises(ValueError):
            c.domain_axis_identity(999)

    def test_Constructs_filter(self):
        """TODO DOCS."""
        c = self.c

        for todict in (False, True):
            d = c.filter(
                filter_by_axis=(),
                filter_by_data=True,
                filter_by_identity=(),
                filter_by_key=(),
                filter_by_measure=(),
                filter_by_method=(),
                filter_by_naxes=(),
                filter_by_ncdim=(),
                filter_by_ncvar=(),
                filter_by_property={},
                filter_by_size=(),
                filter_by_type=(),
                todict=todict,
            )
            self.assertEqual(len(d), 0)

        self.assertEqual(c.filter(cached=999), 999)
        self.assertTrue(c.filter().equals(c))

        with self.assertRaises(TypeError):
            c.filter(bad_kwarg=None)

    def test_Constructs_FILTERING(self):
        """TODO DOCS."""
        c = self.c

        # Axis
        for axis_mode in ("and", "or", "exact", "subset"):
            for args in (
                ["qwerty"],
                ["domainaxis0"],
                ["domainaxis0", "domainaxis1"],
                ["domainaxis0", "domainaxis1", "domainaxis2"],
            ):
                d = c.filter_by_axis(*args, axis_mode=axis_mode)
                e = d.inverse_filter()
                self.assertEqual(len(e), len(c) - len(d))

        # Inverse filter, filters applied
        self.assertEqual(len(c.filters_applied()), 0)
        ci = c.inverse_filter()
        self.assertEqual(len(ci), 0)
        self.assertEqual(len(ci), len(c) - len(c))

        d = c.filter_by_type("dimension_coordinate", "auxiliary_coordinate")
        self.assertEqual(len(d.filters_applied()), 1)
        di = d.inverse_filter()
        self.assertEqual(len(di), len(c) - len(d))

        e = d.filter_by_property(units="degrees")
        self.assertEqual(len(e.filters_applied()), 2)
        ei = e.inverse_filter(1)
        self.assertEqual(len(e.filters_applied()), 2)
        self.assertEqual(len(ei), len(d) - len(e))

        d2 = c.filter_by_type("auxiliary_coordinate")
        e2 = d2.filter_by_naxes(1)
        f2 = e2.inverse_filter(1)
        g2 = f2.inverse_filter(1)
        h2 = g2.inverse_filter(1)
        self.assertTrue(g2.equals(e2, verbose=3))
        self.assertTrue(h2.equals(f2, verbose=3))

        # Unfilter
        self.assertTrue(e.unfilter(1).equals(d, verbose=3))
        self.assertTrue(e.unfilter(1).unfilter().equals(c, verbose=3))
        self.assertTrue(d.unfilter(1).equals(c, verbose=3))
        self.assertTrue(c.unfilter(1).equals(c, verbose=3))

    def test_Constructs_domain_axes(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(c.domain_axes(cached=999), 999)
        self.assertEqual(
            len(c.domain_axes(filter_by_identity=("grid_longitude", ))), 1)

        keys = tuple(c.domain_axes(todict=True))
        self.assertEqual(len(c.domain_axes(*keys)), 4)
        self.assertEqual(len(c.domain_axes(keys)), 0)

        with self.assertRaises(TypeError):
            c.domain_axes(filter_by_type=("dimension_coordinate", ))

        with self.assertRaises(TypeError):
            c.domain_axes("grid_latitude",
                          filter_by_identity=("grid_longitude", ))

    def test_Constructs_filter_by_data(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_data()), 12)
        self.assertEqual(c.filter_by_data(cached=999), 999)

    def test_Constructs_filter_by_type(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(c.filter_by_type(cached=999), 999)

        d = c.filter(
            filter_by_property={"standard_name": None},
            filter_by_type=("dimension_coordinate", ),
            todict=True,
        )
        self.assertEqual(len(d), 4)

        constructs = c.filter_by_type(
            "auxiliary_coordinate",
            "cell_measure",
            "cell_method",
            "coordinate_reference",
            "dimension_coordinate",
            "domain_ancillary",
            "domain_axis",
            "field_ancillary",
        )
        n = 20
        self.assertEqual(len(constructs), n)

        constructs = c.filter_by_type("auxiliary_coordinate")
        n = 3
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.AuxiliaryCoordinate)

        constructs = c.filter_by_type("cell_measure")
        n = 1
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.CellMeasure)

        constructs = c.filter_by_type("cell_method")
        n = 2
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.CellMethod)

        constructs = c.filter_by_type("dimension_coordinate")
        n = 4
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.DimensionCoordinate)

        constructs = c.filter_by_type("coordinate_reference")
        n = 2
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.CoordinateReference)

        constructs = c.filter_by_type("domain_ancillary")
        n = 3
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.DomainAncillary)

        constructs = c.filter_by_type("field_ancillary")
        n = 1
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.FieldAncillary)

        constructs = c.filter_by_type("domain_axis")
        n = 4
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.DomainAxis)

        constructs = c.filter_by_type(*["domain_ancillary"])
        n = 3
        self.assertEqual(len(constructs), n)
        for key, value in constructs.items():
            self.assertIsInstance(value, cfdm.DomainAncillary)

        constructs = c.filter_by_type("domain_ancillary", "domain_axis")
        n = 7
        self.assertEqual(len(constructs), n)

        self.assertEqual(len(c.filter_by_type("qwerty")), 0)

    def test_Constructs_filter_by_method(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_method()), 2)
        self.assertEqual(len(c.filter_by_method("mean")), 1)
        self.assertEqual(len(c.filter_by_method("qwerty")), 0)
        self.assertEqual(c.filter_by_method(cached=999), 999)

    def test_Constructs_filter_by_ncdim(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_ncdim()), 4)
        self.assertEqual(len(c.filter_by_ncdim("y")), 1)
        self.assertEqual(len(c.filter_by_ncdim("qwerty")), 0)
        self.assertEqual(c.filter_by_ncdim(cached=999), 999)

    def test_Constructs_filter_by_ncvar(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_ncvar()), 14)
        self.assertEqual(len(c.filter_by_ncvar("qwerty")), 0)
        self.assertEqual(len(c.filter_by_ncvar("a")), 1)
        self.assertEqual(c.filter_by_ncvar(cached=999), 999)

    def test_Constructs_filter_by_measure(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_measure()), 1)
        self.assertEqual(len(c.filter_by_measure("qwerty")), 0)
        self.assertEqual(len(c.filter_by_measure("area")), 1)
        self.assertEqual(c.filter_by_measure(cached=999), 999)

    def test_Constructs_filter_by_identity(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_identity()), 20)
        self.assertEqual(len(c.filter_by_identity("qwerty")), 0)
        self.assertEqual(len(c.filter_by_identity("latitude")), 1)
        self.assertEqual(c.filter_by_identity(cached=999), 999)

        self.assertEqual(
            len(c.filter_by_identity("dimensioncoordinate1", "longitude")), 2)

        with self.assertRaises(TypeError):
            c("latitude", filter_by_identity=("longitude", ))

    def test_Constructs_filter_by_axis(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_axis()), 12)
        self.assertEqual(len(c.filter_by_axis(0, 1, 2, axis_mode="or")), 11)
        self.assertEqual(len(c.filter_by_axis("domainaxis1", axis_mode="or")),
                         7)
        self.assertEqual(len(c.filter_by_axis("grid_longitude")), 6)
        self.assertEqual(len(c.filter_by_axis(re.compile("^grid_lon"))), 6)
        self.assertEqual(len(c.filter_by_axis(re.compile("^grid"))), 0)
        self.assertEqual(len(c.filter_by_axis("ncdim%x")), 6)

        self.assertEqual(c.filter_by_axis(cached=999), 999)

        self.assertEqual(len(c.filter_by_axis(99)), 0)
        self.assertEqual(len(c.filter_by_axis(99, todict=True)), 0)

        with self.assertRaises(ValueError):
            c.filter_by_axis(0, 1, axis_mode="bad_mode")

    def test_Constructs_clear_filters_applied(self):
        """TODO DOCS."""
        c = self.c

        d = c.shallow_copy()
        d.clear_filters_applied()

    def test_Constructs_filter_by_naxes(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_naxes()), 12)
        self.assertEqual(len(c.filter_by_naxes(1)), 7)
        self.assertEqual(c.filter_by_naxes(cached=999), 999)

    def test_Constructs_filter_by_property(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_property()), 12)

        for mode in ([], ["and"], ["or"]):
            for kwargs in (
                {
                    "qwerty": 34
                },
                {
                    "standard_name": "surface_altitude"
                },
                {
                    "standard_name": "surface_altitude",
                    "units": "m"
                },
                {
                    "standard_name": "surface_altitude",
                    "units": "degrees"
                },
                {
                    "standard_name": "surface_altitude",
                    "units": "qwerty"
                },
            ):
                d = c.filter_by_property(*mode, **kwargs)
                e = d.inverse_filter()
                self.assertEqual(len(e), len(c) - len(d))

        self.assertEqual(len(c.filter_by_property(standard_name=None)), 8)

        with self.assertRaises(ValueError):
            c.filter_by_property("too many", "modes")

        with self.assertRaises(ValueError):
            c.filter_by_property("bad_mode")

    def test_Constructs_filter_by_size(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_size(9)), 1)
        self.assertEqual(len(c.filter_by_size(9, 10)), 2)
        self.assertEqual(len(c.filter_by_size()), 4)
        self.assertEqual(len(c.filter_by_size(-1)), 0)
        self.assertEqual(c.filter_by_size(cached=999), 999)

    def test_Constructs_filter_by_key(self):
        """TODO DOCS."""
        c = self.c

        self.assertEqual(len(c.filter_by_key()), 20)
        self.assertEqual(len(c.filter_by_key("qwerty")), 0)
        self.assertEqual(len(c.filter_by_key("dimensioncoordinate1")), 1)
        self.assertEqual(len(c.filter_by_key(re.compile("^dim"))), 4)
        self.assertEqual(c.filter_by_key(cached=999), 999)

    def test_Constructs_copy(self):
        """TODO DOCS."""
        c = self.c

        copy.copy(c)
        copy.deepcopy(c)

    def test_Constructs__getitem__(self):
        """TODO DOCS."""
        c = self.c

        self.assertIsInstance(c["auxiliarycoordinate1"],
                              cfdm.AuxiliaryCoordinate)

        with self.assertRaises(KeyError):
            c["bad_key"]

    def test_Constructs_get_data_axes(self):
        """TODO DOCS."""
        c = self.c

        with self.assertRaises(ValueError):
            c.get_data_axes("bad key")

    def test_Constructs_todict(self):
        """TODO DOCS."""
        c = self.c

        self.assertIsInstance(c.todict(), dict)

    def test_Constructs_private(self):
        """TODO DOCS."""
        c = self.f.domain.constructs

        # _construct_type_description
        self.assertEqual(
            c._construct_type_description("auxiliary_coordinate"),
            "auxiliary coordinate",
        )

        # _check_construct_type
        self.assertIsNone(c._check_construct_type(None))
        self.assertIsNone(c._check_construct_type("cell_method", None))

        x = c.shallow_copy()
        del x._constructs["auxiliary_coordinate"]
        with self.assertRaises(KeyError):
            x["auxiliarycoordinate1"]

        # _del_construct
        x = self.c.shallow_copy()
        x._del_construct("domainancillary0")

        with self.assertRaises(ValueError):
            x._del_construct("domainaxis1")

        x._del_construct("dimensioncoordinate3")
        with self.assertRaises(ValueError):
            x._del_construct("domainaxis3")

        x = c.shallow_copy()
        x._del_construct("dimensioncoordinate3")
        self.assertIsInstance(x._del_construct("domainaxis3"), cfdm.DomainAxis)

        # _set_construct
        x = self.c.shallow_copy()
        with self.assertRaises(ValueError):
            x._set_construct(self.f.construct("cellmethod0"),
                             axes=["domainaxis0"])

        # _set_construct_data_axes
        x = self.c.shallow_copy()
        with self.assertRaises(ValueError):
            x._set_construct_data_axes("qwerty", ["domainaxis"])

        with self.assertRaises(ValueError):
            x._set_construct_data_axes("auxiliarycoordinate1", ["qwerty"])

        with self.assertRaises(ValueError):
            x._set_construct_data_axes("auxiliarycoordinate1", ["domainaxis0"])

        # _pop
        x = c.shallow_copy()
        with self.assertRaises(KeyError):
            x._pop("qwerty")
예제 #30
0
파일: test_groups.py 프로젝트: bradyrx/cfdm
    def test_groups(self):
        f = cfdm.example_field(1)

        ungrouped_file = ungrouped_file1
        grouped_file = grouped_file1

        # Add a second grid mapping
        datum = cfdm.Datum(parameters={'earth_radius': 7000000})
        conversion = cfdm.CoordinateConversion(
            parameters={'grid_mapping_name': 'latitude_longitude'})

        grid = cfdm.CoordinateReference(
            coordinate_conversion=conversion,
            datum=datum,
            coordinates=['auxiliarycoordinate0', 'auxiliarycoordinate1'])

        f.set_construct(grid)

        grid0 = f.construct('grid_mapping_name:rotated_latitude_longitude')
        grid0.del_coordinate('auxiliarycoordinate0')
        grid0.del_coordinate('auxiliarycoordinate1')

        cfdm.write(f, ungrouped_file)
        g = cfdm.read(ungrouped_file, verbose=1)
        self.assertEqual(len(g), 1)
        g = g[0]
        self.assertTrue(f.equals(g, verbose=2))

        # ------------------------------------------------------------
        # Move the field construct to the /forecast/model group
        # ------------------------------------------------------------
        g.nc_set_variable_groups(['forecast', 'model'])
        cfdm.write(g, grouped_file)

        nc = netCDF4.Dataset(grouped_file, 'r')
        self.assertIn(f.nc_get_variable(),
                      nc.groups['forecast'].groups['model'].variables)
        nc.close()

        h = cfdm.read(grouped_file, verbose=1)
        self.assertEqual(len(h), 1, repr(h))
        self.assertTrue(f.equals(h[0], verbose=2))

        # ------------------------------------------------------------
        # Move constructs one by one to the /forecast group. The order
        # in which we do this matters!
        # ------------------------------------------------------------
        for name in (
                'longitude',  # Auxiliary coordinate
                'latitude',  # Auxiliary coordinate
                'long_name=Grid latitude name',  # Auxiliary coordinate
                'measure:area',  # Cell measure
                'surface_altitude',  # Domain ancillary
                'air_temperature standard_error',  # Field ancillary
                'grid_mapping_name:rotated_latitude_longitude',
                'time',  # Dimension coordinate
                'grid_latitude',  # Dimension coordinate
        ):
            g.construct(name).nc_set_variable_groups(['forecast'])
            cfdm.write(g, grouped_file, verbose=1)

            # Check that the variable is in the right group
            nc = netCDF4.Dataset(grouped_file, 'r')
            self.assertIn(
                f.construct(name).nc_get_variable(),
                nc.groups['forecast'].variables)
            nc.close()

            # Check that the field construct hasn't changed
            h = cfdm.read(grouped_file, verbose=1)
            self.assertEqual(len(h), 1, repr(h))
            self.assertTrue(f.equals(h[0], verbose=2), name)

        # ------------------------------------------------------------
        # Move bounds to the /forecast group
        # ------------------------------------------------------------
        name = 'grid_latitude'
        g.construct(name).bounds.nc_set_variable_groups(['forecast'])
        cfdm.write(g, grouped_file)

        nc = netCDF4.Dataset(grouped_file, 'r')
        self.assertIn(
            f.construct(name).bounds.nc_get_variable(),
            nc.groups['forecast'].variables)
        nc.close()

        h = cfdm.read(grouped_file, verbose='WARNING')
        self.assertEqual(len(h), 1, repr(h))
        self.assertTrue(f.equals(h[0], verbose=2))