-
Notifications
You must be signed in to change notification settings - Fork 279
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
First cut of hybrid-pressure for GRIB. #95
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -19,4 +19,4 @@ | |
IF | ||
f.lbuser[3] == 33 | ||
THEN | ||
Reference('orography',) | ||
ReferenceTarget('orography', None) |
This file contains 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 |
---|---|---|
|
@@ -42,6 +42,46 @@ | |
|
||
RuleResult = collections.namedtuple('RuleResult', ['cube', 'matching_rules', 'factories']) | ||
Factory = collections.namedtuple('Factory', ['factory_class', 'args']) | ||
ReferenceTarget = collections.namedtuple('ReferenceTarget', | ||
('name', 'transform')) | ||
|
||
|
||
class ConcreteReferenceTarget(object): | ||
"""Everything you need to make a real Cube for a named reference.""" | ||
|
||
def __init__(self, name, transform=None): | ||
self.name = name | ||
"""The name used to connect references with referencees.""" | ||
self.transform = transform | ||
"""An optional transformation to apply to the cubes.""" | ||
self._src_cubes = iris.cube.CubeList() | ||
self._final_cube = None | ||
|
||
def add_cube(self, cube): | ||
self._src_cubes.append(cube) | ||
|
||
def as_cube(self): | ||
if self._final_cube is None: | ||
src_cubes = self._src_cubes | ||
if len(src_cubes) > 1: | ||
# Merge the reference cubes to allow for | ||
# time-varying surface pressure in hybrid-presure. | ||
src_cubes = src_cubes.merge() | ||
if len(src_cubes) > 1: | ||
warnings.warn('Multiple reference cubes for {}' | ||
.format(self.name)) | ||
src_cube = src_cubes[-1] | ||
|
||
if self.transform is None: | ||
self._final_cube = src_cube | ||
else: | ||
final_cube = src_cube.copy() | ||
attributes = self.transform(final_cube) | ||
for name, value in attributes.iteritems(): | ||
setattr(final_cube, name, value) | ||
self._final_cube = final_cube | ||
|
||
return self._final_cube | ||
|
||
|
||
# Controls the deferred import of all the symbols from iris.coords. | ||
|
@@ -343,7 +383,7 @@ def run_actions(self, cube, field): | |
except AttributeError, err: | ||
print >> sys.stderr, 'Failed to get value (%(error)s) to execute: %(command)s' % {'command':action, 'error': err} | ||
except Exception, err: | ||
print >> sys.stderr, 'Failed (msg:%(error)s) to run: %(command)s\nFrom the rule:%(me)r' % {'me':self, 'command':action, 'error': err} | ||
print >> sys.stderr, 'Failed (msg:%(error)s) to run:\n %(command)s\nFrom the rule:\n%(me)r' % {'me':self, 'command':action, 'error': err} | ||
raise err | ||
return factories | ||
|
||
|
@@ -591,22 +631,13 @@ class _ReferenceError(Exception): | |
pass | ||
|
||
|
||
def _dereference_args(factory, reference_cubes, regrid_cache, cube): | ||
def _dereference_args(factory, reference_targets, regrid_cache, cube): | ||
"""Converts all the arguments for a factory into concrete coordinates.""" | ||
args = [] | ||
for arg in factory.args: | ||
if isinstance(arg, iris.fileformats.rules.Reference): | ||
if arg.name in reference_cubes: | ||
# Merge the reference cubes to allow for | ||
# time-varying surface pressure in hybrid-presure. | ||
if len(reference_cubes[arg.name]) > 1: | ||
ref_cubes = iris.cube.CubeList(reference_cubes[arg.name]) | ||
merged = ref_cubes.merge() | ||
if len(merged) > 1: | ||
warnings.warn('Multiple reference cubes for {}' | ||
.format(arg.name)) | ||
reference_cubes[arg.name] = merged[-1:] | ||
src = reference_cubes[arg.name][0] | ||
if arg.name in reference_targets: | ||
src = reference_targets[arg.name].as_cube() | ||
# If necessary, regrid the reference cube to | ||
# match the grid of this cube. | ||
src = _ensure_aligned(regrid_cache, src, cube) | ||
|
@@ -708,7 +739,7 @@ def _ensure_aligned(regrid_cache, src_cube, target_cube): | |
|
||
|
||
def load_cubes(filenames, user_callback, loader): | ||
reference_cubes = {} | ||
concrete_reference_targets = {} | ||
results_needing_reference = [] | ||
|
||
if isinstance(filenames, basestring): | ||
|
@@ -730,7 +761,15 @@ def load_cubes(filenames, user_callback, loader): | |
rules = loader.cross_ref_rules.matching_rules(field) | ||
for rule in rules: | ||
reference, = rule.run_actions(cube, field) | ||
reference_cubes.setdefault(reference.name, []).append(cube) | ||
name = reference.name | ||
# Register this cube as a source cube for the named | ||
# reference. | ||
concrete_reference_target = concrete_reference_targets.get(name) | ||
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. One letter difference between Rename |
||
if concrete_reference_target is None: | ||
concrete_reference_target = ConcreteReferenceTarget( | ||
name, reference.transform) | ||
concrete_reference_targets[name] = concrete_reference_target | ||
concrete_reference_target.add_cube(cube) | ||
|
||
if rules_result.factories: | ||
results_needing_reference.append(rules_result) | ||
|
@@ -742,7 +781,7 @@ def load_cubes(filenames, user_callback, loader): | |
cube = result.cube | ||
for factory in result.factories: | ||
try: | ||
args = _dereference_args(factory, reference_cubes, | ||
args = _dereference_args(factory, concrete_reference_targets, | ||
regrid_cache, cube) | ||
except _ReferenceError as e: | ||
msg = 'Unable to create instance of {factory}. ' + e.message | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 658-659:
Not very helpful to the user, seeing a literal
{filenames}
doesn't clarify the issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not a mistake. The message of this error is used as a format string in line 789.