def test_fewer_inputs(self):
     """Test routine can produce output from a shorter list of inputs"""
     result = generate_optical_flow_components(
         [self.second_cube, self.third_cube], self.ofc_box_size, self.iterations
     )
     for cube in result:
         self.assertAlmostEqual(cube.coord("time").points[0], self.expected_time)
예제 #2
0
def process(original_cube_list,
            orographic_enhancement_cube=None,
            attributes_dict=None,
            ofc_box_size=30,
            smart_smoothing_iterations=100):
    """Calculate optical flow components from input fields.

    Args:
        original_cube_list (iris.cube.CubeList):
            Cubelist from which to calculate optical flow velocities.
            The cubes require a 'time' coordinate on which they are sorted,
            so the order of cubes does not matter.
        orographic_enhancement_cube (iris.cube.Cube):
            Cube containing the orographic enhancement fields.
            Default is None.
        attributes_dict (dict):
            Dictionary containing required changes to the attributes.
            Every output file will have the attributes_dict applied.
            Default is None.
        ofc_box_size (int):
            Size of square 'box' (in grid spaces) within which to solve
            the optical flow equations.
            Default is 30.
        smart_smoothing_iterations (int):
            Number of iterations to perform in enforcing smoothness constraint
            for optical flow velocities.
            Default is 100.

    Returns:
        iris.cube.CubeList:
            List of the umean and vmean cubes.

    Raises:
        ValueError:
            If there is no oe_cube but a cube is called 'precipitation_rate'.

    """
    # order input files by validity time
    original_cube_list.sort(key=lambda x: x.coord("time").points[0])

    # subtract orographic enhancement
    if orographic_enhancement_cube:
        cube_list = ApplyOrographicEnhancement("subtract").process(
            original_cube_list, orographic_enhancement_cube)
    else:
        cube_list = original_cube_list
        if any("precipitation_rate" in cube.name() for cube in cube_list):
            cube_names = [cube.name() for cube in cube_list]
            msg = ("For precipitation fields, orographic enhancement "
                   "filepaths must be supplied. The names of the cubes "
                   "supplied were: {}".format(cube_names))
            raise ValueError(msg)

    # calculate optical flow velocities from T-1 to T and T-2 to T-1, and
    # average to produce the velocities for use in advection
    u_mean, v_mean = generate_optical_flow_components(
        cube_list, ofc_box_size, smart_smoothing_iterations, attributes_dict)

    return CubeList([u_mean, v_mean])
 def test_time_ordering(self):
     """Test output timestamps are insensitive to input cube order"""
     cubelist = [self.second_cube, self.third_cube, self.first_cube]
     result = generate_optical_flow_components(cubelist, self.ofc_box_size,
                                               self.iterations)
     for cube in result:
         self.assertAlmostEqual(
             cube.coord("time").points[0], self.expected_time)
 def test_basic(self):
     """Test output is a tuple of cubes"""
     cubelist = [self.first_cube, self.second_cube, self.third_cube]
     result = generate_optical_flow_components(cubelist, self.ofc_box_size,
                                               self.iterations)
     for cube in result:
         self.assertIsInstance(cube, iris.cube.Cube)
         self.assertAlmostEqual(
             cube.coord("time").points[0], self.expected_time)
예제 #5
0
def process(orographic_enhancement: cli.inputcube,
            *cubes: cli.inputcube,
            attributes_config: cli.inputjson = None,
            ofc_box_size: int = 30,
            smart_smoothing_iterations: int = 100):
    """Calculate optical flow components from input fields.

    Args:
        orographic_enhancement (iris.cube.Cube):
            Cube containing the orographic enhancement fields.
        cubes (iris.cube.CubeList):
            Cubes from which to calculate optical flow velocities.
            These three cubes will be sorted by their time coords.
        attributes_config (dict):
            Dictionary containing required changes to the attributes.
            Every output file will have the attributes_config applied.
        ofc_box_size (int):
            Size of square 'box' (in grid spaces) within which to solve
            the optical flow equations.
        smart_smoothing_iterations (int):
            Number of iterations to perform in enforcing smoothness constraint
            for optical flow velocities.

    Returns:
        iris.cube.CubeList:
            List of the umean and vmean cubes.

    """
    from iris.cube import CubeList

    from improver.nowcasting.optical_flow import \
        generate_optical_flow_components
    from improver.nowcasting.utilities import ApplyOrographicEnhancement

    original_cube_list = CubeList(cubes)
    # order input files by validity time
    original_cube_list.sort(key=lambda x: x.coord("time").points[0])

    # subtract orographic enhancement
    cube_list = ApplyOrographicEnhancement("subtract").process(
            original_cube_list, orographic_enhancement)

    # calculate optical flow velocities from T-1 to T and T-2 to T-1, and
    # average to produce the velocities for use in advection
    u_mean, v_mean = generate_optical_flow_components(
        cube_list, ofc_box_size, smart_smoothing_iterations, attributes_config)

    return CubeList([u_mean, v_mean])
예제 #6
0
def process(
    orographic_enhancement: cli.inputcube, *cubes: cli.inputcube,
):
    """Calculate optical flow components from input fields.

    Args:
        orographic_enhancement (iris.cube.Cube):
            Cube containing the orographic enhancement fields.
        cubes (iris.cube.CubeList):
            Cubes from which to calculate optical flow velocities.
            These three cubes will be sorted by their time coords.

    Returns:
        iris.cube.CubeList:
            List of the umean and vmean cubes.

    """
    from iris.cube import CubeList

    from improver.nowcasting.optical_flow import generate_optical_flow_components
    from improver.nowcasting.utilities import ApplyOrographicEnhancement

    original_cube_list = CubeList(cubes)
    # order input files by validity time
    original_cube_list.sort(key=lambda x: x.coord("time").points[0])

    # subtract orographic enhancement
    cube_list = ApplyOrographicEnhancement("subtract")(
        original_cube_list, orographic_enhancement
    )

    # calculate optical flow velocities from T-1 to T and T-2 to T-1, and
    # average to produce the velocities for use in advection
    u_mean, v_mean = generate_optical_flow_components(
        cube_list, ofc_box_size=30, smart_smoothing_iterations=100
    )

    return CubeList([u_mean, v_mean])