Пример #1
0
 def setUp(self):
     self.values = np.array((10., 12., 16., 9.))
     self.measure = CellMeasure(self.values,
                                units='m^2',
                                standard_name='cell_area',
                                long_name='measured_area',
                                var_name='area',
                                attributes={'notes': '1m accuracy'},
                                measure='area')
Пример #2
0
 def setUp(self):
     self.values = np.array((10.0, 12.0, 16.0, 9.0))
     self.measure = CellMeasure(
         self.values,
         units="m^2",
         standard_name="cell_area",
         long_name="measured_area",
         var_name="area",
         attributes={"notes": "1m accuracy"},
     )
Пример #3
0
    def test(self):
        # Check that "coord.cube_dims(cube)" calls "cube.coord_dims(coord)".
        mock_dims_result = mock.sentinel.CM_DIMS
        mock_dims_call = mock.Mock(return_value=mock_dims_result)
        mock_cube = mock.Mock(Cube, cell_measure_dims=mock_dims_call)
        test_cm = CellMeasure([1], long_name="test_name")

        result = test_cm.cube_dims(mock_cube)
        self.assertEqual(result, mock_dims_result)
        self.assertEqual(mock_dims_call.call_args_list, [mock.call(test_cm)])
Пример #4
0
    def test_section_scalar_cell_measures(self):
        cube = Cube(np.zeros((2, 3)), long_name="name", units=1)
        cube.add_cell_measure(CellMeasure([0], long_name="cm"))

        rep = cube_replines(cube)
        expected = [
            "name / (1)                          (-- : 2; -- : 3)",
            "    Scalar cell measures:",
            "        cm",
        ]
        self.assertEqual(rep, expected)
Пример #5
0
    def test_cell_measure(self):
        cube = self.cube
        cell_measure = CellMeasure([1, 2, 3], long_name="foo")
        cube.add_cell_measure(cell_measure, 0)
        rep = iris._representation.CubeSummary(cube)

        cm_section = rep.vector_sections["Cell measures:"]
        self.assertEqual(len(cm_section.contents), 1)

        cm_summary = cm_section.contents[0]
        self.assertEqual(cm_summary.name, "foo")
        self.assertEqual(cm_summary.dim_chars, ["x", "-"])
Пример #6
0
    def test_cell_measures(self):
        # Note: using a CDL string as a test data reference, rather than a binary file.
        ref_cdl = """
            netcdf cm_attr {
            dimensions:
                axv = 3 ;
                ayv = 2 ;
            variables:
                int64 qqv(ayv, axv) ;
                    qqv:long_name = "qq" ;
                    qqv:units = "1" ;
                    qqv:cell_measures = "area: my_areas" ;
                int64 ayv(ayv) ;
                    ayv:units = "1" ;
                    ayv:long_name = "y" ;
                int64 axv(axv) ;
                    axv:units = "1" ;
                    axv:long_name = "x" ;
                double my_areas(ayv, axv) ;
                    my_areas:units = "m2" ;
                    my_areas:long_name = "standardised cell areas" ;
                    my_areas:custom = "extra-attribute";
            data:
                axv = 11, 12, 13;
                ayv = 21, 22;
                my_areas = 110., 120., 130., 221., 231., 241.;
            }
            """
        nc_path = cdl_to_nc(ref_cdl)

        # Load with iris.fileformats.netcdf.load_cubes, and check expected content.
        cubes = list(load_cubes(nc_path))
        self.assertEqual(len(cubes), 1)
        cms = cubes[0].cell_measures()
        self.assertEqual(len(cms), 1)
        expected = CellMeasure(
            np.ma.array([[110.0, 120.0, 130.0], [221.0, 231.0, 241.0]]),
            measure="area",
            var_name="my_areas",
            long_name="standardised cell areas",
            units="m2",
            attributes={"custom": "extra-attribute"},
        )
        self.assertEqual(cms[0], expected)
Пример #7
0
 def test_cellmeasure(self):
     # Check we can print an AncillaryVariable
     # N.B. practically, identical to an AuxCoord (without bounds)
     # Check we can print an AncillaryVariable
     # Practically, ~identical to an AuxCoord, but without bounds, and the
     # array is called 'data'.
     data = self.sample_data()
     cell_measure = CellMeasure(data,
                                measure="area",
                                long_name="cell_area",
                                units="m^2")
     result = self.repr_str_strings(cell_measure)
     expected = [
         "<CellMeasure: cell_area / (m^2)  [0., 1., 2., 3., 4.]  shape(5,)>",
         "CellMeasure :  cell_area / (m^2)",
         "    data: [0., 1., 2., 3., 4.]",
         "    shape: (5,)",
         "    dtype: float64",
         "    long_name: 'cell_area'",
         "    measure: 'area'",
     ]
     self.assertLines(expected, result)
Пример #8
0
def _make_cube(x,
               y,
               data,
               aux=None,
               cell_measure=None,
               ancil=None,
               offset=0,
               scalar=None):
    """
    A convenience test function that creates a custom 2D cube.

    Args:

    * x:
        A (start, stop, step) tuple for specifying the
        x-axis dimensional coordinate points. Bounds are
        automatically guessed.

    * y:
        A (start, stop, step) tuple for specifying the
        y-axis dimensional coordinate points. Bounds are
        automatically guessed.

    * data:
        The data payload for the cube.

    Kwargs:

    * aux:
        A CSV string specifying which points only auxiliary
        coordinates to create. Accepts either of 'x', 'y', 'xy'.

    * offset:
        Offset value to be added to the 'xy' auxiliary coordinate
        points.

    * scalar:
        Create a 'height' scalar coordinate with the given value.

    Returns:
        The newly created 2D :class:`iris.cube.Cube`.

    """
    x_range = np.arange(*x, dtype=np.float32)
    y_range = np.arange(*y, dtype=np.float32)
    x_size = len(x_range)
    y_size = len(y_range)

    cube_data = np.empty((y_size, x_size), dtype=np.float32)
    cube_data[:] = data
    cube = iris.cube.Cube(cube_data)
    coord = DimCoord(y_range, long_name="y", units="1")
    coord.guess_bounds()
    cube.add_dim_coord(coord, 0)
    coord = DimCoord(x_range, long_name="x", units="1")
    coord.guess_bounds()
    cube.add_dim_coord(coord, 1)

    if aux is not None:
        aux = aux.split(",")
        if "y" in aux:
            coord = AuxCoord(y_range * 10, long_name="y-aux", units="1")
            cube.add_aux_coord(coord, (0, ))
        if "x" in aux:
            coord = AuxCoord(x_range * 10, long_name="x-aux", units="1")
            cube.add_aux_coord(coord, (1, ))
        if "xy" in aux:
            payload = np.arange(y_size * x_size,
                                dtype=np.float32).reshape(y_size, x_size)
            coord = AuxCoord(payload * 100 + offset,
                             long_name="xy-aux",
                             units="1")
            cube.add_aux_coord(coord, (0, 1))

    if cell_measure is not None:
        cell_measure = cell_measure.split(",")
        if "y" in cell_measure:
            cm = CellMeasure(y_range * 10, long_name="y-aux", units="1")
            cube.add_cell_measure(cm, (0, ))
        if "x" in cell_measure:
            cm = CellMeasure(x_range * 10, long_name="x-aux", units="1")
            cube.add_cell_measure(cm, (1, ))
        if "xy" in cell_measure:
            payload = x_range + y_range[:, np.newaxis]
            cm = CellMeasure(payload * 100 + offset,
                             long_name="xy-aux",
                             units="1")
            cube.add_cell_measure(cm, (0, 1))

    if ancil is not None:
        ancil = ancil.split(",")
        if "y" in ancil:
            av = AncillaryVariable(y_range * 10, long_name="y-aux", units="1")
            cube.add_ancillary_variable(av, (0, ))
        if "x" in ancil:
            av = AncillaryVariable(x_range * 10, long_name="x-aux", units="1")
            cube.add_ancillary_variable(av, (1, ))
        if "xy" in ancil:
            payload = x_range + y_range[:, np.newaxis]
            av = AncillaryVariable(payload * 100 + offset,
                                   long_name="xy-aux",
                                   units="1")
            cube.add_ancillary_variable(av, (0, 1))

    if scalar is not None:
        data = np.array([scalar], dtype=np.float32)
        coord = AuxCoord(data, long_name="height", units="m")
        cube.add_aux_coord(coord, ())

    return cube