Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch CMIP5/CMIP6 #127

Merged
merged 5 commits into from
Aug 15, 2019
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,26 @@ Features
- Revert back to using official DRS attributes when building CMIP5 and CMIP6 collections.
(:pr:`126`) `Anderson Banihirwe`_

- Add ``.df`` property for interfacing with the built collection via dataframe
To maintain backwards compatiblity. (:pr:`127`) `Anderson Banihirwe`_


.. _NA-CORDEX: https://na-cordex.org/
.. _intake-esm-datastore: https://github.com/NCAR/intake-esm-datastore


Bug Fixes
----------

- For CMIP6, extract ``grid_label`` from directory path instead of file name. (:pr:`127`) `Anderson Banihirwe`_


Internal Changes
----------------




Intake-esm v2019.8.5 (2019-08-05)
==================================

Expand Down
1 change: 1 addition & 0 deletions intake_esm/cesm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _get_file_attrs(self, filepath):
fileparts = {key: None for key in keys}
fileparts['file_basename'] = file_basename
fileparts['file_fullpath'] = filepath
fileparts['file_dirname'] = os.path.dirname(filepath) + '/'

date_str_regex = r'\d{4}\-\d{4}|\d{6}\-\d{6}|\d{8}\-\d{8}|\d{10}Z\-\d{10}Z|\d{12}Z\-\d{12}Z'
datestr = _extract_attr_with_regex(file_basename, regex=date_str_regex)
Expand Down
2 changes: 1 addition & 1 deletion intake_esm/cesm_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pandas as pd
import xarray as xr
from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import aggregate, config
from .bld_collection_utils import get_subset
Expand Down
10 changes: 7 additions & 3 deletions intake_esm/cmip.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ def _get_file_attrs(self, filepath):
fileparts.update(f)

parent = os.path.dirname(filepath).strip('/')
parent_split = parent.split(fileparts['model'])
parent_split = parent.split(f"/{fileparts['model']}/")
part_1 = parent_split[0].strip('/').split('/')
part_2 = parent_split[1].strip('/').split('/')

fileparts['file_dirname'] = parent + '/'
fileparts['institute'] = part_1[-1]
fileparts['frequency'] = part_2[1]
fileparts['modeling_realm'] = part_2[2]
Expand Down Expand Up @@ -150,10 +151,13 @@ def _get_file_attrs(self, filepath):
fileparts.update(f)

parent = os.path.dirname(filepath).strip('/')
parent_split = parent.split(fileparts['source_id'])
parent_split = parent.split(f"/{fileparts['source_id']}/")
part_1 = parent_split[0].strip('/').split('/')
# part_2 = parent_split[1].strip('/').split('/')

grid_label = parent.split(f"/{fileparts['variable_id']}/")[1].strip('/').split('/')[0]

fileparts['file_dirname'] = parent + '/'
fileparts['grid_label'] = grid_label
fileparts['activity_id'] = part_1[-2]
fileparts['institution_id'] = part_1[-1]

Expand Down
2 changes: 1 addition & 1 deletion intake_esm/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import docrep
import pandas as pd
import xarray as xr
from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import config
from .bld_collection_utils import make_attrs
Expand Down
21 changes: 0 additions & 21 deletions intake_esm/collection_defs/.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions intake_esm/collection_defs/cmip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ collections:
- experiment
- file_basename
- file_fullpath
- file_dirname
- frequency
- institute
- mip_table
Expand Down Expand Up @@ -40,6 +41,7 @@ collections:
- experiment_id
- file_basename
- file_fullpath
- file_dirname
- grid_label
- institution_id
- member_id
Expand Down
4 changes: 3 additions & 1 deletion intake_esm/collection_defs/cordex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ collections:
- bias_corrected_or_raw
- file_fullpath
- file_basename
- file_dirname
order_by_columns:
- file_fullpath
- variable
required_columns:
- variable
- file_fullpath
2 changes: 2 additions & 0 deletions intake_esm/collection_defs/gmet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ collections:
- resolution
- file_fullpath
- file_basename
- file_dirname
order_by_columns:
- member_id
- time_range
Expand All @@ -23,6 +24,7 @@ collections:
- direct_access
- file_basename
- file_fullpath
- file_dirname
- forecast_initial_date
- forecast_initial_hour
- grid
Expand Down
1 change: 1 addition & 0 deletions intake_esm/collection_defs/mpige.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ collections:
- ensemble
- file_fullpath
- file_basename
- file_dirname
- ctrl_branch_year
- year_offset
- sequence_order
Expand Down
3 changes: 2 additions & 1 deletion intake_esm/cordex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Implementation for The ECMWF ERA5 Reanalyses data holdings """
import os

from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import aggregate, config
from .bld_collection_utils import _ensure_file_access, _reverse_filename_format, get_subset
Expand All @@ -25,6 +25,7 @@ def _get_file_attrs(self, filepath):
fileparts = {key: None for key in keys}
fileparts['file_basename'] = file_basename
fileparts['file_fullpath'] = filepath
fileparts['file_dirname'] = os.path.dirname(filepath) + '/'
filename_template = '{variable}.{experiment}.{global_climate_model}.{regional_climate_model}.{frequency}.{grid}.{bias_corrected_or_raw}.nc'

f = _reverse_filename_format(file_basename, filename_template)
Expand Down
5 changes: 5 additions & 0 deletions intake_esm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import s3fs
from cached_property import cached_property
from intake.catalog import Catalog
from intake.catalog.local import LocalCatalogEntry
from intake.utils import yaml_load
Expand Down Expand Up @@ -123,6 +124,10 @@ def __init__(

self._entries = {}

@cached_property
def df(self):
return self.ds.to_dataframe()

def describe(self, variables=()):
"""
TODO: Generate summary for variables in the .ds attribute
Expand Down
3 changes: 2 additions & 1 deletion intake_esm/era5.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

import xarray as xr
from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import aggregate, config
from .bld_collection_utils import get_subset
Expand All @@ -28,6 +28,7 @@ def _get_file_attrs(self, filepath):
fileparts = {key: None for key in keys}
fileparts['file_basename'] = file_basename
fileparts['file_fullpath'] = filepath
fileparts['file_dirname'] = os.path.dirname(filepath) + '/'

fileparts['stream'] = fs[1]
if fs[2] == 'an':
Expand Down
3 changes: 2 additions & 1 deletion intake_esm/gmet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Implementation for The Gridded Meteorological Ensemble Tool (GMET) data holdings """
import os

from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import aggregate, config
from .bld_collection_utils import _extract_attr_with_regex, get_subset
Expand All @@ -24,6 +24,7 @@ def _get_file_attrs(self, filepath):
fileparts = {key: None for key in keys}
fileparts['file_basename'] = file_basename
fileparts['file_fullpath'] = filepath
fileparts['file_dirname'] = os.path.dirname(filepath) + '/'

date_str_regex = r'\d{8}\_\d{8}'
datestr = _extract_attr_with_regex(filepath, regex=date_str_regex)
Expand Down
3 changes: 2 additions & 1 deletion intake_esm/mpige.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pandas as pd
import xarray as xr
from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import aggregate, config
from .bld_collection_utils import _extract_attr_with_regex, get_subset
Expand Down Expand Up @@ -36,6 +36,7 @@ def _get_file_attrs(self, filepath):
fileparts = {key: None for key in keys}
fileparts['file_basename'] = file_basename
fileparts['file_fullpath'] = filepath
fileparts['file_dirname'] = os.path.dirname(filepath) + '/'

date_str_regex = r'\d{4}\_\d{4}|\d{6}\_\d{6}|\d{8}\_\d{8}|\d{10}\_\d{10}|\d{12}\_\d{12}'
datestr = _extract_attr_with_regex(file_basename, regex=date_str_regex)
Expand Down
7 changes: 6 additions & 1 deletion intake_esm/source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import intake_xarray
import xarray as xr
from tqdm.autonotebook import tqdm
from cached_property import cached_property
from tqdm.auto import tqdm

from . import aggregate, config
from .bld_collection_utils import _ensure_file_access, get_subset
Expand Down Expand Up @@ -40,6 +41,10 @@ def get_results(self):
ds = get_subset(self.collection_name, self.query)
return ds

@cached_property
def df(self):
return self.ds.to_dataframe()

def _validate_kwargs(self, kwargs):

_kwargs = kwargs.copy()
Expand Down
2 changes: 1 addition & 1 deletion intake_esm/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from time import sleep
from warnings import warn

from tqdm.autonotebook import tqdm
from tqdm.auto import tqdm

from . import config

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pyyaml
tqdm
fsspec
s3fs
cached-property
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ select = B,C,E,F,W,T4,B9

[isort]
known_first_party=intake_esm
known_third_party=click,dask,docrep,fsspec,intake,intake_xarray,numpy,pandas,pkg_resources,pytest,s3fs,setuptools,tqdm,xarray,yaml
known_third_party=cached_property,click,dask,docrep,fsspec,intake,intake_xarray,numpy,pandas,pkg_resources,pytest,s3fs,setuptools,tqdm,xarray,yaml
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
Expand Down
18 changes: 18 additions & 0 deletions tests/cmip6/test_cmip6.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ def test_build_collection_file():
)
col = intake.open_esm_metadatastore(collection_name='cmip6_test_collection')
assert isinstance(col.ds, xr.Dataset)
assert isinstance(col.df, pd.DataFrame)
assert set(col.df.grid_label.unique()) == set(['gr', 'gn'])
assert set(col.df.variable_id.unique()) == set(
[
'prsn',
'prra',
'tasmax',
'evspsblveg',
'landCoverFrac',
'mrso',
'co3',
'gpp',
'residualFrac',
'mrfso',
]
)


def test_search():
Expand All @@ -39,6 +55,8 @@ def test_search():
assert isinstance(cat.ds, xr.Dataset)
assert len(cat.ds.index) > 0

assert isinstance(cat.df, pd.DataFrame)


@pytest.mark.parametrize(
'chunks, expected_chunks',
Expand Down