# -*- coding: utf-8 -*-
"""
**Utility functions for the omdpb package**.
"""
import importlib
import json
from datetime import datetime
from dateutil import parser
# Python3 and Python2 compatibility
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
DATE_FORMATS = ('%Y%m%d', '%Y%m%d%H%M')
[docs]def http_get_api(request):
""" *Makes a HTML GET request to the url ``request``*.
The content of the HTML GET request has to be either json or csv.
Parameters
----------
request : str
A string containing a url.
Returns
-------
pandas.core.frame.DataFrame
Returns a DataFrame containing the parsed request content.
Raises
------
urllib.error.HTTPError
If there is a problem with the connection (internal server error,
bad request, ...).
AssertionError
If the response status (HTML) code is not 200.
Examples
--------
>>> http_get_api(API_URL + 'stations')
ALT LAT ... coordinates type
0 210.00 46.243333 ... [669803.015640121, 5123442.04315501] Point
1 873.99 46.621876 ... [626295.144332811, 5164467.60475602] Point
2 2152.00 46.841700 ... [744723.350800056, 5192575.70046406] Point
3 2260.00 46.615600 ... [688387.444866793, 5165389.11604176] Point
4 2747.00 46.856100 ... [743964.273827689, 5194147.3777763] Point
5 2926.00 46.776700 ... [613580.465752357, 5181424.77411351] Point
"""
# make the GET request to the API
response = urlopen(Request(request))
# check if the request is valid and results in no http error
assert response.code == 200, 'not a valid response, \
http error {}'.format(response.code)
# read the response content (json or csv)
response_content = json.loads(response.read().decode('utf-8'))
return response_content
def _merge_dicts(dict1, dict2):
""" *Merges two dictionaries*.
This function is intended for internal use.
Parameters
----------
dict1 : dict
A dictionary to merge to ``dict2``.
dict2 : dict
A dictionary.
Returns
-------
dict
The merged dictionary.
Raises
------
AttributeError
If one of the input parameters is not a dictionary.
"""
dict2.update(dict1)
return dict2
[docs]def check_dependencies(package_names, ret=False):
""" *Check if a package can be imported, if not throw a warning*.
Parameters
----------
package_names : list
A list containing the packages' names (str) to be imported.
Returns
-------
NoneType
If all packages can be imported.
list
If ``ret`` is True; list of all packages that could not be found.
"""
not_met = []
for package in package_names:
try:
importlib.import_module(package)
except ImportError:
not_met.append(package)
if not_met:
errmsg = 'WARNING: the following packages could not be found: '
print(errmsg + ', '.join(not_met))
if ret:
return not_met
def _to_date_format(date, dformat, utc=False):
""" *Converts the Open Meteo Data Api date format to custom date format*.
This function is intended for internal use.
If ``utc`` is true, ``date`` will first be converted to UTC and
then returned in ``dformat``.
Throws a warning if ``date`` is not in the '%Y-%m-%dT%H:%M:%SCEST' or
'%Y-%m-%dT%H:%M:%SCET'format.
Parameters
----------
date : str
A date string from the Api.
dformat : str
The desired date format.
utc : boolean
Convert the date to UTC.
Returns
-------
str
The converted date string.
Examples
--------
>>> _to_date_format(date='2018-09-05T15:00:00CEST', dformat='%Y%m%d%H%M%S')
'20180903150000'
>>> _to_date_format(date='2018-09-05T15:00:00CEST',
dformat='%Y %b %d, %H:%M')
'2018 Sep 03, 15:00'
>>> _to_date_format(date='2018-09-05T15:00:00CEST',
format='%Y-%m-%d %H:%M:%S',
utc=True)
'2018-09-05 13:00:00'
>>> _to_date_format(date='2018-09-01T00:00:00CET',
format='%a %d %B, %I:%M %p',
utc=True)
'Fri 31 August, 11:00 PM'
"""
if 'CEST' in date:
dtime = parser.parse(date.replace('CEST', '+02:00'))
elif 'CET' in date:
dtime = parser.parse(date.replace('CET', '+01:00'))
else:
print('WARNING: date should have format %Y-%m-%dT%H:%M:%SCEST or ' +
'%Y-%m-%dT%H:%M:%SCET.')
return None
if utc:
dtime -= dtime.utcoffset()
return dtime.strftime(dformat)