def _group_result(self, fields): # Run the testee, but returning just the groups (not FieldCollations). with mock.patch( 'iris.fileformats.um._fast_load_structured_fields.' 'FieldCollation', new=lambda args: args): result = list(group_structured_fields(fields)) return result
def _basic_load_function(filename, pp_filter=None, **kwargs): # The low-level 'fields from filename' loader. # # This is the 'loader.generator' in the control structure passed to the # generic rules code, :meth:`iris.fileformats.rules.load_cubes`. # # As called by generic rules code, this replaces pp.load (and the like). # It yields a sequence of "fields", but in this case the 'fields' are # :class:`iris.fileformats.um._fast_load_structured_fields.FieldCollation` # objects. # # NOTE: so, a FieldCollation is passed as the 'field' in user callbacks. # # Also in our case, we need to support the basic single-field filtering # operation that speeds up phenomenon selection. # Therefore, the actual loader will pass this as the 'pp_filter' keyword, # when it is present. # Additional load keywords are 'passed on' to the lower-level function. # Helper function to select the correct fields loader call. def _select_raw_fields_loader(fname): # Return the PPfield loading function for a file name. # # This decides whether the underlying file is an FF or PP file. # Because it would be too awkward to modify the whole iris loading # callchain to "pass down" the file format, this function instead # 'recreates' that information by calling the format picker again. # NOTE: this may be inefficient, especially for web resources. from iris.fileformats import FORMAT_AGENT from iris.fileformats.pp import load as pp_load from iris.fileformats.um import um_to_pp with open(fname, "rb") as fh: spec = FORMAT_AGENT.get_spec(os.path.basename(fname), fh) if spec.name.startswith(_FF_SPEC_NAME): loader = um_to_pp elif spec.name.startswith(_PP_SPEC_NAME): loader = pp_load else: emsg = "Require {!r} to be a structured FieldsFile or a PP file." raise ValueError(emsg.format(fname)) return loader loader = _select_raw_fields_loader(filename) def iter_fields_decorated_with_load_indices(fields_iter): for i_field, field in enumerate(fields_iter): field._index_in_structured_load_file = i_field yield field fields = iter_fields_decorated_with_load_indices( field for field in loader(filename, **kwargs) if pp_filter is None or pp_filter(field)) return group_structured_fields(fields, collation_class=FieldCollation, filepath=filename)
def _basic_load_function(filename, pp_filter=None, **kwargs): # The low-level 'fields from filename' loader. # # This is the 'loader.generator' in the control structure passed to the # generic rules code, :meth:`iris.fileformats.rules.load_cubes`. # # As called by generic rules code, this replaces pp.load (and the like). # It yields a sequence of "fields", but in this case the 'fields' are # :class:`iris.fileformats.um._fast_load_structured_fields.FieldCollation` # objects. # # NOTE: so, a FieldCollation is passed as the 'field' in user callbacks. # # Also in our case, we need to support the basic single-field filtering # operation that speeds up phenomenon selection. # Therefore, the actual loader will pass this as the 'pp_filter' keyword, # when it is present. # Additional load keywords are 'passed on' to the lower-level function. # Helper function to select the correct fields loader call. def _select_raw_fields_loader(fname): # Return the PPfield loading function for a file name. # # This decides whether the underlying file is an FF or PP file. # Because it would be too awkward to modify the whole iris loading # callchain to "pass down" the file format, this function instead # 'recreates' that information by calling the format picker again. # NOTE: this may be inefficient, especially for web resources. from iris.fileformats import FORMAT_AGENT from iris.fileformats.pp import load as pp_load from iris.fileformats.um import um_to_pp with open(fname, 'rb') as fh: spec = FORMAT_AGENT.get_spec(os.path.basename(fname), fh) if spec.name.startswith(_FF_SPEC_NAME): loader = um_to_pp elif spec.name.startswith(_PP_SPEC_NAME): loader = pp_load else: emsg = 'Require {!r} to be a structured FieldsFile or a PP file.' raise ValueError(emsg.format(fname)) return loader loader = _select_raw_fields_loader(filename) def iter_fields_decorated_with_load_indices(fields_iter): for i_field, field in enumerate(fields_iter): field._index_in_structured_load_file = i_field yield field fields = iter_fields_decorated_with_load_indices( field for field in loader(filename, **kwargs) if pp_filter is None or pp_filter(field)) return group_structured_fields(fields, collation_class=FieldCollation, filepath=filename)
def _group_result(self, fields): # Run the testee, but returning just the groups (not FieldCollations). with mock.patch('iris.fileformats.um._fast_load_structured_fields.' 'FieldCollation', new=lambda args: args): result = list(group_structured_fields(fields)) return result
def _group_result(self, fields): # Run the testee, but returning just the groups (not FieldCollations). result = list(group_structured_fields(fields, collation_class=tuple)) return result
def _collations_from_filename(filename): fields = iter(um_to_pp(filename)) return group_structured_fields(fields)
def _collations_from_filename(filename): loader = _structured_loader(filename) fields = iter(loader(filename)) return group_structured_fields(fields)