def resample(gdata, **kwargs): """resample ``gdata`` according to rules. currently, only monthly resampling for ``time`` coordinate is supported. time='monthly' """ # TODO: this is very preliminary, and verrrry ugggly # TODO: this only works if the coordinates are ordered time-long-lat for kw in list(kwargs.keys()): if kw in list(gdata.coordinates.keys()): if kw != 'time': raise ValueError("You asked me to resample along coordinate " "'%s', but I don't know how to do that." % kw) if kwargs[kw] not in ['annual', 'yearly', 'monthly']: raise ValueError("You asked me to resample alon the time " "coordinate according to the rule '%$s', " "but I don't know now to do that." % kwargs[kw]) if kwargs[kw] in ['annual', 'yearly']: freq = 'Y' elif kwargs[kw] in ['monthly']: freq = 'M' times = pd.period_range( start=gdata.coordinates['time'].min().astype(object), end=gdata.coordinates['time'].max().astype(object), freq=freq) data = np.ones(np.r_[times.size, gdata.data.shape[1:]]) * np.nan for i, m in enumerate(times): f = _get_timeselect_func(m, kwargs[kw]) tmpdata = select(gdata, time=f) data[i] = tmpdata.mean(axis='time').data newcoords = copy.copy(gdata.coordinates) newcoords['time'] = np.array(times.to_datetime()) newdata = gridded_array(data, newcoords, title=gdata.title) return newdata
def select(gdata, **kwargs): """pass selection lambda function as kwargs, e.g.: time=lambda x: x.month == 1 """ # TODO: this is very preliminary, and verrrry ugggly for kw in list(kwargs.keys()): if kw in list(gdata.coordinates.keys()): f = kwargs[kw] if isinstance(f, str): f = _timeselect_funcs[f] if not hasattr(f, '__call__'): raise ValueError("You asked me to select using a descriptor I" "don't understand!") if kw in ['time', 'datetime', 'date']: # TODO idx = ([f(d) for d in gdata.coordinates[kw].astype(object)]) # TODO: boolean indexing with 1d index array on 3d data # array doesn't work # uggggly workaround idx = np.where(idx, np.arange(len(idx), dtype=float), np.nan) idx = ma.masked_invalid(idx) idx = idx.compressed() idx = np.int32(idx) # end of ugggggly workaround newcoords = copy.copy(gdata.coordinates) newcoords['time'] = newcoords['time'][idx] newdata = gdata.data[idx] return gridded_array(newdata, newcoords, gdata.title)
def resample(gdata, **kwargs): """resample ``gdata`` according to rules. currently, only monthly resampling for ``time`` coordinate is supported. time='monthly' """ # TODO: this is very preliminary, and verrrry ugggly # TODO: this only works if the coordinates are ordered time-long-lat for kw in list(kwargs.keys()): if kw in list(gdata.coordinates.keys()): if kw != 'time': raise ValueError("You asked me to resample along coordinate " "'%s', but I don't know how to do that." % kw) if kwargs[kw] not in ['annual', 'yearly', 'monthly']: raise ValueError("You asked me to resample alon the time " "coordinate according to the rule '%$s', " "but I don't know now to do that." % kwargs[kw]) if kwargs[kw] in ['annual', 'yearly']: freq = 'Y' elif kwargs[kw] in ['monthly']: freq = 'M' times = pd.period_range(start=gdata.coordinates['time'].min().astype(object), end=gdata.coordinates['time'].max().astype(object), freq=freq) data = np.ones(np.r_[times.size, gdata.data.shape[1:]]) * np.nan for i, m in enumerate(times): f = _get_timeselect_func(m, kwargs[kw]) tmpdata = select(gdata, time=f) data[i] = tmpdata.mean(axis='time').data newcoords = copy.copy(gdata.coordinates) newcoords['time'] = np.array(times.to_datetime()) newdata = gridded_array(data, newcoords, title=gdata.title) return newdata
def get_slice(gdata, **kwargs): # TODO: This really should go into the gridded_array class # open the coordinate variables dimorder = {gdata.coordinates.keys().index(k) : k for k in gdata.coordinates.keys()} coordinates = OrderedDict() for d in range(len(dimorder)): coordinates[dimorder[d]] = gdata.coordinates[dimorder[d]] # coordinate slicing slices = get_coordinate_slices(coordinates, kwargs) # slice the coordinate arrays themselves for i, c in enumerate(list(coordinates.keys())): coordinates[c] = coordinates[c][slices[i]] # read requested slice from disk data = gdata.data[slices] return gridded_array(data, coordinates, gdata.title)
def get_slice(gdata, **kwargs): # TODO: This really should go into the gridded_array class # open the coordinate variables dimorder = { gdata.coordinates.keys().index(k): k for k in gdata.coordinates.keys() } coordinates = OrderedDict() for d in range(len(dimorder)): coordinates[dimorder[d]] = gdata.coordinates[dimorder[d]] # coordinate slicing slices = get_coordinate_slices(coordinates, kwargs) # slice the coordinate arrays themselves for i, c in enumerate(list(coordinates.keys())): coordinates[c] = coordinates[c][slices[i]] # read requested slice from disk data = gdata.data[slices] return gridded_array(data, coordinates, gdata.title)