Skip to content

Commit

Permalink
Unstructured Scheme - Cube Creation 2D (#39)
Browse files Browse the repository at this point in the history
* support creation of 2D cubes

* lint fixes

* rewrite test

* remove mesh from _create_cube arguments

* add TODO

* add TODO
  • Loading branch information
stephenworsley authored Mar 23, 2021
1 parent 852ba0d commit f75498a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
25 changes: 20 additions & 5 deletions esmf_regrid/experimental/unstructured_scheme.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Provides an iris interface for unstructured regridding."""

import copy

import iris
from iris.analysis._interpolation import get_xy_coords
import numpy as np
Expand Down Expand Up @@ -75,7 +77,7 @@ def _cube_to_GridInfo(cube):
# return result


def _create_cube(data, src_cube, mesh_dim, mesh, grid_x, grid_y):
def _create_cube(data, src_cube, mesh_dim, grid_x, grid_y):
# Here we expect the args to be as follows:
# data: a masked array containing the result of the regridding operation
# src_cube: the source cube which data is regrid from
Expand All @@ -86,7 +88,21 @@ def _create_cube(data, src_cube, mesh_dim, mesh, grid_x, grid_y):

new_cube = iris.cube.Cube(data)

# TODO: add coords and metadata.
# TODO: The following code assumes a 1D source cube and mesh_dim = 0.
# This is therefore simple code which should be updated when we start
# supporting the regridding of extra dimensions.

# TODO: The following code is rigid with respect to which dimensions
# the x coord and y coord are assigned to. We should decide if it is
# appropriate to copy the dimension ordering from the target cube
# instead.
new_cube.add_dim_coord(grid_x, mesh_dim + 1)
new_cube.add_dim_coord(grid_y, mesh_dim)

new_cube.metadata = copy.deepcopy(src_cube.metadata)

for coord in src_cube.coords(dimensions=()):
new_cube.add_aux_coord(coord.copy())

return new_cube

Expand All @@ -111,13 +127,13 @@ def _regrid_unstructured_to_rectilinear__prepare(src_mesh_cube, target_grid_cube

regridder = Regridder(meshinfo, gridinfo)

regrid_info = (mesh, mesh_dim, grid_x, grid_y, regridder)
regrid_info = (mesh_dim, grid_x, grid_y, regridder)

return regrid_info


def _regrid_unstructured_to_rectilinear__perform(src_cube, regrid_info, mdtol):
mesh, mesh_dim, grid_x, grid_y, regridder = regrid_info
mesh_dim, grid_x, grid_y, regridder = regrid_info

# Perform regridding with realised data for the moment. This may be changed
# in future to handle src_cube.lazy_data.
Expand All @@ -129,7 +145,6 @@ def _regrid_unstructured_to_rectilinear__perform(src_cube, regrid_info, mdtol):
new_data,
src_cube,
mesh_dim,
mesh,
grid_x,
grid_y,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Unit tests for miscellaneous helper functions in `esmf_regrid.experimental.unstructured_scheme`."""

import iris
import numpy as np

from esmf_regrid.experimental.unstructured_scheme import _create_cube


def test_create_cube_2D():
"""Test creation of 2D output grid."""
data = np.ones([2, 3])

# Create a source cube with metadata and scalar coords
src_cube = iris.cube.Cube(np.zeros(5))
src_cube.units = "K"
src_cube.attributes = {"a": 1}
src_cube.standard_name = "air_temperature"
scalar_height = iris.coords.AuxCoord([5], units="m", standard_name="height")
scalar_time = iris.coords.DimCoord([10], units="s", standard_name="time")
src_cube.add_aux_coord(scalar_height)
src_cube.add_aux_coord(scalar_time)

mesh_dim = 0

grid_x = iris.coords.DimCoord(np.arange(3), standard_name="longitude")
grid_y = iris.coords.DimCoord(np.arange(2), standard_name="latitude")

cube = _create_cube(data, src_cube, mesh_dim, grid_x, grid_y)
src_metadata = src_cube.metadata

expected_cube = iris.cube.Cube(data)
expected_cube.metadata = src_metadata
expected_cube.add_dim_coord(grid_x, 1)
expected_cube.add_dim_coord(grid_y, 0)
expected_cube.add_aux_coord(scalar_height)
expected_cube.add_aux_coord(scalar_time)
assert expected_cube == cube

0 comments on commit f75498a

Please sign in to comment.