Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for varying longitude ranges. #350

Merged
merged 1 commit into from
Feb 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/iris/analysis/interpolate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2012, Met Office
# (C) British Crown Copyright 2010 - 2013, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -645,6 +645,7 @@ def linear(cube, sample_points, extrapolation_mode='linear'):
# Now we must be down to a single sample coordinate and its
# values.
src_coord, requested_points = sample_points[0]
sample_values = np.array(requested_points)

# 1) Define the interpolation characteristics.

Expand Down Expand Up @@ -682,7 +683,12 @@ def linear(cube, sample_points, extrapolation_mode='linear'):
cube.data.mask[tuple(coord_slice_in_cube)],
axis=sample_dim)
data = ma.array(new_data, mask=new_mask)


# Map all the requested values into the range of the source
# data.
if modulus:
offset = src_coord.points[0]
sample_values = ((sample_values - offset) % modulus) + offset
else:
src_points = src_coord.points
data = cube.data
Expand Down Expand Up @@ -721,7 +727,7 @@ def interpolate(fx, new_x, **kwargs):
return new_fx

# 2) Interpolate the data and produce our new Cube.
data = interpolate(data, requested_points, axis=sample_dim, copy=False)
data = interpolate(data, sample_values, axis=sample_dim, copy=False)
new_cube = iris.cube.Cube(data)
new_cube.metadata = cube.metadata

Expand Down
20 changes: 20 additions & 0 deletions lib/iris/tests/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# (C) British Crown Copyright 2013, Met Office
#
# This file is part of Iris.
#
# Iris is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Iris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what impact having a sub-folder will have our visual testing. I know cartopy can handle it, but not 100% that the standard mpl image testing functionality can. We can cross that bridge when we get to it though.

Package for testing the iris.analysis package.

"""
62 changes: 62 additions & 0 deletions lib/iris/tests/analysis/test_interpolate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# (C) British Crown Copyright 2013, Met Office
#
# This file is part of Iris.
#
# Iris is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Iris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
"""
Test the iris.analysis.interpolate module.

"""
# Import iris tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests

import numpy as np

import iris.analysis.interpolate as interpolate
from iris.coords import DimCoord
from iris.cube import Cube


class Test_linear__circular_wrapping(tests.IrisTest):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be other interpolation tests somewhere? Is it worth putting this with those? Or moving those to here (in a separate PR?).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be other interpolation tests somewhere?

Indeed there are. And yes, it would be good to move them here in a separate PR (I've deliberately not done that in this PR to keep it obvious what is going on).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a follow-on issue to do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is now ... #353 😉

def _create_cube(self, longitudes):
# Return a Cube with circular longitude with the given values.
data = np.arange(12).reshape((3, 4)) * 0.1
cube = Cube(data)
lon = DimCoord(longitudes, standard_name='longitude',
units='degrees', circular=True)
cube.add_dim_coord(lon, 1)
return cube

def test_symmetric(self):
# Check we can interpolate from a Cube defined over [-180, 180).
cube = self._create_cube([-180, -90, 0, 90])
samples = [('longitude', range(-360, 720, 45))]
result = interpolate.linear(cube, samples)
self.assertCMLApproxData(result, ('analysis', 'interpolation',
'linear', 'circular_wrapping',
'symmetric'))

def test_positive(self):
# Check we can interpolate from a Cube defined over [0, 360).
cube = self._create_cube([0, 90, 180, 270])
samples = [('longitude', range(-360, 720, 45))]
result = interpolate.linear(cube, samples)
self.assertCMLApproxData(result, ('analysis', 'interpolation',
'linear', 'circular_wrapping',
'positive'))


if __name__ == "__main__":
tests.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube units="unknown">
<coords>
<coord datadims="[1]">
<dimCoord id="a523d045" points="[-360.0, -315.0, -270.0, -225.0, -180.0, -135.0,
-90.0, -45.0, 0.0, 45.0, 90.0, 135.0, 180.0,
225.0, 270.0, 315.0, 360.0, 405.0, 450.0, 495.0,
540.0, 585.0, 630.0, 675.0]" shape="(24,)" standard_name="longitude" units="Unit('degrees')" value_type="float64"/>
</coord>
</coords>
<cellMethods/>
<data dtype="float64" shape="(3, 24)" state="loaded"/>
</cube>
</cubes>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube units="unknown">
<coords>
<coord datadims="[1]">
<dimCoord id="a523d045" points="[-360.0, -315.0, -270.0, -225.0, -180.0, -135.0,
-90.0, -45.0, 0.0, 45.0, 90.0, 135.0, 180.0,
225.0, 270.0, 315.0, 360.0, 405.0, 450.0, 495.0,
540.0, 585.0, 630.0, 675.0]" shape="(24,)" standard_name="longitude" units="Unit('degrees')" value_type="float64"/>
</coord>
</coords>
<cellMethods/>
<data dtype="float64" shape="(3, 24)" state="loaded"/>
</cube>
</cubes>
Binary file not shown.