def _testcube_latlon_1d(self, lats, lons):
     cube = Cube(np.zeros(len(lons)))
     co_x = AuxCoord(lons, standard_name='longitude', units='degrees')
     co_y = AuxCoord(lats, standard_name='latitude', units='degrees')
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     return cube
Exemplo n.º 2
0
    def setUp(self):
        nt = 10
        data = np.arange(nt, dtype=np.float32)
        cube = Cube(data, standard_name='air_temperature', units='K')
        # Temporal coordinate.
        t_units = Unit('hours since 1970-01-01 00:00:00', calendar='gregorian')
        t_coord = DimCoord(points=np.arange(nt),
                           standard_name='time',
                           units=t_units)
        cube.add_dim_coord(t_coord, 0)

        # Increasing 1D time-series cube.
        self.series_inc_cube = cube
        self.series_inc = CubeSignature(self.series_inc_cube)

        # Decreasing 1D time-series cube.
        self.series_dec_cube = self.series_inc_cube.copy()
        self.series_dec_cube.remove_coord('time')
        t_tmp = DimCoord(points=t_coord.points[::-1],
                         standard_name='time',
                         units=t_units)
        self.series_dec_cube.add_dim_coord(t_tmp, 0)
        self.series_dec = CubeSignature(self.series_dec_cube)

        # Scalar 0D time-series cube with scalar time coordinate.
        cube = Cube(0, standard_name='air_temperature', units='K')
        cube.add_aux_coord(DimCoord(points=nt,
                                    standard_name='time',
                                    units=t_units))
        self.scalar_cube = cube
Exemplo n.º 3
0
class Test_rolling_window(tests.IrisTest):
    def setUp(self):
        self.cube = Cube(np.arange(6))
        val_coord = DimCoord([0, 1, 2, 3, 4, 5], long_name="val")
        month_coord = AuxCoord(['jan', 'feb', 'mar', 'apr', 'may', 'jun'],
                               long_name='month')
        self.cube.add_dim_coord(val_coord, 0)
        self.cube.add_aux_coord(month_coord, 0)
        self.mock_agg = mock.Mock(spec=Aggregator)
        self.mock_agg.aggregate = mock.Mock(
            return_value=np.empty([4]))

    def test_string_coord(self):
        # Rolling window on a cube that contains a string coordinate.
        res_cube = self.cube.rolling_window('val', self.mock_agg, 3)
        val_coord = DimCoord(np.array([1, 2, 3, 4]),
                             bounds=np.array([[0, 2], [1, 3], [2, 4], [3, 5]]),
                             long_name='val')
        month_coord = AuxCoord(
            np.array(['jan|feb|mar', 'feb|mar|apr', 'mar|apr|may',
                      'apr|may|jun']),
            bounds=np.array([['jan', 'mar'], ['feb', 'apr'],
                             ['mar', 'may'], ['apr', 'jun']]),
            long_name='month')
        self.assertEqual(res_cube.coord('val'), val_coord)
        self.assertEqual(res_cube.coord('month'), month_coord)
Exemplo n.º 4
0
def create_cube(lon_min, lon_max, bounds=False):
    n_lons = max(lon_min, lon_max) - min(lon_max, lon_min)
    data = np.arange(4 * 3 * n_lons, dtype='f4').reshape(4, 3, n_lons)
    data = biggus.NumpyArrayAdapter(data)
    cube = Cube(data, standard_name='x_wind', units='ms-1')
    cube.add_dim_coord(iris.coords.DimCoord([0, 20, 40, 80],
                                            long_name='level_height',
                                            units='m'), 0)
    cube.add_aux_coord(iris.coords.AuxCoord([1.0, 0.9, 0.8, 0.6],
                                            long_name='sigma'), 0)
    cube.add_dim_coord(iris.coords.DimCoord([-45, 0, 45], 'latitude',
                                            units='degrees'), 1)
    step = 1 if lon_max > lon_min else -1
    circular = (abs(lon_max - lon_min) == 360)
    cube.add_dim_coord(iris.coords.DimCoord(np.arange(lon_min, lon_max, step),
                                            'longitude', units='degrees',
                                            circular=circular), 2)
    if bounds:
        cube.coord('longitude').guess_bounds()
    cube.add_aux_coord(iris.coords.AuxCoord(
        np.arange(3 * n_lons).reshape(3, n_lons) * 10, 'surface_altitude',
        units='m'), [1, 2])
    cube.add_aux_factory(iris.aux_factory.HybridHeightFactory(
        cube.coord('level_height'), cube.coord('sigma'),
        cube.coord('surface_altitude')))
    return cube
Exemplo n.º 5
0
class Test_merge_cube(tests.IrisTest):
    def setUp(self):
        self.cube1 = Cube([1, 2, 3], "air_temperature", units="K")
        self.cube1.add_aux_coord(AuxCoord([0], "height", units="m"))

    def test_pass(self):
        cube2 = self.cube1.copy()
        cube2.coord("height").points = [1]
        result = CubeList([self.cube1, cube2]).merge_cube()
        self.assertIsInstance(result, Cube)

    def test_fail(self):
        cube2 = self.cube1.copy()
        cube2.rename("not air temperature")
        with self.assertRaises(iris.exceptions.MergeError):
            CubeList([self.cube1, cube2]).merge_cube()

    def test_empty(self):
        with self.assertRaises(ValueError):
            CubeList([]).merge_cube()

    def test_single_cube(self):
        result = CubeList([self.cube1]).merge_cube()
        self.assertEqual(result, self.cube1)
        self.assertIsNot(result, self.cube1)

    def test_repeated_cube(self):
        with self.assertRaises(iris.exceptions.MergeError):
            CubeList([self.cube1, self.cube1]).merge_cube()
Exemplo n.º 6
0
 def test_cell_datetime_objects(self):
     # Check the scalar coordinate summary still works even when
     # iris.FUTURE.cell_datetime_objects is True.
     cube = Cube(0)
     cube.add_aux_coord(AuxCoord(42, units='hours since epoch'))
     with FUTURE.context(cell_datetime_objects=True):
         summary = cube.summary()
     self.assertIn('1970-01-02 18:00:00', summary)
Exemplo n.º 7
0
def unrolled_cube():
    data = np.arange(5, dtype='f4')
    cube = Cube(data)
    cube.add_aux_coord(iris.coords.AuxCoord([5.0, 10.0, 8.0, 5.0, 3.0],
                                            'longitude', units='degrees'), 0)
    cube.add_aux_coord(iris.coords.AuxCoord([1.0, 3.0, -2.0, -1.0, -4.0],
                                            'latitude'), 0)
    return cube
Exemplo n.º 8
0
 def test_time_mean_from_forecast_reference_time(self):
     cube = Cube(np.zeros((3, 4)))
     cube.add_aux_coord(AuxCoord(standard_name="forecast_reference_time", units="hours since epoch", points=72))
     cube.add_aux_coord(
         AuxCoord(standard_name="time", units="hours since epoch", points=72 + 36, bounds=[72 + 24, 72 + 48])
     )
     field = self.convert_cube_to_field(cube)
     self.assertEqual(field.lbft, 48)
Exemplo n.º 9
0
 def test_cell_datetime_objects(self):
     # Check the scalar coordinate summary still works even when
     # iris.FUTURE.cell_datetime_objects is True.
     cube = Cube(0)
     cube.add_aux_coord(AuxCoord(42, units='hours since epoch'))
     with FUTURE.context(cell_datetime_objects=True):
         summary = cube.summary()
     self.assertIn('1970-01-02 18:00:00', summary)
Exemplo n.º 10
0
 def test_multi_dimensional(self):
     time = AuxCoord(np.arange(12).reshape(3, 4), 'time',
                     units='hours since 2013-10-29 18:00:00')
     cube = Cube(np.arange(12).reshape(3, 4))
     cube.add_aux_coord(time, (0, 1))
     constraint = TimeConstraint(hour=12)
     with self.assertRaises(CoordinateMultiDimError):
         sub_cube = constraint.extract(cube)
Exemplo n.º 11
0
def _gridlike_mesh_cube(n_lons, n_lats, location="face"):
    mesh = _gridlike_mesh(n_lons, n_lats)
    mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords(location)
    data = np.zeros_like(mesh_coord_x.points)
    cube = Cube(data)
    cube.add_aux_coord(mesh_coord_x, 0)
    cube.add_aux_coord(mesh_coord_y, 0)
    return cube
Exemplo n.º 12
0
def get_cube(name, lat=((0.5, 1.5), (2.5, 3.5)), lon=((0.5, 1.5), (2.5, 3.5))):
    lat = np.array(lat)
    lon = np.array(lon)
    lat_bounds = np.array((lat - 0.5, lat + 0.5))
    lon_bounds = np.array((lon - 0.5, lon + 0.5))
    cube = Cube(np.ones((2, 2, 2)), name)
    cube.add_aux_coord(AuxCoord(lat, 'latitude', bounds=lat_bounds), (1, 2))
    cube.add_aux_coord(AuxCoord(lon, 'longitude', bounds=lon_bounds), (1, 2))
    return cube
Exemplo n.º 13
0
 def test_multi_dimensional(self):
     time = AuxCoord(np.arange(12).reshape(3, 4),
                     'time',
                     units='hours since 2013-10-29 18:00:00')
     cube = Cube(np.arange(12).reshape(3, 4))
     cube.add_aux_coord(time, (0, 1))
     constraint = TimeConstraint(hour=12)
     with self.assertRaises(CoordinateMultiDimError):
         sub_cube = constraint.extract(cube)
Exemplo n.º 14
0
 def test_scalar_cube_coord_match(self):
     # Ensure that extract is able to extract a scalar cube according to
     # constrained scalar coordinate.
     constraint = iris.Constraint(scalar_coord=0)
     cube = Cube(1, long_name='a1')
     coord = iris.coords.AuxCoord(0, long_name='scalar_coord')
     cube.add_aux_coord(coord, None)
     res = cube.extract(constraint)
     self.assertIs(res, cube)
 def test_nonlatlon_simple_1d(self):
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0], long_name='x')
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0], long_name='y')
     cube = Cube(np.zeros(6))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [('x', 2.8), ('y', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5, )])
Exemplo n.º 16
0
 def test_scalar_cube_coord_nomatch(self):
     # Ensure that extract is not extracting a scalar cube with scalar
     # coordinate that does not match the constraint.
     constraint = iris.Constraint(scalar_coord=1)
     cube = Cube(1, long_name='a1')
     coord = iris.coords.AuxCoord(0, long_name='scalar_coord')
     cube.add_aux_coord(coord, None)
     res = cube.extract(constraint)
     self.assertIs(res, None)
 def test_nonlatlon_simple_1d(self):
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0], long_name='x')
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0], long_name='y')
     cube = Cube(np.zeros(6))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [('x', 2.8), ('y', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5,)])
Exemplo n.º 18
0
    def setUp(self):
        # Basic test values.
        src_x_y_value = np.array([
            [20.12, 11.73, 0.01],
            [120.23, -20.73, 1.12],
            [290.34, 33.88, 2.23],
            [-310.45, 57.8, 3.34],
        ])
        tgt_grid_x = np.array([-173.2, -100.3, -32.5, 1.4, 46.6, 150.7])
        tgt_grid_y = np.array([-80.1, -30.2, 0.3, 47.4, 75.5])

        # Make sample 1-D source cube.
        src = Cube(src_x_y_value[:, 2])
        src.add_aux_coord(
            AuxCoord(src_x_y_value[:, 0],
                     standard_name="longitude",
                     units="degrees"),
            0,
        )
        src.add_aux_coord(
            AuxCoord(src_x_y_value[:, 1],
                     standard_name="latitude",
                     units="degrees"),
            0,
        )
        self.src_cube = src

        # Make sample grid cube.
        grid = Cube(np.zeros(tgt_grid_y.shape + tgt_grid_x.shape))
        grid.add_dim_coord(
            DimCoord(tgt_grid_y, standard_name="latitude", units="degrees"), 0)
        grid.add_dim_coord(
            DimCoord(tgt_grid_x, standard_name="longitude", units="degrees"),
            1)
        self.grid_cube = grid

        # Make expected-result, from the expected source-index at each point.
        expected_result_indices = np.array([
            [1, 1, 1, 1, 1, 1],
            [1, 2, 0, 0, 0, 1],
            [1, 2, 2, 0, 0, 1],
            [3, 2, 2, 3, 3, 3],
            [3, 2, 3, 3, 3, 3],
        ])
        self.expected_data = self.src_cube.data[expected_result_indices]

        # Make a 3D source cube, based on the existing 2d test data.
        z_cubes = [src.copy() for _ in range(3)]
        for i_z, z_cube in enumerate(z_cubes):
            z_cube.add_aux_coord(DimCoord([i_z], long_name="z"))
            z_cube.data = z_cube.data + 100.0 * i_z
        self.src_z_cube = CubeList(z_cubes).merge_cube()

        # Make a corresponding 3d expected result.
        self.expected_data_zxy = self.src_z_cube.data[:,
                                                      expected_result_indices]
Exemplo n.º 19
0
 def _sample_wide_cube(self):
     cube = Cube([0, 1])
     cube.add_aux_coord(
         AuxCoord(
             [0, 1],
             long_name="long long long long long long long long name",
         ),
         0,
     )
     return cube
Exemplo n.º 20
0
    def tgt_cube(self):
        from esmf_regrid.tests.unit.experimental.unstructured_scheme.test__mesh_to_MeshInfo import (
            _gridlike_mesh, )

        tgt = Cube(np.ones([self.target_grid_size * self.target_grid_size]))
        mesh = _gridlike_mesh(self.target_grid_size, self.target_grid_size)
        mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords("face")
        tgt.add_aux_coord(mesh_coord_x, 0)
        tgt.add_aux_coord(mesh_coord_y, 0)
        return tgt
Exemplo n.º 21
0
def unrolled_cube():
    data = np.arange(5, dtype='f4')
    cube = Cube(data)
    cube.add_aux_coord(
        iris.coords.AuxCoord([5.0, 10.0, 8.0, 5.0, 3.0],
                             'longitude',
                             units='degrees'), 0)
    cube.add_aux_coord(
        iris.coords.AuxCoord([1.0, 3.0, -2.0, -1.0, -4.0], 'latitude'), 0)
    return cube
Exemplo n.º 22
0
 def test_time_mean_from_forecast_reference_time(self):
     cube = Cube(np.zeros((3, 4)))
     cube.add_aux_coord(AuxCoord(standard_name='forecast_reference_time',
                                 units='hours since epoch',
                                 points=72))
     cube.add_aux_coord(AuxCoord(standard_name='time',
                                 units='hours since epoch',
                                 points=72 + 36, bounds=[72 + 24, 72 + 48]))
     field = self.convert_cube_to_field(cube)
     self.assertEqual(field.lbft, 48)
Exemplo n.º 23
0
    def reconstructedField(self, neofs):
        """Reconstructed data field based on a subset of EOFs.

        If weights were passed to the `Eof` instance the returned
        reconstructed field will automatically have this weighting
        removed. Otherwise the returned field will have the same
        weighting as the `Eof` input *dataset*.

        Returns the reconstructed field in a `~iris.cube.Cube`.

        **Argument:**

        *neofs*
            Number of EOFs to use for the reconstruction.
            Alternatively this argument can be an iterable of mode
            numbers (where the first mode is 1) in order to facilitate
            reconstruction with arbitrary modes.

        **Returns:**

        *reconstruction*
            A `~iris.cube.Cube` with the same dimensions `Eof` input
            *dataset* containing the reconstruction using *neofs* EOFs.

        **Example:**

        Reconstruct the input field using 3 EOFs::

            reconstruction = solver.reconstructedField(3)

        Reconstruct the input field using EOFs 1, 2 and 5::

            reconstruction = solver.reconstuctedField([1, 2, 5])

        """
        rfield = self._solver.reconstructedField(neofs)
        coords = [copy(self._time)] + [copy(coord) for coord in self._coords]
        if isinstance(neofs, collections.Iterable):
            name_part = 'EOFs_{}'.format('_'.join([str(e) for e in neofs]))
        else:
            name_part = '{}_EOFs'.format(neofs)
        rfield = Cube(
            rfield,
            dim_coords_and_dims=list(zip(coords, list(range(rfield.ndim)))),
            var_name=self._cube_var_name or 'dataset',
            long_name='{:s}_reconstructed_with_{:s}'.format(
                self._cube_name, name_part))
        rfield.attributes.update({'neofs': neofs})
        # Add any auxiliary coordinates to the returned cube.
        for coord, dims in (self._time_aux_coords +
                            self._space_aux_coords +
                            self._time_space_aux_coords):
            rfield.add_aux_coord(copy(coord), dims)
        return rfield
 def test_latlon_simple_1d(self):
     cube = Cube([11.0, 12.0, 13.0, 21.0, 22.0, 23.0])
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0],
                     standard_name='longitude', units='degrees')
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0],
                     standard_name='latitude', units='degrees')
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [('longitude', 2.8), ('latitude', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5,)])
 def test_sample_dictionary(self):
     # Pass sample_point arg as a dictionary: this usage mode is deprecated.
     co_x = AuxCoord([1.0, 2.0, 3.0], long_name="x")
     co_y = AuxCoord([10.0, 20.0], long_name="y")
     cube = Cube(np.zeros((2, 3)))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 1)
     sample_point = {"x": 2.8, "y": 18.5}
     exp_emsg = r"must be a list of \(coordinate, value\) pairs"
     with self.assertRaisesRegex(TypeError, exp_emsg):
         nn_ndinds(cube, sample_point)
Exemplo n.º 26
0
    def reconstructedField(self, neofs):
        """Reconstructed data field based on a subset of EOFs.

        If weights were passed to the `Eof` instance the returned
        reconstructed field will automatically have this weighting
        removed. Otherwise the returned field will have the same
        weighting as the `Eof` input *dataset*.

        Returns the reconstructed field in a `~iris.cube.Cube`.

        **Argument:**

        *neofs*
            Number of EOFs to use for the reconstruction.
            Alternatively this argument can be an iterable of mode
            numbers (where the first mode is 1) in order to facilitate
            reconstruction with arbitrary modes.

        **Returns:**

        *reconstruction*
            A `~iris.cube.Cube` with the same dimensions `Eof` input
            *dataset* containing the reconstruction using *neofs* EOFs.

        **Example:**

        Reconstruct the input field using 3 EOFs::

            reconstruction = solver.reconstructedField(3)

        Reconstruct the input field using EOFs 1, 2 and 5::

            reconstruction = solver.reconstuctedField([1, 2, 5])

        """
        rfield = self._solver.reconstructedField(neofs)
        coords = [copy(self._time)] + [copy(coord) for coord in self._coords]
        if isinstance(neofs, collections.Iterable):
            name_part = 'EOFs_{}'.format('_'.join([str(e) for e in neofs]))
        else:
            name_part = '{}_EOFs'.format(neofs)
        rfield = Cube(
            rfield,
            dim_coords_and_dims=list(zip(coords, list(range(rfield.ndim)))),
            var_name=self._cube_var_name or 'dataset',
            long_name='{:s}_reconstructed_with_{:s}'.format(
                self._cube_name, name_part))
        rfield.attributes.update({'neofs': neofs})
        # Add any auxiliary coordinates to the returned cube.
        for coord, dims in (self._time_aux_coords +
                            self._space_aux_coords +
                            self._time_space_aux_coords):
            rfield.add_aux_coord(copy(coord), dims)
        return rfield
Exemplo n.º 27
0
def simple_3d_w_multidim_coords(with_bounds=True):
    """
    Returns an abstract, two-dimensional, optionally bounded, cube.

    >>> print simple_3d_w_multidim_coords()
    thingness                           (wibble: 2; *ANONYMOUS*: 3; *ANONYMOUS*: 4)
         Dimension coordinates:
              wibble                           x               -               -
         Auxiliary coordinates:
              bar                              -               x               x
              foo                              -               x               x

    >>> print simple_3d_w_multidim_coords().data
    [[[ 0  1  2  3]
      [ 4  5  6  7]
      [ 8  9 10 11]]

     [[12 13 14 15]
      [16 17 18 19]
      [20 21 22 23]]]

    """
    cube = Cube(np.arange(24, dtype=np.int32).reshape((2, 3, 4)))
    cube.long_name = 'thingness'
    cube.units = '1'

    y_points = np.array([[2.5, 7.5, 12.5, 17.5], [10., 17.5, 27.5, 42.5],
                         [15., 22.5, 32.5, 50.]])
    y_bounds = np.array([[[0, 5], [5, 10], [10, 15], [15, 20]],
                         [[5, 15], [15, 20], [20, 35], [35, 50]],
                         [[10, 20], [20, 25], [25, 40], [40, 60]]],
                        dtype=np.int32)
    y_coord = iris.coords.AuxCoord(points=y_points,
                                   long_name='bar',
                                   units='1',
                                   bounds=y_bounds if with_bounds else None)
    x_points = np.array([[-7.5, 7.5, 22.5, 37.5], [-12.5, 4., 26.5, 47.5],
                         [2.5, 14., 36.5, 44.]])
    x_bounds = np.array([[[-15, 0], [0, 15], [15, 30], [30, 45]],
                         [[-25, 0], [0, 8], [8, 45], [45, 50]],
                         [[-5, 10], [10, 18], [18, 55], [18, 70]]],
                        dtype=np.int32)
    x_coord = iris.coords.AuxCoord(points=x_points,
                                   long_name='foo',
                                   units='1',
                                   bounds=x_bounds if with_bounds else None)
    wibble_coord = iris.coords.DimCoord(np.array([10., 30.], dtype=np.float32),
                                        long_name='wibble',
                                        units='1')

    cube.add_dim_coord(wibble_coord, [0])
    cube.add_aux_coord(y_coord, [1, 2])
    cube.add_aux_coord(x_coord, [1, 2])
    return cube
 def test_sample_dictionary(self):
     # Pass sample_point arg as a dictionary: this usage mode is deprecated.
     co_x = AuxCoord([1.0, 2.0, 3.0], long_name='x')
     co_y = AuxCoord([10.0, 20.0], long_name='y')
     cube = Cube(np.zeros((2, 3)))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 1)
     sample_point = {'x': 2.8, 'y': 18.5}
     exp_emsg = 'must be a list of \(coordinate, value\) pairs'
     with self.assertRaisesRegexp(TypeError, exp_emsg):
         nn_ndinds(cube, sample_point)
Exemplo n.º 29
0
def simple_3d_w_multidim_coords(with_bounds=True):
    """
    Returns an abstract, two-dimensional, optionally bounded, cube.

    >>> print simple_3d_w_multidim_coords()
    thingness                           (wibble: 2; *ANONYMOUS*: 3; *ANONYMOUS*: 4)
         Dimension coordinates:
              wibble                           x               -               -
         Auxiliary coordinates:
              bar                              -               x               x
              foo                              -               x               x

    >>> print simple_3d_w_multidim_coords().data
    [[[ 0  1  2  3]
      [ 4  5  6  7]
      [ 8  9 10 11]]

     [[12 13 14 15]
      [16 17 18 19]
      [20 21 22 23]]]

    """
    cube = Cube(np.arange(24, dtype=np.int32).reshape((2, 3, 4)))
    cube.long_name = 'thingness'
    cube.units = '1'

    y_points = np.array([[2.5, 7.5, 12.5, 17.5],
                         [10., 17.5, 27.5, 42.5],
                         [15., 22.5, 32.5, 50.]])
    y_bounds = np.array([[[0, 5], [5, 10], [10, 15], [15, 20]],
                         [[5, 15], [15, 20], [20, 35], [35, 50]],
                         [[10, 20], [20, 25], [25, 40], [40, 60]]],
                        dtype=np.int32)
    y_coord = iris.coords.AuxCoord(points=y_points, long_name='bar',
                                   units='1',
                                   bounds=y_bounds if with_bounds else None)
    x_points = np.array([[-7.5, 7.5, 22.5, 37.5],
                         [-12.5, 4., 26.5, 47.5],
                         [2.5, 14., 36.5, 44.]])
    x_bounds = np.array([[[-15, 0], [0, 15], [15, 30], [30, 45]],
                         [[-25, 0], [0, 8], [8, 45], [45, 50]],
                         [[-5, 10], [10, 18],  [18, 55], [18, 70]]],
                        dtype=np.int32)
    x_coord = iris.coords.AuxCoord(points=x_points, long_name='foo',
                                   units='1',
                                   bounds=x_bounds if with_bounds else None)
    wibble_coord = iris.coords.DimCoord(np.array([10., 30.],
                                                 dtype=np.float32),
                                        long_name='wibble', units='1')

    cube.add_dim_coord(wibble_coord, [0])
    cube.add_aux_coord(y_coord, [1, 2])
    cube.add_aux_coord(x_coord, [1, 2])
    return cube
Exemplo n.º 30
0
 def test_latlon_simple_1d(self):
     cube = Cube([11.0, 12.0, 13.0, 21.0, 22.0, 23.0])
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0],
                     standard_name='longitude', units='degrees')
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0],
                     standard_name='latitude', units='degrees')
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [('longitude', 2.8), ('latitude', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5,)])
Exemplo n.º 31
0
def ocean_sigma_z():
    """
    Return a sample cube with an
    :class:`iris.aux_factory.OceanSigmaZFactory` vertical coordinate.

    This is a fairly small cube with real coordinate arrays.  The coordinate
    values are derived from the sample data linked at
    https://github.com/SciTools/iris/pull/509#issuecomment-23565381.

    """
    co_time = DimCoord([0.0, 1.0], standard_name='time', units='')
    co_lats = DimCoord([-58.1, -52.7, -46.9],
                       standard_name='latitude', units=Unit('degrees'))
    co_lons = DimCoord([65.1,   72.9,   83.7,  96.5],
                       standard_name='longitude', units=Unit('degrees'))
    co_ssh = AuxCoord([[[-0.63157895, -0.52631579, -0.42105263, -0.31578947],
                        [-0.78947368, -0.68421053, -0.57894737, -0.47368421],
                        [-0.94736842, -0.84210526, -0.73684211, -0.63157895]],
                       [[-0.84210526, -0.73684211, -0.63157895, -0.52631579],
                        [-1.00000000, -0.89473684, -0.78947368, -0.68421053],
                        [-1.15789474, -1.05263158, -0.94736842, -0.84210526]]],
                      standard_name=u'sea_surface_height', units=Unit('m'))

    co_sigma = AuxCoord([0., -0.1, -0.6, -1., -1.],
                        standard_name=u'ocean_sigma_z_coordinate',
                        units=Unit('1'),
                        attributes={'positive': 'up'})

    co_zlay = AuxCoord([-137.2, -137.3, -137.4, -368.4, -1495.6],
                       long_name='layer_depth', units=Unit('m'))
    co_depth = AuxCoord([[1625.7, 3921.2, 4106.4, 5243.5],
                         [3615.4, 4942.6, 3883.6, 4823.1],
                         [3263.2, 2816.3, 2741.8, 3883.6]],
                        standard_name=u'depth', units=Unit('m'))
    co_depthc = DimCoord(137.9, long_name='depth_c', units=Unit('m'))
    co_nsigma = DimCoord(3, long_name='nsigma')

    cube = Cube(np.zeros((2, 5, 3, 4)))
    cube.add_dim_coord(co_time, 0)
    cube.add_dim_coord(co_lats, 2)
    cube.add_dim_coord(co_lons, 3)
    cube.add_aux_coord(co_zlay, 1)
    cube.add_aux_coord(co_sigma, 1)
    cube.add_aux_coord(co_ssh, (0, 2, 3))
    cube.add_aux_coord(co_depth, (2, 3))
    cube.add_aux_coord(co_depthc)
    cube.add_aux_coord(co_nsigma)

    fact = iris.aux_factory.OceanSigmaZFactory(
        depth=co_depth, eta=co_ssh, depth_c=co_depthc, zlev=co_zlay,
        sigma=co_sigma, nsigma=co_nsigma)
    cube.add_aux_factory(fact)
    return cube
Exemplo n.º 32
0
    def _add_forecast_period(advected_cube: Cube, timestep: timedelta) -> None:
        """Add or replace a forecast period on the advected cube"""
        try:
            advected_cube.remove_coord("forecast_period")
        except CoordinateNotFoundError:
            pass

        forecast_period_seconds = np.int32(timestep.total_seconds())
        forecast_period_coord = AuxCoord(forecast_period_seconds,
                                         standard_name="forecast_period",
                                         units="seconds")
        advected_cube.add_aux_coord(forecast_period_coord)
 def test_multidimensional_xy(self):
     # Recast the 4-point source cube as 2*2 : should yield the same result.
     co_x = self.src_cube.coord(axis='x')
     co_y = self.src_cube.coord(axis='y')
     new_src = Cube(self.src_cube.data.reshape((2, 2)))
     new_x_co = AuxCoord(co_x.points.reshape((2, 2)),
                         standard_name='longitude', units='degrees')
     new_y_co = AuxCoord(co_y.points.reshape((2, 2)),
                         standard_name='latitude', units='degrees')
     new_src.add_aux_coord(new_x_co, (0, 1))
     new_src.add_aux_coord(new_y_co, (0, 1))
     self._check_expected(src_cube=new_src)
Exemplo n.º 34
0
def set_up_probability_above_threshold_spot_cube(data,
                                                 phenomenon_standard_name,
                                                 phenomenon_units,
                                                 forecast_thresholds=np.array(
                                                     [8, 10, 12]),
                                                 y_dimension_length=9,
                                                 x_dimension_length=9):
    """
    Create a cube containing multiple realizations, where one of the
    dimensions is an index used for spot forecasts.
    """
    cube_long_name = (
        "probability_of_{}_above_threshold".format(phenomenon_standard_name))
    cube = Cube(data, long_name=cube_long_name, units=1)

    try:
        cube.add_dim_coord(
            DimCoord(forecast_thresholds,
                     phenomenon_standard_name,
                     units=phenomenon_units,
                     var_name="threshold"), 0)
    except ValueError:
        cube.add_dim_coord(
            DimCoord(forecast_thresholds,
                     long_name=phenomenon_standard_name,
                     units=phenomenon_units,
                     var_name="threshold"), 0)

    time_origin = "hours since 1970-01-01 00:00:00"
    calendar = "gregorian"
    tunit = Unit(time_origin, calendar)
    cube.add_dim_coord(
        DimCoord(np.array([412227], dtype=np.float64), "time", units=tunit), 1)
    cube.add_dim_coord(
        DimCoord(np.arange(9, dtype=np.float32), long_name='locnum',
                 units="1"), 2)
    cube.add_aux_coord(AuxCoord(np.linspace(-45.0,
                                            45.0,
                                            y_dimension_length,
                                            dtype=np.float32),
                                'latitude',
                                units='degrees'),
                       data_dims=2)
    cube.add_aux_coord(AuxCoord(np.linspace(120,
                                            180,
                                            x_dimension_length,
                                            dtype=np.float32),
                                'longitude',
                                units='degrees'),
                       data_dims=2)
    cube.coord(var_name="threshold"
               ).attributes["spp__relative_to_threshold"] = "above"
    return cube
Exemplo n.º 35
0
class Test_aggregated_by(tests.IrisTest):
    def setUp(self):
        self.cube = Cube(np.arange(11))
        val_coord = AuxCoord([0, 0, 0, 1, 1, 2, 0, 0, 2, 0, 1],
                             long_name="val")
        label_coord = AuxCoord(['alpha', 'alpha', 'beta',
                                'beta', 'alpha', 'gamma',
                                'alpha', 'alpha', 'alpha',
                                'gamma', 'beta'],
                               long_name='label', units='no_unit')
        self.cube.add_aux_coord(val_coord, 0)
        self.cube.add_aux_coord(label_coord, 0)
        self.mock_agg = mock.Mock(spec=Aggregator)
        self.mock_agg.aggregate = mock.Mock(
            return_value=mock.Mock(dtype='object'))

    def test_string_coord_agg_by_label(self):
        # Aggregate a cube on a string coordinate label where label
        # and val entries are not in step; the resulting cube has a val
        # coord of bounded cells and a label coord of single string entries.
        res_cube = self.cube.aggregated_by('label', self.mock_agg)
        val_coord = AuxCoord(np.array([1., 0.5, 1.]),
                             bounds=np.array([[0, 2], [0, 1], [2, 0]]),
                             long_name='val')
        label_coord = AuxCoord(np.array(['alpha', 'beta', 'gamma']),
                               long_name='label', units='no_unit')
        self.assertEqual(res_cube.coord('val'), val_coord)
        self.assertEqual(res_cube.coord('label'), label_coord)

    def test_string_coord_agg_by_val(self):
        # Aggregate a cube on a numeric coordinate val where label
        # and val entries are not in step; the resulting cube has a label
        # coord with serialised labels from the aggregated cells.
        res_cube = self.cube.aggregated_by('val', self.mock_agg)
        val_coord = AuxCoord(np.array([0,  1,  2]), long_name='val')
        exp0 = 'alpha|alpha|beta|alpha|alpha|gamma'
        exp1 = 'beta|alpha|beta'
        exp2 = 'gamma|alpha'
        label_coord = AuxCoord(np.array((exp0, exp1, exp2)),
                               long_name='label', units='no_unit')
        self.assertEqual(res_cube.coord('val'), val_coord)
        self.assertEqual(res_cube.coord('label'), label_coord)

    def test_single_string_aggregation(self):
        aux_coords = [(AuxCoord(['a', 'b', 'a'], long_name='foo'), 0),
                      (AuxCoord(['a', 'a', 'a'], long_name='bar'), 0)]
        cube = iris.cube.Cube(np.arange(12).reshape(3, 4),
                              aux_coords_and_dims=aux_coords)
        result = cube.aggregated_by('foo', MEAN)
        self.assertEqual(result.shape, (2, 4))
        self.assertEqual(result.coord('bar'),
                         AuxCoord(['a|a', 'a'], long_name='bar'))
Exemplo n.º 36
0
    def create_difference_cube(cube, coord_name, diff_along_axis):
        """
        Put the difference array into a cube with the appropriate
        metadata.

        Parameters
        ----------
        cube : Iris.cube.Cube
            Cube from which the differences have been calculated.
        coord_name : String
            The name of the coordinate over which the difference
            have been calculated.
        diff_along_axis : numpy array
            Array containing the differences.

        Returns
        -------
        diff_cube : Iris.cube.Cube
            Cube containing the differences calculated along the
            specified axis.
        """
        points = cube.coord(coord_name).points
        mean_points = (points[1:] + points[:-1]) / 2

        # Copy cube metadata and coordinates into a new cube.
        # Create a new coordinate for the coordinate along which the
        # difference has been calculated.
        metadata_dict = copy.deepcopy(cube.metadata._asdict())
        diff_cube = Cube(diff_along_axis, **metadata_dict)

        for coord in cube.dim_coords:
            dims = cube.coord_dims(coord)
            if coord.name() in [coord_name]:
                coord = coord.copy(points=mean_points)
            diff_cube.add_dim_coord(coord.copy(), dims)
        for coord in cube.aux_coords:
            dims = cube.coord_dims(coord)
            diff_cube.add_aux_coord(coord.copy(), dims)
        for coord in cube.derived_coords:
            dims = cube.coord_dims(coord)
            diff_cube.add_aux_coord(coord.copy(), dims)

        # Add metadata to indicate that a difference has been calculated.
        # TODO: update this metadata when proper conventions have been
        #       agreed upon.
        cell_method = CellMethod("difference",
                                 coords=[coord_name],
                                 intervals='1 grid length')
        diff_cube.add_cell_method(cell_method)
        diff_cube.attributes["form_of_difference"] = ("forward_difference")
        diff_cube.rename('difference_of_' + cube.name())
        return diff_cube
Exemplo n.º 37
0
    def test_section_vector_auxcoords(self):
        cube = Cube(np.zeros((2, 3)), long_name="name", units=1)
        cube.add_aux_coord(DimCoord([0, 1], long_name="y"), 0)
        cube.add_aux_coord(DimCoord([0, 1, 2], long_name="x"), 1)

        rep = cube_replines(cube)
        expected = [
            "name / (1)                          (-- : 2; -- : 3)",
            "    Auxiliary coordinates:",
            "        y                               x       -",
            "        x                               -       x",
        ]
        self.assertEqual(rep, expected)
Exemplo n.º 38
0
 def _add_forecast_reference_time(input_time: Coord, advected_cube: Cube) -> None:
     """Add or replace a forecast reference time on the advected cube"""
     try:
         advected_cube.remove_coord("forecast_reference_time")
     except CoordinateNotFoundError:
         pass
     frt_coord_name = "forecast_reference_time"
     frt_coord_spec = TIME_COORDS[frt_coord_name]
     frt_coord = input_time.copy()
     frt_coord.rename(frt_coord_name)
     frt_coord.convert_units(frt_coord_spec.units)
     frt_coord.points = round_close(frt_coord.points, dtype=frt_coord_spec.dtype)
     advected_cube.add_aux_coord(frt_coord)
Exemplo n.º 39
0
    def test_no_bounds(self):
        t_coord = DimCoord(15, 'time', units='hours since epoch')
        cube = Cube(23)
        cube.add_aux_coord(t_coord)

        res = _missing_forecast_period(cube)
        expected_rt = datetime.datetime(1970, 1, 1, 15, 0)
        expected_rt_type = 3
        expected_fp = 0
        expected_fp_type = 1
        expected = (expected_rt, expected_rt_type, expected_fp,
                    expected_fp_type)
        self.assertEqual(res, expected)
Exemplo n.º 40
0
    def eofs(self, eofscaling=0, neofs=None):
        """Emipirical orthogonal functions (EOFs).

        **Optional arguments:**

        *eofscaling*
            Sets the scaling of the EOFs. The following values are
            accepted:

            * *0* : Un-scaled EOFs (default).
            * *1* : EOFs are divided by the square-root of their
              eigenvalues.
            * *2* : EOFs are multiplied by the square-root of their
              eigenvalues.

        *neofs*
            Number of EOFs to return. Defaults to all EOFs. If the
            number of EOFs requested is more than the number that are
            available, then all available EOFs will be returned.

        **Returns:**

        *eofs*
           A `~iris.cube.Cube` containing the ordered EOFs. The EOFs are
           numbered from 0 to *neofs* - 1.

        **Examples:**

        All EOFs with no scaling::

            eofs = solver.eofs()

        First 3 EOFs with scaling applied::

            eofs = solver.eofs(neofs=3, eofscaling=1)

        """
        eofs = self._solver.eofs(eofscaling, neofs)
        eofdim = DimCoord(list(range(eofs.shape[0])),
                          var_name='eof',
                          long_name='eof_number')
        coords = [eofdim] + [copy(coord) for coord in self._coords]
        eofs = Cube(
            eofs,
            dim_coords_and_dims=list(zip(coords, list(range(eofs.ndim)))),
            var_name='eofs',
            long_name='empirical_orthogonal_functions')
        # Add any auxiliary coordinates spanning space to the returned cube.
        for coord, dims in self._space_aux_coords:
            eofs.add_aux_coord(copy(coord), dims)
        return eofs
Exemplo n.º 41
0
    def eofsAsCorrelation(self, neofs=None):
        """
        Empirical orthogonal functions (EOFs) expressed as the
        correlation between the principal component time series (PCs)
        and the time series of the `Eof` input *dataset* at each grid
        point.

        .. note::

            These are not related to the EOFs computed from the
            correlation matrix.

        **Optional argument:**

        *neofs*
            Number of EOFs to return. Defaults to all EOFs. If the
            number of EOFs requested is more than the number that are
            available, then all available EOFs will be returned.

        **Returns:**

        *eofs*
           A `~iris.cube.Cube` containing the ordered EOFs. The EOFs are
           numbered from 0 to *neofs* - 1.

        **Examples:**

        All EOFs::

            eofs = solver.eofsAsCorrelation()

        The leading EOF::

            eof1 = solver.eofsAsCorrelation(neofs=1)

        """
        eofs = self._solver.eofsAsCorrelation(neofs)
        eofdim = DimCoord(list(range(eofs.shape[0])),
                          var_name='eof',
                          long_name='eof_number')
        coords = [eofdim] + [copy(coord) for coord in self._coords]
        eofs = Cube(
            eofs,
            dim_coords_and_dims=list(zip(coords, list(range(eofs.ndim)))),
            var_name='eofs',
            long_name='correlation_between_pcs_and_{:s}'.format(
                self._cube_name))
        # Add any auxiliary coordinates spanning space to the returned cube.
        for coord, dims in self._space_aux_coords:
            eofs.add_aux_coord(copy(coord), dims)
        return eofs
Exemplo n.º 42
0
    def pcs(self, pcscaling=0, npcs=None):
        """Principal component time series (PCs).

        **Optional arguments:**

        *pcscaling*
            Set the scaling of the retrieved PCs. The following
            values are accepted:

            * *0* : Un-scaled principal components (default).
            * *1* : Principal components are scaled to unit variance
              (divided by the square-root of their eigenvalue).
            * *2* : Principal components are multiplied by the
              square-root of their eigenvalue.

        *npcs*
            Number of PCs to retrieve. Defaults to all the PCs. If the
            number of requested PCs is more than the number that are
            available, then all available PCs will be returned.

        **Returns:**

        *pcs*
            A `~iris.cube.Cube` containing the ordered PCs. The PCs are
            numbered from 0 to *npcs* - 1.

        **Examples:**

        All un-scaled PCs::

            pcs = solver.pcs()

        First 3 PCs scaled to unit variance::

            pcs = solver.pcs(npcs=3, pcscaling=1)

        """
        pcs = self._solver.pcs(pcscaling, npcs)
        pcdim = DimCoord(list(range(pcs.shape[1])),
                         var_name='pc',
                         long_name='pc_number')
        coords = [copy(self._time), pcdim]
        pcs = Cube(
            pcs,
            dim_coords_and_dims=list(zip(coords, list(range(pcs.ndim)))),
            var_name='pcs',
            long_name='principal_components')
        # Add any auxiliary coords spanning time back to the returned cube.
        for coord, dims in self._time_aux_coords:
            pcs.add_aux_coord(copy(coord), dims)
        return pcs
Exemplo n.º 43
0
    def create_difference_cube(self, cube, coord_name, diff_along_axis):
        """
        Put the difference array into a cube with the appropriate
        metadata.

        Args:
            cube (iris.cube.Cube):
                Cube from which the differences have been calculated.
            coord_name (str):
                The name of the coordinate over which the difference
                have been calculated.
            diff_along_axis (numpy.ndarray):
                Array containing the differences.

        Returns:
            iris.cube.Cube:
                Cube containing the differences calculated along the
                specified axis.
        """
        points = cube.coord(coord_name).points
        mean_points = (points[1:] + points[:-1]) / 2

        # Copy cube metadata and coordinates into a new cube.
        # Create a new coordinate for the coordinate along which the
        # difference has been calculated.
        metadata_dict = copy.deepcopy(cube.metadata._asdict())
        diff_cube = Cube(diff_along_axis, **metadata_dict)

        for coord in cube.dim_coords:
            dims = cube.coord_dims(coord)
            if coord.name() in [coord_name]:
                coord = coord.copy(points=mean_points)
            diff_cube.add_dim_coord(coord.copy(), dims)
        for coord in cube.aux_coords:
            dims = cube.coord_dims(coord)
            diff_cube.add_aux_coord(coord.copy(), dims)
        for coord in cube.derived_coords:
            dims = cube.coord_dims(coord)
            diff_cube.add_aux_coord(coord.copy(), dims)

        # Add metadata to indicate that a difference has been calculated.
        # TODO: update metadata for difference and add metadata for gradient
        #       when proper conventions have been agreed upon.
        if not self.is_gradient:
            cell_method = CellMethod("difference",
                                     coords=[coord_name],
                                     intervals="1 grid length")
            diff_cube.add_cell_method(cell_method)
            diff_cube.attributes["form_of_difference"] = "forward_difference"
        diff_cube.rename("difference_of_" + cube.name())
        return diff_cube
Exemplo n.º 44
0
def _2d_multicells_testcube(cellsize_degrees=1.0):
    """
    Create a test cube with a grid of X and Y points, where each gridcell
    is independent (disjoint), arranged at an angle == the x-coord point.

    """
    # Setup np.linspace arguments to make the coordinate points.
    x0, x1, nx = -164, 164, 9
    y0, y1, ny = -75, 75, 7

    lats = np.linspace(y0, y1, ny, endpoint=True)
    lons_angles = np.linspace(x0, x1, nx, endpoint=True)
    x_pts_2d, y_pts_2d = np.meshgrid(lons_angles, lats)

    # Make gridcells rectangles surrounding these centrepoints, but also
    # tilted at various angles (= same as x-point lons, as that's easy).

    # Calculate centrepoint lons+lats : in radians, and shape (ny, nx, 1).
    xangs, yangs = np.deg2rad(x_pts_2d), np.deg2rad(y_pts_2d)
    xangs, yangs = [arr[..., None] for arr in (xangs, yangs)]
    # Program which corners are up+down on each gridcell axis.
    dx_corners = [[[-1, 1, 1, -1]]]
    dy_corners = [[[-1, -1, 1, 1]]]
    # Calculate the relative offsets in x+y at the 4 corners.
    x_ofs_2d = cellsize_degrees * np.cos(xangs) * dx_corners
    x_ofs_2d -= cellsize_degrees * np.sin(xangs) * dy_corners
    y_ofs_2d = cellsize_degrees * np.cos(xangs) * dy_corners
    y_ofs_2d += cellsize_degrees * np.sin(xangs) * dx_corners
    # Apply a latitude stretch to make correct angles on the globe.
    y_ofs_2d *= np.cos(yangs)
    # Make bounds arrays by adding the corner offsets to the centrepoints.
    x_bds_2d = x_pts_2d[..., None] + x_ofs_2d
    y_bds_2d = y_pts_2d[..., None] + y_ofs_2d

    # Create a cube with these points + bounds in its 'X' and 'Y' coords.
    co_x = AuxCoord(
        points=x_pts_2d,
        bounds=x_bds_2d,
        standard_name="longitude",
        units="degrees",
    )
    co_y = AuxCoord(
        points=y_pts_2d,
        bounds=y_bds_2d,
        standard_name="latitude",
        units="degrees",
    )
    cube = Cube(np.zeros((ny, nx)))
    cube.add_aux_coord(co_x, (0, 1))
    cube.add_aux_coord(co_y, (0, 1))
    return cube
Exemplo n.º 45
0
Arquivo: iris.py Projeto: zhe233/eofs
    def pcs(self, pcscaling=0, npcs=None):
        """Principal component time series (PCs).

        **Optional arguments:**

        *pcscaling*
            Set the scaling of the retrieved PCs. The following
            values are accepted:

            * *0* : Un-scaled PCs (default).
            * *1* : PCs are scaled to unit variance (divided by the
              square-root of their eigenvalue).
            * *2* : PCs are multiplied by the square-root of their
              eigenvalue.

        *npcs*
            Number of PCs to retrieve. Defaults to all the PCs. If the
            number of PCs requested is more than the number that are
            available, then all available PCs will be returned.

        **Returns:**

        *pcs*
            A `~iris.cube.Cube` containing the ordered PCs.

        **Examples:**

        All un-scaled PCs::

            pcs = solver.pcs()

        First 3 PCs scaled to unit variance::

            pcs = solver.pcs(npcs=3, pcscaling=1)

        """
        pcs = self._solver.pcs(pcscaling, npcs)
        pcdim = DimCoord(list(range(pcs.shape[1])),
                         var_name='pc',
                         long_name='pc_number')
        coords = [copy(self._time[0]), pcdim]
        pcs = Cube(
            pcs,
            dim_coords_and_dims=list(zip(coords, list(range(pcs.ndim)))),
            var_name='pcs',
            long_name='principal_components')
        # Add any AuxCoords that described the time dimension of all the input
        # cubes.
        for coord, dims in self._common_time_aux_coords:
            pcs.add_aux_coord(copy(coord), dims)
        return pcs
Exemplo n.º 46
0
    def eofsAsCorrelation(self, neofs=None):
        """
        Empirical orthogonal functions (EOFs) expressed as the
        correlation between the principal component time series (PCs)
        and the time series of the `Eof` input *dataset* at each grid
        point.

        .. note::

            These are not related to the EOFs computed from the
            correlation matrix.

        **Optional argument:**

        *neofs*
            Number of EOFs to return. Defaults to all EOFs. If the
            number of EOFs requested is more than the number that are
            available, then all available EOFs will be returned.

        **Returns:**

        *eofs*
           A `~iris.cube.Cube` containing the ordered EOFs. The EOFs are
           numbered from 0 to *neofs* - 1.

        **Examples:**

        All EOFs::

            eofs = solver.eofsAsCorrelation()

        The leading EOF::

            eof1 = solver.eofsAsCorrelation(neofs=1)

        """
        eofs = self._solver.eofsAsCorrelation(neofs)
        eofdim = DimCoord(list(range(eofs.shape[0])),
                          var_name='eof',
                          long_name='eof_number')
        coords = [eofdim] + [copy(coord) for coord in self._coords]
        eofs = Cube(
            eofs,
            dim_coords_and_dims=list(zip(coords, list(range(eofs.ndim)))),
            var_name='eofs',
            long_name='correlation_between_pcs_and_{:s}'.format(
                self._cube_name))
        # Add any auxiliary coordinates spanning space to the returned cube.
        for coord, dims in self._space_aux_coords:
            eofs.add_aux_coord(copy(coord), dims)
        return eofs
Exemplo n.º 47
0
    def eofs(self, eofscaling=0, neofs=None):
        """Emipirical orthogonal functions (EOFs).

        **Optional arguments:**

        *eofscaling*
            Sets the scaling of the EOFs. The following values are
            accepted:

            * *0* : Un-scaled EOFs (default).
            * *1* : EOFs are divided by the square-root of their
              eigenvalues.
            * *2* : EOFs are multiplied by the square-root of their
              eigenvalues.

        *neofs*
            Number of EOFs to return. Defaults to all EOFs. If the
            number of EOFs requested is more than the number that are
            available, then all available EOFs will be returned.

        **Returns:**

        *eofs*
           A `~iris.cube.Cube` containing the ordered EOFs. The EOFs are
           numbered from 0 to *neofs* - 1.

        **Examples:**

        All EOFs with no scaling::

            eofs = solver.eofs()

        First 3 EOFs with scaling applied::

            eofs = solver.eofs(neofs=3, eofscaling=1)

        """
        eofs = self._solver.eofs(eofscaling, neofs)
        eofdim = DimCoord(list(range(eofs.shape[0])),
                          var_name='eof',
                          long_name='eof_number')
        coords = [eofdim] + [copy(coord) for coord in self._coords]
        eofs = Cube(
            eofs,
            dim_coords_and_dims=list(zip(coords, list(range(eofs.ndim)))),
            var_name='eofs',
            long_name='empirical_orthogonal_functions')
        # Add any auxiliary coordinates spanning space to the returned cube.
        for coord, dims in self._space_aux_coords:
            eofs.add_aux_coord(copy(coord), dims)
        return eofs
Exemplo n.º 48
0
    def setUp(self):
        """Create a cube with a single non-zero point."""
        data = np.zeros((2, 2, 2))
        data[0][:][:] = 1.0
        data[1][:][:] = 2.0
        cube = Cube(data,
                    standard_name="precipitation_amount",
                    units="kg m^-2 s^-1")
        cube.add_dim_coord(
            DimCoord(np.linspace(-45.0, 45.0, 2), 'latitude', units='degrees'),
            1)
        cube.add_dim_coord(
            DimCoord(np.linspace(120, 180, 2), 'longitude', units='degrees'),
            2)
        time_origin = "hours since 1970-01-01 00:00:00"
        calendar = "gregorian"
        tunit = Unit(time_origin, calendar)
        cube.add_dim_coord(DimCoord([402192.5, 402193.5], "time", units=tunit),
                           0)
        cube.add_aux_coord(AuxCoord([402190.0, 402191.0],
                                    "forecast_reference_time",
                                    units=tunit),
                           data_dims=0)
        cube.add_aux_coord(AuxCoord([3.0, 4.0], "forecast_period",
                                    units=tunit),
                           data_dims=0)

        self.cube = cube
        new_scalar_coord = iris.coords.AuxCoord(1,
                                                long_name='dummy_scalar_coord',
                                                units='no_unit')
        cube_with_scalar = cube.copy()
        cube_with_scalar.add_aux_coord(new_scalar_coord)
        self.cube_with_scalar = cube_with_scalar
        data_threshold = np.zeros((2, 2, 2, 2))
        data_threshold[:, 0, :, :] = 0.5
        data_threshold[:, 1, :, :] = 0.8
        cube_threshold = Cube(data_threshold,
                              long_name="probability_of_precipitation_amount")
        cube_threshold.add_dim_coord(
            DimCoord([0.4, 1.0], long_name="threshold", units="kg m^-2 s^-1"),
            0)
        cube_threshold.add_dim_coord(
            DimCoord([402192.5, 402193.5], "time", units=tunit), 1)
        cube_threshold.add_dim_coord(
            DimCoord(np.linspace(-45.0, 45.0, 2), 'latitude', units='degrees'),
            2)
        cube_threshold.add_dim_coord(
            DimCoord(np.linspace(120, 180, 2), 'longitude', units='degrees'),
            3)
        cube_threshold.add_aux_coord(AuxCoord([402190.0, 402191.0],
                                              "forecast_reference_time",
                                              units=tunit),
                                     data_dims=0)
        cube_threshold.add_aux_coord(AuxCoord([3.0, 4.0],
                                              "forecast_period",
                                              units=tunit),
                                     data_dims=0)
        cube_threshold.attributes.update({'relative_to_threshold': 'below'})
        self.cube_threshold = cube_threshold
Exemplo n.º 49
0
def uk_cube():
    data = np.arange(12, dtype=np.float32).reshape(3, 4)
    uk = Cube(data)
    cs = OSGB()
    y_coord = DimCoord(np.arange(3), 'projection_y_coordinate', units='m',
                       coord_system=cs)
    x_coord = DimCoord(np.arange(4), 'projection_x_coordinate', units='m',
                       coord_system=cs)
    uk.add_dim_coord(y_coord, 0)
    uk.add_dim_coord(x_coord, 1)
    surface = AuxCoord(data * 10, 'surface_altitude', units='m')
    uk.add_aux_coord(surface, (0, 1))
    uk.add_aux_factory(HybridHeightFactory(orography=surface))
    return uk
Exemplo n.º 50
0
 def test_multidimensional_xy(self):
     # Recast the 4-point source cube as 2*2 : should yield the same result.
     co_x = self.src_cube.coord(axis='x')
     co_y = self.src_cube.coord(axis='y')
     new_src = Cube(self.src_cube.data.reshape((2, 2)))
     new_x_co = AuxCoord(co_x.points.reshape((2, 2)),
                         standard_name='longitude',
                         units='degrees')
     new_y_co = AuxCoord(co_y.points.reshape((2, 2)),
                         standard_name='latitude',
                         units='degrees')
     new_src.add_aux_coord(new_x_co, (0, 1))
     new_src.add_aux_coord(new_y_co, (0, 1))
     self._check_expected(src_cube=new_src)
Exemplo n.º 51
0
 def _standard_grid_cube(grid, name):
     if grid[0].ndim == 1:
         shape = [coord.points.size for coord in grid]
     else:
         shape = grid[0].shape
     data = np.zeros(shape)
     cube = Cube(data, var_name=name, long_name=name)
     if grid[0].ndim == 1:
         cube.add_dim_coord(grid[0], 0)
         cube.add_dim_coord(grid[1], 1)
     else:
         cube.add_aux_coord(grid[0], [0, 1])
         cube.add_aux_coord(grid[1], [0, 1])
     return cube
def uk_cube():
    data = np.arange(12, dtype=np.float32).reshape(3, 4)
    uk = Cube(data)
    cs = OSGB()
    y_coord = DimCoord(range(3), 'projection_y_coordinate', units='m',
                       coord_system=cs)
    x_coord = DimCoord(range(4), 'projection_x_coordinate', units='m',
                       coord_system=cs)
    uk.add_dim_coord(y_coord, 0)
    uk.add_dim_coord(x_coord, 1)
    surface = AuxCoord(data * 10, 'surface_altitude', units='m')
    uk.add_aux_coord(surface, (0, 1))
    uk.add_aux_factory(HybridHeightFactory(orography=surface))
    return uk
    def setUp(self):
        # Basic test values.
        src_x_y_value = np.array([
            [20.12, 11.73, 0.01],
            [120.23, -20.73, 1.12],
            [290.34, 33.88, 2.23],
            [-310.45, 57.8, 3.34]])
        tgt_grid_x = np.array([-173.2, -100.3, -32.5, 1.4, 46.6, 150.7])
        tgt_grid_y = np.array([-80.1, -30.2, 0.3, 47.4, 75.5])

        # Make sample 1-D source cube.
        src = Cube(src_x_y_value[:, 2])
        src.add_aux_coord(AuxCoord(src_x_y_value[:, 0],
                                   standard_name='longitude', units='degrees'),
                          0)
        src.add_aux_coord(AuxCoord(src_x_y_value[:, 1],
                                   standard_name='latitude', units='degrees'),
                          0)
        self.src_cube = src

        # Make sample grid cube.
        grid = Cube(np.zeros(tgt_grid_y.shape + tgt_grid_x.shape))
        grid.add_dim_coord(DimCoord(tgt_grid_y,
                                    standard_name='latitude', units='degrees'),
                           0)
        grid.add_dim_coord(DimCoord(tgt_grid_x,
                                    standard_name='longitude',
                                    units='degrees'),
                           1)
        self.grid_cube = grid

        # Make expected-result, from the expected source-index at each point.
        expected_result_indices = np.array([
            [1, 1, 1, 1, 1, 1],
            [1, 2, 0, 0, 0, 1],
            [1, 2, 2, 0, 0, 1],
            [3, 2, 2, 3, 3, 3],
            [3, 2, 3, 3, 3, 3]])
        self.expected_data = self.src_cube.data[expected_result_indices]

        # Make a 3D source cube, based on the existing 2d test data.
        z_cubes = [src.copy() for _ in range(3)]
        for i_z, z_cube in enumerate(z_cubes):
            z_cube.add_aux_coord(DimCoord([i_z], long_name='z'))
            z_cube.data = z_cube.data + 100.0 * i_z
        self.src_z_cube = CubeList(z_cubes).merge_cube()

        # Make a corresponding 3d expected result.
        self.expected_data_zxy = \
            self.src_z_cube.data[:, expected_result_indices]
Exemplo n.º 54
0
def test_multidim_cubes():
    """
    Test for :func:`esmf_regrid.experimental.unstructured_scheme.MeshToGridESMFRegridder`.

    Tests with multidimensional cubes. The source cube contains
    coordinates on the dimensions before and after the mesh dimension.
    """
    mesh = _full_mesh()
    mesh_length = mesh.connectivity(contains_face=True).shape[0]

    h = 2
    t = 3
    height = DimCoord(np.arange(h), standard_name="height")
    time = DimCoord(np.arange(t), standard_name="time")

    src_data = np.empty([t, mesh_length, h])
    src_data[:] = np.arange(t * h).reshape([t, h])[:, np.newaxis, :]
    mesh_cube = Cube(src_data)
    mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords("face")
    mesh_cube.add_aux_coord(mesh_coord_x, 1)
    mesh_cube.add_aux_coord(mesh_coord_y, 1)
    mesh_cube.add_dim_coord(time, 0)
    mesh_cube.add_dim_coord(height, 2)

    n_lons = 6
    n_lats = 5
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)

    src_cube = mesh_cube.copy()
    src_cube.transpose([1, 0, 2])
    regridder = MeshToGridESMFRegridder(src_cube, tgt)
    result = regridder(mesh_cube)

    # Lenient check for data.
    expected_data = np.empty([t, n_lats, n_lons, h])
    expected_data[:] = np.arange(t * h).reshape(t, h)[:, np.newaxis,
                                                      np.newaxis, :]
    assert np.allclose(expected_data, result.data)

    expected_cube = Cube(expected_data)
    expected_cube.add_dim_coord(time, 0)
    expected_cube.add_dim_coord(tgt.coord("latitude"), 1)
    expected_cube.add_dim_coord(tgt.coord("longitude"), 2)
    expected_cube.add_dim_coord(height, 3)

    # Check metadata and scalar coords.
    result.data = expected_data
    assert expected_cube == result
    def test_no_bounds(self):
        t_coord = DimCoord(15, 'time', units='hours since epoch')
        cube = Cube(23)
        cube.add_aux_coord(t_coord)

        res = _missing_forecast_period(cube)
        expected_rt = datetime.datetime(1970, 1, 1, 15, 0)
        expected_rt_type = 3
        expected_fp = 0
        expected_fp_type = 1
        expected = (expected_rt,
                    expected_rt_type,
                    expected_fp,
                    expected_fp_type)
        self.assertEqual(res, expected)
 def test_sample_dictionary(self):
     # Pass sample_point arg as a dictionary: this usage mode is deprecated.
     co_x = AuxCoord([1.0, 2.0, 3.0], long_name='x')
     co_y = AuxCoord([10.0, 20.0], long_name='y')
     cube = Cube(np.zeros((2, 3)))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 1)
     sample_point = {'x': 2.8, 'y': 18.5}
     warn_call = self.patch(
         'iris.analysis._interpolate_private.warn_deprecated')
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, 2)])
     self.assertEqual(warn_call.call_count, 1)
     self.assertIn('dictionary to specify points is deprecated',
                   warn_call.call_args[0][0])
Exemplo n.º 57
0
 def _nonlatlon_uv_cubes(x, y, u, v):
     # Create u and v test cubes from x, y, u, v arrays.
     coord_cls = DimCoord if x.ndim == 1 else AuxCoord
     x_coord = coord_cls(x, long_name='x')
     y_coord = coord_cls(y, long_name='y')
     u_cube = Cube(u, long_name='u', units='ms-1')
     if x.ndim == 1:
         u_cube.add_dim_coord(y_coord, 0)
         u_cube.add_dim_coord(x_coord, 1)
     else:
         u_cube.add_aux_coord(y_coord, (0, 1))
         u_cube.add_aux_coord(x_coord, (0, 1))
     v_cube = u_cube.copy()
     v_cube.rename('v')
     v_cube.data = v
     return u_cube, v_cube
Exemplo n.º 58
0
    def test_with_bounds(self):
        t_coord = DimCoord(15, 'time', bounds=[14, 16],
                           units='hours since epoch')
        cube = Cube(23)
        cube.add_aux_coord(t_coord)

        res = _missing_forecast_period(cube)
        expected_rt = t_coord.units.num2date(14)
        expected_rt_type = 3
        expected_fp = 0
        expected_fp_type = 1
        expected = (expected_rt,
                    expected_rt_type,
                    expected_fp,
                    expected_fp_type)
        self.assertEqual(res, expected)
Exemplo n.º 59
0
def as_cube(dim_array, copy=True):
    """
    Convert a dimarray array into an Iris cube.

    Parameters
    ----------
    dim_array : DimArray instance
    copy : Whether to make a copy of the data.
                      Defaults to True.

    Returns
    -------
    iris cube

    Examples
    --------
    >>> from dimarray.geo import GeoArray
    >>> a = GeoArray([[1.,2.,3.],[4.,5.,6.]], lat=[30., 70.], lon=[-50., 30., 110.])
    >>> a.units = 'meters'
    >>> a.name = 'myarray'
    >>> a.long_name = 'test array for iris conversion'
    >>> as_cube(a)
    <iris 'Cube' of test array for iris conversion / (meters) (latitude: 2; longitude: 3)>
    """
    # Make the copy work consistently across NumPy 1.6 and 1.7.
    # (When 1.7 takes a copy it preserves the C/Fortran ordering, but
    # 1.6 doesn't. Since we don't care about preserving the order we can
    # just force it back to C-order.)
    order = 'C' if copy else 'A'
    data = np.array(dim_array, copy=copy, order=order)
    cube = Cube(np.ma.masked_invalid(data, copy=False))

    # add coordinates
    for i, ax in enumerate(dim_array.axes):
        coord = as_coord(ax)

        if isinstance(coord, DimCoord):
            cube.add_dim_coord(coord, i)
        else:
            cube.add_aux_coord(coord, i)

    # add cube metadata
    _add_iris_metadata(cube, dim_array._metadata)

    return cube
    def test_with_bounds(self):
        t_coord = DimCoord(3, 'time', bounds=[2, 4], units='days since epoch')
        frt_coord = DimCoord(8, 'forecast_reference_time',
                             units='hours since epoch')
        cube = Cube(23)
        cube.add_aux_coord(t_coord)
        cube.add_aux_coord(frt_coord)

        res = _missing_forecast_period(cube)
        expected_rt = datetime.datetime(1970, 1, 1, 8, 0)
        expected_rt_type = 1
        expected_fp = 2 * 24 - 8
        expected_fp_type = 1
        expected = (expected_rt,
                    expected_rt_type,
                    expected_fp,
                    expected_fp_type)
        self.assertEqual(res, expected)