7
7
import requests
8
8
import io
9
9
import warnings
10
+ from pvlib import tools
10
11
12
+ from pvlib ._deprecation import deprecated
11
13
12
14
URL = 'api.soda-solardata.com'
13
15
@@ -145,7 +147,7 @@ def get_cams(latitude, longitude, start, end, email, identifier='mcclear',
145
147
146
148
See Also
147
149
--------
148
- pvlib.iotools.read_cams, pvlib.iotools.parse_cams
150
+ pvlib.iotools.read_cams
149
151
150
152
Raises
151
153
------
@@ -231,20 +233,22 @@ def get_cams(latitude, longitude, start, end, email, identifier='mcclear',
231
233
# Successful requests returns a csv data file
232
234
else :
233
235
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 )
236
238
return data , metadata
237
239
238
240
239
- def parse_cams ( fbuf , integrated = False , label = None , map_variables = True ):
241
+ def read_cams ( filename , integrated = False , label = None , map_variables = True ):
240
242
"""
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]_.
243
247
244
248
Parameters
245
249
----------
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.
248
252
integrated: boolean, default False
249
253
Whether to return radiation parameters as integrated values (Wh/m^2)
250
254
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):
264
268
265
269
See Also
266
270
--------
267
- pvlib.iotools.read_cams, pvlib.iotools. get_cams
271
+ pvlib.iotools.get_cams
268
272
269
273
References
270
274
----------
271
275
.. [1] `CAMS solar radiation documentation
272
276
<https://atmosphere.copernicus.eu/solar-radiation>`_
273
277
"""
274
278
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 )
284
296
285
297
# Convert latitude, longitude, and altitude values from strings to floats
286
298
for k_old in list (metadata .keys ()):
@@ -296,8 +308,6 @@ def parse_cams(fbuf, integrated=False, label=None, map_variables=True):
296
308
metadata ['Summarization (integration) period' ]]
297
309
metadata ['time_step' ] = time_step
298
310
299
- data = pd .read_csv (fbuf , sep = ';' , comment = '#' , header = None , names = names )
300
-
301
311
obs_period = data ['Observation period' ].str .split ('/' )
302
312
303
313
# 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):
336
346
return data , metadata
337
347
338
348
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 )
0 commit comments