def load_files(filenames, callback, constraints=None): """ Takes a list of filenames which may also be globs, and optionally a constraint set and a callback function, and returns a generator of Cubes from the given files. .. note:: Typically, this function should not be called directly; instead, the intended interface for loading is :func:`iris.load`. """ from iris.fileformats import FORMAT_AGENT all_file_paths = expand_filespecs(filenames) # Create default dict mapping iris format handler to its associated filenames handler_map = collections.defaultdict(list) for fn in all_file_paths: with open(fn, "rb") as fh: handling_format_spec = FORMAT_AGENT.get_spec( os.path.basename(fn), fh) handler_map[handling_format_spec].append(fn) # Call each iris format handler with the approriate filenames for handling_format_spec in sorted(handler_map): fnames = handler_map[handling_format_spec] if handling_format_spec.constraint_aware_handler: for cube in handling_format_spec.handler(fnames, callback, constraints): yield cube else: for cube in handling_format_spec.handler(fnames, callback): yield cube
def _structured_loader(fname): with open(fname, 'rb') as fh: spec = FORMAT_AGENT.get_spec(os.path.basename(fname), fh) if spec.name.startswith(_FF_SPEC_NAME): result = um_to_pp elif spec.name.startswith(_PP_SPEC_NAME): result = pp_load else: emsg = 'Require {!r} to be a structured FieldsFile or a PP file.' raise ValueError(emsg.format(fname)) return result
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
def load_http(urls, callback): """ Takes a list of OPeNDAP URLs and a callback function, and returns a generator of Cubes from the given URLs. .. note:: Typically, this function should not be called directly; instead, the intended interface for loading is :func:`iris.load`. """ # Create default dict mapping iris format handler to its associated filenames from iris.fileformats import FORMAT_AGENT handler_map = collections.defaultdict(list) for url in urls: handling_format_spec = FORMAT_AGENT.get_spec(url, None) handler_map[handling_format_spec].append(url) # Call each iris format handler with the appropriate filenames for handling_format_spec in sorted(handler_map): fnames = handler_map[handling_format_spec] for cube in handling_format_spec.handler(fnames, callback): yield cube