btd-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
btd_client/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .source import BalticTransparencyClient
btd_client/source.py ADDED
@@ -0,0 +1,178 @@
1
+ import requests
2
+ import json
3
+ import pandas as pd
4
+ from datetime import datetime
5
+
6
+ class BalticTransparencyClient:
7
+ """
8
+ Form GET requests to Baltic Transparency dashboard. Retrieve relevant data, format it and return as Pandas DataFrame.
9
+
10
+ Parameters
11
+ ----------
12
+ start_date : str
13
+ Starting date of data. Format 'yyyy-mm-ddTHH-mm'.
14
+ end_date : str
15
+ Last date of data. Format 'yyyy-mm-ddTHH-mm'.
16
+ """
17
+
18
+ def __init__(self, start_date: str, end_date: str):
19
+ # Validate format: 'YYYY-MM-DD'
20
+ try:
21
+ datetime.strptime(start_date, "%Y-%m-%d")
22
+ datetime.strptime(end_date, "%Y-%m-%d")
23
+ except ValueError:
24
+ raise ValueError("Dates must be in format 'YYYY-MM-DD'")
25
+ self.start_date = start_date + 'T00:00'
26
+ self.end_date = end_date + 'T00:00'
27
+
28
+ def _get_data(self, api_id: str) -> str:
29
+ """Send API request with GET method."""
30
+ api_url = 'https://api-baltic.transparency-dashboard.eu/api/v1/export'
31
+
32
+ params = {
33
+ "id": api_id,
34
+ "start_date": self.start_date,
35
+ "end_date": self.end_date,
36
+ "output_time_zone": 'EET',
37
+ "output_format": 'json'
38
+ }
39
+
40
+ response = requests.get(api_url, params=params)
41
+ if response.status_code == 200:
42
+ return response.text
43
+ else:
44
+ raise TimeoutError(f"Could not retrieve data. API status code {response.status_code}")
45
+
46
+ def _unravel_response(self, response: str, levels: int = 1) -> pd.DataFrame:
47
+ """
48
+ Parse and convert the JSON response to a pandas DataFrame.
49
+
50
+ Parameters
51
+ ----------
52
+ response : str
53
+ JSON-formatted response string.
54
+ levels : int
55
+ Number of group levels to use in column names (1, 2, or 3).
56
+
57
+ Returns
58
+ -------
59
+ pd.DataFrame
60
+ Formatted time-indexed DataFrame.
61
+ """
62
+ response_json = json.loads(response)
63
+
64
+ # Generate column names based on level depth
65
+ columns_info = response_json['data']['columns']
66
+ if levels == 3:
67
+ columns_map = {
68
+ col['index']: f"{col['group_level_0']} {col['group_level_1']} {col['label']}" for col in columns_info
69
+ }
70
+ elif levels == 2:
71
+ columns_map = {
72
+ col['index']: f"{col['group_level_0']} {col['label']}" for col in columns_info
73
+ }
74
+ else: # levels == 1
75
+ columns_map = {
76
+ col['index']: col['group_level_0'] for col in columns_info
77
+ }
78
+
79
+ # Build records
80
+ data_records = []
81
+ for entry in response_json['data']['timeseries']:
82
+ record = {'datetime': entry['from']}
83
+ for idx, value in enumerate(entry['values']):
84
+ if idx in columns_map:
85
+ record[columns_map[idx]] = value
86
+ data_records.append(record)
87
+
88
+ # Create DataFrame
89
+ df = pd.DataFrame(data_records)
90
+ df['datetime'] = pd.to_datetime(df['datetime']).dt.tz_convert('Europe/Vilnius').dt.tz_localize(None)
91
+ df.set_index('datetime', inplace=True)
92
+
93
+ return df
94
+
95
+ def procured_balancing_reserve_prices_df(self) -> pd.DataFrame:
96
+ """Get df of TSO procured balancing reserve prices (EUR/MW)."""
97
+ response = self._get_data(api_id='price_procured_reserves')
98
+ return self._unravel_response(response, levels=3)
99
+
100
+ def cbmp_df(self) -> pd.DataFrame:
101
+ """Get df of cross border marginal prices (EUR/MWh)."""
102
+ response = self._get_data(api_id='cross_border_marginal_price')
103
+ return self._unravel_response(response, levels=3)
104
+
105
+ def imbalance_volumes_df(self) -> pd.DataFrame:
106
+ """Get df of imbalance volumes for each country (MWh)."""
107
+ response = self._get_data(api_id='imbalance_volumes_v2')
108
+ return self._unravel_response(response, levels=1)
109
+
110
+ def activated_afrr_volumes_df(self) -> pd.DataFrame:
111
+ """Get df of activated afrr energy (MWh)"""
112
+ response = self._get_data(api_id='activations_afrr')
113
+ return self._unravel_response(response, levels=2)
114
+
115
+ def balancing_energy_ref_prices_df(self) -> pd.DataFrame:
116
+ """Get df of balancing energy reference prices (EUR/MWh)."""
117
+ response = self._get_data(api_id='balancing_energy_prices')
118
+ return self._unravel_response(response, levels=2)
119
+
120
+ def current_balancing_state_df(self) -> pd.DataFrame:
121
+ """Get df of imbalance volumes (MW) for each country with 1 minute resolution."""
122
+ response = self._get_data(api_id='current_balancing_state_v2')
123
+ return self._unravel_response(response, levels=1)
124
+
125
+ def direction_of_system_balancing_df(self) -> pd.DataFrame:
126
+ """Get df of the directions of system balancing (-1/+1)."""
127
+ response = self._get_data(api_id='direction_of_balancing_v2')
128
+ return self._unravel_response(response, levels=1)
129
+
130
+ def imbalance_prices_df(self) -> pd.DataFrame:
131
+ """Get df of imabalnce prices calculated by each Baltics TSO."""
132
+ response = self._get_data(api_id='imbalance_prices')
133
+ return self._unravel_response(response, levels=1)
134
+
135
+ def local_marginal_prices_df(self) -> pd.DataFrame:
136
+ """Get df of local marginal prices."""
137
+ response = self._get_data(api_id='local_marginal_price')
138
+ return self._unravel_response(response, levels=2)
139
+
140
+ def local_marginal_afrr_prices_df(self) -> pd.DataFrame:
141
+ """Get df of clearing (marginal) aFRR prices (EUR/MWh)."""
142
+ response = self._get_data(api_id='local_marginal_price_afrr')
143
+ return self._unravel_response(response, levels=2)
144
+
145
+ def normal_activations_da_volumes_df(self) -> pd.DataFrame:
146
+ """Get df of total direct mFRR energy activation volumes."""
147
+ response = self._get_data(api_id='normal_activations_da_mfrr')
148
+ return self._unravel_response(response, levels=2)
149
+
150
+ def normal_activations_sa_volumes_df(self) -> pd.DataFrame:
151
+ """Get df of total scheduled mFRR energy activation volumes."""
152
+ response = self._get_data(api_id='normal_activations_sa_mfrr')
153
+ return self._unravel_response(response, levels=2)
154
+
155
+ def normal_activations_total_volumes_df(self) -> pd.DataFrame:
156
+ """Get df of total normal balancing energy activated volumes."""
157
+ response = self._get_data(api_id='normal_activations_total')
158
+ return self._unravel_response(response, levels=2)
159
+
160
+ def total_satisfied_demand_for_balancing_purposes_df(self) -> pd.DataFrame:
161
+ """Get df total satisfied demands for balancing purposes (MWh)."""
162
+ response = self._get_data(api_id='total_satisfied_demand_for_balancing_purposes')
163
+ df = self._unravel_response(response, levels=2)
164
+ return df
165
+
166
+ def total_satisfied_demand_for_balancing_purposes_mod_df(self) -> pd.DataFrame:
167
+ """Get df total satisfied demands for balancing purposes. Convertet to MW and
168
+ total Baltics demand computed and returned."""
169
+ response = self._get_data(api_id='total_satisfied_demand_for_balancing_purposes')
170
+ df = self._unravel_response(response, levels=2)
171
+
172
+ # Apply processing logic
173
+ downward_columns = [col for col in df.columns if "Downward" in col]
174
+ df[downward_columns] = df[downward_columns].apply(pd.to_numeric, errors='coerce') * -1
175
+ df = df.dropna()
176
+ df['Baltics_net'] = df.sum(axis=1)
177
+ df *= 4
178
+ return df
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: btd-python
3
+ Version: 0.1.0
4
+ Summary: A python client for Baltic Transparency Data
5
+ Author-email: Sarunas Jurgilas <jurgilassarunas@yahoo.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/sarunasign/btd-python
8
+ Project-URL: Repository, https://github.com/sarunasign/btd-python
9
+ Requires-Python: >=3.7
10
+ Description-Content-Type: text/markdown
@@ -0,0 +1,6 @@
1
+ btd_client/__init__.py,sha256=NKx61aoZMTUf7x5NAqvXnoH3UBSoczWF-dJsUheIukw,46
2
+ btd_client/source.py,sha256=cG9SOlBNn4Ky4apPAwi_dgIz9Wt69VAFImodc12L7CY,7698
3
+ btd_python-0.1.0.dist-info/METADATA,sha256=5EcZR_6rHvLPK5fDMd5ytq6IiMTMQ3S2MyUIQbMx5so,383
4
+ btd_python-0.1.0.dist-info/WHEEL,sha256=DK49LOLCYiurdXXOXwGJm6U4DkHkg4lcxjhqwRa0CP4,91
5
+ btd_python-0.1.0.dist-info/top_level.txt,sha256=qKWvKwOewTDImdWKs3C9gIBptkGylL2yyzsptxJH0IQ,11
6
+ btd_python-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (78.0.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ btd_client