home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 137478562

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/557#issuecomment-137478562 https://api.github.com/repos/pydata/xarray/issues/557 137478562 MDEyOklzc3VlQ29tbWVudDEzNzQ3ODU2Mg== 6883049 2015-09-03T15:05:35Z 2015-09-04T09:49:54Z CONTRIBUTOR

I agree with your arguments against creating too many datetime specific methods. However, I am not convinced about using .sel with 'time.year', it looks a bit hackish to me. Maybe all "CDO methods" can be integrated in one single "seltimes" method. Please look at the function below. The time_coord arguments lets you choose the time coordinate that you want to filter, and by using numpy.logical_and is it possible to do the whole operation in one single function call. It could be turned into a method. What do you think?

``` python %matplotlib inline import xray import numpy as np from matplotlib import pyplot as plt

ifile = "HadCRUT.4.3.0.0.median.nc" ixd = xray.open_dataset(ifile) ```

You can download the example file from here http://www.metoffice.gov.uk/hadobs/hadcrut4/data/4.3.0.0/gridded_fields/HadCRUT.4.3.0.0.median_netcdf.zip Now comes the function.

``` python def seltime(ixd, time_coord, **kwargs): """ Select time steps by groups of years, months, etc.

Parameters
----------
ixd : xray.Dataset or xray.DataArray, input dataset. Will be "self" if 
the function is turned into a method.
time_coord: str, name of the time coordinate to use. Needed to avoid ambiguities.
**kwargs: String or interable: Currently years, months, days and hours are supported.

Returns
----------
xray.Dataset or xrat.DataArray filtered by the constraints defined
by the kwargs.
"""
ntimes = len(ixd.coords[time_coord])
time_mask = np.repeat(True, ntimes)

for time_unit, time_values in kwargs.iteritems():
    if time_unit not in ("year", "month", "day", "hour"):
        raise KeyError(time_unit)
    time_str = "{}.{}".format(time_coord, time_unit)
    time_mask_temp = np.in1d(ixd[time_str], time_values)
    time_mask = np.logical_and(time_mask, time_mask_temp)
oxd = ixd.sel(**{time_coord : time_mask})
return oxd

```

It works, and it looks fast enough.

python elnino_years = (1958, 1973, 1983, 1987, 1998) months = (1, 2, 3, 4) %timeit elnino_xd = seltime(ixd, "time", year=elnino_years, month=months) elnino_xd.time

``` 1000 loops, best of 3: 1.05 ms per loop

<xray.DataArray 'time' (time: 20)>
array(['1958-01-16T12:00:00.000000000Z', '1958-02-15T00:00:00.000000000Z',
       '1958-03-16T12:00:00.000000000Z', '1958-04-16T00:00:00.000000000Z',
       '1973-01-16T13:00:00.000000000+0100',
       '1973-02-15T01:00:00.000000000+0100',
       '1973-03-16T13:00:00.000000000+0100',
       '1973-04-16T01:00:00.000000000+0100',
       '1983-01-16T13:00:00.000000000+0100',
       '1983-02-15T01:00:00.000000000+0100',
       '1983-03-16T13:00:00.000000000+0100',
       '1983-04-16T02:00:00.000000000+0200',
       '1987-01-16T13:00:00.000000000+0100',
       '1987-02-15T01:00:00.000000000+0100',
       '1987-03-16T13:00:00.000000000+0100',
       '1987-04-16T02:00:00.000000000+0200',
       '1998-01-16T13:00:00.000000000+0100',
       '1998-02-15T01:00:00.000000000+0100',
       '1998-03-16T13:00:00.000000000+0100',
       '1998-04-16T02:00:00.000000000+0200'], dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 1958-01-16T12:00:00 1958-02-15 ...
Attributes:
    start_month: 1
    start_year: 1850
    end_month: 4
    end_year: 2015
    long_name: time
    standard_name: time
    axis: T

```

If we average in time we can see the warm tonge in the pacific.

python plt.figure(figsize=(12, 5)) elnino_xd.temperature_anomaly.mean("time").plot() plt.gca().invert_yaxis()

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  104484316
Powered by Datasette · Queries took 0.666ms · About: xarray-datasette