Skip to content

Add read_bsrn function #1145

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 46 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
73e6f9e
Add bsrn file to read bsrn files
AdamRJensen Jan 15, 2021
31203b3
simplified read_bsrn function
AdamRJensen Jan 16, 2021
7a437c3
Simplified selection of rows in read_bsrn
AdamRJensen Jan 16, 2021
93b92c8
Added read_bsrn to api.rst
AdamRJensen Jan 16, 2021
b0a6ddf
Merge pull request #1 from AdamRJensen/AdamRJensen-patch-1
AdamRJensen Jan 24, 2021
c6447a3
Delete 2021_01_16_read_bsrn_pull_request_v2.py
AdamRJensen Jan 24, 2021
52a952a
Improved format, e.g removed trailing white spaces
AdamRJensen Jan 24, 2021
870095a
Fixed spacing issues
AdamRJensen Jan 24, 2021
444e979
Update v0.9.0.rst
AdamRJensen Jan 24, 2021
f11288e
Add iotools.bsrn and import read_bsrn
AdamRJensen Jan 25, 2021
93deb2f
Merge pull request #2 from AdamRJensen/patch-3
AdamRJensen Jan 25, 2021
2fc95ad
Split multiple lines to obey 75 character limit
AdamRJensen Jan 25, 2021
656bbda
Corrected indentation
AdamRJensen Jan 25, 2021
c10d75b
Fixed indentation again
AdamRJensen Jan 25, 2021
d16d935
Remove bsrn email in description
AdamRJensen Feb 1, 2021
86cfb17
Correct COL_SPEC variable
AdamRJensen Feb 1, 2021
2eb3d44
Changed air_temperature to temp_air
AdamRJensen Feb 2, 2021
3218ab5
Add test_bsrn file
AdamRJensen Feb 2, 2021
ad8d45a
Reference to FTP updated
AdamRJensen Feb 8, 2021
fe632b8
Add zipped bsrn test file
AdamRJensen Feb 9, 2021
db1ac24
Update test filename
AdamRJensen Feb 9, 2021
a4c1d6f
Get file month/year from file instead of filename
AdamRJensen Feb 9, 2021
fc6f56d
Fixed formatting/stickler issues
AdamRJensen Feb 9, 2021
d7a5af8
Fixed formatting/stickler issues
AdamRJensen Feb 9, 2021
17d9b0b
Fixed formatting/stickler issues
AdamRJensen Feb 9, 2021
6ab294f
Fix to test_format_index
AdamRJensen Feb 9, 2021
5f59024
Refactored file opening and utc localization
AdamRJensen Feb 9, 2021
66209e4
Fixed indentation issue
AdamRJensen Feb 9, 2021
32a7cfb
Fixed hyperlink
AdamRJensen Feb 9, 2021
536f53c
Fixed doc error
AdamRJensen Feb 9, 2021
63dd3ac
Handle file start date explicitly
AdamRJensen Feb 10, 2021
6924183
Correct pytest fixture magic
AdamRJensen Feb 10, 2021
5e5f9d5
Fix indentation broken by previous commit
AdamRJensen Feb 10, 2021
5cf3d30
Correct Dataframe to DataFrame in doc string
AdamRJensen Feb 10, 2021
71895d1
Add offset to line num after explicitly handling start date
AdamRJensen Feb 10, 2021
b5ed6ee
Update test_bsrn.py
AdamRJensen Feb 10, 2021
23d4525
Added compression='infer', fixed end line number issue
AdamRJensen Feb 10, 2021
8dae943
Fixed test issue
AdamRJensen Feb 10, 2021
23c3455
Changed timedelta unit from min to minute
AdamRJensen Feb 10, 2021
260b68a
Add files via upload
AdamRJensen Feb 10, 2021
6b52a72
Changed to_timedelta unit from minute' to 'T'
AdamRJensen Feb 10, 2021
7c12848
Updated test to cover unzipped and zipped files
AdamRJensen Feb 10, 2021
17206df
Removed error causing blank line in test file
AdamRJensen Feb 10, 2021
9466626
Change to Unix end of line character from file by wholmgren
AdamRJensen Feb 10, 2021
4d3a21c
Remove extra line at end of file
AdamRJensen Feb 10, 2021
4db55be
Fix typo in bsrn.py doc string
AdamRJensen Feb 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/sphinx/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ relevant to solar energy modeling.
iotools.parse_psm3
iotools.get_pvgis_tmy
iotools.read_pvgis_tmy
iotools.read_bsrn

A :py:class:`~pvlib.location.Location` object may be created from metadata
in some files.
Expand Down
3 changes: 3 additions & 0 deletions docs/sphinx/source/whatsnew/v0.9.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Deprecations

Enhancements
~~~~~~~~~~~~
* Add :func:`~pvlib.iotools.read_bsrn` for reading BSRN solar radiation data
files. (:pull:`1145`, :issue:`1015`)
* In :py:class:`~pvlib.modelchain.ModelChain`, attributes which contain
output of models are now collected into ``ModelChain.results``.
(:pull:`1076`, :issue:`1067`)
Expand Down Expand Up @@ -113,3 +115,4 @@ Contributors
* Mark Mikofski (:ghuser:`mikofski`)
* Nate Croft (:ghuser:`ncroft-b4`)
* Kevin Anderson (:ghuser:`kanderso-nrel`)
* Adam R. Jensen (:ghuser:`AdamRJensen`)
86,901 changes: 86,901 additions & 0 deletions pvlib/data/bsrn-lr0100-pay0616.dat

Large diffs are not rendered by default.

Binary file added pvlib/data/bsrn-pay0616.dat.gz
Binary file not shown.
1 change: 1 addition & 0 deletions pvlib/iotools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
from pvlib.iotools.psm3 import read_psm3 # noqa: F401
from pvlib.iotools.psm3 import parse_psm3 # noqa: F401
from pvlib.iotools.pvgis import get_pvgis_tmy, read_pvgis_tmy # noqa: F401
from pvlib.iotools.bsrn import read_bsrn # noqa: F401
142 changes: 142 additions & 0 deletions pvlib/iotools/bsrn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"""Functions to read data from the Baseline Surface Radiation Network (BSRN).
.. codeauthor:: Adam R. Jensen<[email protected]>
"""

import pandas as pd
import gzip

COL_SPECS = [(0, 3), (4, 9), (10, 16), (16, 22), (22, 27), (27, 32), (32, 39),
(39, 45), (45, 50), (50, 55), (55, 64), (64, 70), (70, 75)]

BSRN_COLUMNS = ['day', 'minute',
'ghi', 'ghi_std', 'ghi_min', 'ghi_max',
'dni', 'dni_std', 'dni_min', 'dni_max',
'empty', 'empty', 'empty', 'empty', 'empty',
'dhi', 'dhi_std', 'dhi_min', 'dhi_max',
'lwd', 'lwd_std', 'lwd_min', 'lwd_max',
'temp_air', 'relative_humidity', 'pressure']


def read_bsrn(filename):
"""
Read a BSRN station-to-archive file into a DataFrame.

The BSRN (Baseline Surface Radiation Network) is a world wide network
of high-quality solar radiation monitoring stations as described in [1]_.
The function only parses the basic measurements (LR0100), which include
global, diffuse, direct and downwelling long-wave radiation [2]_. Future
updates may include parsing of additional data and meta-data.

BSRN files are freely available and can be accessed via FTP [3]_. Required

username and password are easily obtainable as described in the BSRN's
Data Release Guidelines [4]_.



Parameters
----------
filename: str
A relative or absolute file path.

Returns
-------
data: DataFrame
A DataFrame with the columns as described below. For more extensive
description of the variables, consult [2]_.

Notes
-----
The data DataFrame includes the following fields:

======================= ====== ==========================================
Key Format Description
======================= ====== ==========================================
day int Day of the month 1-31
minute int Minute of the day 0-1439
ghi float Mean global horizontal irradiance [W/m^2]
ghi_std float Std. global horizontal irradiance [W/m^2]
ghi_min float Min. global horizontal irradiance [W/m^2]
ghi_max float Max. global horizontal irradiance [W/m^2]
dni float Mean direct normal irradiance [W/m^2]
dni_std float Std. direct normal irradiance [W/m^2]
dni_min float Min. direct normal irradiance [W/m^2]
dni_max float Max. direct normal irradiance [W/m^2]
dhi float Mean diffuse horizontal irradiance [W/m^2]
dhi_std float Std. diffuse horizontal irradiance [W/m^2]
dhi_min float Min. diffuse horizontal irradiance [W/m^2]
dhi_max float Max. diffuse horizontal irradiance [W/m^2]
lwd float Mean. downward long-wave radiation [W/m^2]
lwd_std float Std. downward long-wave radiation [W/m^2]
lwd_min float Min. downward long-wave radiation [W/m^2]
lwd_max float Max. downward long-wave radiation [W/m^2]
temp_air float Air temperature [°C]
relative_humidity float Relative humidity [%]
pressure float Atmospheric pressure [hPa]
======================= ====== ==========================================

References
----------
.. [1] `World Radiation Monitoring Center - Baseline Surface Radiation
Network (BSRN)
<https://bsrn.awi.de/>`_
.. [2] `Update of the Technical Plan for BSRN Data Management, 2013,
Global Climate Observing System (GCOS) GCOS-172.
<https://bsrn.awi.de/fileadmin/user_upload/bsrn.awi.de/Publications/gcos-174.pdf>`_
.. [3] `BSRN Data Retrieval via FTP
<https://bsrn.awi.de/data/data-retrieval-via-ftp/>`_
.. [4] `BSRN Data Release Guidelines
<https://bsrn.awi.de/data/conditions-of-data-release/>`_
"""

# Read file and store the starting line number for each logical record (LR)
line_no_dict = {}
if str(filename).endswith('.gz'): # check if file is a gzipped (.gz) file
open_func, mode = gzip.open, 'rt'
else:
open_func, mode = open, 'r'
with open_func(filename, mode) as f:
f.readline() # first line should be *U0001, so read it and discard
line_no_dict['0001'] = 0
date_line = f.readline() # second line contains the year and month
start_date = pd.Timestamp(year=int(date_line[7:11]),
month=int(date_line[3:6]), day=1,
tz='UTC') # BSRN timestamps are UTC
for num, line in enumerate(f, start=2):
if line.startswith('*'): # Find start of all logical records
line_no_dict[line[2:6]] = num # key is 4 digit LR number

# Determine start and end line of logical record LR0100 to be parsed
start_row = line_no_dict['0100'] + 1 # Start line number
# If LR0100 is the last logical record, then read rest of file
if start_row-1 == max(line_no_dict.values()):
end_row = num # then parse rest of the file
else: # otherwise parse until the beginning of the next logical record
end_row = min([i for i in line_no_dict.values() if i > start_row]) - 1
nrows = end_row-start_row+1

# Read file as a fixed width file (fwf)
data = pd.read_fwf(filename, skiprows=start_row, nrows=nrows, header=None,
colspecs=COL_SPECS, na_values=[-999.0, -99.9],
compression='infer')

# Create multi-index and unstack, resulting in one column for each variable
data = data.set_index([data.index // 2, data.index % 2])
data = data.unstack(level=1).swaplevel(i=0, j=1, axis='columns')

# Sort columns to match original order and assign column names
data = data.reindex(sorted(data.columns), axis='columns')
data.columns = BSRN_COLUMNS
# Drop empty columns
data = data.drop('empty', axis='columns')

# Change day and minute type to integer
data['day'] = data['day'].astype('Int64')
data['minute'] = data['minute'].astype('Int64')

# Set datetime index
data.index = (start_date
+ pd.to_timedelta(data['day']-1, unit='d')
+ pd.to_timedelta(data['minute'], unit='T'))

return data
26 changes: 26 additions & 0 deletions pvlib/tests/iotools/test_bsrn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
tests for :mod:`pvlib.iotools.bsrn`
"""


import pandas as pd
import pytest

from pvlib.iotools import bsrn
from conftest import DATA_DIR, assert_index_equal


@pytest.mark.parametrize('testfile,expected_index', [
('bsrn-pay0616.dat.gz',
pd.date_range(start='20160601', periods=43200, freq='1min', tz='UTC')),
('bsrn-lr0100-pay0616.dat',
pd.date_range(start='20160601', periods=43200, freq='1min', tz='UTC')),
])
def test_read_bsrn(testfile, expected_index):
data = bsrn.read_bsrn(DATA_DIR / testfile)
assert_index_equal(expected_index, data.index)
assert 'ghi' in data.columns
assert 'dni_std' in data.columns
assert 'dhi_min' in data.columns
assert 'lwd_max' in data.columns
assert 'relative_humidity' in data.columns