File size: 2,666 Bytes
271c5a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#%%

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,"%")


# %%