-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: BaseIO class and prefer_env_var
#65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ | |
| solar_wind as solar_wind, | ||
| sme as sme, | ||
| ) | ||
| from swvo.io.base import BaseIO as BaseIO | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # SPDX-FileCopyrightText: 2025 GFZ Helmholtz Centre for Geosciences | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Base class for all IO modules. | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
| from abc import ABC, abstractmethod | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| import pandas as pd | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class BaseIO(ABC): | ||
| """Abstract base class for all IO classes. | ||
|
|
||
| This base class defines the common interface for external data I/O operations, | ||
| including initialization, reading, and downloading/processing data. | ||
|
|
||
| Subclasses can implement flexible signatures for `read()` and `download_and_process()` | ||
| methods to accommodate different data sources and requirements. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data_dir : Path | None | ||
| Data directory for storing downloaded/processed data. | ||
| If not provided, it will be read from the environment variable | ||
| defined by the subclass's `ENV_VAR_NAME`. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| Raises `ValueError` if necessary environment variable is not set | ||
| and `data_dir` is not provided. | ||
| """ | ||
|
|
||
| ENV_VAR_NAME: str = "" # Must be set by subclasses | ||
| LABEL: str = "" # Must be set by subclasses | ||
|
|
||
| def __init__(self, data_dir: Optional[Path] = None, prefer_env_var: bool = False) -> None: | ||
| """Initialize the BaseIO class. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data_dir : Path | None | ||
| Data directory for storing data. If not provided, it will be read | ||
| from the environment variable defined by ENV_VAR_NAME. | ||
| prefer_env_var : bool, optional | ||
| If True, the environment variable takes precedence over the passed data_dir argument. | ||
| If False (default), the passed data_dir is used if provided, otherwise the environment variable is used. | ||
| Raises | ||
| ------ | ||
| ValueError | ||
| If data_dir is None and ENV_VAR_NAME is not set in environment, | ||
| or if prefer_env_var is True and ENV_VAR_NAME is not set. | ||
| """ | ||
| if prefer_env_var and self.ENV_VAR_NAME in os.environ: | ||
| data_dir = Path(os.environ[self.ENV_VAR_NAME]) | ||
| elif data_dir is None: | ||
| if not self.ENV_VAR_NAME or self.ENV_VAR_NAME not in os.environ: | ||
| raise ValueError(f"Necessary environment variable {self.ENV_VAR_NAME} not set!") | ||
| data_dir = Path(os.environ[self.ENV_VAR_NAME]) | ||
|
|
||
| self.data_dir: Path = Path(data_dir) | ||
| self.data_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| logger.info(f"{self.__class__.__name__} data directory: {self.data_dir}") | ||
|
|
||
| @abstractmethod | ||
| def read(self, *args, **kwargs) -> pd.DataFrame | list[pd.DataFrame]: | ||
| """Read data. | ||
|
|
||
| Subclasses should implement this method with their specific signature. | ||
| Common parameters include: | ||
| - start_time: datetime | ||
| Start time of the data to read. Must be timezone-aware. | ||
| - end_time: datetime | ||
| End time of the data to read. Must be timezone-aware. | ||
| - download: bool, optional | ||
| Download data on the go if not available locally. | ||
| - Additional parameters specific to each data source. | ||
|
|
||
| Returns | ||
| ------- | ||
| pd.DataFrame or list[pd.DataFrame] | ||
| Data for the specified parameters. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def download_and_process(self, *args, **kwargs) -> None: | ||
| """Download and process data. | ||
|
|
||
| Subclasses should implement this method with their specific signature. | ||
| Common parameters include: | ||
| - start_time: datetime | ||
| Start time of the data to download. Must be timezone-aware. | ||
| - end_time: datetime | ||
| End time of the data to download. Must be timezone-aware. | ||
| - target_date: datetime | ||
| Target date for data (for single-day sources). | ||
| - request_time: datetime | ||
| Request time for data (for streaming sources). | ||
| - reprocess_files: bool, optional | ||
| If True, re-download and re-process existing files. | ||
| - Additional parameters specific to each data source. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.