1 from configparser import ConfigParser
2 from csv import DictReader
3 from datetime import datetime, timedelta
4 from logging import getLogger
5 from logging.config import fileConfig
6 from pathlib import Path
8 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
14 def __init__(self, config_file = None,
15 start = None, end = None, timestep = None, cumulative = None):
17 self._config = ConfigParser()
18 self._config.read(config_file)
22 self._timestep = timestep
23 self._cumulative = cumulative
25 logger.info('Initialization of target variable')
28 while current <= self._end:
30 current += self._timestep
32 self._timestep = timestep
33 self._stream_file = eval(self._config['DATA']['csv_file'])
34 self._get_located_interventions()
66 def _get_located_interventions(self):
67 if not self._config['SPECIFICATION'].getboolean('origin')\
68 and not self._config['SPECIFICATION'].getboolean('destination'):
69 logger.info('Integrating interventions for the whole area')
70 with open(self._stream_file) as f:
71 reader = DictReader(f, delimiter=',')
74 if row['start'] != '':
75 start_interv = datetime.strptime(row['start'], '%d/%m/%Y %H:%M:%S')
76 start_interv = start_interv.replace(minute=0)
77 end_interv = datetime.strptime(row['end'], '%d/%m/%Y %H:%M:%S')
78 end_interv = end_interv.replace(minute=0)
79 if not (start_interv > self._end or end_interv < self._start):
80 if start_interv < self._start and end_interv <= self._end:
82 while current <= end_interv:
84 current += self._timestep
85 elif start_interv >= self._start and end_interv > self._end:
86 current = start_interv
87 while current not in self._y:
88 current -= timedelta(hours=1)
89 while current <= self._end:
91 current += self._timestep
92 elif start_interv >= self._start and end_interv <= self._end:
93 current = start_interv
94 while current not in self._y:
95 current -= timedelta(hours=1)
96 while current <= end_interv:
98 current += self._timestep
101 if row['start'] != '':
102 start_interv = datetime.strptime(row['start'], '%d/%m/%Y %H:%M:%S')
103 start_interv = start_interv.replace(minute=0)
104 if start_interv in self._y:
105 self._y[start_interv] += 1