Example #1
0
 def test_reversed_points(self):
     cube1, cube2 = self.SetUpReversed()
     with self.assertRaises(ValueError):
         multiply(cube1, cube2)
Example #2
0
 def test_no_match(self):
     cube1, cube2 = self.SetUpNonMatching()
     with self.assertRaises(ValueError):
         multiply(cube1, cube2)
Example #3
0
 def test_reversed_points(self):
     cube1, cube2 = self.SetUpReversed()
     with self.assertRaises(ValueError):
         multiply(cube1, cube2)
Example #4
0
def apply_gridded_lapse_rate(temperature, lapse_rate, source_orog, dest_orog):
    """
    Function to apply a lapse rate adjustment to temperature data forecast
    at "source_orog" heights, to be applicable at "dest_orog" heights.

    Args:
        temperature (iris.cube.Cube):
            Input temperature field to be adjusted
        lapse_rate (iris.cube.Cube):
            Cube of pre-calculated lapse rates (units modified in place), which
            must match the temperature cube
        source_orog (iris.cube.Cube):
            2D cube of source orography heights (units modified in place)
        dest_orog (iris.cube.Cube):
            2D cube of destination orography heights (units modified in place)

    Returns:
        iris.cube.Cube:
            Lapse-rate adjusted temperature field
    """
    # check dimensions and coordinates match on input cubes
    for crd in temperature.coords(dim_coords=True):
        try:
            if crd != lapse_rate.coord(crd.name()):
                raise ValueError(
                    'Lapse rate cube coordinate "{}" does not match '
                    'temperature cube coordinate'.format(crd.name()))
        except CoordinateNotFoundError:
            raise ValueError('Lapse rate cube has no coordinate '
                             '"{}"'.format(crd.name()))

    if not spatial_coords_match(temperature, source_orog):
        raise ValueError('Source orography spatial coordinates do not match '
                         'temperature grid')
    if not spatial_coords_match(temperature, dest_orog):
        raise ValueError(
            'Destination orography spatial coordinates do not match '
            'temperature grid')

    # calculate height difference (in m) on which to adjust
    source_orog.convert_units('m')
    dest_orog.convert_units('m')
    orog_diff = (
        next(
            dest_orog.slices(
                [dest_orog.coord(axis='y'),
                 dest_orog.coord(axis='x')])) -
        next(
            source_orog.slices(
                [source_orog.coord(axis='y'),
                 source_orog.coord(axis='x')])))

    # convert lapse rate cube to K m-1
    lapse_rate.convert_units('K m-1')

    # adjust temperatures
    adjusted_temperature = []
    for lrsubcube, tempsubcube in zip(
            lapse_rate.slices(
                [lapse_rate.coord(axis='y'),
                 lapse_rate.coord(axis='x')]),
            temperature.slices(
                [temperature.coord(axis='y'),
                 temperature.coord(axis='x')])):

        # calculate temperature adjustment in K
        adjustment = multiply(orog_diff, lrsubcube)

        # apply adjustment to each spatial slice of the temperature cube
        newcube = tempsubcube.copy()
        newcube.convert_units('K')
        newcube.data += adjustment.data
        adjusted_temperature.append(newcube)

    return iris.cube.CubeList(adjusted_temperature).merge_cube()
Example #5
0
 def test_no_match(self):
     cube1, cube2 = self.SetUpNonMatching()
     with self.assertRaises(ValueError):
         multiply(cube1, cube2)