def get_time_region(self, time_region, return_indices=False): assert isinstance(time_region, dict) # return the values to use for the temporal region subsetting. value = self.value_datetime bounds = self.bounds_datetime # switch to indicate if bounds or centroid datetimes are to be used. use_bounds = False if bounds is None else True # remove any none values in the time_region dictionary. this will save # time in iteration. time_region = time_region.copy() time_region = {k: v for k, v in time_region.iteritems() if v is not None} assert len(time_region) > 0 # this is the boolean selection array. select = np.zeros(self.shape[0], dtype=bool) # for each row, determine if the date criterion are met updating the # select matrix accordingly. row_check = np.zeros(len(time_region), dtype=bool) for idx_row in range(select.shape[0]): # do the comparison for each time_region element. if use_bounds: row = bounds[idx_row, :] else: row = value[idx_row] for ii, (k, v) in enumerate(time_region.iteritems()): if use_bounds: to_include = [] for element in v: kwds = {k: element} to_include.append(get_is_date_between(row[0], row[1], **kwds)) fill = any(to_include) else: part = getattr(row, k) fill = True if part in v else False row_check[ii] = fill if row_check.all(): select[idx_row] = True if not select.any(): raise EmptySubsetError(origin='temporal') ret = self[select] if return_indices: raw_idx = np.arange(0, self.shape[0])[select] ret = (ret, raw_idx) return ret
def test_get_is_date_between(self): lower = dt(1971,1,1) upper = dt(2000,2,1) self.assertFalse(get_is_date_between(lower,upper,month=6)) self.assertFalse(get_is_date_between(lower,upper,month=2)) self.assertTrue(get_is_date_between(lower,upper,month=1)) self.assertFalse(get_is_date_between(lower,upper,year=1968)) self.assertTrue(get_is_date_between(lower,upper,year=1995)) lower = dt(2013, 1, 1, 0, 0) upper = dt(2013, 1, 2, 0, 0) self.assertTrue(get_is_date_between(lower,upper,year=2013))
def get_time_region(self, time_region, return_indices=False): """ :param dict time_region: A dictionary defining the time region subset. >>> time_region = {'month': [1, 2, 3], 'year': [2000]} :param bool return_indices: If ``True``, also return the indices used to subset the variable. :return: shallow copy of the sliced time variable :rtype: :class:`~ocgis.TemporalVariable` """ assert isinstance(time_region, dict) # return the values to use for the temporal region subsetting. value = self.value_datetime if self.has_bounds: bounds = self.bounds.value_datetime else: bounds = None # switch to indicate if bounds or centroid datetimes are to be used. use_bounds = False if bounds is None else True # remove any none values in the time_region dictionary. this will save # time in iteration. time_region = time_region.copy() time_region = {k: v for k, v in time_region.items() if v is not None} assert len(time_region) > 0 # this is the boolean selection array. select = np.zeros(self.shape[0], dtype=bool) # for each row, determine if the date criterion are met updating the # select matrix accordingly. row_check = np.zeros(len(time_region), dtype=bool) for idx_row in range(select.shape[0]): # do the comparison for each time_region element. if use_bounds: row = bounds[idx_row, :] else: row = value[idx_row] for ii, (k, v) in enumerate(time_region.items()): if use_bounds: to_include = [] for element in v: kwds = {k: element} to_include.append(get_is_date_between(row[0], row[1], **kwds)) fill = any(to_include) else: part = getattr(row, k) fill = True if part in v else False row_check[ii] = fill if row_check.all(): select[idx_row] = True if not select.any(): raise EmptySubsetError(origin='temporal') ret = self[select] if return_indices: raw_idx = np.arange(0, self.shape[0])[select] ret = (ret, raw_idx) return ret