示例#1
0
def create_new_diagnostic_cube(
    name: str,
    units: Union[Unit, str],
    template_cube: Cube,
    mandatory_attributes: Union[Dict[str, str], LimitedAttributeDict],
    optional_attributes: Optional[Union[Dict[str, str],
                                        LimitedAttributeDict]] = None,
    data: Optional[Union[MaskedArray, ndarray]] = None,
    dtype: Type = np.float32,
) -> Cube:
    """
    Creates a new diagnostic cube with suitable metadata.

    Args:
        name:
            Standard or long name for output cube
        units:
            Units for output cube
        template_cube:
            Cube from which to copy dimensional and auxiliary coordinates
        mandatory_attributes:
            Dictionary containing values for the mandatory attributes
            "title", "source" and "institution".  These are overridden by
            values in the optional_attributes dictionary, if specified.
        optional_attributes:
            Dictionary of optional attribute names and values.  If values for
            mandatory attributes are included in this dictionary they override
            the values of mandatory_attributes.
        data:
            Data array.  If not set, cube is filled with zeros using a lazy
            data object, as this will be overwritten later by the caller
            routine.
        dtype:
            Datatype for dummy cube data if "data" argument is None.

    Returns:
        Cube with correct metadata to accommodate new diagnostic field
    """
    attributes = mandatory_attributes
    if optional_attributes is not None:
        attributes.update(optional_attributes)

    error_msg = ""
    for attr in MANDATORY_ATTRIBUTES:
        if attr not in attributes:
            error_msg += "{} attribute is required\n".format(attr)
    if error_msg:
        raise ValueError(error_msg)

    if data is None:
        data = da.zeros_like(template_cube.core_data(), dtype=dtype)

    aux_coords_and_dims, dim_coords_and_dims = [[
        (coord.copy(), template_cube.coord_dims(coord))
        for coord in getattr(template_cube, coord_type)
    ] for coord_type in ("aux_coords", "dim_coords")]

    cube = iris.cube.Cube(
        data,
        units=units,
        attributes=attributes,
        dim_coords_and_dims=dim_coords_and_dims,
        aux_coords_and_dims=aux_coords_and_dims,
    )
    cube.rename(name)

    return cube
 def test_basic(self):
     real_data = np.arange(3.0)
     cube = Cube(as_lazy_data(real_data))
     co_realise_cubes(cube)
     self.assertFalse(cube.has_lazy_data())
     self.assertArrayAllClose(cube.core_data(), real_data)
示例#3
0
 def test_basic(self):
     real_data = np.arange(3.)
     cube = Cube(as_lazy_data(real_data))
     co_realise_cubes(cube)
     self.assertFalse(cube.has_lazy_data())
     self.assertArrayAllClose(cube.core_data(), real_data)