def at(self, points, time, units=None, extrapolate=False, memoize=True, _hash=None, _auto_align=True, **kwargs): pts = _reorganize_spatial_data(points) mem = memoize if hash is None: _hash = self._get_hash(points, time) if mem: res = self._get_memoed(points, time, self._result_memo, _hash=_hash) if res is not None: return res value = np.column_stack([var.at(points=points, time=time, units=units, extrapolate=extrapolate, memoize=memoize, _hash=_hash, **kwargs) for var in self.variables]) if _auto_align == True: value = _align_results_to_spatial_data(value.copy(), points) if mem: self._memoize_result(points, time, value, self._result_memo, _hash=_hash) return value
def test_reorganize_spatial_data(): # 1-dimensional data sample_1 = [1, 2, 3] sample_2 = [(1, ), (2, ), (3, )] sample_3 = [ (1, 2, 3), ] a1 = utilities._reorganize_spatial_data(sample_1) a2 = utilities._reorganize_spatial_data(sample_2) a3 = utilities._reorganize_spatial_data(sample_3) assert np.all(a1 == a2) assert np.all(a3 == np.array([ (1, 2, 3), ])) assert np.all(a2 == a3) # impossible cases sample_1 = np.array([[1, 2, 3], [4, 5, 6]]) sample_2 = np.array([[1, 2], [3, 4], [5, 6]]) a1 = utilities._reorganize_spatial_data(sample_1) a2 = utilities._reorganize_spatial_data(sample_2) assert np.all(sample_2 == a2) assert np.all(sample_1 == a1) # dim 0 > dim 1 sample_1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) a1 = utilities._reorganize_spatial_data(sample_1) assert np.all(a1 == sample_1) # dim 1 > dim 0 sample_2 = [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]] a2 = utilities._reorganize_spatial_data(sample_2) assert np.all(a2 == sample_1)
def at(self, points, time, units=None, extrapolate=False, _hash=None, _mem=True, _auto_align=True, **kwargs): ''' Find the value of the property at positions P at time T :param points: Coordinates to be queried (P) :param time: The time at which to query these points (T) :param units: units the values will be returned in (or converted to) :param extrapolate: if True, extrapolation will be supported :type points: Nx2 array of double :type time: datetime.datetime object :type depth: integer :type units: string such as ('mem/s', 'knots', etc) :type extrapolate: boolean (True or False) :return: returns a Nx1 array of interpolated values :rtype: double ''' pts = _reorganize_spatial_data(points) if _hash is None: _hash = self._get_hash(pts, time) if _mem: res = self._get_memoed(pts, time, self._result_memo, _hash=_hash) if res is not None: return res order = self.dimension_ordering if order[0] == 'time': value = self._time_interp(pts, time, extrapolate, _mem=_mem, _hash=_hash, **kwargs) elif order[0] == 'depth': value = self._depth_interp(pts, time, extrapolate, _mem=_mem, _hash=_hash, **kwargs) else: value = self._xy_interp(pts, time, extrapolate, _mem=_mem, _hash=_hash, **kwargs) if _auto_align == True: value = _align_results_to_spatial_data(value.copy(), points) if _mem: self._memoize_result(pts, time, value, self._result_memo, _hash=_hash) return value
def at(self, points, time, units=None, extrapolate=None, auto_align=True, **kwargs): ''' Interpolates this property to the given points at the given time with the units specified. :param points: A Nx2 array of lon,lat points :param time: A datetime object. May be None; if this is so, the variable is assumed to be gridded but time-invariant :param units: The units that the result would be converted to ''' pts = _reorganize_spatial_data(points) value = None if len(self.time) == 1: value = self.data else: if extrapolate is None: extrapolate = self.extrapolate if not extrapolate: self.time.valid_time(time) if extrapolate and time > self.time.max_time: value = self.data[-1] if extrapolate and time <= self.time.min_time: value = self.data[0] if value is None: t_index = self.time.index_of(time, extrapolate) t_alphas = self.time.interp_alpha(time, extrapolate) d0 = self.data[max(t_index - 1, 0)] d1 = self.data[t_index] value = d0 + (d1 - d0) * t_alphas data_units = self.units if self.units else self._gnome_unit req_units = units if units else data_units #Try to convert units. This is the same as in gridded_objects_base.Variable if data_units is not None and data_units != req_units: try: value = uc.convert(data_units, req_units, value) except uc.NotSupportedUnitError: if (not uc.is_supported(data_units)): warnings.warn("{0} units is not supported: {1}".format( self.name, data_units)) elif (not uc.is_supported(req_units)): warnings.warn( "Requested unit is not supported: {1}".format( req_units)) else: raise if points is None: return value else: rval = np.full((pts.shape[0], 1), value, dtype=np.float64) if auto_align: return _align_results_to_spatial_data(rval, points) else: return rval