Skip to content
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
51 changes: 51 additions & 0 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,57 @@ def from_coord(coord):
coord_system=copy.deepcopy(coord.coord_system),
circular=getattr(coord, 'circular', False))

@classmethod
def from_regular(cls, zeroth, step, count, standard_name=None,
long_name=None, var_name=None, units='1', attributes=None,
coord_system=None, circular=False, with_bounds=False):
"""
Create a :class:`DimCoord` with regularly spaced points, and
optionally bounds.

The majority of the arguments are defined as for
:meth:`Coord.__init__`, but those which differ are defined below.

Args:

* zeroth:
The value *prior* to the first point value.
* step:
The numeric difference between successive point values.
* count:
The number of point values.

Kwargs:

* with_bounds:
If True, the resulting DimCoord will possess bound values
which are equally spaced around the points. Otherwise no
bounds values will be defined. Defaults to False.

"""
coord = DimCoord.__new__(cls)
Copy link
Contributor

Choose a reason for hiding this comment

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

@esc24's cls.__new__(cls) is better because it works for subclasses too.
We want SparklyDimCoord.from_regular to return a SparklyDimCoord.

Copy link
Member Author

Choose a reason for hiding this comment

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

Except we have no idea what arguments the SparklyDimCoord constructor needs. NB. If SparklyDimCoord wants to override from_regular it still can.

Copy link
Member Author

Choose a reason for hiding this comment

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

NB. DimCoord.__new__(cls) will return an instance of SparklyDimCoord ... that's what the cls parameter is for.

Copy link
Member

Choose a reason for hiding this comment

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

@rhattersley is too polite to point out I was talking nonsense with cls.__new__(cls) 😞

Copy link
Contributor

Choose a reason for hiding this comment

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

NB. DimCoord.new(cls) will return an instance of SparklyDimCoord ... that's what the cls parameter is for.

Great, no problem then.

Copy link
Contributor

Choose a reason for hiding this comment

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

@rhattersley is too polite to point out I was talking nonsense

@esc24 is too polite to point out that's "we", not "I" 😀


coord.standard_name = standard_name
coord.long_name = long_name
coord.var_name = var_name
coord.units = units
coord.attributes = attributes
coord.coord_system = coord_system
coord.circular = circular
Copy link
Member

Choose a reason for hiding this comment

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

Do you know of any smart way of keeping the attribute definitions in sync? I'm concerned that a new attribute will be added in init which is expected, but is not added by this method.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we were calling the constructor, as I queried above, and this new attribute is expected, it'll be an arg and would fail if omitted?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you know of any smart way of keeping the attribute definitions in sync?

In short ... no. 😒

Copy link
Contributor

Choose a reason for hiding this comment

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

Coord._set_metadata ?

Copy link
Member Author

Choose a reason for hiding this comment

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

A method such as Coord._set_metadata doesn't help unless it's also used by Coord.__init__. But then how do we provide docstrings for the instance attributes?

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm...
i suppose, because init needs to accept them all and pass them to set_metadata, it's not hard to put them in init, as =None, with docstrings.
i'm not going to push for this but it would help protect these functions from drift.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll see what the negative performance impact is of setting the values twice...

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding Coord._set_metadata completely negates the performance benefit. 😑

Plus there's still the question of how to handle attributes which are specific to DimCoord (i.e. currently just circular). One can't just override _set_metadata because it's used by Coord.__init__ which has no knowledge of the extra attributes. Anyway ... it's all hypothetical ...there's no point in the PR if there's no benefit 😉


points = (zeroth+step) + step*np.arange(count, dtype=np.float32)
points.flags.writeable = False
coord._points = points

if with_bounds:
bounds = np.concatenate([[points - delta], [points + delta]]).T
bounds.flags.writeable = False
coord._bounds = bounds
else:
coord._bounds = None

return coord

def __init__(self, points, standard_name=None, long_name=None,
var_name=None, units='1', bounds=None, attributes=None,
coord_system=None, circular=False):
Expand Down
14 changes: 12 additions & 2 deletions lib/iris/fileformats/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,8 +1109,13 @@ def regular_points(self, xy):
Args:

* xy - a string, "x" or "y" to specify the dimension for which to return points.


.. deprecated:: 1.5

"""
msg = "The 'regular_points' method is deprecated."
warnings.warn(msg, UserWarning, stacklevel=2)

if xy.lower() == "x":
bz = self.bzx
bd = self.bdx
Expand All @@ -1130,8 +1135,13 @@ def regular_bounds(self, xy):
Args:

* xy - a string, "x" or "y" to specify the dimension for which to return points.


.. deprecated:: 1.5

"""
msg = "The 'regular_bounds' method is deprecated."
warnings.warn(msg, UserWarning, stacklevel=2)

if xy.lower() == "x":
delta = 0.5 * self.bdx
elif xy.lower() == "y":
Expand Down
15 changes: 9 additions & 6 deletions lib/iris/fileformats/pp_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.

# DO NOT EDIT DIRECTLY
# Auto-generated from SciTools/iris-code-generators:tools/gen_rules.py

import warnings

import numpy as np
Expand Down Expand Up @@ -102,25 +105,25 @@ def convert(cube, f):
(f.bdx != 0.0) and \
(len(f.lbcode) != 5) and \
(f.lbcode[0] == 1):
cube.add_dim_coord(DimCoord(f.regular_points("x"), standard_name=f._x_coord_name(), units='degrees', circular=(f.lbhem in [0, 4]), coord_system=f.coord_system()), 1)
cube.add_dim_coord(DimCoord.from_regular(f.bzx, f.bdx, f.lbnpt, standard_name=f._x_coord_name(), units='degrees', circular=(f.lbhem in [0, 4]), coord_system=f.coord_system()), 1)

if \
(f.bdx != 0.0) and \
(len(f.lbcode) != 5) and \
(f.lbcode[0] == 2):
cube.add_dim_coord(DimCoord(f.regular_points("x"), standard_name=f._x_coord_name(), units='degrees', circular=(f.lbhem in [0, 4]), coord_system=f.coord_system(), bounds=f.regular_bounds("x")), 1)
cube.add_dim_coord(DimCoord.from_regular(f.bzx, f.bdx, f.lbnpt, standard_name=f._x_coord_name(), units='degrees', circular=(f.lbhem in [0, 4]), coord_system=f.coord_system(), with_bounds=True), 1)

if \
(f.bdy != 0.0) and \
(len(f.lbcode) != 5) and \
(f.lbcode[0] == 1):
cube.add_dim_coord(DimCoord(f.regular_points("y"), standard_name=f._y_coord_name(), units='degrees', coord_system=f.coord_system()), 0)
cube.add_dim_coord(DimCoord.from_regular(f.bzy, f.bdy, f.lbrow, standard_name=f._y_coord_name(), units='degrees', coord_system=f.coord_system()), 0)

if \
(f.bdy != 0.0) and \
(len(f.lbcode) != 5) and \
(f.lbcode[0] == 2):
cube.add_dim_coord(DimCoord(f.regular_points("y"), standard_name=f._y_coord_name(), units='degrees', coord_system=f.coord_system(), bounds=f.regular_bounds("y")), 0)
cube.add_dim_coord(DimCoord.from_regular(f.bzy, f.bdy, f.lbrow, standard_name=f._y_coord_name(), units='degrees', coord_system=f.coord_system(), with_bounds=True), 0)

if \
(f.bdy == 0.0) and \
Expand All @@ -142,7 +145,7 @@ def convert(cube, f):
(len(f.lbcode) == 5) and \
(f.lbcode.ix == 10) and \
(f.bdx != 0):
cube.add_dim_coord(DimCoord(f.regular_points("x"), standard_name=f._y_coord_name(), units='degrees', coord_system=f.coord_system()), 1)
cube.add_dim_coord(DimCoord.from_regular(f.bzx, f.bdx, f.lbnpt, standard_name=f._y_coord_name(), units='degrees', coord_system=f.coord_system()), 1)

if \
(len(f.lbcode) == 5) and \
Expand Down Expand Up @@ -173,7 +176,7 @@ def convert(cube, f):
(f.lbcode[-1] == 1) and \
(f.lbcode.ix == 13) and \
(f.bdx != 0):
cube.add_dim_coord(DimCoord(f.regular_points("x"), long_name='site_number', units='1'), 1)
cube.add_dim_coord(DimCoord.from_regular(f.bzx, f.bdx, f.lbnpt, long_name='site_number', units='1'), 1)

if \
(len(f.lbcode) == 5) and \
Expand Down