Ejemplo n.º 1
0
 def test_cube_var_name_is_none(self):
     """
     Test that the utility returns an iris.cube.Cube with a
     var_name of None.
     """
     result = strip_var_names(self.cube)
     self.assertIsNone(result[0].var_name, None)
Ejemplo n.º 2
0
 def test_cubelist(self):
     """Test that the utility returns an iris.cube.CubeList."""
     cubes = iris.cube.CubeList([self.cube, self.cube])
     result = strip_var_names(cubes)
     self.assertIsInstance(result, iris.cube.CubeList)
     for cube in result:
         for coord in cube.coords():
             self.assertIsNone(coord.var_name, None)
Ejemplo n.º 3
0
 def test_cube_coord_var_name_is_none(self):
     """
     Test that the coordinates have var_names of None.
     """
     self.cube.coord("time").var_name = "time"
     self.cube.coord("latitude").var_name = "latitude"
     self.cube.coord("longitude").var_name = "longitude"
     result = strip_var_names(self.cube)
     for cube in result:
         for coord in cube.coords():
             self.assertIsNone(coord.var_name, None)
Ejemplo n.º 4
0
 def test_cubelist(self):
     """Test that the utility returns an iris.cube.CubeList."""
     cube1 = self.cube
     cube2 = self.cube
     cubes = iris.cube.CubeList([cube1, cube2])
     self.cube.var_name = "air_temperature"
     result = strip_var_names(cubes)
     self.assertIsInstance(result, iris.cube.CubeList)
     for cube in result:
         for coord in cube.coords():
             self.assertIsNone(coord.var_name, None)
Ejemplo n.º 5
0
 def test_basic(self):
     """Test that the utility returns an iris.cube.CubeList."""
     result = strip_var_names(self.cube)
     self.assertIsInstance(result, iris.cube.CubeList)
Ejemplo n.º 6
0
def load_cubelist(
    filepath: Union[str, List[str]],
    constraints: Optional[Union[Constraint, str]] = None,
    no_lazy_load: bool = False,
) -> CubeList:
    """Load cubes from filepath(s) into a cubelist. Strips off all
    var names except for "threshold"-type coordinates, where this is different
    from the standard or long name.

    Args:
        filepath:
            Filepath(s) that will be loaded.
        constraints:
            Constraint to be applied when loading from the input filepath.
            This can be in the form of an iris.Constraint or could be a string
            that is intended to match the name of the cube.
            The default is None.
        no_lazy_load:
            If True, bypass cube deferred (lazy) loading and load the whole
            cube into memory. This can increase performance at the cost of
            memory. If False (default) then lazy load.

    Returns:
        CubeList that has been created from the input filepath given the
        constraints provided.
    """
    # Remove legacy metadata prefix cube if present
    constraints = (
        iris.Constraint(cube_func=lambda cube: cube.long_name != "prefixes")
        & constraints)

    # Load each file individually to avoid partial merging (not used
    # iris.load_raw() due to issues with time representation)
    if isinstance(filepath, str):
        cubes = iris.load(filepath, constraints=constraints)
    else:
        cubes = iris.cube.CubeList([])
        for item in filepath:
            cubes.extend(iris.load(item, constraints=constraints))

    if not cubes:
        message = "No cubes found using constraints {}".format(constraints)
        raise ValueError(message)

    # Remove var_name from cubes and coordinates (except where needed to
    # describe probabilistic data)
    cubes = strip_var_names(cubes)

    for cube in cubes:

        # Remove metadata attributes pointing to legacy prefix cube
        cube.attributes.pop("bald__isPrefixedBy", None)

        # Ensure the probabilistic coordinates are the first coordinates within
        # a cube and are in the specified order.
        enforce_coordinate_ordering(cube,
                                    ["realization", "percentile", "threshold"])
        # Ensure the y and x dimensions are the last within the cube.
        y_name = cube.coord(axis="y").name()
        x_name = cube.coord(axis="x").name()
        enforce_coordinate_ordering(cube, [y_name, x_name], anchor_start=False)
        if no_lazy_load:
            # Force cube's data into memory by touching the .data attribute.
            cube.data

    return cubes