Newer
Older
# -*- coding: utf-8 -*-
import netCDF4
import threddsclient
"""## Function for getting EBAS data as opendap or direct download for a given parameter
EBAS component name
Specify component name and protocol.
Default output protocol is "opendap urls" which provide access to data remotely, without having to download the physical file.
Also, "http" can be specified to get links to direct download of netcdf files.
"""
def get_opendap_urls_by_parameter(
station_opendap_urls, parameter, protocol='opendap'):
"""
Get all opendap URL's for a given parameter (will try best match in the variable list)
"""
urls = station_opendap_urls
datasets = []
for url in urls:
for var in list(netCDF4.Dataset(url).variables.keys()):
if var == parameter and url not in datasets:
datasets.append(url)
elif var.startswith('{0}_'.format(parameter)) and url not in datasets:
datasets.append(url)
else:
pass
if protocol == 'http':
download_urls = []
baseurl = 'https://thredds.nilu.no/thredds/fileServer/ebas/'
for ds in datasets:
ds = ds.rsplit('/', 1)[1]
ds = "{0}{1}".format(baseurl, ds)
download_urls.append(ds)
return download_urls
else:
return datasets