77import requests
88import io
99import warnings
10+ from pvlib import tools
1011
12+ from pvlib ._deprecation import deprecated
1113
1214URL = '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 )
0 commit comments