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

First cut of hybrid-pressure for GRIB. #95

Merged
merged 2 commits into from
Sep 21, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion lib/iris/etc/grib_cross_reference_rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.

# PLACEHOLDER
# Equivalent to grib.paramId == 152
IF
grib.edition == 2
grib.centre == 'ecmf'
grib.discipline == 0
grib.parameterCategory == 3
grib.parameterNumber == 25
grib.typeOfFirstFixedSurface == 105
THEN
ReferenceTarget('surface_pressure', lambda cube: {'standard_name': 'surface_air_pressure', 'units': 'Pa', 'data': numpy.exp(cube.data)})
22 changes: 12 additions & 10 deletions lib/iris/etc/grib_rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -582,16 +582,18 @@ CoordAndDims(DimCoord(points=0.5*(grib.scaledValueOfFirstFixedSurface/(10.0**gri
#ExplicitCoord("model_level", "1", 'z', points=grib.scaledValueOfFirstFixedSurface)
#ExplicitCoord('level_height', 'm', 'z', points=grib.pv[grib.scaledValueOfFirstFixedSurface], definitive=True, coord_system=HybridHeightCS(Reference('orography')))
#ExplicitCoord('sigma', '1', 'z', points=grib.pv[grib.numberOfVerticalCoordinateValues/2 + grib.scaledValueOfFirstFixedSurface], coord_system=HybridHeightCS(Reference('orography')))
#
# hybrid pressure.105 deprecated for 119.
#IF
#grib.edition == 2
#grib.typeOfFirstFixedSurface in [105, 119]
#grib.numberOfCoordinatesValues > 0
#THEN
#ExplicitCoord("model_level", "1", 'z', points=grib.scaledValueOfFirstFixedSurface)
#ExplicitCoord('level_height', 'm', 'z', points=grib.pv[grib.scaledValueOfFirstFixedSurface], definitive=True, coord_system=HybridPressureCS(Reference('surface_pressure')))
#ExplicitCoord('sigma', '1', 'z', points=grib.pv[grib.numberOfPoints/2 + grib.scaledValueOfFirstFixedSurface], coord_system=HybridPressureCS(Reference('surface_pressure')))


# hybrid pressure. 105 deprecated for 119.
IF
grib.edition == 2
grib.typeOfFirstFixedSurface in [105, 119]
grib.numberOfCoordinatesValues > 0
THEN
CoordAndDims(AuxCoord(grib.scaledValueOfFirstFixedSurface, standard_name='model_level_number', attributes={'positive': 'up'}))
CoordAndDims(DimCoord(grib.pv[grib.scaledValueOfFirstFixedSurface], long_name='level_pressure', units='Pa'))
CoordAndDims(AuxCoord(grib.pv[grib.numberOfCoordinatesValues/2 + grib.scaledValueOfFirstFixedSurface], long_name='sigma'))
Factory(HybridPressureFactory, [{'long_name': 'level_pressure'}, {'long_name': 'sigma'}, Reference('surface_pressure')])



Expand Down
2 changes: 1 addition & 1 deletion lib/iris/etc/pp_cross_reference_rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
IF
f.lbuser[3] == 33
THEN
Reference('orography',)
ReferenceTarget('orography', None)
71 changes: 55 additions & 16 deletions lib/iris/fileformats/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 658-659:

raise _ReferenceError("The file(s) {{filenames}} don't contain"
                      " field(s) for {!r}.".format(arg.name))

Not very helpful to the user, seeing a literal {filenames} doesn't clarify the issue.

Copy link
Member Author

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.

Expand Down Expand Up @@ -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):
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One letter difference between concrete_reference_target and concrete_reference_targets.

Rename concrete_reference_target to target ? or something else perhaps ...

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)
Expand All @@ -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
Expand Down