Skip to content

Commit 35ca540

Browse files
committed
deprecate parse_cams
1 parent 7b49520 commit 35ca540

File tree

2 files changed

+41
-62
lines changed

2 files changed

+41
-62
lines changed

pvlib/iotools/sodapro.py

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import requests
88
import io
99
import warnings
10+
from pvlib import tools
1011

12+
from pvlib._deprecation import deprecated
1113

1214
URL = 'api.soda-solardata.com'
1315

@@ -145,7 +147,7 @@ def get_cams(latitude, longitude, start, end, email, identifier='mcclear',
145147
146148
See Also
147149
--------
148-
pvlib.iotools.read_cams, pvlib.iotools.parse_cams
150+
pvlib.iotools.read_cams
149151
150152
Raises
151153
------
@@ -231,20 +233,22 @@ def get_cams(latitude, longitude, start, end, email, identifier='mcclear',
231233
# Successful requests returns a csv data file
232234
else:
233235
fbuf = io.StringIO(res.content.decode('utf-8'))
234-
data, metadata = parse_cams(fbuf, integrated=integrated, label=label,
235-
map_variables=map_variables)
236+
data, metadata = read_cams(fbuf, integrated=integrated, label=label,
237+
map_variables=map_variables)
236238
return data, metadata
237239

238240

239-
def parse_cams(fbuf, integrated=False, label=None, map_variables=True):
241+
def read_cams(filename, integrated=False, label=None, map_variables=True):
240242
"""
241-
Parse a file-like buffer with data in the format of a CAMS Radiation or
242-
McClear file. The CAMS solar radiation services are described in [1]_.
243+
Read a file or file-like buffer with data in the format of a CAMS
244+
Radiation or McClear file.
245+
246+
The CAMS solar radiation services are described in [1]_.
243247
244248
Parameters
245249
----------
246-
fbuf: file-like object
247-
File-like object containing data to read.
250+
filename: str, path-like, or buffer
251+
Filename or in-memory buffer of a file containing data to read.
248252
integrated: boolean, default False
249253
Whether to return radiation parameters as integrated values (Wh/m^2)
250254
or as average irradiance values (W/m^2) (pvlib preferred units)
@@ -264,23 +268,31 @@ def parse_cams(fbuf, integrated=False, label=None, map_variables=True):
264268
265269
See Also
266270
--------
267-
pvlib.iotools.read_cams, pvlib.iotools.get_cams
271+
pvlib.iotools.get_cams
268272
269273
References
270274
----------
271275
.. [1] `CAMS solar radiation documentation
272276
<https://atmosphere.copernicus.eu/solar-radiation>`_
273277
"""
274278
metadata = {}
275-
# Initial lines starting with # contain metadata
276-
while True:
277-
line = fbuf.readline().rstrip('\n')
278-
if line.startswith('# Observation period'):
279-
# The last line of the metadata section contains the column names
280-
names = line.lstrip('# ').split(';')
281-
break # End of metadata section has been reached
282-
elif ': ' in line:
283-
metadata[line.split(': ')[0].lstrip('# ')] = line.split(': ')[1]
279+
280+
with tools._file_context_manager(filename) as fbuf:
281+
282+
# Initial lines starting with # contain metadata
283+
while True:
284+
line = fbuf.readline().rstrip('\n')
285+
if line.startswith('# Observation period'):
286+
# The last line of the metadata section contains the column names
287+
names = line.lstrip('# ').split(';')
288+
break # End of metadata section has been reached
289+
elif ': ' in line:
290+
key = line.split(': ')[0].lstrip('# ')
291+
value = line.split(': ')[1]
292+
metadata[key] = value
293+
294+
data = pd.read_csv(fbuf, sep=';', comment='#', header=None,
295+
names=names)
284296

285297
# Convert latitude, longitude, and altitude values from strings to floats
286298
for k_old in list(metadata.keys()):
@@ -296,8 +308,6 @@ def parse_cams(fbuf, integrated=False, label=None, map_variables=True):
296308
metadata['Summarization (integration) period']]
297309
metadata['time_step'] = time_step
298310

299-
data = pd.read_csv(fbuf, sep=';', comment='#', header=None, names=names)
300-
301311
obs_period = data['Observation period'].str.split('/')
302312

303313
# Set index as the start observation time (left) and localize to UTC
@@ -336,43 +346,5 @@ def parse_cams(fbuf, integrated=False, label=None, map_variables=True):
336346
return data, metadata
337347

338348

339-
def read_cams(filename, integrated=False, label=None, map_variables=True):
340-
"""
341-
Read a CAMS Radiation or McClear file into a pandas DataFrame.
342-
343-
CAMS Radiation and McClear are described in [1]_.
344-
345-
Parameters
346-
----------
347-
filename: str
348-
Filename of a file containing data to read.
349-
integrated: boolean, default False
350-
Whether to return radiation parameters as integrated values (Wh/m^2)
351-
or as average irradiance values (W/m^2) (pvlib preferred units)
352-
label : {'right', 'left}, optional
353-
Which bin edge label to label time-step with. The default is 'left' for
354-
all time steps except for '1M' which has a default of 'right'.
355-
map_variables: bool, default: True
356-
When true, renames columns of the Dataframe to pvlib variable names
357-
where applicable. See variable :const:`VARIABLE_MAP`.
358-
359-
Returns
360-
-------
361-
data: pandas.DataFrame
362-
Timeseries data from CAMS Radiation or McClear.
363-
See :func:`pvlib.iotools.get_cams` for fields.
364-
metadata: dict
365-
Metadata available in the file.
366-
367-
See Also
368-
--------
369-
pvlib.iotools.parse_cams, pvlib.iotools.get_cams
370-
371-
References
372-
----------
373-
.. [1] `CAMS solar radiation documentation
374-
<https://atmosphere.copernicus.eu/solar-radiation>`_
375-
"""
376-
with open(str(filename), 'r') as fbuf:
377-
content = parse_cams(fbuf, integrated, label, map_variables)
378-
return content
349+
parse_cams = deprecated(since="0.12.1", name="parse_cams",
350+
alternative="read_cams")(read_cams)

tests/iotools/test_sodapro.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from tests.conftest import TESTS_DATA_DIR, assert_frame_equal
1212

1313

14+
from pvlib._deprecation import pvlibDeprecationWarning
15+
1416
testfile_mcclear_verbose = TESTS_DATA_DIR / 'cams_mcclear_1min_verbose.csv'
1517
testfile_mcclear_monthly = TESTS_DATA_DIR / 'cams_mcclear_monthly.csv'
1618
testfile_radiation_verbose = TESTS_DATA_DIR / 'cams_radiation_1min_verbose.csv'
@@ -144,7 +146,6 @@
144146
0.9897]])
145147

146148

147-
# @pytest.fixture
148149
def generate_expected_dataframe(values, columns, index, dtypes):
149150
"""Create dataframe from arrays of values, columns and index, in order to
150151
use this dataframe to compare to.
@@ -185,6 +186,12 @@ def test_read_cams_integrated_unmapped_label():
185186
assert_frame_equal(out, expected, check_less_precise=True)
186187

187188

189+
def test_parse_cams_deprecated():
190+
with pytest.warns(pvlibDeprecationWarning, match='Use read_cams instead'):
191+
with open(testfile_radiation_verbose, mode="r") as fbuf:
192+
_ = sodapro.parse_cams(fbuf)
193+
194+
188195
def test_read_cams_metadata():
189196
_, metadata = sodapro.read_cams(testfile_mcclear_monthly, integrated=False)
190197
assert metadata['Time reference'] == 'Universal time (UT)'
@@ -203,7 +210,7 @@ def test_read_cams_metadata():
203210
values_radiation_monthly, dtypes_radiation, 'cams_radiation')])
204211
def test_get_cams(requests_mock, testfile, index, columns, values, dtypes,
205212
identifier):
206-
"""Test that get_cams generates the correct URI request and that parse_cams
213+
"""Test that get_cams generates the correct URI request and that read_cams
207214
is being called correctly"""
208215
# Open local test file containing McClear mothly data
209216
with open(testfile, 'r') as test_file:

0 commit comments

Comments
 (0)