Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 26 additions & 29 deletions clef/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,32 @@ def matching(session, cols, fixed, project='CMIP5', local=True, latest=True, **k
"""

results = pd.DataFrame()
try:
# use local search
if local:
msg = "There are no simulations stored locally"
# perform the query for each variable separately and concatenate the results
combs = [dict(zip(kwargs, x)) for x in itertools.product(*kwargs.values())]
for c in combs:
results = results.append(search(session,project=project.upper(),
latest=latest, **c),
ignore_index=True)
# use ESGF search
else:
msg = "There are no simulations currently available on the ESGF nodes"
kwquery = {k:tuple(v) for k,v in kwargs.items()}
kwquery['project']=project.upper()
attrs = ['dataset_id', 'version'] # datetime_start, datetime_stop
attrs.extend( load_vocabularies(project)['attributes'])
query=None
response = esgf_query(query, ','.join(attrs), latest=latest, **kwquery)
# can't create dataframe in one go because many values are unidimensional lists
res_list = []
for row in response['response']['docs']:
row['version'] = row['dataset_id'].split("|")[0].split(".")[-1],
res_list.append({k:(v[0] if isinstance(v,list) else v) for k,v in row.items()})
results = pd.DataFrame(res_list)

except Exception as e:
print('ERROR',str(e))
return None

# use local search
if local:
msg = "There are no simulations stored locally"
# perform the query for each variable separately and concatenate the results
combs = [dict(zip(kwargs, x)) for x in itertools.product(*kwargs.values())]
for c in combs:
results = results.append(search(session,project=project.upper(),
latest=latest, **c),
ignore_index=True)
# use ESGF search
else:
msg = "There are no simulations currently available on the ESGF nodes"
kwquery = {k:tuple(v) for k,v in kwargs.items()}
kwquery['project']=project.upper()
attrs = ['dataset_id', 'version'] # datetime_start, datetime_stop
attrs.extend( load_vocabularies(project)['attributes'])
query=None
response = esgf_query(query, ','.join(attrs), latest=latest, **kwquery)
# can't create dataframe in one go because many values are unidimensional lists
res_list = []
for row in response['response']['docs']:
row['version'] = row['dataset_id'].split("|")[0].split(".")[-1],
res_list.append({k:(v[0] if isinstance(v,list) else v) for k,v in row.items()})
results = pd.DataFrame(res_list)


# if nothing turned by query print warning and return
if len(results.index) == 0:
Expand Down
7 changes: 4 additions & 3 deletions test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pytest

from clef.code import and_filter, matching, local_latest, search, stats, ids_df
from clef.exception import ClefException
from code_fixtures import *

# Tests for the functions defined in code.py
Expand Down Expand Up @@ -145,9 +146,9 @@ def test_matching(session):
'ensemble':'r1i1p1',
'variable': ['x'],
}
# Errors should print a message and return 'None'
r = matching(session, ['variable','experiment'],['model','ensemble'], **facets)
assert r is None
# Errors should raise
with pytest.raises(ClefException):
matching(session, ['variable','experiment'],['model','ensemble'], **facets)


def test_ids_df(dids6, results6, dids5, results5):
Expand Down