def _convert_collation(collation): """ Converts a FieldCollation into the corresponding items of Cube metadata. Args: * collation: A FieldCollation object. Returns: A :class:`iris.fileformats.rules.ConversionMetadata` object. .. note: This is the 'loader.converter', in the control structure passed to the generic rules code, :meth:`iris.fileformats.rules.load_cubes`. """ from iris.fileformats.rules import ConversionMetadata from iris.fileformats.pp_load_rules import \ (_convert_time_coords, _convert_vertical_coords, _convert_scalar_realization_coords, _convert_scalar_pseudo_level_coords, _all_other_rules) # For all the scalar conversions, all fields in the collation will # give the same result, so the choice is arbitrary. field = collation.fields[0] # Call "all other" rules. (references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims) = _all_other_rules(field) # Adjust any dimension bindings to account for the extra leading # dimensions added by the collation. if collation.vector_dims_shape: def _adjust_dims(coords_and_dims, n_dims): def adjust(dims): if dims is not None: dims += n_dims return dims return [(coord, adjust(dims)) for coord, dims in coords_and_dims] n_collation_dims = len(collation.vector_dims_shape) dim_coords_and_dims = _adjust_dims(dim_coords_and_dims, n_collation_dims) aux_coords_and_dims = _adjust_dims(aux_coords_and_dims, n_collation_dims) # Dimensions to which we've already assigned dimension coordinates. dim_coord_dims = set() # Helper call to choose which coords are dimensions and which auxiliary. def _bind_coords(coords_and_dims, dim_coord_dims, dim_coords_and_dims, aux_coords_and_dims): def key_func(item): return _HINTS.get(item[0].name(), len(_HINTS)) # Target the first DimCoord for a dimension at dim_coords, # and target everything else at aux_coords. for coord, dims in sorted(coords_and_dims, key=key_func): if (isinstance(coord, DimCoord) and dims is not None and len(dims) == 1 and dims[0] not in dim_coord_dims): dim_coords_and_dims.append((coord, dims)) dim_coord_dims.add(dims[0]) else: aux_coords_and_dims.append((coord, dims)) # Call "time" rules. # # For "normal" (non-cross-sectional) time values. vector_headers = collation.element_arrays_and_dims # If the collation doesn't define a vector of values for a # particular header then it must be constant over all fields in the # collation. In which case it's safe to get the value from any field. t1, t1_dims = vector_headers.get('t1', (field.t1, ())) t2, t2_dims = vector_headers.get('t2', (field.t2, ())) lbft, lbft_dims = vector_headers.get('lbft', (field.lbft, ())) coords_and_dims = _convert_time_coords(field.lbcode, field.lbtim, field.time_unit('hours'), t1, t2, lbft, t1_dims, t2_dims, lbft_dims) # Bind resulting coordinates to dimensions, where suitable. _bind_coords(coords_and_dims, dim_coord_dims, dim_coords_and_dims, aux_coords_and_dims) # Call "vertical" rules. # # "Normal" (non-cross-sectional) vertical levels blev, blev_dims = vector_headers.get('blev', (field.blev, ())) lblev, lblev_dims = vector_headers.get('lblev', (field.lblev, ())) bhlev, bhlev_dims = vector_headers.get('bhlev', (field.bhlev, ())) bhrlev, bhrlev_dims = vector_headers.get('bhrlev', (field.bhrlev, ())) brsvd1, brsvd1_dims = vector_headers.get('brsvd1', (field.brsvd[0], ())) brsvd2, brsvd2_dims = vector_headers.get('brsvd2', (field.brsvd[1], ())) brlev, brlev_dims = vector_headers.get('brlev', (field.brlev, ())) # Find all the non-trivial dimension values dims = set( filter(None, [ blev_dims, lblev_dims, bhlev_dims, bhrlev_dims, brsvd1_dims, brsvd2_dims, brlev_dims ])) if len(dims) > 1: raise TranslationError('Unsupported multiple values for vertical ' 'dimension.') if dims: v_dims = dims.pop() if len(v_dims) > 1: raise TranslationError('Unsupported multi-dimension vertical ' 'headers.') else: v_dims = () coords_and_dims, factories = _convert_vertical_coords( field.lbcode, field.lbvc, blev, lblev, field.stash, bhlev, bhrlev, brsvd1, brsvd2, brlev, v_dims) # Bind resulting coordinates to dimensions, where suitable. _bind_coords(coords_and_dims, dim_coord_dims, dim_coords_and_dims, aux_coords_and_dims) # Realization (aka ensemble) (--> scalar coordinates) aux_coords_and_dims.extend( _convert_scalar_realization_coords(lbrsvd4=field.lbrsvd[3])) # Pseudo-level coordinate (--> scalar coordinates) aux_coords_and_dims.extend( _convert_scalar_pseudo_level_coords(lbuser5=field.lbuser[4])) return ConversionMetadata(factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims)
def test_missing_indicator(self): coords_and_dims = _convert_scalar_realization_coords(lbrsvd4=0) self.assertEqual(coords_and_dims, [])
def test_valid(self): coords_and_dims = _convert_scalar_realization_coords(lbrsvd4=21) self.assertEqual(coords_and_dims, [(DimCoord([21], standard_name='realization'), None)])
def _convert_collation(collation): """ Converts a FieldCollation into the corresponding items of Cube metadata. Args: * collation: A FieldCollation object. Returns: A :class:`iris.fileformats.rules.ConversionMetadata` object. .. note: This is the 'loader.converter', in the control structure passed to the generic rules code, :meth:`iris.fileformats.rules.load_cubes`. """ from iris.fileformats.rules import ConversionMetadata from iris.fileformats.pp_load_rules import \ (_convert_time_coords, _convert_vertical_coords, _convert_scalar_realization_coords, _convert_scalar_pseudo_level_coords, _all_other_rules) # For all the scalar conversions, all fields in the collation will # give the same result, so the choice is arbitrary. field = collation.fields[0] # Call "all other" rules. (references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims) = _all_other_rules(field) # Adjust any dimension bindings to account for the extra leading # dimensions added by the collation. if collation.vector_dims_shape: def _adjust_dims(coords_and_dims, n_dims): def adjust(dims): if dims is not None: dims += n_dims return dims return [(coord, adjust(dims)) for coord, dims in coords_and_dims] n_collation_dims = len(collation.vector_dims_shape) dim_coords_and_dims = _adjust_dims(dim_coords_and_dims, n_collation_dims) aux_coords_and_dims = _adjust_dims(aux_coords_and_dims, n_collation_dims) # Dimensions to which we've already assigned dimension coordinates. dim_coord_dims = set() # Helper call to choose which coords are dimensions and which auxiliary. def _bind_coords(coords_and_dims, dim_coord_dims, dim_coords_and_dims, aux_coords_and_dims): def key_func(item): return _HINTS.get(item[0].name(), len(_HINTS)) # Target the first DimCoord for a dimension at dim_coords, # and target everything else at aux_coords. for coord, dims in sorted(coords_and_dims, key=key_func): if (isinstance(coord, DimCoord) and dims is not None and len(dims) == 1 and dims[0] not in dim_coord_dims): dim_coords_and_dims.append((coord, dims)) dim_coord_dims.add(dims[0]) else: aux_coords_and_dims.append((coord, dims)) # Call "time" rules. # # For "normal" (non-cross-sectional) time values. vector_headers = collation.element_arrays_and_dims # If the collation doesn't define a vector of values for a # particular header then it must be constant over all fields in the # collation. In which case it's safe to get the value from any field. t1, t1_dims = vector_headers.get('t1', (field.t1, ())) t2, t2_dims = vector_headers.get('t2', (field.t2, ())) lbft, lbft_dims = vector_headers.get('lbft', (field.lbft, ())) coords_and_dims = _convert_time_coords(field.lbcode, field.lbtim, field.time_unit('hours'), t1, t2, lbft, t1_dims, t2_dims, lbft_dims) # Bind resulting coordinates to dimensions, where suitable. _bind_coords(coords_and_dims, dim_coord_dims, dim_coords_and_dims, aux_coords_and_dims) # Call "vertical" rules. # # "Normal" (non-cross-sectional) vertical levels blev, blev_dims = vector_headers.get('blev', (field.blev, ())) lblev, lblev_dims = vector_headers.get('lblev', (field.lblev, ())) bhlev, bhlev_dims = vector_headers.get('bhlev', (field.bhlev, ())) bhrlev, bhrlev_dims = vector_headers.get('bhrlev', (field.bhrlev, ())) brsvd1, brsvd1_dims = vector_headers.get('brsvd1', (field.brsvd[0], ())) brsvd2, brsvd2_dims = vector_headers.get('brsvd2', (field.brsvd[1], ())) brlev, brlev_dims = vector_headers.get('brlev', (field.brlev, ())) # Find all the non-trivial dimension values dims = set(filter(None, [blev_dims, lblev_dims, bhlev_dims, bhrlev_dims, brsvd1_dims, brsvd2_dims, brlev_dims])) if len(dims) > 1: raise TranslationError('Unsupported multiple values for vertical ' 'dimension.') if dims: v_dims = dims.pop() if len(v_dims) > 1: raise TranslationError('Unsupported multi-dimension vertical ' 'headers.') else: v_dims = () coords_and_dims, factories = _convert_vertical_coords(field.lbcode, field.lbvc, blev, lblev, field.stash, bhlev, bhrlev, brsvd1, brsvd2, brlev, v_dims) # Bind resulting coordinates to dimensions, where suitable. _bind_coords(coords_and_dims, dim_coord_dims, dim_coords_and_dims, aux_coords_and_dims) # Realization (aka ensemble) (--> scalar coordinates) aux_coords_and_dims.extend(_convert_scalar_realization_coords( lbrsvd4=field.lbrsvd[3])) # Pseudo-level coordinate (--> scalar coordinates) aux_coords_and_dims.extend(_convert_scalar_pseudo_level_coords( lbuser5=field.lbuser[4])) return ConversionMetadata(factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims)
def test_missing_indicator(self): coords_and_dims = _convert_scalar_realization_coords(lbrsvd4=0) self.assertEqual(coords_and_dims, [])
def test_valid(self): coords_and_dims = _convert_scalar_realization_coords(lbrsvd4=21) self.assertEqual(coords_and_dims, [(DimCoord([21], standard_name='realization'), None)])