def construct(d): if ta.size() != d.size: raise ERAInterimDataRepositoryError("Time axis size {} not equal to the number of " "data points ({}) for {}" "".format(ta.size(), d.size, key)) return tsc(ta.size(), ta.start, ta.delta_t, api.DoubleVector_FromNdArray(d.flatten()), self.series_type[key])
def construct(d): if ta.size() != d.size: raise GFSDataRepositoryError("Time axis size {} not equal to the number of " "data points ({}) for {}" "".format(ta.size(), d.size, key)) return tsc(ta.size(), ta.start, ta.delta_t, api.DoubleVector_FromNdArray(d.flatten()), api.point_interpretation_policy.POINT_AVERAGE_VALUE)
def _numpy_to_geo_ts_vec(data, x, y, z, err): """ Convert timeseries from numpy structures to shyft.api geo-timeseries vector. Parameters ---------- data: dict of tuples (np.ndarray, (list of)api.Time Axis) array with shape (nb_forecasts, nb_lead_times, nb_ensemble_members, nb_points) or (nb_lead_times, nb_ensemble_members, nb_points) or (nb_lead_times, nb_points) one time axis of size nb_lead_times for each forecast x: np.ndarray X coordinates in meters in cartesian coordinate system, with array shape = (nb_points) y: np.ndarray Y coordinates in meters in cartesian coordinate system, with array shape = (nb_points) z: np.ndarray elevation in meters, with array shape = (nb_points) Returns ------- timeseries: dict Time series arrays keyed by type """ geo_pts = api.GeoPointVector.create_from_x_y_z(*[api.DoubleVector_FromNdArray(arr) for arr in [x, y, z]]) shape = list(data.values())[0][0].shape ndim = len(shape) if ndim == 4: nb_forecasts = shape[0] nb_ensemble_members = shape[2] geo_ts = [[{key: create_geo_ts_type_map[key](ta[i], geo_pts, arr[i, :, j, :].transpose(), series_type[key]) for key, (arr, ta) in data.items()} for j in range(nb_ensemble_members)] for i in range(nb_forecasts)] elif ndim == 3: nb_ensemble_members = shape[1] geo_ts = [{key: create_geo_ts_type_map[key](ta, geo_pts, arr[:, j, :].transpose(), series_type[key]) for key, (arr, ta) in data.items()} for j in range(nb_ensemble_members)] elif ndim == 2: geo_ts = {key: create_geo_ts_type_map[key](ta, geo_pts, arr[:, :].transpose(), series_type[key]) for key, (arr, ta) in data.items()} else: raise err("Number of dimensions, ndim, of Numpy array to be converted to shyft GeoTsVector not 2<=ndim<=4.") return geo_ts
def dummy_var(self, input_src_types: list, utc_period: "api.UtcPeriod", geo_location_criteria, ts_interval=86400): """ Purpose is to provide dummy radiation, humidity and wind_speed called from `get_timeseries` method of a `shyft.GeoTsRepository` Will return one source 'station', from the lower left corner of the center of the bounding box. Shyft interpolation will take care of the rest... Returns a timeseries covering the period at the interval defined by ts_interval. Parameters ---------- input_source_types: list List of source types to retrieve (precipitation,temperature..) utc_period: api.UtcPeriod The utc time period that should (as a minimum) be covered. geo_location_criteria: {shapely.geometry.Polygon, shapely.geometry.MultiPolygon} Polygon defining the boundary for selecting points. All points located inside this boundary will be fetched. ts_interval: int [86400] describes the interval used to calculate the periodicity of the timeseries Returns ------- geo_loc_ts: dictionary dictionary keyed by source type, where values are api vectors of geo located timeseries. Important notice: The returned time-series should at least cover the requested period. It could return *more* data than in the requested period, but must return sufficient data so that the f(t) can be evaluated over the requested period. """ utc = api.Calendar() # can use utc calendar as input in utc period n_steps = utc.diff_units(utc_period.start, utc_period.end, ts_interval) if utc_period.end > utc.add(utc_period.start, ts_interval, n_steps): n_steps += 1 ta = api.TimeAxis(utc_period.start.seconds, ts_interval, n_steps) # TODO: could make more sophisticated to get mid point, etc. x, y, urx, ury = geo_location_criteria.bounds x = np.array([x]) y = np.array([y]) z = np.array([1000]) geo_pts = api.GeoPointVector.create_from_x_y_z(*[api.DoubleVector_FromNdArray(arr) for arr in [x, y, z]]) data = {} # TODO: this is where the 'dummy' data is generated. Could be made more robust, quick fix for now for var in input_src_types: if var == 'radiation': data[var] = (np.ones((ta.size(), len(x))) * 50.0, ta) if var == 'wind_speed': data[var] = (np.ones((ta.size(), len(x))) * 2.0, ta) if var == 'relative_humidity': data[var] = (np.ones((ta.size(), len(x))) * 0.6, ta) ndim = len(list(data.values())[0][0].shape) if ndim == 4: raise (RepositoryUtilsError("Dummy not implemented for ensembles")) elif ndim == 3: raise (RepositoryUtilsError("Dummy not implemented for ensembles")) elif ndim == 2: geo_ts = {key: create_geo_ts_type_map[key](ta, geo_pts, arr[:, :].transpose(), self.series_type[key]) for key, (arr, ta) in data.items()} else: raise RepositoryUtilsError( "Number of dimensions, ndim, of Numpy array to be converted to shyft GeoTsVector not 2<=ndim<=4.") return geo_ts
def dummy_var(input_src_types, utc_period, geo_location_criteria): """ A copy of utils._numpy_to_geo_ts_vec in order to dummy radiation, humidity and wind_speed Convert timeseries from numpy structures to shyft.api geo-timeseries vector. Parameters ---------- data: dict of np.ndarray array with shape (nb_forecasts, nb_lead_times, nb_ensemble_members, nb_points) or (nb_lead_times, nb_ensemble_members, nb_points) or (nb_lead_times, nb_points) x: np.ndarray X coordinates in meters in cartesian coordinate system, with array shape = (nb_points) y: np.ndarray Y coordinates in meters in cartesian coordinate system, with array shape = (nb_points) z: np.ndarray elevation in meters, with array shape = (nb_points) Returns ------- timeseries: dict Time series arrays keyed by type """ ndays = (utc_period.end - utc_period.start) / 86400 def _ta(t): t0 = int(t[0]) t1 = int(t[1]) return api.TimeAxis(t0, t1 - t0, len(t)) times = np.linspace(utc_period.start, utc_period.end, ndays, 86400) ta = _ta(times) x, y, urx, ury = geo_location_criteria.bounds x = np.array([x]) y = np.array([y]) z = np.array([1000]) geo_pts = api.GeoPointVector.create_from_x_y_z(*[api.DoubleVector_FromNdArray(arr) for arr in [x, y, z]]) data = {} for var in input_src_types: if var == 'radiation': data[var] = ( np.ones((len(times), len(x))) * 1, ta) if var == 'wind_speed': data[var] = (np.ones((len(times), len(x))) * 2, ta) if var == 'relative_humidity': data[var] = (np.ones((len(times), len(x))) * 0.6, ta) shape = list(data.values())[0][0].shape ndim = len(shape) if ndim == 4: raise(InterfaceError("Dummy not implemented for ensembles")) elif ndim == 3: raise (InterfaceError("Dummy not implemented for ensembles")) elif ndim == 2: geo_ts = {key: create_geo_ts_type_map[key](ta, geo_pts, arr[:, :].transpose(), series_type[key]) for key, (arr, ta) in data.items()} else: raise err("Number of dimensions, ndim, of Numpy array to be converted to shyft GeoTsVector not 2<=ndim<=4.") return geo_ts