def handle(self, **options):
        grower = GrowerInfo.objects.get(pk=1)
        basin =  grower.basin_set.all()[0]
        self.ds = FewsJdbcDataSource(basin)
        tbl = DemandTable()
        model = CalculationModel(tbl, self.ds)

        now = datetime.datetime.now(pytz.utc)
        history = now - datetime.timedelta(days=1)
        future = now + datetime.timedelta(days=5)

        now = round_date(now)
        history = round_date(history)
        future = round_date(future)

        self.mkplot('history', history, now)
        self.mkplot('span', history, future)
        self.mkplot('future', now, future)

        now = mktim(2012, 8, 5, 8, 0)  # some rain fell here
        now = round_date(datetime.datetime.now(tz=pytz.utc))
        future = now + settings.CONTROLNEXT_FILL_PREDICT_FUTURE

        ts = model.predict_fill(now, future, 20, 100, 100)

        plot('predict_fill', ts['scenarios']['mean']['prediction'],
             ts['history'])
        # plot('predict_fill_rain', ts['rain'])
        plot('predict_fill_uitstroom',
             ts['scenarios']['mean']['intermediate']['uitstroom'])
        plot('predict_fill_toestroom',
             ts['scenarios']['mean']['intermediate']['toestroom'])
        plot('predict_fill_max_uitstroom',
             ts['intermediate']['max_uitstroom'])
        plot('predict_fill_watervraag', ts['intermediate']['demand'])
Beispiel #2
0
    def test_calc_model(self):
        now = mktim(2012, 8, 5, 8, 0)  # some rain fell here
        now = round_date(datetime.datetime.now(tz=pytz.utc))
        future = now + settings.CONTROLNEXT_FILL_PREDICT_FUTURE

        ts = self.model.predict_fill(now, future, 20, 100, 100)

        self.assertGreater(len(ts['scenarios']['mean']['prediction']), 10)
Beispiel #3
0
    def get(self, request, random_url_slug, *args, **kwargs):
        try:
            self.basin = Basin.objects.filter(
                random_url_slug=random_url_slug)[0]
        except IndexError:
            raise Http404

        self.constants = Constants(self.basin)
        graph_type = request.GET.get('graph_type', None)
        hours_diff = request.GET.get('hours_diff', None)  # debug param

        # overridable variables for demo purposes
        rain_flood_surface = request.GET.get('rain_flood_surface', None)

        # debug params
        if not rain_flood_surface:
            rain_flood_surface = request.GET.get('basin_surface', None)
        basin_storage = request.GET.get('basin_storage', None)
        reverse_osmosis = request.GET.get('reverse_osmosis', None)

        if rain_flood_surface:
            try:
                # basic validation, if not integer, default value is used
                # other validations can be put here, like upper bounds
                self.constants.rain_flood_surface = int(rain_flood_surface)
            except ValueError:
                logger.error("invalid value for rain_flood_surface: %s" %
                             rain_flood_surface)
        if basin_storage:
            try:
                # basic validation, if not integer, default value is used
                # other validations can be put here, like upper bounds
                self.constants.max_storage = int(basin_storage)
            except ValueError:
                logger.error("invalid value for basin_storage: %s" %
                             basin_storage)

        if reverse_osmosis:
            try:
                # basic validation, if not integer, default value is used
                # other validations can be put here, like upper bounds
                self.constants.reverse_osmosis = float(reverse_osmosis)
            except ValueError:
                logger.error("invalid value for reverse_osmosis: %s" %
                             reverse_osmosis)
        osmose_till_date = request.GET.get('osmose_date_till', None)
        if osmose_till_date is not None:
            date_format = "%d-%m-%Y"
            self.constants.osmose_till_date = datetime.datetime.strptime(
                osmose_till_date, date_format).date()

        # note: t0 is Math.floor() 'ed to a full quarter
        t0 = round_date(datetime.datetime.now(pytz.utc))
        if hours_diff:
            t0 += datetime.timedelta(hours=int(hours_diff))

        if graph_type == 'rain':
            response_dict = self.rain(t0)
        elif graph_type == 'prediction' or graph_type == 'meter_comparison':
            outflow_open = request.GET.get('outflowOpen', None)
            outflow_closed = request.GET.get('outflowClosed', None)
            outflow_capacity = request.GET.get('outflowCapacity', 0)
            if outflow_open is not None:
                outflow_open = datetime.datetime.fromtimestamp(
                    int(outflow_open) / 1000, pytz.utc)
            if outflow_closed is not None:
                outflow_closed = datetime.datetime.fromtimestamp(
                    int(outflow_closed) / 1000, pytz.utc)
            response_dict = self.prediction(
                t0, outflow_open, outflow_closed, outflow_capacity)
        else:
            table = EvaporationTable(self.basin,
                                     self.constants.rain_flood_surface)
            future = t0 + settings.CONTROLNEXT_FILL_PREDICT_FUTURE
            history = t0 - settings.CONTROLNEXT_FILL_HISTORY
            data = table.get_demand_raw(history, future)
            result = {
                'graph_info': {
                    'data': self.series_to_js(data),
                }
            }
            response_dict = result

        return RestResponse(response_dict)