def openFile(self, filename, weakreference=False): gc.collect() for item in self.rootItem: if item.file.filename == filename: ddict = {} ddict['event'] = "fileUpdated" ddict['filename'] = filename self.sigFileUpdated.emit(ddict) return item.file phynxFile = h5open(filename) if weakreference: def phynxFileInstanceDistroyed(weakrefObject): idx = self.rootItem._identifiers.index(id(weakrefObject)) child = self.rootItem._children[idx] child.clearChildren() del self._idMap[id(child)] self.rootItem.deleteChild(child) if not self.rootItem.hasChildren: self.clear() return refProxy = weakref.proxy(phynxFile, phynxFileInstanceDistroyed) self.rootItem.appendChild(refProxy) else: self.rootItem.appendChild(phynxFile) ddict = {} ddict['event'] = "fileAppended" ddict['filename'] = filename self.sigFileAppended.emit(ddict) return phynxFile
def getDatasetDialog(filename=None, value=False, message=None): # function kept for backward compatibility, in case someone # uses it with value=False outside PyMca5 if value: return getDatasetValueDialog(filename, message) hdf5Dialog = Hdf5SelectionDialog(None, filename, message, "dataset") ret = hdf5Dialog.exec_() if not ret: return None selectedHdf5Uri = hdf5Dialog.selectedItemUri hdf5File = h5open(filename) return hdf5File[selectedHdf5Uri.split("::")[-1]]
def getDatasetDialog(filename=None, value=False, message=None, parent=None): # function kept for backward compatibility, in case someone # uses it with value=False outside PyMca5 if value: return getDatasetValueDialog(filename=filename, message=message, parent=parent) hdf5Dialog = Hdf5SelectionDialog(parent, filename, message, "dataset") ret = hdf5Dialog.exec_() if not ret: return None selectedHdf5Uri = hdf5Dialog.selectedItemUri hdf5File = h5open(filename) return hdf5File[selectedHdf5Uri.split("::")[-1]]
def getDatasetValueDialog(filename=None, message=None, parent=None): """Open a dialog to select a dataset in a HDF5 file. Return the value of the dataset. If the dataset selection was cancelled, None is returned. :param str filename: HDF5 file path. If None, a file dialog is used to select the file. :param str message: Message used as window title for dialog :return: HDF5 dataset as numpy array, or None """ hdf5Dialog = Hdf5SelectionDialog(parent, filename, message, "dataset") ret = hdf5Dialog.exec_() if not ret: return None selectedHdf5Uri = hdf5Dialog.selectedItemUri with h5open(filename) as hdf5File: hdf5Item = hdf5File[selectedHdf5Uri.split("::")[-1]] data = hdf5Item[()] return data
def exec_(self): with h5open(self.filename) as hdf5File: self.fileModel.appendPhynxFile(hdf5File, weakreference=True) ret = qt.QDialog.exec_(self) return ret
def h5todict(h5file, path="/", exclude_names=None): """Read a HDF5 file and return a nested dictionary with the complete file structure and all data. Example of usage:: from silx.io.dictdump import h5todict # initialize dict with file header and scan header header94 = h5todict("oleg.dat", "/94.1/instrument/specfile") # add positioners subdict header94["positioners"] = h5todict("oleg.dat", "/94.1/instrument/positioners") # add scan data without mca data header94["detector data"] = h5todict("oleg.dat", "/94.1/measurement", exclude_names="mca_") .. note:: This function requires `h5py <http://www.h5py.org/>`_ to be installed. .. note:: If you write a dictionary to a HDF5 file with :func:`dicttoh5` and then read it back with :func:`h5todict`, data types are not preserved. All values are cast to numpy arrays before being written to file, and they are read back as numpy arrays (or scalars). In some cases, you may find that a list of heterogeneous data types is converted to a numpy array of strings. :param h5file: File name or :class:`h5py.File` object or spech5 file or fabioh5 file. :param str path: Name of HDF5 group to use as dictionary root level, to read only a sub-group in the file :param list[str] exclude_names: Groups and datasets whose name contains a string in this list will be ignored. Default is None (ignore nothing) :return: Nested dictionary """ if h5py_missing: raise h5py_import_error if not is_file(h5file): h5f = h5open(h5file) else: h5f = h5file ddict = {} for key in h5f[path]: if _name_contains_string_in_list(key, exclude_names): continue if is_group(h5f[path + "/" + key]): ddict[key] = h5todict(h5f, path + "/" + key, exclude_names=exclude_names) else: # Convert HDF5 dataset to numpy array ddict[key] = h5f[path + "/" + key][...] if not is_file(h5file): # close file, if we opened it h5f.close() return ddict
indices.sort() if len(indices): scanned = [measurement[key].name for idx, key in indices] return scanned if __name__ == "__main__": import sys import h5py try: sourcename=sys.argv[1] except: print("Usage: NexusTools <file> <key>") sys.exit() try: from silx.io import open as h5open h5 = h5open(sourcename) except: h5 = h5py.File(sourcename, 'r') entries = getNXClassGroups(h5, "/", ["NXentry", b"NXentry"], single=False) if not len(entries): entries = [item for name, item in h5["/"].items() if isGroup(item)] for entry in entries: print("Entry name = %s" % entry.name) if "title" in entry: print("Entry title = %s" % entry["title"][()]) measurement = getMeasurementGroup(h5, entry.name) if measurement is None: print("No measurement") else: print("Measurement name = %s " % measurement.name) instrument = getInstrumentGroup(h5, entry.name)