def save_timeserie_into_database(wb_timeserie, sheet, row, date_col, value_col, stick_to_last_value=False): # set timeserie settings timeserie_stub = TimeseriesStub() wb_timeserie.use_fews = False try: for rownr in range(row, row + 10000): try: date = xlrd.xldate_as_tuple(sheet.cell(rownr, date_col).value, 0) value = float(sheet.cell(rownr, value_col).value) timeserie_stub.add_value(datetime(date[0], date[1], date[2]), value) except ValueError: # skip timestamp pass except IndexError: pass if wb_timeserie.local_timeseries: timeseries = wb_timeserie.local_timeseries timeseries.stick_to_last_value = stick_to_last_value timeseries.timeseries_events.all().delete() else: timeseries = Timeseries.objects.create(name=wb_timeserie.name, stick_to_last_value=stick_to_last_value) wb_timeserie.local_timeseries = timeseries print "start save timeserie %s" % datetime.now() timeseries.save_timeserie_stub(timeserie_stub) print "finish save timeserie %s" % datetime.now() wb_timeserie.save()
def retrieve_timeseries(timeseries_retriever, name, start_date, end_date): timeseries = TimeseriesStub() for event in timeseries_retriever.get_timeseries(name).events(): if event[0] < start_date: continue if event[0] < end_date: timeseries.add_value(event[0], event[1]) else: break return timeseries
def ts_factory(*args): """Return a TimeseriesStub for the given arguments. Parameters: * arg[0] * first date of the time series * arg[1:] * values of the daily events starting on the first date """ date = args[0] ts = TimeseriesStub() for arg in args[1:]: ts.add_value(date, arg) date = date + timedelta(1) return ts
def create_save_yearly_timeserie_into_database( wb_timeserie, array_date_value, start_year=1996, end_year=2015, stick_to_last_value=True ): # set timeserie settings timeserie_stub = TimeseriesStub() wb_timeserie.use_fews = False for year in range(start_year, end_year): for month, day, value in array_date_value: timeserie_stub.add_value(datetime(year, month, day), value) if wb_timeserie.local_timeseries: timeseries = wb_timeserie.local_timeseries timeseries.stick_to_last_value = True timeseries.timeseries_events.all().delete() else: timeseries = Timeseries.objects.create(name=wb_timeserie.name, stick_to_last_value=True) wb_timeserie.local_timeseries = timeseries print "start save timeserie %s" % datetime.now() timeseries.save_timeserie_stub(timeserie_stub) print "finish save timeserie %s" % datetime.now() wb_timeserie.save()
def test_k(self): """Test compute_timeseries starts with the correct initial water volume.""" today = datetime(2010,12,2) evaporation = TimeseriesStub() evaporation.add_value(today, 20) precipitation = TimeseriesStub() precipitation.add_value(today, 5) seepage = TimeseriesStub() seepage.add_value(today, 10) mock = Mock({"compute": (0, 0, 0, 0, 0)}) # we do not need the return value of the next call and we discard it compute_timeseries(self.bucket, evaporation, precipitation, seepage, mock.compute) calls_to_compute = mock.getNamedCalls('compute') self.assertTrue(len(calls_to_compute) > 0) supplied_volume = calls_to_compute[0].getParam(1) expected_volume = self.bucket.init_water_level * self.bucket.surface * self.bucket.porosity self.assertAlmostEqual(supplied_volume, expected_volume)
def test_m(self): """Test compute_timeseries supplies the correct time serie values.""" today = datetime(2010,12,2) tomorrow = datetime(2010,12,3) evaporation = TimeseriesStub() evaporation.add_value(today, 20) evaporation.add_value(tomorrow, 30) precipitation = TimeseriesStub() precipitation.add_value(today, 5) precipitation.add_value(tomorrow, 10) seepage = TimeseriesStub() seepage.add_value(today, 10) seepage.add_value(tomorrow, 20) mock = Mock({"compute": (0, 0, 0, 0, 0)}) # we do not need the return value of the next call and we discard it compute_timeseries(self.bucket, evaporation, precipitation, seepage, mock.compute) calls_to_compute = mock.getNamedCalls('compute') self.assertEqual(2, len(calls_to_compute)) supplied_precipitation = calls_to_compute[1].getParam(2) expected_precipitation = 30 self.assertAlmostEqual(supplied_precipitation, expected_precipitation) supplied_evaporation = calls_to_compute[1].getParam(3) expected_evaporation = 10 self.assertAlmostEqual(supplied_evaporation, expected_evaporation) supplied_seepage = calls_to_compute[1].getParam(4) expected_seepage = 20 self.assertAlmostEqual(supplied_seepage, expected_seepage)