|
| 1 | +""" |
| 2 | +Import functions for NOAA SURFRAD Data. |
| 3 | +""" |
| 4 | +import io |
| 5 | + |
| 6 | +try: |
| 7 | + # python 2 compatibility |
| 8 | + from urllib2 import urlopen, Request |
| 9 | +except ImportError: |
| 10 | + from urllib.request import urlopen, Request |
| 11 | + |
| 12 | +import pandas as pd |
| 13 | +import numpy as np |
| 14 | + |
| 15 | +SURFRAD_COLUMNS = [ |
| 16 | + 'year', 'jday', 'month', 'day', 'hour', 'minute', 'dt', 'zen', |
| 17 | + 'dw_solar', 'dw_solar_flag', 'uw_solar', 'uw_solar_flag', 'direct_n', |
| 18 | + 'direct_n_flag', 'diffuse', 'diffuse_flag', 'dw_ir', 'dw_ir_flag', |
| 19 | + 'dw_casetemp', 'dw_casetemp_flag', 'dw_dometemp', 'dw_dometemp_flag', |
| 20 | + 'uw_ir', 'uw_ir_flag', 'uw_casetemp', 'uw_casetemp_flag', 'uw_dometemp', |
| 21 | + 'uw_dometemp_flag', 'uvb', 'uvb_flag', 'par', 'par_flag', 'netsolar', |
| 22 | + 'netsolar_flag', 'netir', 'netir_flag', 'totalnet', 'totalnet_flag', |
| 23 | + 'temp', 'temp_flag', 'rh', 'rh_flag', 'windspd', 'windspd_flag', |
| 24 | + 'winddir', 'winddir_flag', 'pressure', 'pressure_flag'] |
| 25 | + |
| 26 | +# Dictionary mapping surfrad variables to pvlib names |
| 27 | +VARIABLE_MAP = { |
| 28 | + 'zen': 'solar_zenith', |
| 29 | + 'dw_solar': 'ghi', |
| 30 | + 'dw_solar_flag': 'ghi_flag', |
| 31 | + 'direct_n': 'dni', |
| 32 | + 'direct_n_flag': 'dni_flag', |
| 33 | + 'diffuse': 'dhi', |
| 34 | + 'diffuse_flag': 'dhi_flag', |
| 35 | + 'temp': 'temp_air', |
| 36 | + 'temp_flag': 'temp_air_flag', |
| 37 | + 'windspd': 'wind_speed', |
| 38 | + 'windspd_flag': 'wind_speed_flag', |
| 39 | + 'winddir': 'wind_direction', |
| 40 | + 'winddir_flag': 'wind_direction_flag', |
| 41 | + 'rh': 'relative_humidity', |
| 42 | + 'rh_flag': 'relative_humidity_flag' |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def read_surfrad(filename, map_variables=True): |
| 47 | + """Read in a daily NOAA SURFRAD[1] file. |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + filename: str |
| 52 | + Filepath or url. |
| 53 | + map_variables: bool |
| 54 | + When true, renames columns of the Dataframe to pvlib variable names |
| 55 | + where applicable. See variable SURFRAD_COLUMNS. |
| 56 | +
|
| 57 | + Returns |
| 58 | + ------- |
| 59 | + Tuple of the form (data, metadata). |
| 60 | +
|
| 61 | + data: Dataframe |
| 62 | + Dataframe with the fields found below. |
| 63 | + metadata: dict |
| 64 | + Site metadata included in the file. |
| 65 | +
|
| 66 | + Notes |
| 67 | + ----- |
| 68 | + Metadata dictionary includes the following fields: |
| 69 | +
|
| 70 | + =============== ====== =============== |
| 71 | + Key Format Description |
| 72 | + =============== ====== =============== |
| 73 | + station String site name |
| 74 | + latitude Float site latitude |
| 75 | + longitude Float site longitude |
| 76 | + elevation Int site elevation |
| 77 | + surfrad_version Int surfrad version |
| 78 | + tz String Timezone (UTC) |
| 79 | + =============== ====== =============== |
| 80 | +
|
| 81 | + Dataframe includes the following fields: |
| 82 | +
|
| 83 | + ======================= ====== ========================================== |
| 84 | + raw, mapped Format Description |
| 85 | + ======================= ====== ========================================== |
| 86 | + **Mapped field names are returned when the map_variables argument is True** |
| 87 | + --------------------------------------------------------------------------- |
| 88 | + year int year as 4 digit int |
| 89 | + jday int day of year 1-365(or 366) |
| 90 | + month int month (1-12) |
| 91 | + day int day of month(1-31) |
| 92 | + hour int hour (0-23) |
| 93 | + minute int minute (0-59) |
| 94 | + dt float decimal time i.e. 23.5 = 2330 |
| 95 | + zen, solar_zenith float solar zenith angle (deg) |
| 96 | + **Fields below have associated qc flags labeled <field>_flag.** |
| 97 | + --------------------------------------------------------------------------- |
| 98 | + dw_solar, ghi float downwelling global solar(W/m^2) |
| 99 | + uw_solar float updownwelling global solar(W/m^2) |
| 100 | + direct_n, dni float direct normal solar (W/m^2) |
| 101 | + diffuse, dhi float downwelling diffuse solar (W/m^2) |
| 102 | + dw_ir float downwelling thermal infrared (W/m^2) |
| 103 | + dw_casetemp float downwelling IR case temp (K) |
| 104 | + dw_dometemp float downwelling IR dome temp (K) |
| 105 | + uw_ir float upwelling thermal infrared (W/m^2) |
| 106 | + uw_casetemp float upwelling IR case temp (K) |
| 107 | + uw_dometemp float upwelling IR case temp (K) |
| 108 | + uvb float global uvb (miliWatts/m^2) |
| 109 | + par float photosynthetically active radiation(W/m^2) |
| 110 | + netsolar float net solar (dw_solar - uw_solar) (W/m^2) |
| 111 | + netir float net infrared (dw_ir - uw_ir) (W/m^2) |
| 112 | + totalnet float net radiation (netsolar+netir) (W/m^2) |
| 113 | + temp, temp_air float 10-meter air temperature (?C) |
| 114 | + rh, relative_humidity float relative humidity (%) |
| 115 | + windspd, wind_speed float wind speed (m/s) |
| 116 | + winddir, wind_direction float wind direction (deg, clockwise from north) |
| 117 | + pressure float station pressure (mb) |
| 118 | + ======================= ====== ========================================== |
| 119 | +
|
| 120 | + See README files located in the station directories in the SURFRAD |
| 121 | + data archives[2] for details on SURFRAD daily data files. |
| 122 | +
|
| 123 | + References |
| 124 | + ---------- |
| 125 | + [1] NOAA Earth System Research Laboratory Surface Radiation Budget Network |
| 126 | + `SURFRAD Homepage <https://www.esrl.noaa.gov/gmd/grad/surfrad/>`_ |
| 127 | + [2] NOAA SURFRAD Data Archive |
| 128 | + `SURFRAD Archive <ftp://aftp.cmdl.noaa.gov/data/radiation/surfrad/>`_ |
| 129 | + """ |
| 130 | + if filename.startswith('ftp'): |
| 131 | + req = Request(filename) |
| 132 | + response = urlopen(req) |
| 133 | + file_buffer = io.StringIO(response.read().decode(errors='ignore')) |
| 134 | + else: |
| 135 | + file_buffer = open(filename, 'r') |
| 136 | + |
| 137 | + # Read and parse the first two lines to build the metadata dict. |
| 138 | + station = file_buffer.readline() |
| 139 | + file_metadata = file_buffer.readline() |
| 140 | + |
| 141 | + metadata_list = file_metadata.split() |
| 142 | + metadata = {} |
| 143 | + metadata['name'] = station.strip() |
| 144 | + metadata['latitude'] = float(metadata_list[0]) |
| 145 | + metadata['longitude'] = float(metadata_list[1]) |
| 146 | + metadata['elevation'] = float(metadata_list[2]) |
| 147 | + metadata['surfrad_version'] = int(metadata_list[-1]) |
| 148 | + metadata['tz'] = 'UTC' |
| 149 | + |
| 150 | + data = pd.read_csv(file_buffer, delim_whitespace=True, |
| 151 | + header=None, names=SURFRAD_COLUMNS) |
| 152 | + file_buffer.close() |
| 153 | + |
| 154 | + data = format_index(data) |
| 155 | + missing = data == -9999.9 |
| 156 | + data = data.where(~missing, np.NaN) |
| 157 | + |
| 158 | + if map_variables: |
| 159 | + data.rename(columns=VARIABLE_MAP, inplace=True) |
| 160 | + return data, metadata |
| 161 | + |
| 162 | + |
| 163 | +def format_index(data): |
| 164 | + """Create UTC localized DatetimeIndex for the dataframe. |
| 165 | +
|
| 166 | + Parameters |
| 167 | + ---------- |
| 168 | + data: Dataframe |
| 169 | + Must contain columns 'year', 'jday', 'hour' and |
| 170 | + 'minute'. |
| 171 | +
|
| 172 | + Return |
| 173 | + ------ |
| 174 | + data: Dataframe |
| 175 | + Dataframe with a DatetimeIndex localized to UTC. |
| 176 | + """ |
| 177 | + year = data.year.apply(str) |
| 178 | + jday = data.jday.apply(lambda x: '{:03d}'.format(x)) |
| 179 | + hours = data.hour.apply(lambda x: '{:02d}'.format(x)) |
| 180 | + minutes = data.minute.apply(lambda x: '{:02d}'.format(x)) |
| 181 | + index = pd.to_datetime(year + jday + hours + minutes, format="%Y%j%H%M") |
| 182 | + data.index = index |
| 183 | + data = data.tz_localize('UTC') |
| 184 | + return data |
0 commit comments