Example #1
0
 def _makecube(self, y, cm=False, av=False):
     cube = iris.cube.Cube([0, 0])
     cube.add_dim_coord(iris.coords.DimCoord([0, 1], long_name="x"), 0)
     cube.add_aux_coord(iris.coords.DimCoord(y, long_name="y"))
     if cm:
         cube.add_cell_measure(
             iris.coords.CellMeasure([1, 1], long_name="foo"), 0)
     if av:
         cube.add_ancillary_variable(
             iris.coords.AncillaryVariable([1, 1], long_name="bar"), 0)
     return cube
Example #2
0
 def setUp(self):
     data = np.arange(24, dtype=np.float32).reshape(2, 3, 4)
     cube = iris.cube.Cube(data, standard_name="air_temperature", units="K")
     # Time coord
     t_unit = cf_units.Unit("hours since 1970-01-01 00:00:00",
                            calendar="gregorian")
     t_coord = iris.coords.DimCoord(
         points=np.arange(2, dtype=np.float32),
         standard_name="time",
         units=t_unit,
     )
     cube.add_dim_coord(t_coord, 0)
     # Lats and lons
     x_coord = iris.coords.DimCoord(
         points=np.arange(3, dtype=np.float32),
         standard_name="longitude",
         units="degrees",
     )
     cube.add_dim_coord(x_coord, 1)
     y_coord = iris.coords.DimCoord(
         points=np.arange(4, dtype=np.float32),
         standard_name="latitude",
         units="degrees",
     )
     cube.add_dim_coord(y_coord, 2)
     # Scalars
     cube.add_aux_coord(iris.coords.AuxCoord([0], "height", units="m"))
     # Aux Coords
     cube.add_aux_coord(
         iris.coords.AuxCoord(data, long_name="wibble", units="1"),
         data_dims=(0, 1, 2),
     )
     cube.add_aux_coord(
         iris.coords.AuxCoord([0, 1, 2], long_name="foo", units="1"),
         data_dims=(1, ),
     )
     cube.add_cell_measure(
         iris.coords.CellMeasure([0, 1, 2], long_name="bar", units="1"),
         data_dims=(1, ),
     )
     cube.add_ancillary_variable(
         iris.coords.AncillaryVariable([0, 1, 2],
                                       long_name="baz",
                                       units="1"),
         data_dims=(1, ),
     )
     self.cube = cube
Example #3
0
    def create_cube(self):
        data = np.arange(4).reshape(2, 2)

        lat = iris.coords.DimCoord([0, 30],
                                   standard_name="latitude",
                                   units="degrees")
        quality = iris.coords.AncillaryVariable([0, 15], long_name="quality")
        height = iris.coords.AncillaryVariable([1.5],
                                               standard_name="height",
                                               units="m")
        t_unit = cf_units.Unit("hours since 1970-01-01 00:00:00",
                               calendar="gregorian")
        time = iris.coords.DimCoord([0, 6], standard_name="time", units=t_unit)

        cube = iris.cube.Cube(data, standard_name="air_temperature", units="K")
        cube.add_dim_coord(time, 0)
        cube.add_dim_coord(lat, 1)
        cube.add_ancillary_variable(quality, 1)
        cube.add_ancillary_variable(height)
        return cube
Example #4
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