Source code for RES.tech
import yaml
import logging
from requests import get
from pathlib import Path
# Setup logging
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
# * Check 'windpowerlib' package
[docs]
class OEDBTurbines:
def __init__(self,
OEDB_config:dict):
"""
Class to manage turbine configuration data.
"""
self.OEDB_config = OEDB_config
[docs]
def load_turbine_config(self):
# Read the YAML file into a dictionary
with open(self.turbine_config_file, 'r') as file:
turbine_config:dict=yaml.safe_load(file)
print(f">> selected Wind Turbine Model : {turbine_config['name']} @ {turbine_config['hub_height']}m Hub Height")
return turbine_config
[docs]
def fetch_turbine_config(self, model):
"""
Fetches turbine data based on the resource type (e.g., 'wind') and saves the formatted
configuration for the turbines found.
"""
OEDB_id = self.OEDB_config['models'][model]['ID']
OEDB_source = self.OEDB_config['source']
self.turbine_name = self.OEDB_config['models'][model]['name']
# Define the directory and file path
self.turbine_config_dir = Path('data/downloaded_data') / "OEDB"
self.turbine_config_dir.mkdir(parents=True, exist_ok=True) # Ensure the directory exists
self.turbine_config_file = self.turbine_config_dir / f"{self.turbine_name}.yaml"
print(f"Fetching Turbine: '{self.turbine_name}' data from OEDB")
# Check if the turbine config file already exists
if self.turbine_config_file.exists():
if self.turbine_config_file.is_file():
print(f">> Loading turbine config from: {self.turbine_config_file}")
return self.load_turbine_config()
else:
print(f">> Expected a file but found a directory: {self.turbine_config_file}")
return None
else:
print(f">> Fetching turbine config for: {self.turbine_name} from OEDB")
try:
OEDB_data = get(OEDB_source).json()
except Exception as e:
logging.error(f">> !! Failed to fetch OEDB data from {OEDB_source}: {e}")
return None
# Process fetched data
turbine_data = self.__get_required_turbines__(OEDB_data, 'id', OEDB_id)
if turbine_data:
try:
self.format_and_save_turbine_config(turbine_data, self.turbine_config_file)
return self.load_turbine_config()
except Exception as e:
logging.error(f">> !! Error saving turbine configuration: {e}")
return None
else:
print(f">> !! No data found for turbine ID {OEDB_id}")
return None
def __get_required_turbines__(self,
OEDB_data: dict,
key: str,
value: str):
"""
Searches for a turbine configuration in the fetched data from OEDB.
Args:
- OEDB_turbines_dict (dict): The dictionary containing OEDB turbine data.
- key (str): The field name to search by (e.g., 'id').
- value (str): The value to search for.
Returns:
- dict or None: The matching turbine's data or None if not found.
"""
for entry in OEDB_data:
if entry.get(key) == value:
return entry
return None # Return None if no match is found
def __create_blank_yaml__(self,
filepath: Path):
"""
Create a blank YAML file.
Parameters:
filepath (str): Path to the file.
"""
with open(filepath, 'w') as file:
file.write('')