def open(cls, obj, nids=None, **kwargs): """ Flexible constructor. obj can be a :class:`Flow` or a string with the directory containing the Flow. nids is an optional list of :class:`Node` identifiers used to filter the set of :class:`Task` in the Flow. """ has_dirpath = False if is_string(obj): try: obj = Flow.pickle_load(obj) except: has_dirpath = True if not has_dirpath: # We have a Flow. smeth is the name of the Task method used to open the file. items = [] smeth = "open_" + cls.EXT.lower() for task in obj.iflat_tasks(nids=nids): #, status=obj.S_OK): open_method = getattr(task, smeth, None) if open_method is None: continue ncfile = open_method() if ncfile is not None: items.append((task.pos_str, ncfile)) return cls(*items) else: # directory --> search for files with the appropriate extension and open it with abiopen. if nids is not None: raise ValueError("nids cannot be used when obj is a directory.") return cls.from_dir(obj)
def abiopen(filepath): """ Factory function that opens any file supported by abipy. File type is detected from the extension Args: filepath: string with the filename. """ if os.path.basename(filepath) == "__AbinitFlow__.pickle": return Flow.pickle_load(filepath) cls = abifile_subclass_from_filename(filepath) return cls.from_file(filepath)
def abiopen(filepath): """ Factory function that opens any file supported by abipy. File type is detected from the extension Args: filepath: string with the filename. """ if os.path.basename(filepath) == "__AbinitFlow__.pickle": return Flow.pickle_load(filepath) # Handle old output files produced by Abinit. import re outnum = re.compile(".+\.out[\d]+") abonum = re.compile(".+\.abo[\d]+") if outnum.match(filepath) or abonum.match(filepath): return AbinitOutputFile.from_file(filepath) cls = abifile_subclass_from_filename(filepath) return cls.from_file(filepath)
def open(cls, obj, nids=None, **kwargs): """ Flexible constructor. obj can be a :class:`Flow` or a string with the directory containing the Flow. nids is an optional list of :class:`Node` identifiers used to filter the set of :class:`Task` in the Flow. """ has_dirpath = False if is_string(obj): try: obj = Flow.pickle_load(obj) except: has_dirpath = True items = [] if not has_dirpath: # We have a Flow. smeth is the name of the Task method used to open the file. smeth = "open_" + cls.EXT.lower() for task in obj.iflat_tasks(nids=nids): #, status=obj.S_OK): open_method = getattr(task, smeth, None) if open_method is None: continue ncfile = open_method() if ncfile is not None: items.append((task.pos_str, ncfile)) else: # directory --> search for files with the appropriate extension and open it with abiopen. if nids is not None: raise ValueError("nids cannot be used when obj is a directory.") from abipy.abilab import abiopen for dirpath, dirnames, filenames in os.walk(obj): filenames = [f for f in filenames if f.endswith(cls.EXT + ".nc") or f.endswith(cls.EXT)] for f in filenames: ncfile = abiopen(os.path.join(dirpath, f)) if ncfile is not None: items.append((ncfile.filepath, ncfile)) new = cls(*items) # Save a reference to the initial object so that we can reload it if needed #new._initial_object = obj return new