Repository URL to install this package:
|
Version:
0.6.1 ▾
|
url-data-connectors
/
argo_fleetmonitoring_connect.py
|
|---|
# Connects to argo fleet monitoring and parses json response
# e.g. https://fleetmonitoring.euro-argo.eu/floats/7900561
import datetime
from json import JSONDecodeError
import requests
import pandas as pd
from connectors.noaa_erddap_connect import known_platforms as noa_kp
argo_fleetmonitoring_url = "https://fleetmonitoring.euro-argo.eu/floats/{platform_code}"
argo_fleetmonitoring_time_format = "%Y-%m-%dT%H:%M:%S.%f%z"
# re-use known platforms of noaa erddap connect, filter ARGO platforms
known_platforms = {key:value for (key,value) in noa_kp.items() if value.startswith('ARGO')}
class ArgoFleetmonitoringConnectError(Exception):
pass
class ArgoFleetmonitoringConnect:
"""
This class knows how to connect to argo fleet monitoring and parses json response
as a pandas dataframe suitable for ingest into geoserver
"""
@staticmethod
def get_all_argo_fleetmonitoring_data():
ret = []
for platform_code, platform_shortname in known_platforms.items():
try:
ret.append(ArgoFleetmonitoringConnect.get_argo_fleetmonitoring_date(platform_code, platform_shortname))
except ArgoFleetmonitoringConnectError:
import logging
logging.warning(f'caught exception getting data for {platform_code}: {platform_shortname}')
if len(ret) > 0:
return pd.DataFrame(ret)
else:
# return empty Dataframe
return pd.DataFrame(columns=['obs_timestamp', 'platform_shortname', 'lat', 'lon'])
@staticmethod
def get_argo_fleetmonitoring_date(platform_code, platform_shortname):
"""Get dataset for a single platform"""
r = requests.get(argo_fleetmonitoring_url.format(platform_code= platform_code))
if r.ok:
try:
data = r.json()
except JSONDecodeError:
raise ArgoFleetmonitoringConnectError(f'No valid json response for {platform_code}: {platform_shortname}')
if not 'lastCycleBasicInfo' in data.keys():
raise ArgoFleetmonitoringConnectError(f'No last cycle for {platform_code}: {platform_shortname}')
ts = data['lastCycleBasicInfo']['date']
if not ts:
raise ArgoFleetmonitoringConnectError(f'No date for lastCycleBasicInfo for {platform_code}: {platform_shortname}')
ts = datetime.datetime.strptime(ts, argo_fleetmonitoring_time_format).replace(tzinfo=datetime.timezone.utc)
lat = data['lastCycleBasicInfo']['lat']
lon = data['lastCycleBasicInfo']['lon']
return {
'platform_shortname': platform_shortname,
'obs_timestamp': ts,
'lat': lat,
'lon': lon
}
else:
raise ArgoFleetmonitoringConnectError(f'Unable to get data for {platform_code}: {platform_shortname}: {r.reason}')