-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement integrate #2653
Implement integrate #2653
Changes from 4 commits
b0e843f
968b6d0
721ff5a
3decc22
88a8385
9ed470d
42bab02
ecf9318
0561113
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 |
---|---|---|
|
@@ -3866,6 +3866,70 @@ def differentiate(self, coord, edge_order=1, datetime_unit=None): | |
variables[k] = v | ||
return self._replace_vars_and_dims(variables) | ||
|
||
def integrate(self, dim, datetime_unit=None): | ||
""" integrate the array with the trapezoidal rule. | ||
|
||
.. note:: | ||
This feature is limited to simple cartesian geometry, i.e. coord | ||
must be one dimensional. | ||
|
||
Parameters | ||
---------- | ||
dim: str | ||
fujiisoup marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The coordinate to be used to compute the integration. | ||
datetime_unit | ||
Can be specify the unit if datetime coordinate is specified. One of | ||
{'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', 'ps', 'fs', | ||
'as'} | ||
|
||
Returns | ||
------- | ||
integrated: Dataset | ||
|
||
See also | ||
-------- | ||
DataArray.integrate | ||
numpy.trapz: corresponding numpy function | ||
""" | ||
from .variable import Variable | ||
|
||
if dim not in self.variables and dim not in self.dims: | ||
raise ValueError('Coordinate {} does not exist.'.format(dim)) | ||
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 think splitting these checks into two would be a little clearer:
|
||
|
||
coord_var = self[dim].variable | ||
if coord_var.ndim != 1: | ||
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 think this is currently not possible due to xarray's data model, but it's a good idea to add this anyways given that we want to change this soon (e.g., see #2405). I would recommend adjusting this to 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 first thought that it would be nice if we could integrate even along non-dimensional (1d) coordinate (as 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. Yes, that seems reasonable to support 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. Then, 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 don't have a strong opinion here. 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. Well, 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. OK, +1 for consistency with |
||
raise ValueError('Coordinate {} must be 1 dimensional but is {}' | ||
' dimensional'.format(dim, coord_var.ndim)) | ||
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. Likewise, maybe:
|
||
|
||
dim = coord_var.dims[0] | ||
if _contains_datetime_like_objects(coord_var): | ||
if coord_var.dtype.kind in 'mM' and datetime_unit is None: | ||
datetime_unit, _ = np.datetime_data(coord_var.dtype) | ||
elif datetime_unit is None: | ||
datetime_unit = 's' # Default to seconds for cftime objects | ||
coord_var = datetime_to_numeric( | ||
coord_var, datetime_unit=datetime_unit) | ||
|
||
variables = OrderedDict() | ||
coord_names = [] | ||
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.
|
||
for k, v in self.variables.items(): | ||
if k in self.coords: | ||
if dim not in v.dims: | ||
variables[k] = v | ||
coord_names.append(k) | ||
else: | ||
if (k in self.data_vars and dim in v.dims): | ||
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. nit: no need for extra parenthesis here |
||
if _contains_datetime_like_objects(v): | ||
v = datetime_to_numeric(v, datetime_unit=datetime_unit) | ||
integ = duck_array_ops.trapz( | ||
v.data, coord_var.data, axis=v.get_axis_num(dim)) | ||
v_dims = list(v.dims) | ||
v_dims.remove(dim) | ||
variables[k] = Variable(v_dims, integ) | ||
else: | ||
variables[k] = v | ||
return self._replace_vars_and_dims(variables, coord_names=coord_names) | ||
|
||
@property | ||
def real(self): | ||
return self._unary_op(lambda x: x.real, | ||
|
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.
Should we make the default
dim=None
do integration over all dimensions?