-
Notifications
You must be signed in to change notification settings - Fork 48
Attempt concatenation when cubes have overlapping data #280
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
mattiarighi
merged 32 commits into
master
from
development_experiment_better_concatenation
Feb 11, 2020
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b2109a7
optional for multiple exps
valeriupredoi 1b4ac2c
cleaned up
valeriupredoi 745d7c3
cleaned up more
valeriupredoi e594c07
Add test and refactor code
689eae0
Merge branch 'fix_extract_time' into development_experiment_better_co…
18d6a25
Remove debug prints
f0bfcdd
Small refactor
e25a372
removed test that fails due to overlapping cubes
valeriupredoi 3ac8cd1
fixed proper the exceptional concatenation
valeriupredoi 706146a
adjusted according to comments
valeriupredoi c6f646f
Merge branch 'development' into development_experiment_better_concate…
valeriupredoi fa9fc7c
Merge branch 'development' into development_experiment_better_concate…
valeriupredoi 8f2a1cb
added fail case straight in func
valeriupredoi 53b7efa
added moar tests
valeriupredoi 4497a4f
pinning python
valeriupredoi 32ca777
pinning python in meta
valeriupredoi bda670c
one moar test
valeriupredoi 7992312
added doc on concatenation
valeriupredoi fc788ec
fixed indentation
valeriupredoi 8e5709d
full coverage for concatenate smart
valeriupredoi 8801029
explained the cube overlap case for multiple experiments
valeriupredoi 0651780
Fix minor style issues
bouweandela e747d68
Merge branch 'development' into development_experiment_better_concate…
bouweandela 63bccfe
Fix imports
bouweandela 6cdf4c4
Merge branch 'development' into development_experiment_better_concate…
bouweandela fa49c0d
Merge branch 'master' into development_experiment_better_concatenation
valeriupredoi 53f0949
added bunch of debug message to please the Bouwe gods heh
valeriupredoi 7e7b17b
Bouwe suggestion
valeriupredoi 0736cf6
Bouwe suggestion
valeriupredoi 24e305f
Merge branch 'master' into development_experiment_better_concatenation
valeriupredoi f0475dd
Fix syntax error and clean up a bit
bouweandela 3c8baf5
Merge branch 'master' into development_experiment_better_concatenation
bouweandela 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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import yaml | ||
|
|
||
| from .._task import write_ncl_settings | ||
| from ._time import extract_time | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -87,9 +88,20 @@ def _fix_cube_attributes(cubes): | |
| def concatenate(cubes): | ||
| """Concatenate all cubes after fixing metadata.""" | ||
| _fix_cube_attributes(cubes) | ||
|
|
||
| concatenated = iris.cube.CubeList(cubes).concatenate() | ||
| if len(concatenated) == 2: | ||
| try: | ||
| concatenated[0].coord('time') | ||
| concatenated[1].coord('time') | ||
| except iris.exceptions.CoordinateNotFoundError: | ||
| pass | ||
| else: | ||
| concatenated = _concatenate_overlapping_cubes(concatenated) | ||
|
|
||
| if len(concatenated) == 1: | ||
| return concatenated[0] | ||
|
|
||
| logger.error('Can not concatenate cubes into a single one.') | ||
| logger.error('Resulting cubes:') | ||
| for cube in concatenated: | ||
|
|
@@ -269,3 +281,90 @@ def _write_ncl_metadata(output_dir, metadata): | |
| write_ncl_settings(info, filename) | ||
|
|
||
| return filename | ||
|
|
||
|
|
||
| def _concatenate_overlapping_cubes(cubes): | ||
| """Concatenate time-overlapping cubes (two cubes only).""" | ||
| # we arrange [cube1, cube2] so that cube1.start <= cube2.start | ||
| if cubes[0].coord('time').points[0] <= cubes[1].coord('time').points[0]: | ||
| cubes = [cubes[0], cubes[1]] | ||
| logger.debug( | ||
| "Will attempt to concatenate cubes %s " | ||
| "and %s in this order", cubes[0], cubes[1]) | ||
| else: | ||
| cubes = [cubes[1], cubes[0]] | ||
| logger.debug( | ||
| "Will attempt to concatenate cubes %s " | ||
| "and %s in this order", cubes[1], cubes[0]) | ||
|
|
||
| # get time end points | ||
| time_1 = cubes[0].coord('time') | ||
|
bouweandela marked this conversation as resolved.
|
||
| time_2 = cubes[1].coord('time') | ||
| data_start_1 = time_1.cell(0).point | ||
| data_start_2 = time_2.cell(0).point | ||
| data_end_1 = time_1.cell(-1).point | ||
| data_end_2 = time_2.cell(-1).point | ||
|
|
||
| # case 1: both cubes start at the same time -> return longer cube | ||
| if data_start_1 == data_start_2: | ||
| if data_end_1 <= data_end_2: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! |
||
| logger.debug( | ||
| "Both cubes start at the same time but cube %s " | ||
| "ends before %s", cubes[0], cubes[1]) | ||
| logger.debug("Cube %s contains all needed data so using it fully", | ||
| cubes[1]) | ||
| cubes = [cubes[1]] | ||
| else: | ||
| logger.debug( | ||
| "Both cubes start at the same time but cube %s " | ||
| "ends before %s", cubes[1], cubes[0]) | ||
| logger.debug("Cube %s contains all needed data so using it fully", | ||
| cubes[0]) | ||
| cubes = [cubes[0]] | ||
|
bouweandela marked this conversation as resolved.
|
||
|
|
||
| # case 2: cube1 starts before cube2 | ||
| else: | ||
| # find time overlap, if any | ||
| start_overlap = next((time_1.units.num2date(t) | ||
| for t in time_1.points if t in time_2.points), | ||
| None) | ||
| # case 2.0: no overlap (new iris implementaion does allow | ||
| # concatenation of cubes with no overlap) | ||
| if not start_overlap: | ||
| logger.debug( | ||
| "Unable to concatenate non-overlapping cubes\n%s\nand\n%s" | ||
| "separated in time.", cubes[0], cubes[1]) | ||
| # case 2.1: cube1 ends after cube2 -> return cube1 | ||
| elif data_end_1 > data_end_2: | ||
| cubes = [cubes[0]] | ||
| logger.debug("Using only data from %s", cubes[0]) | ||
| # case 2.2: cube1 ends before cube2 -> use full cube2 and shorten cube1 | ||
| else: | ||
| logger.debug( | ||
| "Extracting time slice between %s and %s from cube %s to use " | ||
| "it for concatenation with cube %s", "-".join([ | ||
| str(data_start_1.year), | ||
| str(data_start_1.month), | ||
| str(data_start_1.day) | ||
| ]), "-".join([ | ||
| str(start_overlap.year), | ||
| str(start_overlap.month), | ||
| str(start_overlap.day) | ||
| ]), cubes[0], cubes[1]) | ||
| c1_delta = extract_time(cubes[0], data_start_1.year, | ||
| data_start_1.month, data_start_1.day, | ||
| start_overlap.year, start_overlap.month, | ||
| start_overlap.day) | ||
| cubes = iris.cube.CubeList([c1_delta, cubes[1]]) | ||
| logger.debug("Attempting concatenatenation of %s with %s", | ||
| c1_delta, cubes[1]) | ||
| try: | ||
| cubes = [iris.cube.CubeList(cubes).concatenate_cube()] | ||
| except iris.exceptions.ConcatenateError as ex: | ||
| logger.error('Can not concatenate cubes: %s', ex) | ||
| logger.error('Cubes:') | ||
| for cube in cubes: | ||
| logger.error(cube) | ||
| raise ex | ||
|
|
||
| return cubes | ||
Oops, something went wrong.
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.