def get_vals_between_times(dataset_id, start_time, end_time, timestep,increment,**kwargs): """ Retrive data between two specified times within a timeseries. The times need not be specified in the timeseries. This function will 'fill in the blanks'. Two types of data retrieval can be done. If the timeseries is timestamp-based, then start_time and end_time must be datetimes and timestep must be specified (minutes, seconds etc). 'increment' reflects the size of the timestep -- timestep = 'minutes' and increment = 2 means 'every 2 minutes'. If the timeseries is float-based (relative), then start_time and end_time must be decimal values. timestep is ignored and 'increment' represents the increment to be used between the start and end. Ex: start_time = 1, end_time = 5, increment = 1 will get times at 1, 2, 3, 4, 5 """ try: server_start_time = get_datetime(start_time) server_end_time = get_datetime(end_time) times = [server_start_time] next_time = server_start_time while next_time < server_end_time: if int(increment) == 0: raise HydraError("%s is not a valid increment for this search."%increment) next_time = next_time + datetime.timedelta(**{timestep:int(increment)}) times.append(next_time) except ValueError: try: server_start_time = Decimal(start_time) server_end_time = Decimal(end_time) times = [server_start_time] next_time = server_start_time while next_time < server_end_time: next_time = next_time + increment times.append(next_time) except: raise HydraError("Unable to get times. Please check to and from times.") td = DBSession.query(Dataset).filter(Dataset.dataset_id==dataset_id).one() log.debug("Number of times to fetch: %s", len(times)) data = td.get_val(timestamp=times) data_to_return = [] if type(data) is list: for d in data: if d is not None: data_to_return.append(list(d)) elif data is None: data_to_return = [] else: data_to_return.append(data) dataset = {'data' : json.dumps(data_to_return)} return dataset
def get_time_axis(self, start_time, end_time, time_step, time_axis=None): """ Create a list of datetimes based on an start time, end time and time step. If such a list is already passed in, then this is not necessary. Often either the start_time, end_time, time_step is passed into an app or the time_axis is passed in directly. This function returns a time_axis in both situations. """ if time_axis is not None: actual_dates_axis = [] for t in time_axis: #If the user has entered the time_axis with commas, remove them. t = t.replace(',', '').strip() if t == '': continue actual_dates_axis.append(get_datetime(t)) return actual_dates_axis else: if start_time is None: raise HydraPluginError("A start time must be specified") if end_time is None: raise HydraPluginError("And end time must be specified") if time_step is None: raise HydraPluginError("A time-step must be specified") start_date = get_datetime(start_time) end_date = get_datetime(end_time) delta_t, value, units = self.parse_time_step(time_step) time_axis = [start_date] value = int(value) while start_date < end_date: #Months and years are a special case, so treat them differently if(units.lower() == "mon"): start_date = start_date + relativedelta(months=value) elif (units.lower() == "yr"): start_date = start_date + relativedelta(years=value) else: start_date += timedelta(seconds=delta_t) time_axis.append(start_date) return time_axis
def get_time_axis(self, start_time, end_time, time_step, time_axis=None): """ Create a list of datetimes based on an start time, end time and time step. If such a list is already passed in, then this is not necessary. Often either the start_time, end_time, time_step is passed into an app or the time_axis is passed in directly. This function returns a time_axis in both situations. """ if time_axis is not None: actual_dates_axis = [] for t in time_axis: #If the user has entered the time_axis with commas, remove them. t = t.replace(',', '').strip() if t == '': continue actual_dates_axis.append(get_datetime(t)) return actual_dates_axis else: if start_time is None: raise HydraPluginError("A start time must be specified") if end_time is None: raise HydraPluginError("And end time must be specified") if time_step is None: raise HydraPluginError("A time-step must be specified") start_date = get_datetime(start_time) end_date = get_datetime(end_time) delta_t, value, units = self.parse_time_step(time_step) time_axis = [start_date] value = int(value) while start_date < end_date: #Months and years are a special case, so treat them differently if (units.lower() == "mon"): start_date = start_date + relativedelta(months=value) elif (units.lower() == "yr"): start_date = start_date + relativedelta(years=value) else: start_date += timedelta(seconds=delta_t) time_axis.append(start_date) return time_axis
def check_network(client, request_net, response_net): assert repr(response_net.layout) == repr(request_net['layout']) assert response_net.scenarios.Scenario[0].created_by is not None for n in response_net.nodes.Node: assert n.x is not None assert n.y is not None assert len(n.attributes.ResourceAttr) > 0 before_times = [] s = request_net['scenarios'].Scenario[0] for rs0 in s['resourcescenarios'].ResourceScenario: if rs0['value']['type'] == 'timeseries': val = json.loads(rs0['value']['value']) before_ts_times = val.values()[0].keys() before_times = [] for t in before_ts_times: try: before_times.append(get_datetime(t)) except: before_times.append(t) after_times = [] s = response_net.scenarios.Scenario[0] for rs0 in s.resourcescenarios.ResourceScenario: if rs0.value.type == 'timeseries': val = json.loads(rs0.value.value) after_ts_times = val.values()[0].keys() after_times = [] for t in after_ts_times: try: after_times.append(get_datetime(t)) except: after_times.append(t) for d in after_times: assert d in before_times, "%s is incorrect"%(d)
def check_network(client, request_net, response_net): assert repr(response_net.layout) == repr(request_net['layout']) assert response_net.scenarios.Scenario[0].created_by is not None for n in response_net.nodes.Node: assert n.x is not None assert n.y is not None assert len(n.attributes.ResourceAttr) > 0 before_times = [] s = request_net['scenarios'].Scenario[0] for rs0 in s['resourcescenarios'].ResourceScenario: if rs0['value']['type'] == 'timeseries': val = json.loads(rs0['value']['value']) before_ts_times = val.values()[0].keys() before_times = [] for t in before_ts_times: try: before_times.append(get_datetime(t)) except: before_times.append(t) after_times = [] s = response_net.scenarios.Scenario[0] for rs0 in s.resourcescenarios.ResourceScenario: if rs0.value.type == 'timeseries': val = json.loads(rs0.value.value) after_ts_times = val.values()[0].keys() after_times = [] for t in after_ts_times: try: after_times.append(get_datetime(t)) except: after_times.append(t) for d in after_times: assert d in before_times, "%s is incorrect" % (d)
def get_val_at_time(dataset_id, timestamps,**kwargs): """ Given a timestamp (or list of timestamps) and some timeseries data, return the values appropriate to the requested times. If the timestamp is before the start of the timeseries data, return None If the timestamp is after the end of the timeseries data, return the last value. """ t = [] for time in timestamps: t.append(get_datetime(time)) dataset_i = DBSession.query(Dataset).filter(Dataset.dataset_id==dataset_id).one() #for time in t: # data.append(td.get_val(timestamp=time)) data = dataset_i.get_val(timestamp=t) if data is not None: dataset = {'data': json.dumps(data)} else: dataset = {'data': None} return dataset
def get_multiple_vals_at_time(dataset_ids, timestamps,**kwargs): """ Given a timestamp (or list of timestamps) and a list of timeseries datasets, return the values appropriate to the requested times. If the timestamp is before the start of the timeseries data, return None If the timestamp is after the end of the timeseries data, return the last value. """ datasets = _get_datasets(dataset_ids) datetimes = [] for time in timestamps: datetimes.append(get_datetime(time)) return_vals = {} for dataset_i in datasets.values(): data = dataset_i.get_val(timestamp=datetimes) ret_data = {} if type(data) is list: for i, t in enumerate(timestamps): ret_data[t] = data[i] return_vals['dataset_%s'%dataset_i.dataset_id] = json.dumps(ret_data) return return_vals
def get_multiple_vals_at_time(dataset_ids, timestamps, **kwargs): """ Given a timestamp (or list of timestamps) and a list of timeseries datasets, return the values appropriate to the requested times. If the timestamp is before the start of the timeseries data, return None If the timestamp is after the end of the timeseries data, return the last value. """ datasets = _get_datasets(dataset_ids) datetimes = [] for time in timestamps: datetimes.append(get_datetime(time)) return_vals = {} for dataset_i in datasets.values(): data = dataset_i.get_val(timestamp=datetimes) ret_data = {} if type(data) is list: for i, t in enumerate(timestamps): ret_data[t] = data[i] return_vals['dataset_%s' % dataset_i.dataset_id] = json.dumps(ret_data) return return_vals