-
Notifications
You must be signed in to change notification settings - Fork 301
Switch to using DimCoord.from_regular #642
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In short ... no. 😒
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A method such as
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding Plus there's still the question of how to handle attributes which are specific to DimCoord (i.e. currently just |
||
|
|
||
| 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): | ||
|
|
||
There was a problem hiding this comment.
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_regularto return aSparklyDimCoord.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ofSparklyDimCoord... that's what theclsparameter is for.There was a problem hiding this comment.
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)😞There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, no problem then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@esc24 is too polite to point out that's "we", not "I" 😀