def compute(self, surface, crop_evaporation_factor, precipitation, evaporation, seepage):
        """Compute and return the vertical time series for the given surface as dictionary.

        The incoming time series precipitation and evaporation always contain
        non-negative event values although precipitation adds volume to the
        open water and evaporation removes volume. The incoming time series
        seepage can contain both positive and negative event values.

        This method returns the vertical time series as the list of time series
        [precipitation, evaporation, seepage, infiltation], where each time
        series is specified in [m3/day].

        The returned timeseries for precipitation and seepage only contain
        non-negative event values as they add volume to the open water. The
        returned timeseries for evaporation and infiltation only contain
        non-positive event values as they remove volume.

        * surface -- surface in [m2]
        * crop_evaporation factor -- factor to multiply with the evaporation
        * precipitation -- precipitation time series in [mm/day]
        * evaporation -- evaporation time series in [mm/day]
        * seepage -- seepage time series in [mm/day]

        vertical_timeseries_list = [
        timeseries_list = [precipitation, evaporation, seepage]
        index_evaporation = 1
        for event_tuple in enumerate_events(*timeseries_list):
            date = event_tuple[0][0]
            if self.inside_range(date) < 0:
            elif self.inside_range(date) > 0:

            for index in range(0, len(timeseries_list)):
                value = event_tuple[index][1] * surface * 0.001
                if index == index_evaporation:
                    value = value * crop_evaporation_factor
                    if value > 0:
                        value = -value
                vertical_timeseries_list[index].add_value(date, value)
        vertical_timeseries_list[3], vertical_timeseries_list[2] = split_timeseries(vertical_timeseries_list[2])
        logger.debug("precipitation size: %d", len(list(vertical_timeseries_list[0].events())))
        logger.debug("seepage size: %d", len(list(vertical_timeseries_list[2].events())))

        return {
            "precipitation": vertical_timeseries_list[0],
            "evaporation": vertical_timeseries_list[1],
            "seepage": vertical_timeseries_list[2],
            "infiltration": vertical_timeseries_list[3],
    def handle(self, *args, **options):

        parser = OptionParser(option_list=self.option_list)
        (options, args) = parser.parse_args()

        directory = args[1]
        area_slug = args[2]
        start_date = datetime.strptime(args[3], "%Y-%m-%d")
        end_date = datetime.strptime(args[4], "%Y-%m-%d")

        if options.export_table:
            export_table(area_slug, start_date, end_date, directory, directory)
            filename = join(directory, "timeserie.csv")

            area, waterbalance_computer = create_waterbalance_computer(area_slug, start_date, end_date, filename)

            bucket2outcome, level_control, waterbalance_outcome = \
                            waterbalance_computer.compute(area, start_date, end_date)

            f = open(join(directory, "intermediate-results.csv"), "w")
            for bucket, outcome in bucket2outcome.items():
                self.write_timeseries(f,, "afstroming", outcome.flow_off)
                (drainage_timeseries, timeseries) = split_timeseries(outcome.net_drainage)
                self.write_timeseries(f,, "drainage", drainage_timeseries)
                self.write_timeseries(f,, "intrek", timeseries)
                self.write_timeseries(f,, "kwel", outcome.seepage)
                (evaporation_timeseries, timeseries) = split_timeseries(outcome.net_precipitation)
                self.write_timeseries(f,, "verdamping", evaporation_timeseries)
                self.write_timeseries(f,, "neerslag", timeseries)
            self.write_timeseries(f, "openwater", "inlaat peilbeheer", level_control[0])
            self.write_timeseries(f, "openwater", "pomp peilbeheer", level_control[1])

            f = open(join(directory, "Bastiaan-results.csv"), "w")
            f.write("bakje,jaar,maand,dag,berging,afstroming,netto drainage,kwel,netto neerslag\n")
            for bucket, outcome in bucket2outcome.items():
                for event_tuple in enumerate_events(,
                    date = event_tuple[0][0]
                    assert date == event_tuple[1][0]
                    assert date == event_tuple[2][0]
                    assert date == event_tuple[3][0]
                    assert date == event_tuple[4][0]
                    storage = event_tuple[0][1]
                    flow_off = event_tuple[1][1]
                    net_drainage = event_tuple[2][1]
                    seepage = event_tuple[3][1]
                    net_precipitation = event_tuple[4][1]
                    f.write("%s,%d,%d,%d,%f,%f,%f,%f,%f\n" % (, date.year, date.month,, storage, flow_off, net_drainage, seepage, net_precipitation))

            f = open(join(directory, "waterbalance-outcome.csv"), "w")

            for key, timeseries in waterbalance_outcome.open_water_timeseries.iteritems():
                self.write_timeseries(f, "open water timeseries", key, timeseries)

            for key, timeseries in waterbalance_outcome.level_control_assignment.iteritems():
                self.write_timeseries(f, "level control assignment", key, timeseries)

            for key, timeseries in waterbalance_outcome.open_water_fractions.iteritems():
                self.write_timeseries(f, "open water fractions", key, timeseries)

            for key, timeseries in waterbalance_outcome.intake_fractions.iteritems():
                self.write_timeseries(f, "intake fractions", key, timeseries)