Codebase Reference¶
This document provides an overview of the main components of the grid_data_retrieval package, detailing the primary modules and their functionalities.
grid_data_retrieval.runner ¶
Grid Data Retrieval Runner¶
Orchestrates grid data fetching from APIs.
This module handles ONLY data retrieval. Subsequent processing (gap-filling, resampling, timezone conversion) should be done via the data_cleaning_and_joining module.
run_grid_retrieval ¶
run_grid_retrieval(config, *, logger=None, verbose=True)
Execute grid data retrieval from API.
This function ONLY fetches and combines data. Processing happens elsewhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration dictionary containing: - start_date : str (YYYY-MM-DD HH:MM:SS) - end_date : str (YYYY-MM-DD HH:MM:SS) - api_url : str (optional) - overwrite_existing : bool (optional, default: True) - combine_files : bool (optional, default: True) |
required |
logger
|
Logger
|
Pre-configured logger instance. |
None
|
verbose
|
bool
|
Whether to echo logs to console. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
Exit code: 0=success, 1=error. |
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/runner.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
grid_data_retrieval.io.cli ¶
Command-Line Interface¶
CLI for grid data retrieval (fetching only).
Data processing should be done via the data_cleaning_and_joining module.
parse_args ¶
parse_args()
Parse command-line arguments.
Returns:
| Type | Description |
|---|---|
Namespace
|
Parsed arguments. |
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/io/cli.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
main ¶
main()
Main entry point for the grid data retrieval CLI.
This function: 1. Parses CLI arguments or loads configuration file. 2. Initializes logging. 3. Executes the grid data fetching.
Automatically invoked by the osme-grid-fetch CLI script.
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/io/cli.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
grid_data_retrieval.io.config_loader ¶
Configuration File Loading¶
Load and validate JSON configuration files for grid data retrieval.
load_config ¶
load_config(file_path)
Load configuration from a JSON file.
Searches in: 1. Absolute path (if provided) 2. Relative to config_dir() 3. Relative to config_dir()/grid/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str or Path
|
Path to JSON config file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Configuration dictionary. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the configuration file cannot be found. |
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/io/config_loader.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
grid_data_retrieval.sources.carbontracker ¶
CarbonTracker Merit API Data Retrieval¶
Fetches grid electricity data from the CarbonTracker Merit API and saves monthly Parquet files.
Functions:
| Name | Description |
|---|---|
fetch_monthly_batches : Retrieve data for all months in date range |
|
combine_monthly_files : Merge monthly files into single dataset |
|
fetch_monthly_batches ¶
fetch_monthly_batches(
start_date,
end_date,
api_url,
output_dir,
*,
overwrite_existing=True,
logger=None,
echo_console=True
)
Fetch grid data from CarbonTracker API in monthly batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
str
|
Start date in format "YYYY-MM-DD HH:MM:SS". |
required |
end_date
|
str
|
End date in format "YYYY-MM-DD HH:MM:SS". |
required |
api_url
|
str
|
Base URL for the Merit API. |
required |
output_dir
|
Path or str
|
Directory to save monthly Parquet files. |
required |
overwrite_existing
|
bool
|
Whether to overwrite months that already have files. |
True
|
logger
|
Logger
|
Logger instance. |
None
|
echo_console
|
bool
|
Whether to echo to console. |
True
|
Returns:
| Type | Description |
|---|---|
List[Path]
|
List of paths to fetched/existing monthly files. |
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/sources/carbontracker.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
combine_monthly_files ¶
combine_monthly_files(
monthly_dir,
output_dir,
*,
logger=None,
echo_console=True
)
Combine all monthly Parquet files into a single dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monthly_dir
|
Path or str
|
Directory containing monthly Parquet files. |
required |
output_dir
|
Path or str
|
Directory for combined output file. |
required |
logger
|
Logger
|
Logger instance. |
None
|
echo_console
|
bool
|
Whether to echo to console. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the combined output file. |
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/sources/carbontracker.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
grid_data_retrieval.utils.logging ¶
Logging Infrastructure¶
Provides centralized logging for the grid_data_retrieval package.
Reuses patterns from osme_common and weather_data_retrieval for consistency.
setup_logger ¶
setup_logger(save_dir=None, verbose=True)
Initialize and return a configured logger.
Logs are written to
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_dir
|
str or None
|
Directory to save log files. If None, defaults to osme_common.paths.log_dir(). |
None
|
verbose
|
bool
|
Whether to echo logs to console (via console handler). |
True
|
Returns:
| Type | Description |
|---|---|
Logger
|
Configured logger instance. |
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/utils/logging.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
log_msg ¶
log_msg(
msg,
logger,
*,
level="info",
echo_console=False,
force=False
)
Unified logging utility.
- Always logs to file.
- Optionally echoes to console via tqdm.write (non-blocking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
Message to log. |
required |
logger
|
Logger
|
Logger instance. |
required |
level
|
str
|
Log level: "debug", "info", "warning", "error", "exception". |
'info'
|
echo_console
|
bool
|
Print to console when True. |
False
|
force
|
bool
|
Print to console regardless of echo_console (for critical messages). |
False
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in packages/grid_data_retrieval/src/grid_data_retrieval/utils/logging.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |