-
Notifications
You must be signed in to change notification settings - Fork 1
Add functions to import the datasets #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c3cceb8
Update .gitignore
stefsmeets 4f63416
Add basic setup.py
stefsmeets 136e1b1
Add basic functionality to load datasets
stefsmeets f3c9a30
Set cube helper logging level to ERROR to hide warnings
stefsmeets 294833e
Comment out problematic dataset
stefsmeets 869dc5b
Speed up data loading and lose the cube-helper dependency
stefsmeets 24bb39a
Rename data -> cubes
stefsmeets 29885ec
Add package data
stefsmeets 26f3370
Update esmvaltool_sample_data/loader.py
stefsmeets bc38cde
Merge branch 'master' into make_package
stefsmeets 1316756
Add developer imports
stefsmeets e46ae61
Address review comments
stefsmeets 042dd66
Update doc strings and add annotations
stefsmeets 179416d
Select subset of data
stefsmeets 6de6154
Add whitelists for specific subsets of data
stefsmeets 9b08257
Merge branch 'master' into make_package
stefsmeets 710989a
Use ignore list to filter problematic datasets
stefsmeets d478a8d
Ignore dataset that fails to regrid
stefsmeets f2d4e97
Remove unused functions
stefsmeets 256a9a9
Remove code used for testing
stefsmeets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| recursive-include esmvaltool_sample_data/data/ *.nc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from pathlib import Path | ||
|
|
||
| import cf_units | ||
| import iris | ||
| import yaml | ||
|
|
||
| base_dir = Path(__file__).parent | ||
|
|
||
| VERBOSE = False | ||
|
|
||
| with open(base_dir / 'datasets.yml', 'r') as f: | ||
| config = yaml.safe_load(f) | ||
|
|
||
| ignore_list = [fn.replace('.', '/') for fn in config['ignore']] | ||
|
|
||
|
|
||
| def strip_attributes(cube: 'iris.Cube') -> None: | ||
| """Remove attributes in-place that cause issues with merging and | ||
| concatenation.""" | ||
| for attr in ['creation_date', 'tracking_id', 'history']: | ||
| if attr in cube.attributes: | ||
| cube.attributes.pop(attr) | ||
|
|
||
|
|
||
| def simplify_time(cube: 'iris.Cube') -> None: | ||
| """Simplifies the time coordinate in-place.""" | ||
| coord = cube.coord('time') | ||
| coord.convert_units( | ||
| cf_units.Unit('days since 1850-1-1 00:00:00', | ||
| calendar=coord.units.calendar)) | ||
|
|
||
|
|
||
| def load_cubes_from_input_dirs(input_dirs: list) -> 'iris.Cube': | ||
| """Generator that loads all *.nc files from each input dir into a cube.""" | ||
| for i, input_dir in enumerate(sorted(input_dirs)): | ||
| if VERBOSE: | ||
| print(f'Loading #{i:02d}:', input_dir) | ||
|
|
||
| files = input_dir.glob('*.nc') | ||
| cubes = iris.load(str(file) for file in files) | ||
| for cube in cubes: | ||
| strip_attributes(cube) | ||
| simplify_time(cube) | ||
|
|
||
| cube = cubes.concatenate_cube() | ||
|
|
||
| if VERBOSE: | ||
| print(' ', cube.shape, cube.coord('time').units.calendar) | ||
|
|
||
| yield cube | ||
|
|
||
|
|
||
| def filter_ignored_datasets(dirs, root): | ||
| for drc in dirs: | ||
| test_drc = str(drc.relative_to(root)) | ||
| if test_drc not in ignore_list: | ||
| yield drc | ||
| elif VERBOSE: | ||
| print('Ignored:', test_drc) | ||
|
|
||
|
|
||
| def load_timeseries_cubes(mip_table: str = 'Amon') -> list: | ||
| """Returns a list of iris cubes with timeseries data. | ||
|
|
||
| The data are: ta / Amon / historical / r1i1p1f1, any grid, 1950 - onwards. | ||
| All dimensions were reduced to a few steps except for the time dimension. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| mip_table: str | ||
| select monthly (`Amon`) or daily (`day`) data. | ||
|
|
||
| Returns | ||
| ------- | ||
| list of iris.cube | ||
| """ | ||
|
|
||
| timeseries_dir = base_dir / 'data' / 'timeseries' | ||
|
|
||
| paths = timeseries_dir.glob(f'**/{mip_table}/**/*.nc') | ||
| input_dirs = list(set(path.parent for path in paths)) | ||
|
|
||
| input_dirs = list(filter_ignored_datasets(input_dirs, timeseries_dir)) | ||
|
|
||
| cubes = load_cubes_from_input_dirs(input_dirs) | ||
|
|
||
| return list(cubes) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| VERBOSE = True | ||
|
|
||
| for mip_table in ( | ||
| 'Amon', | ||
| 'day', | ||
| ): | ||
| print() | ||
| print(f'Loading `{mip_table}`') | ||
| ts = load_timeseries_cubes(mip_table) | ||
|
|
||
| first_cube = ts[0] | ||
| for i, cube in enumerate(ts): | ||
| print(i) | ||
| cube.regrid(grid=first_cube, scheme=iris.analysis.Linear()) | ||
|
|
||
| # breakpoint() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| from setuptools import setup | ||
stefsmeets marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| with open('README.md') as readme_file: | ||
| readme = readme_file.read() | ||
|
|
||
| PACKAGES = [ | ||
| 'esmvaltool_sample_data', | ||
| ] | ||
|
|
||
| setup( | ||
| name='ESMValTool sample data', | ||
| version='0.0.1', | ||
| description="ESMValTool sample data", | ||
| long_description=readme + '\n\n', | ||
| author="", | ||
| author_email='', | ||
| url='https://github.com/ESMValGroup/ESMValTool_sample_data', | ||
| packages=PACKAGES, | ||
| include_package_data=True, | ||
| license="", | ||
| zip_safe=False, | ||
| keywords='ESMValTool', | ||
| classifiers=[ | ||
| 'Development Status :: 2 - Pre-Alpha', | ||
| 'Intended Audience :: Developers', | ||
| 'License :: OSI Approved :: Apache Software License', | ||
| 'Natural Language :: English', | ||
| 'Programming Language :: Python :: 3', | ||
| 'Programming Language :: Python :: 3.6', | ||
| 'Programming Language :: Python :: 3.7', | ||
| ], | ||
| test_suite='tests', | ||
| install_requires=[ | ||
| 'scitools-iris>=2.2', | ||
| ], | ||
| # tests_require=[ | ||
| # 'pytest', | ||
| # 'pytest-cov', | ||
| # 'pycodestyle', | ||
| # ], | ||
| extras_require={ | ||
| 'develop': [ | ||
| 'codespell', | ||
stefsmeets marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'docformatter', | ||
| 'esgf-pyclient', | ||
| 'isort', | ||
| 'myproxyclient', | ||
| 'pre-commit', | ||
| 'prospector[with_pyroma]!=1.1.6.3,!=1.1.6.4', | ||
| 'yamllint', | ||
| 'yapf', | ||
| ], | ||
| }, | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.