|
| 1 | +"""Functions for reading and retrieving data from NASA POWER.""" |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import requests |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +URL = 'https://power.larc.nasa.gov/api/temporal/hourly/point' |
| 8 | + |
| 9 | +DEFAULT_PARAMETERS = [ |
| 10 | + 'dni', 'dhi', 'ghi', 'temp_air', 'wind_speed' |
| 11 | +] |
| 12 | + |
| 13 | +VARIABLE_MAP = { |
| 14 | + 'ALLSKY_SFC_SW_DWN': 'ghi', |
| 15 | + 'ALLSKY_SFC_SW_DIFF': 'dhi', |
| 16 | + 'ALLSKY_SFC_SW_DNI': 'dni', |
| 17 | + 'CLRSKY_SFC_SW_DWN': 'ghi_clear', |
| 18 | + 'T2M': 'temp_air', |
| 19 | + 'WS2M': 'wind_speed_2m', |
| 20 | + 'WS10M': 'wind_speed', |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +def get_nasa_power(latitude, longitude, start, end, |
| 25 | + parameters=DEFAULT_PARAMETERS, *, community='re', |
| 26 | + elevation=None, wind_height=None, wind_surface=None, |
| 27 | + map_variables=True, url=URL): |
| 28 | + """ |
| 29 | + Retrieve irradiance and weather data from NASA POWER. |
| 30 | +
|
| 31 | + A general description of NASA POWER is given in [1]_ and the API is |
| 32 | + described in [2]_. A detailed list of the available parameters can be |
| 33 | + found in [3]_. |
| 34 | +
|
| 35 | + Parameters |
| 36 | + ---------- |
| 37 | + latitude: float |
| 38 | + In decimal degrees, north is positive (ISO 19115). |
| 39 | + longitude: float |
| 40 | + In decimal degrees, east is positive (ISO 19115). |
| 41 | + start: datetime like |
| 42 | + First timestamp of the requested period. |
| 43 | + end: datetime like |
| 44 | + Last timestamp of the requested period. |
| 45 | + parameters: str, list |
| 46 | + List of parameters. The default parameters are mentioned below; for the |
| 47 | + full list see [3]_. Note that the pvlib naming conventions can also be |
| 48 | + used. |
| 49 | +
|
| 50 | + * Global Horizontal Irradiance (GHI) [Wm⁻²] |
| 51 | + * Diffuse Horizontal Irradiance (DHI) [Wm⁻²] |
| 52 | + * Direct Normal Irradiance (DNI) [Wm⁻²] |
| 53 | + * Air temperature at 2 m [C] |
| 54 | + * Wind speed at 10 m [m/s] |
| 55 | +
|
| 56 | + community: str, default 're' |
| 57 | + Can be one of the following depending on which parameters are of |
| 58 | + interest. Note that in many cases this choice |
| 59 | + might affect the units of the parameter. |
| 60 | +
|
| 61 | + * ``'re'``: renewable energy |
| 62 | + * ``'sb'``: sustainable buildings |
| 63 | + * ``'ag'``: agroclimatology |
| 64 | +
|
| 65 | + elevation: float, optional |
| 66 | + The custom site elevation in meters to produce the corrected |
| 67 | + atmospheric pressure adjusted for elevation. |
| 68 | + wind_height: float, optional |
| 69 | + The custom wind height in meters to produce the wind speed adjusted |
| 70 | + for height. Has to be between 10 and 300 m; see [4]_. |
| 71 | + wind_surface: str, optional |
| 72 | + The definable surface type to adjust the wind speed. For a list of the |
| 73 | + surface types see [4]_. If you provide a wind surface alias please |
| 74 | + include a site elevation with the request. |
| 75 | + map_variables: bool, default True |
| 76 | + When true, renames columns of the Dataframe to pvlib variable names |
| 77 | + where applicable. See variable :const:`VARIABLE_MAP`. |
| 78 | +
|
| 79 | + Raises |
| 80 | + ------ |
| 81 | + requests.HTTPError |
| 82 | + Raises an error when an incorrect request is made. |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + data : pd.DataFrame |
| 87 | + Time series data. The index corresponds to the start (left) of the |
| 88 | + interval. |
| 89 | + meta : dict |
| 90 | + Metadata. |
| 91 | +
|
| 92 | + References |
| 93 | + ---------- |
| 94 | + .. [1] `NASA Prediction Of Worldwide Energy Resources (POWER) |
| 95 | + <https://power.larc.nasa.gov/>`_ |
| 96 | + .. [2] `NASA POWER API |
| 97 | + <https://power.larc.nasa.gov/api/pages/>`_ |
| 98 | + .. [3] `NASA POWER API parameters |
| 99 | + <https://power.larc.nasa.gov/parameters/>`_ |
| 100 | + .. [4] `NASA POWER corrected wind speed parameters |
| 101 | + <https://power.larc.nasa.gov/docs/methodology/meteorology/wind/>`_ |
| 102 | + """ |
| 103 | + start = pd.Timestamp(start) |
| 104 | + end = pd.Timestamp(end) |
| 105 | + |
| 106 | + # allow the use of pvlib parameter names |
| 107 | + parameter_dict = {v: k for k, v in VARIABLE_MAP.items()} |
| 108 | + parameters = [parameter_dict.get(p, p) for p in parameters] |
| 109 | + |
| 110 | + params = { |
| 111 | + 'latitude': latitude, |
| 112 | + 'longitude': longitude, |
| 113 | + 'start': start.strftime('%Y%m%d'), |
| 114 | + 'end': end.strftime('%Y%m%d'), |
| 115 | + 'community': community, |
| 116 | + 'parameters': ','.join(parameters), # make parameters in a string |
| 117 | + 'format': 'json', |
| 118 | + 'user': None, |
| 119 | + 'header': True, |
| 120 | + 'time-standard': 'utc', |
| 121 | + 'site-elevation': elevation, |
| 122 | + 'wind-elevation': wind_height, |
| 123 | + 'wind-surface': wind_surface, |
| 124 | + } |
| 125 | + |
| 126 | + response = requests.get(url, params=params) |
| 127 | + if not response.ok: |
| 128 | + # response.raise_for_status() does not give a useful error message |
| 129 | + raise requests.HTTPError(response.json()) |
| 130 | + |
| 131 | + # Parse the data to dataframe |
| 132 | + data = response.json() |
| 133 | + hourly_data = data['properties']['parameter'] |
| 134 | + df = pd.DataFrame(hourly_data) |
| 135 | + df.index = pd.to_datetime(df.index, format='%Y%m%d%H').tz_localize('UTC') |
| 136 | + |
| 137 | + # Create metadata dictionary |
| 138 | + meta = data['header'] |
| 139 | + meta['times'] = data['times'] |
| 140 | + meta['parameters'] = data['parameters'] |
| 141 | + |
| 142 | + meta['longitude'] = data['geometry']['coordinates'][0] |
| 143 | + meta['latitude'] = data['geometry']['coordinates'][1] |
| 144 | + meta['altitude'] = data['geometry']['coordinates'][2] |
| 145 | + |
| 146 | + # Replace NaN values |
| 147 | + df = df.replace(meta['fill_value'], np.nan) |
| 148 | + |
| 149 | + # Rename according to pvlib convention |
| 150 | + if map_variables: |
| 151 | + df = df.rename(columns=VARIABLE_MAP) |
| 152 | + |
| 153 | + return df, meta |
0 commit comments