10
10
11
11
def energy (country , start_time , end_time , type = "generation" ) -> dict :
12
12
"""
13
- Returns hourly time series of energy production mix for a specified country and time range.
14
-
15
- This method fetches the energy data for the specified country between the specified duration.
16
- It checks if a valid energy data source is available. If not, None is returned. Otherwise, the
17
- energy data is returned as a pandas DataFrame. The structure of data depends on the energy source.
18
-
19
- For example, if the source is ENTSOE, the data contains:
20
-
21
- ========================== ========== ================================================================
22
- Column type Description
23
- ========================== ========== ================================================================
24
- startTimeUTC object Start date in UTC (format YYYYMMDDhhmm)
25
- startTime datetime Start time in local timezone
26
- Biomass float64
27
- Fossil Hard coal float64
28
- Geothermal float64
29
- ....more energy sources float64
30
- **renewableTotal** float64 The total based on all renewable sources
31
- renewableTotalWS float64 The total production using only Wind and Solar energy sources
32
- nonRenewableTotal float64
33
- total float64 Total using all energy sources
34
- percentRenewable int64
35
- percentRenewableWS int64 Percentage of energy produced using only wind and solar energy
36
- Wind_per int64 Percentages of individual energy sources
37
- Solar_per int64
38
- Nuclear_per int64
39
- Hydroelectricity_per int64
40
- Geothermal_per int64
41
- Natural Gas_per int64
42
- Petroleum_per int64
43
- Coal_per int64
44
- Biomass_per int64
45
- ========================== ========== ================================================================
46
-
47
- Note : fields marked bold are calculated based on the data fetched.
48
-
49
- :param str country: The 2 alphabet country code.
50
- :param datetime start_time: The start date for data retrieval. A Datetime object. Note that this date will be rounded to the nearest hour.
51
- :param datetime end_time: The end date for data retrieval. A datetime object. This date is also rounded to the nearest hour.
52
- :param str type: The type of data to retrieve; either 'generation' or 'forecast'. Defaults to 'generation'.
53
- :param boolean interval60: To fix the time interval of data to 60 minutes. True by default. Only applicable for generation data
54
-
55
- :return: A dictionary containing:
56
- - `error`: A string with an error message, empty if no errors.
57
- - `data_available`: A boolean indicating if data was successfully retrieved.
58
- - `data`: A pandas DataFrame containing the energy data if available, empty DataFrame if not.
59
- - `time_interval` : the time interval of the DataFrame
13
+ Returns an hourly time series of the energy production mix for a specified country and time range,
14
+ if a valid energy data source is available.
15
+
16
+ The data is returned as a pandas DataFrame along with additional metadata.
17
+ The columns vary depending on the data source. For example, if the source is ENTSOE,
18
+ the data includes fields such as "Biomass", "Geothermal", "Hydro Pumped Storage",
19
+ "Hydro Run-of-river and Poundage", "Hydro Water Reservoir", etc.
20
+
21
+ However, some fields remain consistent across data sources:
22
+
23
+ ========================= ========== ================================================================
24
+ Column Type Description
25
+ ========================= ========== ================================================================
26
+ startTimeUTC object Start time in UTC (format: YYYYMMDDhhmm)
27
+ startTime datetime Start time in local timezone
28
+ renewableTotal float64 The total production from all renewable sources
29
+ renewableTotalWS float64 Total production using only Wind and Solar energy sources
30
+ nonRenewableTotal float64 Total production from non-renewable sources
31
+ total float64 Total energy production from all sources
32
+ percentRenewable int64 Percentage of total energy from renewable sources
33
+ percentRenewableWS int64 Percentage of energy from Wind and Solar only
34
+ Wind_per int64 Percentage contribution from Wind energy
35
+ Solar_per int64 Percentage contribution from Solar energy
36
+ Nuclear_per int64 Percentage contribution from Nuclear energy
37
+ Hydroelectricity_per int64 Percentage contribution from Hydroelectricity
38
+ Geothermal_per int64 Percentage contribution from Geothermal energy
39
+ Natural Gas_per int64 Percentage contribution from Natural Gas
40
+ Petroleum_per int64 Percentage contribution from Petroleum
41
+ Coal_per int64 Percentage contribution from Coal
42
+ Biomass_per int64 Percentage contribution from Biomass
43
+ ========================= ========== ================================================================
44
+
45
+ :param str country:
46
+ The 2-letter country code (e.g., "DE" for Germany, "FR" for France, etc.).
47
+ :param datetime start_time:
48
+ The start date for data retrieval (rounded to the nearest hour).
49
+ :param datetime end_time:
50
+ The end date for data retrieval (rounded to the nearest hour).
51
+ :param str type:
52
+ The type of data to retrieve; either 'generation' or 'forecast'. Defaults to 'generation'.
53
+
54
+ :return: A dictionary containing the following keys:
55
+
56
+ - **error** (*str*): An error message, empty if no errors occurred.
57
+ - **data_available** (*bool*): Indicates whether data was successfully retrieved.
58
+ - **data** (*pandas.DataFrame*): The retrieved energy data if available; an empty DataFrame otherwise.
59
+ - **time_interval** (*int*): The time interval of the DataFrame (constant value: ``60``).
60
+ - **source** (*str*): Specifies the origin of the retrieved data. Defaults to ``'public_data'``, indicating it was fetched from an external source. If the offline storage feature is enabled, this value may change if the data is available locally.
61
+
60
62
:rtype: dict
63
+
64
+ **Example Usage:**
65
+
66
+ Get generation data for Germany
67
+
68
+ .. code-block:: python
69
+
70
+ from datetime import datetime
71
+ from codegreen_core.data import energy
72
+ result = energy(country="DE", start_time=datetime(2025, 1, 1), end_time=datetime(2025, 1, 2), type="generation")
73
+
74
+ Get forecast data for Norway
75
+
76
+ .. code-block:: python
77
+
78
+ from datetime import datetime
79
+ from codegreen_core.data import energy
80
+ result = energy(country="NO", start_time=datetime(2025, 1, 1), end_time=datetime(2025, 1, 2), type="forecast")
81
+
61
82
"""
62
83
if not isinstance (country , str ):
63
84
raise ValueError ("Invalid country" )
@@ -75,14 +96,6 @@ def energy(country, start_time, end_time, type="generation") -> dict:
75
96
e_source = meta .get_country_energy_source (country )
76
97
if e_source == "ENTSOE" :
77
98
if type == "generation" :
78
- """
79
- let local_found= false
80
- see if caching is enabled, if yes, first check in the cache
81
- if not,
82
- check if offline data is enabled
83
- if yes, check is data is available locally
84
- if no, go online
85
- """
86
99
offline_data = off .get_offline_data (country ,start_time ,end_time )
87
100
if offline_data ["available" ] is True and offline_data ["partial" ] is False and offline_data ["data" ] is not None :
88
101
# todo fix this if partial get remaining data and merge instead of fetching the complete data
@@ -99,3 +112,24 @@ def energy(country, start_time, end_time, type="generation") -> dict:
99
112
else :
100
113
raise CodegreenDataError (Message .NO_ENERGY_SOURCE )
101
114
return None
115
+
116
+ def info ()-> list :
117
+ """
118
+ Returns a list of countries (in two-letter codes) and energy sources for which data can be fetched using the package.
119
+
120
+ :return: A list of dictionary containing:
121
+
122
+ - name of the country
123
+ - `energy_source` : the publicly available energy data source
124
+ - `carbon_intensity_method` : the methodology used to calculate carbon intensity
125
+ - `code` : the 2 letter country code
126
+
127
+ :rtype: list
128
+ """
129
+ data = meta .get_country_metadata ()
130
+ data_list = []
131
+ for key , value in data .items ():
132
+ c = value
133
+ c ["code" ] = key
134
+ data_list .append (c )
135
+ return data_list
0 commit comments