]> AND Private Git Repository - predictops.git/blobdiff - 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
index d0e4ca06cb14a5a9ec18e17ce626a83498434647..e04a41a46470d7981db259b42c0ba833c8c53639 100644 (file)
@@ -1,35 +1,40 @@
+from astral import LocationInfo
+from astral.sun import sun
 from configparser import ConfigParser
 from configparser import ConfigParser
-from csv import DictReader
 from datetime import datetime, timedelta
 from datetime import datetime, timedelta
+from logging import getLogger
+from logging.config import fileConfig
 from pathlib import Path
 
 from pathlib import Path
 
-import time
 import calendar
 import calendar
+import pytz
+import time
+
+fileConfig((Path.cwd() / 'config') / 'logging.cfg')
+logger = getLogger()
 
 
-CSV_FILE = Path.cwd() / 'config' / 'features' / 'ephemeris_features.csv'
 
 class Ephemeris:
 
     _start = None
 
 class Ephemeris:
 
     _start = None
-    _end   = None
+    _end = None
 
 
-    def __init__(self, config_file):
-
-        # Check for the integrity of feature names
-        super(Source, self).__init__()
+    def __init__(self, config_file, start, end):
 
         self._config = ConfigParser()
         self._config.read(config_file)
 
 
         self._config = ConfigParser()
         self._config.read(config_file)
 
+        self._city = LocationInfo("Besançon", "France", "Europe/Paris", 47.237829, -6.0240539)
+        self._start = start
+        self._end = end
         # Collecting ephemeris features
         # Collecting ephemeris features
-        with open(CSV_FILE, "r") as f:
-            reader = DictReader(f, delimiter=',')
-            self._features = [row['name'] for row in reader
-                              if self._config['FEATURES'].getboolean(row['name'])]
+        self._features = [section for section in self._config
+                          if self._config[section].getboolean('binary')
+                          or self._config[section].getboolean('categorical')
+                          or self._config[section].getboolean('numerical')]
 
         self._dated_features = {}
 
 
         self._dated_features = {}
 
-
     @property
     def start(self):
         return self._start
     @property
     def start(self):
         return self._start
@@ -38,7 +43,6 @@ class Ephemeris:
     def start(self, x):
         self._start = x
 
     def start(self, x):
         self._start = x
 
-
     @property
     def end(self):
         return self._end
     @property
     def end(self):
         return self._end
@@ -47,15 +51,18 @@ class Ephemeris:
     def end(self, x):
         self._end = x
 
     def end(self, x):
         self._end = x
 
-
-
     @property
     def dated_features(self):
         if self._dated_features == {}:
     @property
     def dated_features(self):
         if self._dated_features == {}:
+            logger.info("Adding ephemeris features")
+            paris = pytz.timezone('Europe/Paris')
             date = self._start
             while date <= self._end:
             date = self._start
             while date <= self._end:
+                datel = paris.localize(date)
                 dict_hour = {}
                 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
                 dict_hour = {}
                 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
+                s = sun(self._city.observer, date=date,
+                        tzinfo=pytz.timezone('Europe/Paris'))
                 for feature in self._features:
                     if feature == 'hour':
                         dict_hour['hour'] = Date.tm_hour
                 for feature in self._features:
                     if feature == 'hour':
                         dict_hour['hour'] = Date.tm_hour
@@ -71,12 +78,23 @@ class Ephemeris:
                         # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
                         # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
                         # les 24 décembre... tombent toujours
                         # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
                         # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
                         # les 24 décembre... tombent toujours
-                        if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/"+str(Date.tm_year), "%d/%m/%Y"):
-                            dict_hour['dayInYear'] = Date.tm_yday -1
+                        if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/" + str(Date.tm_year), "%d/%m/%Y"):
+                            dict_hour['dayInYear'] = Date.tm_yday - 1
                         else:
                             dict_hour['dayInYear'] = Date.tm_yday
                     elif feature == 'weekInYear':
                         dict_hour['weekInYear'] = date.isocalendar()[1]
                         else:
                             dict_hour['dayInYear'] = Date.tm_yday
                     elif feature == 'weekInYear':
                         dict_hour['weekInYear'] = date.isocalendar()[1]
+                    elif feature == 'sunRised':
+                        dict_hour['sunRised'] = (datel >= s["sunrise"] - timedelta(minutes=30)
+                                                 and datel <= s["sunset"] - timedelta(minutes=30))
+                    elif feature == 'noon':
+                        dict_hour['noon'] = (datel.hour == s["noon"].hour)
+                    elif feature == 'night':
+                        dict_hour['night'] = (datel <= s["dawn"] - timedelta(minutes=30)
+                                              or datel >= s["dusk"] - timedelta(minutes=30))
+                    elif feature == 'daylightSavingTime':
+                        dict_hour['daylightSavingTime'] = (datel.dst() == timedelta(0))
+
                 self._dated_features[date] = dict_hour
                 date += timedelta(hours=1)
                 self._dated_features[date] = dict_hour
                 date += timedelta(hours=1)
-        return self._dated_features
\ No newline at end of file
+        return self._dated_features