#%% from scipy.interpolate import RegularGridInterpolator import numpy as np # import os # os.chdir("C:\\Users\\KN\\Python Projects\\PT\\Solar GSA Model") #%% Interpolator definition class SolarEnergyInterpolator: def __init__(self, data_dir=None): """ 1. Loads 3D grid file 2. Initializes interpolator """ import os if data_dir is None: data_file = "pv_potential_3d.npz" else: data_file = os.path.join(data_dir, "pv_potential_3d.npz") print(f"Loading efficiency data from: {data_file}") print(f"File exists: {os.path.exists(data_file)}") data = np.load(data_file) print("File loaded") # Initialize lats, lons pv_data = data["pv_data"] lons = data["lons"] lats = data["lats"] months_axis = np.arange(1, 13) # Create the interpolator (ignoring NaNs) self.interpolator = RegularGridInterpolator( (lons, lats, months_axis), pv_data, method='linear', # Can be 'nearest' or 'cubic' as well bounds_error=False, # Does NOT raise error when input is out of bounds fill_value=0 # Will return 0 if out of bounds ) print("Grid interpolator initialized") def get_solar_efficiency(self, latitude, longitude): """ Interpolates the solar energy for the given lat, long, and month of year. Calculates efficiency based on yearly average energy. Parameters: latitude (float): Latitude of the query point. (-60 to 65) longitude (float): Longitude of the query point. (-180 to 180) Returns: float: Interpolated solar efficiency value or 0 if out of range. """ days_vec = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] month = list(range(1,13)) # Create month vector specEnergy = self.interpolator((longitude, latitude, month)) # Compute (daily) specific energy vector efficiency = round(np.average(specEnergy)*(365/100),2) # Average over 1 year return efficiency #%% Create interpolator instance (commented out - instantiated on demand) # GSA_3Dinterpolate = SolarEnergyInterpolator() #%% SAMPLE QUERY (commented out) # lat, lon = -22.0, -67.0 # efficiency = GSA_3Dinterpolate.get_solar_efficiency(lat, lon) # print("Efficiency:", efficiency,"%") # %%