Spaces:
Sleeping
Sleeping
PierreJousselin
commited on
Upload daily_energy_pipeline.py
Browse files- daily_energy_pipeline.py +49 -0
daily_energy_pipeline.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datetime
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
from entsoe import EntsoePandasClient
|
5 |
+
from pymongo.mongo_client import MongoClient
|
6 |
+
from pymongo.server_api import ServerApi
|
7 |
+
|
8 |
+
entsoe_api_key = os.getenv("ENSTO_API")
|
9 |
+
mongo_password= os.getenv("mango_password")
|
10 |
+
client = EntsoePandasClient(api_key=entsoe_api_key)
|
11 |
+
country_code = "SE_3"
|
12 |
+
energy_load_data = pd.DataFrame()
|
13 |
+
|
14 |
+
start_date = pd.Timestamp(
|
15 |
+
datetime.datetime.now() + datetime.timedelta(days=-2), tz="Europe/Berlin"
|
16 |
+
)
|
17 |
+
end_date = pd.Timestamp(
|
18 |
+
datetime.datetime.now() + datetime.timedelta(days=1), tz="Europe/Berlin"
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
load = client.query_load(country_code, start=start_date, end=end_date)
|
24 |
+
if load is not None and not load.empty:
|
25 |
+
# Resample hourly data to daily averages
|
26 |
+
daily_load = load.resample("D").mean()
|
27 |
+
daily_load = daily_load.reset_index()
|
28 |
+
daily_load.columns = ["date", "load"]
|
29 |
+
|
30 |
+
# Append to the main DataFrame
|
31 |
+
energy_load_data = pd.concat([energy_load_data, daily_load], axis=0)
|
32 |
+
energy_load_data['country_code']=country_code
|
33 |
+
|
34 |
+
|
35 |
+
energy_load_data['date'] = pd.to_datetime(energy_load_data['date'], format='%Y-%m-%d')
|
36 |
+
energy_load_data['date'] = pd.to_datetime(energy_load_data['date']).dt.date
|
37 |
+
energy_load_data['date'] = pd.to_datetime(energy_load_data['date'])
|
38 |
+
energy_load_data = energy_load_data.iloc[[-1]]
|
39 |
+
|
40 |
+
|
41 |
+
uri = "mongodb+srv://pgmjo:"+mongo_password+"@cluster0.noq3s.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
42 |
+
# Create a new client and connect to the server
|
43 |
+
client = MongoClient(uri)
|
44 |
+
db = client["daily_energy_load"] # Replace 'mydatabase' with your database name
|
45 |
+
collection = db["S3"] # Replace 'mycollection' with your collection name
|
46 |
+
data_dict = energy_load_data.to_dict("records")
|
47 |
+
|
48 |
+
# Insérer dans la collection MongoDB
|
49 |
+
result = collection.insert_many(data_dict)
|