]> AND Private Git Repository - predictops.git/blob - predictops/source/ephemeris.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Refactoring, fin du lever/coucher de soleil, et début de sentinelles
[predictops.git] / predictops / source / ephemeris.py
1 from astral import LocationInfo
2 from astral.sun import sun
3 from configparser import ConfigParser
4 from datetime import datetime, timedelta
5 from logging import getLogger
6 from logging.config import fileConfig
7 from pathlib import Path
8
9 import calendar
10 import pytz
11 import time
12
13 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
14 logger = getLogger()
15
16
17 class Ephemeris:
18
19     _start = None
20     _end = None
21
22     def __init__(self, config_file, start, end):
23
24         self._config = ConfigParser()
25         self._config.read(config_file)
26
27         self._city = LocationInfo("Besançon", "France", "Europe/Paris", 47.237829, -6.0240539)
28         self._start = start
29         self._end = end
30         # Collecting ephemeris features
31         self._features = [section for section in self._config
32                           if self._config[section].getboolean('binary')
33                           or self._config[section].getboolean('categorical')
34                           or self._config[section].getboolean('numerical')]
35
36         self._dated_features = {}
37
38     @property
39     def start(self):
40         return self._start
41
42     @start.setter
43     def start(self, x):
44         self._start = x
45
46     @property
47     def end(self):
48         return self._end
49
50     @end.setter
51     def end(self, x):
52         self._end = x
53
54     @property
55     def dated_features(self):
56         if self._dated_features == {}:
57             logger.info("Adding ephemeris features")
58             paris = pytz.timezone('Europe/Paris')
59             date = self._start
60             while date <= self._end:
61                 datel = paris.localize(date)
62                 dict_hour = {}
63                 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
64                 s = sun(self._city.observer, date=date,
65                         tzinfo=pytz.timezone('Europe/Paris'))
66                 for feature in self._features:
67                     if feature == 'hour':
68                         dict_hour['hour'] = Date.tm_hour
69                     elif feature == 'dayInWeek':
70                         dict_hour['dayInWeek'] = Date.tm_wday
71                     elif feature == 'dayInMonth':
72                         dict_hour['dayInMonth'] = Date.tm_mday
73                     elif feature == 'month':
74                         dict_hour['month'] = Date.tm_mon
75                     elif feature == 'year':
76                         dict_hour['year'] = Date.tm_year
77                     elif feature == 'dayInYear':
78                         # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
79                         # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
80                         # les 24 décembre... tombent toujours
81                         if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/" + str(Date.tm_year), "%d/%m/%Y"):
82                             dict_hour['dayInYear'] = Date.tm_yday - 1
83                         else:
84                             dict_hour['dayInYear'] = Date.tm_yday
85                     elif feature == 'weekInYear':
86                         dict_hour['weekInYear'] = date.isocalendar()[1]
87                     elif feature == 'sunRised':
88                         dict_hour['sunRised'] = (datel >= s["sunrise"] - timedelta(minutes=30)
89                                                  and datel <= s["sunset"] - timedelta(minutes=30))
90                     elif feature == 'noon':
91                         dict_hour['noon'] = (datel.hour == s["noon"].hour)
92                     elif feature == 'night':
93                         dict_hour['night'] = (datel <= s["dawn"] - timedelta(minutes=30)
94                                               or datel >= s["dusk"] - timedelta(minutes=30))
95                     elif feature == 'daylightSavingTime':
96                         dict_hour['daylightSavingTime'] = (datel.dst() == timedelta(0))
97
98                 self._dated_features[date] = dict_hour
99                 date += timedelta(hours=1)
100         return self._dated_features