From 312e79480bf7c60ee9f6231aec2b69891bf79850 Mon Sep 17 00:00:00 2001
From: Le Roux Erwan <erwan.le-roux@irstea.fr>
Date: Mon, 23 Mar 2020 16:08:10 +0100
Subject: [PATCH] [contrasting project] add weather type loader

---
 extreme_data/edf_data/weather_types.py        | 23 +++++++++++++++++++
 .../test_edf_data/test_weather_types.py       | 19 +++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 extreme_data/edf_data/weather_types.py
 create mode 100644 test/test_extreme_data/test_edf_data/test_weather_types.py

diff --git a/extreme_data/edf_data/weather_types.py b/extreme_data/edf_data/weather_types.py
new file mode 100644
index 00000000..e22672dd
--- /dev/null
+++ b/extreme_data/edf_data/weather_types.py
@@ -0,0 +1,23 @@
+import os
+import os.path as op
+import numpy as np
+import pandas as pd
+from root_utils import get_full_path
+
+relative_path = r'local/EDF_data/Weather_types/CatalogueTT_EDF_France0_5308.txt'
+edf_filepath = get_full_path(relative_path=relative_path)
+
+
+def load_df_weather_types() -> pd.DataFrame:
+    global df
+    weather_types = []
+    with open(edf_filepath, 'rb') as f:
+        for i, l in enumerate(f):
+            if i >= 7:
+                l = str(l).split('"')[1:]
+                weather_types.append((l[0], int(l[1][2])))
+    df = pd.DataFrame(weather_types, columns=['Date', 'WP'])
+    df.set_index('Date', inplace=True)
+    return df
+
+
diff --git a/test/test_extreme_data/test_edf_data/test_weather_types.py b/test/test_extreme_data/test_edf_data/test_weather_types.py
new file mode 100644
index 00000000..2616b49b
--- /dev/null
+++ b/test/test_extreme_data/test_edf_data/test_weather_types.py
@@ -0,0 +1,19 @@
+import unittest
+
+from extreme_data.edf_data.weather_types import load_df_weather_types
+from extreme_data.meteo_france_data.scm_models_data.safran.safran import SafranTemperature
+
+
+class TestWeatherTypes(unittest.TestCase):
+
+    def test_df_weather_types(self):
+        df = load_df_weather_types()
+        self.assertEqual(len(df), 20354)
+        first = df.iloc[0].values[0]
+        last = df.iloc[-1].values[0]
+        self.assertEqual(first, 5)
+        self.assertEqual(last, 8)
+
+
+if __name__ == '__main__':
+    unittest.main()
-- 
GitLab