def get_demand_raw(self, _from, to): """Use for demand chart.""" validate_date(_from) validate_date(to) from_adj = _from to_adj = to dayly = pd.date_range(from_adj, to_adj, freq="D", tz=pytz.utc) values = [self.get_day_evaporation_on(date) for date in dayly] ts = pd.Series(values, dayly, name="evaporation") # use fillna with ffill instead of ts.interpolate() to keep the data # as accurate as possible ts.fillna(method="ffill", inplace=True) # divide by amount of quarter hours in a day return ts[from_adj:to_adj]
def get_rain(self, which, _from, to, *args, **kwargs): validate_date(_from) validate_date(to) rain_filter_id = self.basin.rain_filter_id rain_location_id = self.basin.rain_location_id rain = self._get_timeseries_as_pd_series( rain_filter_id, rain_location_id, RAIN_PARAMETER_IDS[which], _from, to, 'rain_' + which ) # convert to quarterly figures rain = rain.resample('15min', fill_method='ffill') # 4 quarters in an hour if which != 'kwadrant': rain /= 4 return rain
def get_fill(self, _from, to, *args, **kwargs): validate_date(_from) validate_date(to) # basin parameters fill_filter_id = self.basin.filter_id fill_location_id = self.basin.location_id fill_parameter_id = self.basin.parameter_id # max_storage can come from request, therefore from self.constants max_storage = self.constants.max_storage # values are given in percentage as to max_storage ts = self._get_timeseries_as_pd_series( fill_filter_id, fill_location_id, fill_parameter_id, _from, to, 'fill' ) ts = self.fill_pct_to_m3(ts, max_storage) return ts
def get_demand(self, _from, to): """Use for prediction chart. Convert demand from m2 to m3.""" validate_date(_from) validate_date(to) # ensure we deal with broader range to avoid resample edge problems from_adj = _from to_adj = to dayly = pd.date_range(from_adj, to_adj, freq="D", tz=pytz.utc) values = [self.get_day_evaporation_on(date) for date in dayly] ts = pd.Series(values, dayly, name="evaporation") ts = ts.resample("15min") # use fillna with ffill instead of ts.interpolate() to keep the data # as accurate as possible ts.fillna(method="ffill", inplace=True) # divide by amount of quarter hours in a day ts /= 24 * 4 ts *= float(0.001) ts *= self.crop_surface ts *= self.basin.recirculation result = ts[from_adj:to_adj] return result
def predict_fill(self, _from, to, outflow_open=None, outflow_closed=None, outflow_capacity=0): # do some input validation here, to ensure we are dealing with sane # numbers validate_date(_from) validate_date(to) rain_mean = self.fews_data.get_rain('mean', _from, to) # create a no rain series periods = (to - _from).days * 4 * 24 + 1 values = np.zeros(periods) dates = pd.date_range(_from, periods=periods, freq='15min', tz=pytz.utc) rain_zero = pd.Series(values, dates, name='uitstroom') # gebruik de datum van de laatst beschikbaar regenvoorspelling als # from en to waarden _from_rain = rain_mean.index[0] to_rain = rain_mean.index[-1] # retrieve fill: just take any data we have, # so we can compare measurements with predictions current_fill = self.fews_data.get_current_fill(to_rain) current_fill_m3 = current_fill['current_fill_m3'] # bereken watervraag over deze periode demand_m3_rain = self.demand_table.get_demand(_from_rain, to_rain) demand_m3_zero_rain = self.demand_table.get_demand(_from, to) # leidt aantal periodes af uit een vd 'input' tijdreeksen periods_rain = len(rain_mean) # bereken uitstroom max_uitstroom_m3 = self.calc_max_uitstroom(_from_rain, periods_rain) if len(max_uitstroom_m3) != len(rain_mean): raise Exception('%s != %s' % (len(max_uitstroom_m3), len(rain_mean))) # return ook tussentijdse waarden, vnml. voor debugging result = { 'scenarios': {}, 'history': fill_m3_to_pct( current_fill['fill_history_m3'], self.constants.max_storage), 'current_fill': fill_m3_to_pct( current_fill_m3, self.constants.max_storage), 'intermediate': { 'rain_mean': rain_mean, 'max_uitstroom': max_uitstroom_m3, 'demand': demand_m3_rain, } } # bereken de drie scenarios calc_scenarios = { 'no_rain': (rain_zero, demand_m3_zero_rain), 'mean': (rain_mean, demand_m3_rain), } for scenario, (rain, demand_m3_) in calc_scenarios.items(): result['scenarios'][scenario] = self.predict_scenario( _from, current_fill_m3, demand_m3_, rain, outflow_open, outflow_closed, outflow_capacity) return result