Ejemplo n.º 1
0
def validate_DATERANGE(value, restriction):
    """
        Test to ensure that the times in a timeseries fall between a lower and upper bound
        Parameters: A timeseries in the form [(datetime, val), (datetime, val)..]
        and a tuple containing the lower and upper bound as datetime objects.
    """
    if len(restriction) != 2:
        raise ValidationError(
            "Template ERROR: Only two values can be specified in a date range."
        )

    if type(value) == pd.DataFrame:
        dates = [get_datetime(v) for v in list(value.index)]
    else:
        dates = value

    if type(dates) is list:
        for date in dates:
            validate_DATERANGE(date, restriction)
        return

    min_date = get_datetime(restriction[0])
    max_date = get_datetime(restriction[1])
    if value < min_date or value > max_date:
        raise ValidationError("DATERANGE: %s <%s> %s" %
                              (min_date, value, max_date))
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def check_network(self, request_net, response_net):
        assert response_net.layout == request_net['layout']

        assert response_net.scenarios[0].created_by is not None

        for n in response_net.nodes:
            assert n.x is not None
            assert n.y is not None
            assert len(n.attributes) > 0

        before_times = []

        s = request_net['scenarios'][0]
        ordered_rs_request = sorted(s['resourcescenarios'],
                                    key=lambda x: x.dataset.value)
        for rs0 in ordered_rs_request:

            if rs0.dataset.type == 'timeseries':
                val = json.loads(rs0.dataset.value)
                before_ts_times = list(val.values())[0].keys()
                before_times = []
                for t in before_ts_times:
                    try:
                        before_times.append(get_datetime(t))
                    except Exception as err:
                        before_times.append(t)

        after_times = []
        s = response_net.scenarios[0]
        ordered_rs_response = sorted(s.resourcescenarios,
                                     key=lambda x: x.dataset.value)

        for rs0 in ordered_rs_response:
            if rs0.dataset.type == 'timeseries':
                val = json.loads(rs0.dataset.value)
                after_ts_times = list(val.values())[0].keys()
                after_times = []
                for t in after_ts_times:
                    try:
                        after_times.append(get_datetime(t))
                    except Exception as err:
                        after_times.append(t)

        for d in after_times:
            assert d in before_times, f"{d} is not in {before_times}"
Ejemplo n.º 4
0
def get_scenario_times(dataset):
    """
        Given a timeseries, get the start_time, end_time and time step of a scenario
    """
    ts = json.loads(dataset['value']['value'])
    times = sorted(ts[ts.keys()[0]].keys())

    start_time = get_datetime(times[0])
    second_time = get_datetime(times[1])
    time_step = ""
    diff = second_time - start_time
    end_time = get_datetime(times[-1])

    rd = relativedelta(second_time, start_time).__dict__
    time_order = [
        'years', 'months', 'days', 'hours', 'minutes', 'seconds',
        'microseconds'
    ]
    for time_unit in time_order:
        if rd[time_unit] > 0:
            time_step = "%s %s" % (rd[time_unit], time_unit)

    return str(start_time), str(end_time), time_step