Skip to content

Allow read_surfrad to parse http files #1459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 19, 2022
6 changes: 5 additions & 1 deletion docs/sphinx/source/whatsnew/v0.9.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Deprecations

Enhancements
~~~~~~~~~~~~
* :py:func:`pvlib.iotools.read_surfrad` now also accepts remote files
with https links in addition to files on the SURFRAD FTP server
(:pull:`1459`)
* Add :py:func:`pvlib.tracking.calc_surface_orientation` for calculating
single-axis tracker ``surface_tilt`` and ``surface_azimuth`` from
rotation angles. (:issue:`1471`, :pull:`1480`)
Expand Down Expand Up @@ -47,8 +50,9 @@ Requirements

Contributors
~~~~~~~~~~~~
* Adam R. Jensen (:ghuser:`AdamRJensen`)
* Naman Priyadarshi (:ghuser:`Naman-Priyadarshi`)
* Chencheng Luo (:ghuser:`roger-lcc`)
* Prajwal Borkar (:ghuser:`PrajwalBorkar`)
* Kevin Anderson (:ghuser:`kanderso-nrel`)
* Cliff Hansen (:ghuser:`cwhanse`)
* Cliff Hansen (:ghuser:`cwhanse`)
9 changes: 6 additions & 3 deletions pvlib/iotools/surfrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def read_surfrad(filename, map_variables=True):
Parameters
----------
filename: str
Filepath or url.
Filepath or URL. URL can be either FTP or HTTP.
map_variables: bool
When true, renames columns of the Dataframe to pvlib variable names
where applicable. See variable :const:`VARIABLE_MAP`.
Expand Down Expand Up @@ -113,7 +113,8 @@ def read_surfrad(filename, map_variables=True):
======================= ====== ==========================================

See README files located in the station directories in the SURFRAD
data archives [2]_ for details on SURFRAD daily data files.
data archives [2]_ for details on SURFRAD daily data files. In addition to
the FTP server, the SURFRAD files are also available via HTTP access [3]_.

References
----------
Expand All @@ -122,8 +123,10 @@ def read_surfrad(filename, map_variables=True):
`SURFRAD Homepage <https://www.esrl.noaa.gov/gmd/grad/surfrad/>`_
.. [2] NOAA SURFRAD Data Archive
`SURFRAD Archive <ftp://aftp.cmdl.noaa.gov/data/radiation/surfrad/>`_
.. [3] `NOAA SURFRAD HTTP Index
<https://gml.noaa.gov/aftp/data/radiation/surfrad/>`_
"""
if str(filename).startswith('ftp'):
if str(filename).startswith('ftp') or str(filename).startswith('http'):
req = Request(filename)
response = urlopen(req)
file_buffer = io.StringIO(response.read().decode(errors='ignore'))
Expand Down
13 changes: 13 additions & 0 deletions pvlib/tests/iotools/test_surfrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
testfile = DATA_DIR / 'surfrad-slv16001.dat'
network_testfile = ('ftp://aftp.cmdl.noaa.gov/data/radiation/surfrad/'
'Alamosa_CO/2016/slv16001.dat')
https_testfile = ('https://gml.noaa.gov/aftp/data/radiation/surfrad/'
'Alamosa_CO/2016/slv16001.dat')


@pytest.mark.remote_data
Expand All @@ -19,6 +21,17 @@ def test_read_surfrad_network():
assert local_data.equals(network_data)


@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_read_surfrad_https():
# Test reading of https files.
# If this test begins failing, SURFRAD's data structure or data
# archive may have changed.
local_data, _ = surfrad.read_surfrad(testfile)
network_data, _ = surfrad.read_surfrad(https_testfile)
assert local_data.equals(network_data)


def test_read_surfrad_columns_no_map():
data, _ = surfrad.read_surfrad(testfile, map_variables=False)
assert 'zen' in data.columns
Expand Down