diff --git a/lib/iris/coords.py b/lib/iris/coords.py index f58d464a9c..55ac3be9e9 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -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) + + 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 + + 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): diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 7a098ab5cb..86a7082f6e 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -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 @@ -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": diff --git a/lib/iris/fileformats/pp_rules.py b/lib/iris/fileformats/pp_rules.py index 46da932d77..c464f0e39e 100644 --- a/lib/iris/fileformats/pp_rules.py +++ b/lib/iris/fileformats/pp_rules.py @@ -15,6 +15,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Iris. If not, see . +# DO NOT EDIT DIRECTLY +# Auto-generated from SciTools/iris-code-generators:tools/gen_rules.py + import warnings import numpy as np @@ -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 \ @@ -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 \ @@ -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 \