def test_bad_units(self):
        ok = global_pp()

        bad = global_pp()
        bad.coord('longitude').units = 'radians'
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)

        bad = Cube(np.arange(12, dtype=np.float32).reshape(3, 4))
        cs = OSGB()
        y_coord = DimCoord(range(3),
                           'projection_y_coordinate',
                           units='m',
                           coord_system=cs)
        x_coord = DimCoord(range(4),
                           'projection_x_coordinate',
                           units='km',
                           coord_system=cs)
        bad.add_dim_coord(y_coord, 0)
        bad.add_dim_coord(x_coord, 1)
        with self.assertRaises(ValueError):
            regrid(bad, ok)
        with self.assertRaises(ValueError):
            regrid(ok, bad)
Пример #2
0
def set_up_cube(data_2d, name, unit):
    """Set up a 2D test cube of wind direction or speed"""

    x_coord = DimCoord(np.linspace(150000, 250000, data_2d.shape[1]),
                       "projection_x_coordinate",
                       units="metres",
                       coord_system=OSGB())
    y_coord = DimCoord(np.linspace(0, 600000, data_2d.shape[0]),
                       "projection_y_coordinate",
                       units="metres",
                       coord_system=OSGB())
    cube = iris.cube.Cube(data_2d,
                          standard_name=name,
                          units=unit,
                          dim_coords_and_dims=[(y_coord, 0), (x_coord, 1)])
    time_unit = Unit("hours since 1970-01-01 00:00:00", "gregorian")
    t_coord = DimCoord(402292.5, "time", units=time_unit)
    cube.add_aux_coord(t_coord)
    return cube
Пример #3
0
def uk_cube():
    data = np.arange(12, dtype=np.float32).reshape(3, 4)
    uk = Cube(data)
    cs = OSGB()
    y_coord = DimCoord(np.arange(3), 'projection_y_coordinate', units='m',
                       coord_system=cs)
    x_coord = DimCoord(np.arange(4), 'projection_x_coordinate', units='m',
                       coord_system=cs)
    uk.add_dim_coord(y_coord, 0)
    uk.add_dim_coord(x_coord, 1)
    surface = AuxCoord(data * 10, 'surface_altitude', units='m')
    uk.add_aux_coord(surface, (0, 1))
    uk.add_aux_factory(HybridHeightFactory(orography=surface))
    return uk
def set_up_cube(data_2d, name, unit):
    """Set up a 2D test cube of wind direction or speed"""

    cube = set_up_variable_cube(data_2d.astype(np.float32),
                                name=name,
                                units=unit,
                                spatial_grid="equalarea")

    cube.coord("projection_x_coordinate").points = np.linspace(
        150000, 250000, data_2d.shape[1])
    cube.coord("projection_y_coordinate").points = np.linspace(
        0, 600000, data_2d.shape[0])
    for axis in ["x", "y"]:
        cube.coord(axis=axis).units = "metres"
        cube.coord(axis=axis).coord_system = OSGB()
        cube.coord(axis=axis).bounds = None

    return cube
Пример #5
0
def set_up_cube_with_no_realizations(zero_point_indices=((0, 7, 7), ),
                                     num_time_points=1,
                                     num_grid_points=16,
                                     source_realizations=None):
    """Set up a normal OSGB UK National Grid cube."""

    zero_point_indices = list(zero_point_indices)
    for index, indices in enumerate(zero_point_indices):
        if len(indices) == 2:
            indices = (0, ) + indices
        zero_point_indices[index] = indices
    zero_point_indices = tuple(zero_point_indices)

    data = np.ones((num_time_points, num_grid_points, num_grid_points))
    for indices in zero_point_indices:
        time_index, lat_index, lon_index = indices
        data[time_index][lat_index][lon_index] = 0

    cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2")

    if source_realizations is not None:
        if isinstance(source_realizations, list):
            cube.attributes.update(
                {'source_realizations': source_realizations})

    tunit = Unit("hours since 1970-01-01 00:00:00", "gregorian")
    time_points = [402192.5 + _ for _ in range(num_time_points)]
    cube.add_dim_coord(
        DimCoord(time_points, standard_name="time", units=tunit), 0)

    coord_system = OSGB()
    scaled_y_coord = OSGBGRID.coord('projection_y_coordinate')
    cube.add_dim_coord(
        DimCoord(scaled_y_coord.points[:num_grid_points],
                 'projection_y_coordinate',
                 units='m',
                 coord_system=coord_system), 1)
    scaled_x_coord = OSGBGRID.coord('projection_x_coordinate')
    cube.add_dim_coord(
        DimCoord(scaled_x_coord.points[:num_grid_points],
                 'projection_x_coordinate',
                 units='m',
                 coord_system=coord_system), 2)
    return cube
Пример #6
0
def set_up_cube(zero_point_indices=((0, 0, 7, 7), ),
                num_time_points=1,
                num_grid_points=16,
                num_realization_points=1):
    """Set up a normal OSGB UK National Grid cube."""

    zero_point_indices = list(zero_point_indices)
    for index, indices in enumerate(zero_point_indices):
        if len(indices) == 3:
            indices = (0, ) + indices
        zero_point_indices[index] = indices
    zero_point_indices = tuple(zero_point_indices)

    data = np.ones((num_realization_points, num_time_points, num_grid_points,
                    num_grid_points))
    for indices in zero_point_indices:
        realization_index, time_index, lat_index, lon_index = indices
        data[realization_index][time_index][lat_index][lon_index] = 0

    cube = Cube(data, standard_name="precipitation_amount", units="kg m^-2")
    coord_system = OSGB()
    scaled_y_coord = OSGBGRID.coord('projection_y_coordinate')
    cube.add_dim_coord(
        DimCoord(range(num_realization_points), standard_name='realization'),
        0)
    tunit = Unit("hours since 1970-01-01 00:00:00", "gregorian")
    time_points = [402192.5 + _ for _ in range(num_time_points)]
    cube.add_dim_coord(
        DimCoord(time_points, standard_name="time", units=tunit), 1)
    cube.add_dim_coord(
        DimCoord(scaled_y_coord.points[:num_grid_points],
                 'projection_y_coordinate',
                 units='m',
                 coord_system=coord_system), 2)
    scaled_x_coord = OSGBGRID.coord('projection_x_coordinate')
    cube.add_dim_coord(
        DimCoord(scaled_x_coord.points[:num_grid_points],
                 'projection_x_coordinate',
                 units='m',
                 coord_system=coord_system), 3)
    return cube