def loadFOCUS(source) :
    f = nxs.open(source,'r')

    f.openpath("entry1/FOCUS/merged/counts")
    dim,datatype= f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    f = nxs.open(source,'r')
    f.openpath("entry1/FOCUS/merged/time_binning")
    tof = f.getdata()
    f.close()

    nEvents = np.sum(data)
    signal = np.empty(nEvents,dtype=neventarray.event_t)

    nEv = 0
    detID = 0
    for row in range(dim[0]):
        for it in range(dim[1]):
            events = data[row][it]
            if events > 0:
                signal[nEv:nEv+events]["x"] = row
                signal[nEv:nEv+events]["y"] = it
                nEv = nEv+events

    if not signal.shape[0] == nEv:
        raise Exception("Error in reading NeXus data: wrong number of events",nEv)

    return signal
def loadAMOR(source) :
    f = nxs.open(source,'r')

    f.openpath("entry1/AMOR/area_detector/data")
    dim,datatype= f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    f = nxs.open(source,'r')
    f.openpath("entry1/AMOR/area_detector/time_binning")
    tof = f.getdata()
    f.close()

    nEvents = np.sum(data)
    signal = np.empty(nEvents,dtype=neventarray.event_t)

    nEv = 0
    detID = 0
    for row in range(dim[0]):
        for col in range(dim[1]):
            for it in range(dim[2]):
                events = data[row][col][it]
                if events > 0:
                    signal[nEv:nEv+events]["ts"] = round(tof[it]/10.)
                    signal[nEv:nEv+events]["x"] = row
                    signal[nEv:nEv+events]["y"] = col
                    nEv = nEv+events

    if not signal.shape[0] == nEv:
        raise Exception("Error in reading NeXus data: wrong number of events",nEv)

    return signal
Example #3
0
def loadFOCUS(source):
    f = nxs.open(source, 'r')

    f.openpath("entry1/FOCUS/merged/counts")
    dim, datatype = f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    f = nxs.open(source, 'r')
    f.openpath("entry1/FOCUS/merged/time_binning")
    tof = f.getdata()
    f.close()

    nEvents = np.sum(data)
    signal = np.empty(nEvents, dtype=neventarray.event_t)

    nEv = 0
    detID = 0
    for row in range(dim[0]):
        for it in range(dim[1]):
            events = data[row][it]
            if events > 0:
                signal[nEv:nEv + events]["x"] = row
                signal[nEv:nEv + events]["y"] = it
                nEv = nEv + events

    if not signal.shape[0] == nEv:
        raise Exception("Error in reading NeXus data: wrong number of events",
                        nEv)

    return signal
Example #4
0
def loadAMOR(source):
    f = nxs.open(source, 'r')

    f.openpath("entry1/AMOR/area_detector/data")
    dim, datatype = f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    f = nxs.open(source, 'r')
    f.openpath("entry1/AMOR/area_detector/time_binning")
    tof = f.getdata()
    f.close()

    nEvents = np.sum(data)
    signal = np.empty(nEvents, dtype=neventarray.event_t)

    nEv = 0
    detID = 0
    for row in range(dim[0]):
        for col in range(dim[1]):
            for it in range(dim[2]):
                events = data[row][col][it]
                if events > 0:
                    signal[nEv:nEv + events]["ts"] = round(tof[it] / 10.)
                    signal[nEv:nEv + events]["x"] = row
                    signal[nEv:nEv + events]["y"] = col
                    nEv = nEv + events

    if not signal.shape[0] == nEv:
        raise Exception("Error in reading NeXus data: wrong number of events",
                        nEv)

    return signal
Example #5
0
 def open(self, **kwds):
     if not self.isopen():
         if self._mode == 'rw':
             self._file = nxs.open(self._filename, 'w5')
         else:
             self._file = nxs.open(self._filename, 'w5')
         self.nxpath = '/'
     return self
 def isValid(self):
     try :
         fp = nxs.open(self.filepath,'r')
         fp.close()
     except Exception:
         return False
     return True
Example #7
0
def check_external(filename,mode):
    ext = dict(w5='.h5',w4='.hdf',wx='.xml')[mode]
    file = nxs.open(filename,'rw')
    
    file.openpath('/entry1/start_time')
    time = file.getdata()
    
    get = file.inquirefile()
    expected = 'nxfile://data/dmc01'+ext
    if expected != get: fail("first external file returned %s"%(get))
    
    file.openpath('/entry2/sample/sample_name')
    sample = file.getdata()

    get = file.inquirefile()
    expected = 'nxfile://data/dmc02'+ext
    if expected != get: fail("second external file returned %s"%(get))

    file.openpath('/')
    remote = file.isexternalgroup('entry1','NXentry')
    if remote is None:
        fail("failed to identify /entry1 as external")
    remote = file.isexternalgroup('entry3','NXentry')
    if remote is not None: 
        fail('incorrectly identified /entry3 as external')
    
    file.close()
Example #8
0
    def __init__(self, nexusname):
        self.logger = logging
        self.detdict = {}
        self.det2fileno = {"SAXS": 1, "CALIB": 2, "WAXS": 3, "TIMES": 4}

        self.logger.info("looking at " + nexusname + " " +
                         datetime.datetime.now().__str__())

        self.nexusname = nexusname
        self.nexusfile = nxs.open(self.nexusname)

        self.instrumentname = "DLS"
        self.inspectfile()

        self.iscommissioningvisit = False
        self.isindlsdata = False

        components = self.nexusname.split("/")
        if len(components) > 6:
            if "dls" == components[1]:
                if "data" == components[3]:
                    try:
                        int(components[4])
                        # is int
                        self.isindlsdata = True
                        self.logger.debug("is in dls data")
                        if components[5].startswith("cm"):
                            self.iscommissioningvisit = True
                            self.logger.debug("is commissioning")
                    except:
                        #not in year dir
                        self.logger.debug("no int year")
                        pass
Example #9
0
 def isValid(self):
     try:
         fp = nxs.open(self.filepath, 'r')
         fp.close()
     except Exception:
         return False
     return True
Example #10
0
def loadRITA2(source):

    f = nxs.open(source, 'r')

    f.openpath("entry1/RITA-2/detector/counts")
    dim, datatype = f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    np.random.seed(1234)
    timestamp = np.sort(np.random.randint(2**32 - 1, size=dim[0]))

    nEvents = np.sum(data)
    signal = np.empty(nEvents, dtype=neventarray.event_t)

    nEv = 0
    detID = 0

    row = np.uint16
    col = np.uint16

    for row in range(dim[1]):
        for col in range(dim[2]):
            for it in range(dim[0]):
                events = data[it][row][col]
                if events > 0:
                    signal[nEv:nEv + events]["ts"] = timestamp[it]
                    signal[nEv:nEv + events]["sync"] = 0
                    signal[nEv:nEv + events]["x"] = row
                    signal[nEv:nEv + events]["y"] = col
                    nEv = nEv + events

    return signal
Example #11
0
def check_external(filename,mode):
    ext = dict(w5='.h5',w4='.hdf',wx='.xml')[mode]
    file = nxs.open(filename,'rw')
    
    file.openpath('/entry1/start_time')
    time = file.getdata()
    
    get = file.inquirefile()
    expected = 'nxfile://data/dmc01'+ext
    if expected != get: fail("first external file returned %s"%(get))
    
    file.openpath('/entry2/sample/sample_name')
    sample = file.getdata()

    get = file.inquirefile()
    expected = 'nxfile://data/dmc02'+ext
    if expected != get: fail("second external file returned %s"%(get))

    file.openpath('/')
    remote = file.isexternalgroup('entry1','NXentry')
    if remote is None:
        fail("failed to identify /entry1 as external")
    remote = file.isexternalgroup('entry3','NXentry')
    if remote is not None: 
        fail('incorrectly identified /entry3 as external')
    
    file.close()
Example #12
0
    def __init__(self, filename, verbosity, elements_list):
        self.filename = filename
        self.verbosity = verbosity
        self.elements_list = elements_list
        
        self.warnings_set = set()
        self.basename = os.path.basename(filename)
        self.nexusfile = nxs.open(self.filename)
        self.metadata = {}
        
        root_entries = self.nexusfile.getentries()
        if len(root_entries) != 1:
            warning = "unexpected extra root entries, reading first one only:" \
                "\n  %r" % root_entries
            print >>sys.stderr, "%s: Warning, %s" % (self.filename, warning)
            self.warnings_set.add(warning)
        
        root_group = root_entries.keys()[0]
        if root_group != "entry1":
            warning = "first root entry is not entry1, using: %s" % root_group
            print >>sys.stderr, "%s: Warning, %s" % (self.filename, warning)
            self.warnings_set.add(warning)

        self.nexusfile.opengroup(root_group)
        self.init_root()
Example #13
0
def populate_external(filename,mode):
    ext = dict(w5='.h5',w4='.hdf',wx='.xml')[mode]
    file = nxs.open(filename,mode)
    file.makegroup('entry1','NXentry')
    file.linkexternal('entry1','NXentry','nxfile://data/dmc01'+ext)
    file.makegroup('entry2','NXentry')
    file.linkexternal('entry2','NXentry','nxfile://data/dmc02'+ext)
    file.makegroup('entry3','NXentry')
    file.close()
Example #14
0
def populate_external(filename,mode):
    ext = dict(w5='.h5',w4='.hdf',wx='.xml')[mode]
    file = nxs.open(filename,mode)
    file.makegroup('entry1','NXentry')
    file.linkexternal('entry1','NXentry','nxfile://data/dmc01'+ext)
    file.makegroup('entry2','NXentry')
    file.linkexternal('entry2','NXentry','nxfile://data/dmc02'+ext)
    file.makegroup('entry3','NXentry')
    file.close()
Example #15
0
 def openFile(self):
     logger.debug("Opening nexus file...")
     if self.file is None:
         try:
             self.file = nxs.open(self.tempFile.name,'r')
         except  Exception:
             logger.info("Problems opening the nexus file...It was either deleted or is not valid!")
             raise
     else:
         logger.warning("Nexus file appears to be open already.")
Example #16
0
    def __init__(self, resource, *args, **kwargs):
        """
        Object constructor

        @param resource: The handle to the input NeXus geometry file
        @type resource: C{file}

        @param args: Argument objects that the class accepts (UNUSED)

        @param kwargs: A list of keyword arguments that the class accepts:
        """
        self.__file = nxs.open(resource, nxs.napi.ACC_CREATE5)
Example #17
0
    def readData(self):
        f = nxs.open(self.filepath)
        f.opengroup('MDHistoWorkspace')

        f.opendata('signal')
        self.data = f.getdata()
        f.closedata()

        self.qs = self.read_axes(f)

        f.closegroup()
        f.close()
Example #18
0
    def __init__(self, nexusname):
        self.logger = logging
        self.detdict = {}
        self.det2fileno = {"SAXS": 1, "CALIB": 2, "WAXS": 3, "TIMES": 4}

        self.logger.info("looking at " + nexusname + " " + datetime.datetime.now().__str__())

        self.nexusname = nexusname
        self.nexusfile = nxs.open(self.nexusname)

        self.instrumentname = "DLS"
        self.inspectfile()
Example #19
0
 def __init__(self, name, mode='r', **kwds):
     """Create a Nexus File object for reading and writing."""
     self._file = None
     name = os.path.abspath(name)
     self.name = name
     if mode == 'w4' or mode == 'wx':
         raise NeXusError('Only HDF5 files supported')
     elif mode == 'w' or mode == 'w-' or mode == 'w5':
         if mode == 'w5':
             mode = 'w'
         self._mode = 'rw'
         self._file = nxs.open(name, 'w5')
     else:
         if mode == 'rw' or mode == 'r+':
             self._mode = 'rw'
             mode = 'r+'
         else:
             self._mode = 'r'
         self._file = nxs.open(name, 'w5')
     self._filename = self._file.filename
     self._path = '/'
    def readData(self):
        f = nxs.open(self.filepath)
        f.opengroup('MDHistoWorkspace')

        f.opendata('signal')
        self.data = f.getdata()
        f.closedata()

        self.qs = self.read_axes(f)

        f.closegroup()
        f.close()
Example #21
0
    def __init__(self, resource, *args, **kwargs):
        """
        Object constructor

        @param resource: The handle to the input NeXus geometry file
        @type resource: C{file}

        @param args: Argument objects that the class accepts (UNUSED)

        @param kwargs: A list of keyword arguments that the class accepts:
        """
        self.__file = nxs.open(resource, nxs.napi.ACC_CREATE5)
Example #22
0
def readData(filePath='/home/leal/Documents/Mantid/IN5/094460.nxs'):
    f = nxs.open(filePath)
    f.opengroup('entry0')
    f.opengroup('data')
    f.opendata('data')
    a = f.getdata()
    f.closedata()
    f.closegroup()
    f.closegroup()
    f.close()
    # a.shape
    # Out[15]: (384, 256, 512)
    return a
Example #23
0
def readData(filePath='/home/leal/Documents/Mantid/D33/Didier TOF/001030.nxs'):
    f = nxs.open(filePath)
    f.opengroup('entry0')
    f.opengroup('data1')
    f.opendata('data1')
    a = f.getdata()
    f.closedata()
    f.closegroup()
    f.closegroup()
    f.close()
    # a.shape
    # Out[15]: (384, 256, 512)
    return a
Example #24
0
def readData(filePath):
    f = nxs.open(filePath)
    f.opengroup('entry0')
    f.opengroup('data')
    f.opendata('multiSD1_data')
    a = f.getdata()
    f.closedata()
    f.closegroup()
    f.closegroup()
    f.close()
    # a.shape
    # Out[15]: (384, 256, 512)
    return a
Example #25
0
    def _addCustomData(self, value, name, nxpath=None, dtype=None, **kwargs):
        '''
        apart from value and name, this recorder can use the following optional parameters:

        :param nxpath: (str) a nexus path (optionally using name:nxclass notation for
                       the group names). See the rules for automatic nxclass
                       resolution used by
                       :meth:`NXscan_FileRecorder._createBranch`.
                       If None given, it defaults to
                       nxpath='custom_data:NXcollection'

        :param dtype: name of data type (it is inferred from value if not given)

        '''
        if nxpath is None:
            nxpath = 'custom_data:NXcollection'
        if dtype is None:
            if numpy.isscalar(value):
                dtype = numpy.dtype(type(value)).name
                if numpy.issubdtype(dtype, str):
                    dtype = 'char'
                if dtype == 'bool':
                    value, dtype = int(value), 'int8'
            else:
                value = numpy.array(value)
                dtype = value.dtype.name

        if dtype not in self.supported_dtypes and dtype != 'char':
            self.warning("cannot write '%s'. Reason: unsupported data type",
                         name)
            return
        # open the file if necessary
        fileWasClosed = self.fd is None or not self.fd.isopen
        if fileWasClosed:
            if not self.overwrite and os.path.exists(self.filename):
                nxfilemode = 'rw'
            import nxs
            self.fd = nxs.open(self.filename, nxfilemode)
        # write the data
        self._createBranch(nxpath)
        try:
            self._writeData(name, value, dtype)
        except ValueError as e:
            msg = "Error writing %s. Reason: %s" % (name, str(e))
            self.warning(msg)
            self.macro.warning(msg)
        # leave the file as it was
        if fileWasClosed:
            self.fd.close()
Example #26
0
 def fillRunData(self):
     #open nexus file
     file = nxs.open(self._infilename, 'r')
     for node in self._nodes:
         try:
             file.openpath(node)
             value =  file.getdata()
             if isinstance(value, numpy.ndarray):
                 value = sum(value)
         except Exception as e:
             print e 
             value = 'N/A' 
             
         self._values.append(str(value))
     file.close()
Example #27
0
    def __init__(self, normalizedfile, alignfile):

        self.normalized_nexusfile = nxs.open(normalizedfile, 'r')
        self.aligned_nexusfile = nxs.nxload(alignfile, 'rw')
        self.util_obj = Utils()

        try:
            vectors = self.aligned_nexusfile['FastAligned']['move_vectors']
            self.move_vectors = vectors
        except Exception: 
            print("\nMove vectors could NOT be found.\n")
            raise
   
        self.nFrames = 0
        self.numrows = 0
        self.numcols = 0
Example #28
0
    def _addCustomData(self, value, name, nxpath=None, dtype=None, **kwargs):
        '''
        apart from value and name, this recorder can use the following optional parameters:

        :param nxpath: (str) a nexus path (optionally using name:nxclass notation for
                       the group names). See the rules for automatic nxclass
                       resolution used by
                       :meth:`NXscan_FileRecorder._createBranch`.
                       If None given, it defaults to
                       nxpath='custom_data:NXcollection'

        :param dtype: name of data type (it is inferred from value if not given)

        '''
        if nxpath is None:
            nxpath = 'custom_data:NXcollection'
        if dtype is None:
            if numpy.isscalar(value):
                dtype = numpy.dtype(type(value)).name
                if numpy.issubdtype(dtype, str):
                    dtype = 'char'
                if dtype == 'bool':
                    value, dtype = int(value), 'int8'
            else:
                value = numpy.array(value)
                dtype = value.dtype.name

        if dtype not in self.supported_dtypes and dtype != 'char':
            self.warning(
                "cannot write '%s'. Reason: unsupported data type", name)
            return
        # open the file if necessary
        fileWasClosed = self.fd is None or not self.fd.isopen
        if fileWasClosed:
            if not self.overwrite and os.path.exists(self.filename):
                nxfilemode = 'rw'
            import nxs
            self.fd = nxs.open(self.filename, nxfilemode)
        # write the data
        self._createBranch(nxpath)
        try:
            self._writeData(name, value, dtype)
        except ValueError, e:
            msg = "Error writing %s. Reason: %s" % (name, str(e))
            self.warning(msg)
            self.macro.warning(msg)
Example #29
0
def show(filename):

    print filename,
    f = nxs.open(filename)
    # /entry0
    f.opengroup('entry0')

    # /entry0/start_time
    f.opendata('start_time')
    print ';', f.getdata(),
    # /entry0
    f.closedata()
    
    # /entry0/title
    f.opendata('title')
    print ';', f.getdata(),
    # /entry0
    f.closedata()
    
    # /entry0/sample
    f.opengroup('sample')
    # /entry0/sample/chemical_formula
    f.opendata('chemical_formula')
    print '; Chem:', f.getdata(),
    # /entry0/sample
    f.closedata()
    # /entry0
    f.closegroup()
    
    # /entry0/walength
    f.opendata('wavelength')
    print ';', f.getdata(), 'A',
    # /entry0
    f.closedata()
    
    # /entry0/experiment_identifier
    f.opendata('experiment_identifier')
    print '; Id:', f.getdata(),
    # /entry0
    f.closedata() 
    
    # /
    f.closegroup()
    f.close()
    
    print
Example #30
0
def export_nxspe2phx(filename):

    file = nxs.open(filename, 'r')
    entries = file.getentries()
    if len(entries) == 0:
        raise IOError(
            " Can not find nexus entries in the file: {0}".format(filename))
    nxspe_data = entries.keys()
    if len(nxspe_data) > 1:
        raise IOError(
            " Currently do not support more then one nexus enrty in the file : {0}"
            .format(filename))

    file.opengroup(nxspe_data[0])
    file.opengroup('data')
    phx_par = {
        "polar": [],
        "polar_width": [],
        "azimuthal": [],
        "azimuthal_width": []
    }

    for key in phx_par:
        file.opendata(key)
        phx_par[key] = file.getdata()
        file.closedata()

    file.closegroup()
    file.closegroup()
    file.close()

    out_file = os.path.splitext(filename)[0] + '.phx'
    if os.path.exists(out_file):
        os.unlink(out_file)

    ft = open(out_file, 'w')
    numdet = len(phx_par["polar"])
    ft.write("{0}\n".format(numdet))
    for i in xrange(0, numdet):
        ft.write(
            "1\t0\t{0: 5.2f}\t{1: 5.2f}\t{2: 5.2f}\t{3: 5.2f}\t0\n".format(
                phx_par['polar'][i], phx_par['azimuthal'][i],
                phx_par['polar_width'][i], phx_par['azimuthal_width'][i]))
    ft.close()

    pass
Example #31
0
def leak_test1(n = 1000, mode='w5'):
#    import gc
#    gc.enable()
#    gc.set_debug(gc.DEBUG_LEAK)
    filename = "leak_test1.nxs"
    try: os.unlink(filename)
    except OSError: pass
    file = nxs.open(filename,mode)
    file.close()
    print "File should exist now"
    for i in range(n):
        if i%100 == 0: 
            print "loop count %d"%i
            memfootprint()
        file.open()
        file.close()
#        gc.collect()
    os.unlink(filename)
def show(filename):

    print filename,
    f = nxs.open(filename)
    # /entry0
    f.opengroup('entry0')

    # /entry0/title
    f.opendata('title')
    print ' : ', f.getdata(),
    # /entry0
    f.closedata()

    # /
    f.closegroup()
    f.close()

    print
Example #33
0
def leak_test1(n = 1000, mode='w5'):
#    import gc
#    gc.enable()
#    gc.set_debug(gc.DEBUG_LEAK)
    filename = "leak_test1.nxs"
    try: os.unlink(filename)
    except OSError: pass
    file = nxs.open(filename,mode)
    file.close()
    print("File should exist now")
    for i in range(n):
        if i%100 == 0: 
            print("loop count %d"%i)
            memfootprint()
        file.open()
        file.close()
#        gc.collect()
    os.unlink(filename)
def show(filename):

    print filename,
    f = nxs.open(filename)
    
    # /entry0
    f.opengroup('entry0')

    # /entry0/start_time
    f.opendata('start_time')
    print ';', f.getdata(),
    # /entry0
    f.closedata()
    
    # /entry0/mode
    f.opendata('mode')
    print '; Mode=', f.getdata(),
    # /entry0
    f.closedata()
    
    # /entry0/D33
    f.opengroup('D22')
    # /entry0/D33/selector
    f.opengroup('selector')
    # /entry0/D33/selector/wavelength 
    f.opendata('wavelength')
    print '; wavelength=', f.getdata(),
    f.closedata()
    f.closegroup()
    f.closegroup()
    
    # /entry0/sample_description
    f.opendata('sample_description')
    print ':', f.getdata(),
    # /entry0
    f.closedata()
    
    
        
    # /
    f.closegroup()
    f.close()
    
    print
Example #35
0
 def showSnapShot(self,filename):
     """
     It prints the snapshot in the Lamp nexus file
     There's no really use for these...
     
     @param filename:
     """
     f = nxs.open(filename)
     f.opengroup('entry1')
     f.opengroup('data1')
     f.opendata('SNAPSHOT')
     a = f.getdata()
     f.closedata()
     f.closegroup()
     f.closegroup()
     f.close()
     import matplotlib.pyplot as plt
     plt.imshow(a)
     plt.show()
Example #36
0
def show(filename):

    print filename,
    f = nxs.open(filename)
    # /entry0
    f.opengroup('entry0')

   
    # /entry0/title
    f.opendata('title')
    print ' : ', f.getdata(),
    # /entry0
    f.closedata()
        
    # /
    f.closegroup()
    f.close()
    
    print
Example #37
0
    def __init__(self, inputfile, avgtomnorm):

        self.avgtomnorm = avgtomnorm
        self.filename_nexus = inputfile        
        self.input_nexusfile=nxs.open(self.filename_nexus, 'r')

        self.tomonorm = nxs.NXentry(name= "TomoNormalized")
               
        self.nxdata = nxs.NXdata(name = 'ratios')
        self.tomonorm.insert(self.nxdata) 
        
        self.outputfilehdf5 = inputfile.split('.')[0]+'_norm'+'.hdf5'
        self.tomonorm.save(self.outputfilehdf5, 'w5')

        # Ratios for the exposure times
        self.ratios_exptimes = list()
        
        # Ratios for the currents.
        self.ratios_currents_tomo = list()
        self.ratios_currents_brightfield = list()  
           
        # Note: brightfield= FlatField= FF   
        self.exposuretimes_brightfield = 0
        self.averagebrightfield_exptimes = 0
        self.currents_brightfield = 0
        self.data_brightfield = 0
        self.nFramesFF = 0
        self.numrowsFF = 0
        self.numcolsFF = 0

        self.exposuretimes_tomo = 0
        self.currents_tomo = 0
        self.data_tomo = 0
        self.nFramesSample = 0                
        self.numrows = 0
        self.numcols = 0
        
        self.averagebright = 0
        self.normalizedtomo_singleimage = 0
        self.normalizedTomoStack = 0

        self.boolean_current_exists = 0
        return
def show(filename):

    print filename,
    f = nxs.open(filename)

    # /entry0
    f.opengroup('entry0')

    # /entry0/start_time
    f.opendata('start_time')
    print ';', f.getdata(),
    # /entry0
    f.closedata()

    # /entry0/mode
    f.opendata('mode')
    print '; Mode=', f.getdata(),
    # /entry0
    f.closedata()

    # /entry0/D33
    f.opengroup('D33')
    # /entry0/D33/selector
    f.opengroup('selector')
    # /entry0/D33/selector/wavelength
    f.opendata('wavelength')
    print '; wavelength=', f.getdata(),
    f.closedata()
    f.closegroup()
    f.closegroup()

    # /entry0/sample_description
    f.opendata('sample_description')
    print ':', f.getdata(),
    # /entry0
    f.closedata()

    # /
    f.closegroup()
    f.close()

    print
Example #39
0
	def __init__(self,filename) :
		print "Initialising Extractor"
		self.filename = filename
		self.nexusfile = nxs.open(self.filename)

		# initialy open the file
		# ok navigate to the correct place in the structure
		entries = self.nexusfile.getentries()
		if len(entries) != 1:
			self.logger.warning("unexpected number of root entries, reading first one only")
		
		# open entry1
		self.nexusfile.opengroup(entries.keys()[0])
		entries = self.nexusfile.getentries()

		if not "instrument" in entries:
			raise RuntimeError("No instrument definition missing")

		# open the instrument data
		self.nexusfile.opengroup("instrument")
def loadRITA2(source):

    f = nxs.open(source, 'r')

    f.openpath("entry1/RITA-2/detector/counts")
    dim, datatype = f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    np.random.seed(1234)
    timestamp = np.sort(np.random.randint(2**32 - 1, size=dim[0]))

    nEvents = np.sum(data)

    signal = np.empty(nEvents, dtype=neventarray.event_t)

    nEv = 0
    detID = 0

    row = np.uint16
    col = np.uint16

    m = np.sum(data, 0)
    np.savetxt('orig.out', m, delimiter=' ')

    for row in range(dim[1]):
        for col in range(dim[2]):
            for it in range(dim[0]):
                events = data[it][row][col]
                if events > 0:
                    signal[nEv:nEv + events]["ts"] = timestamp[it]
                    signal[nEv:nEv +
                           events]["data"] = (1 << 31 | 1 << 30 | 1 << 29
                                              | 1 << 28 | 4 << 24 | col << 12
                                              | row)
                    nEv = nEv + events

    print signal[0:9]
    return signal
def loadRITA2(source) :

    f = nxs.open(source,'r')

    f.openpath("entry1/RITA-2/detector/counts")
    dim,datatype= f.getinfo()
    size = np.prod(dim)
    data = f.getdata()
    f.close()

    np.random.seed(1234)
    timestamp = np.sort(np.random.randint(2**32-1,size=dim[0]))

    nEvents = np.sum(data)

    signal = np.empty(nEvents,dtype=neventarray.event_t)

    nEv = 0
    detID = 0

    row = np.uint16
    col = np.uint16

    m = np.sum(data,0)
    np.savetxt('orig.out',m,delimiter=' ')

    for row in range(dim[1]):
        for col in range(dim[2]):
            for it in range(dim[0]):
                events = data[it][row][col]
                if events > 0:
                    signal[nEv:nEv+events]["ts"] = timestamp[it]
                    signal[nEv:nEv+events]["data"] = (1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 | 4 << 24 | col << 12 | row ) 
                    nEv = nEv+events

    print signal[0:9]
    return signal
Example #42
0
def export_nxspe2phx(filename):

    file = nxs.open(filename,'r');
    entries = file.getentries()
    if len(entries) == 0:
        raise IOError(" Can not find nexus entries in the file: {0}".format(filename));
    nxspe_data = entries.keys();
    if len(nxspe_data)> 1:
        raise IOError(" Currently do not support more then one nexus enrty in the file : {0}".format(filename));

        
    file.opengroup(nxspe_data[0])
    file.opengroup('data')
    phx_par = {"polar":[],"polar_width":[],"azimuthal":[],"azimuthal_width":[]};

    for key in phx_par:
        file.opendata(key)
        phx_par[key]=file.getdata();
        file.closedata();

    file.closegroup();
    file.closegroup();
    file.close();

    out_file = os.path.splitext(filename)[0]+'.phx'
    if os.path.exists(out_file) :
       os.unlink(out_file);

    ft = open(out_file,'w');
    numdet = len(phx_par["polar"]);
    ft.write("{0}\n".format(numdet))
    for i in xrange(0,numdet):
        ft.write("1\t0\t{0: 5.2f}\t{1: 5.2f}\t{2: 5.2f}\t{3: 5.2f}\t0\n".format(phx_par['polar'][i],phx_par['azimuthal'][i],phx_par['polar_width'][i],phx_par['azimuthal_width'][i]));
    ft.close()


    pass
Example #43
0
    def plot_data_as_images(self, normalise=True):
        f = nxs.open(self.filepath)
        f.opengroup('entry')

        if normalise:
            sum = self.get_monitor_counts(f)
        else:
            sum = 1
        entries = f.getentries()
        banks = [key for key, value in entries.items() if value == 'NXdata']
        for bank in banks:
            f.opengroup(bank)
            f.opendata('data_x_y')
            data = f.getdata()

            data = data / np.float32(sum) * sum

            self.save_as_plot(data, bank)

            f.closedata()
            f.closegroup()

        f.closegroup()
        f.close()
    def plot_data_as_images(self, normalise=True):
        f = nxs.open(self.filepath)
        f.opengroup('entry')
        
        if normalise:
            sum = self.get_monitor_counts(f)
        else:
            sum = 1
        entries = f.getentries()
        banks = [ key for key, value in entries.items() if value == 'NXdata']
        for bank in banks:
            f.opengroup(bank)
            f.opendata('data_x_y')
            data = f.getdata()
            
            data = data/np.float32(sum)*sum
            
            self.save_as_plot(data,bank)
            
            f.closedata()
            f.closegroup()

        f.closegroup()
        f.close()
Example #45
0
import nxs

filename = 'REF_L_23554.nxs'

file = nxs.open(filename, 'r')
file.opengroup('entry', 'NXentry')

#start_time
file.opendata('start_time')
print 'start_time: ' + file.getdata()
file.closedata()

#duration
file.opendata('duration')
duration = str(file.getdata())
unit = file.getattr('units', 10, 'char')
print 'Duration is: ' + duration + unit
file.closedata()

#proton charge
file.opendata('proton_charge')
proton_charge = str(file.getdata())
unit = file.getattr('units', 15, 'char')
print 'Proton charge is: ' + proton_charge + unit
file.closedata()

#total counts
file.opendata('total_counts')
print 'Total counts: ' + str(file.getdata())
file.closedata()
Example #46
0
    def retrieve_metadata(self, filename):

        QApplication.setOverrideCursor(Qt.WaitCursor)

        file = nxs.open(str(filename), 'r')
        entry = 'entry'
        if (self.instrument == 'REF_M'):
            entry = 'entry-Off_Off'
        file.opengroup(entry, 'NXentry')

        #start time
        file.opendata('start_time')
        start_time = str(file.getdata())
        file.closedata()
        self.timestartinfo.setText(start_time)

        #duration
        file.opendata('duration')
        duration = str(file.getdata())
        unit = file.getattr('units', 10, 'char')
        file.closedata()
        self.durationinfo.setText(duration + ' ' + unit)

        #proton charge
        file.opendata('proton_charge')
        proton_charge = str(file.getdata())
        unit = file.getattr('units', 10, 'char')
        file.closedata()
        self.protonchargeinfo.setText(proton_charge + ' ' + unit)

        #total counts
        file.opendata('total_counts')
        total_counts = str(file.getdata())
        file.closedata()
        self.totalcountsinfo.setText(total_counts)

        #slit1 - height
        file.opendata('/entry/instrument/aperture1/s1t/value')
        s1t = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s1t = float(s1t)

        file.opendata('/entry/instrument/aperture1/s1b/value')
        s1b = str(file.getdata())
        file.closedata()
        _s1b = float(s1b)

        _s1h = _s1t - _s1b
        self.slit1h.setText(str(_s1h) + ' ' + units)

        #slit1 - width
        file.opendata('/entry/instrument/aperture1/s1r/value')
        s1r = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s1r = float(s1r)

        file.opendata('/entry/instrument/aperture1/s1l/value')
        s1l = str(file.getdata())
        file.closedata()
        _s1l = float(s1l)

        _s1w = _s1r - _s1l
        self.slit1w.setText(str(_s1w) + ' ' + units)

        #slit2 - height
        file.opendata('/entry/instrument/aperture2/s2t/value')
        s2t = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s2t = float(s2t)

        file.opendata('/entry/instrument/aperture2/s2b/value')
        s2b = str(file.getdata())
        file.closedata()
        _s2b = float(s2b)

        _s2h = _s2t - _s2b
        self.slit2h.setText(str(_s2h) + ' ' + units)

        # width

        file.opendata('/entry/instrument/aperture2/s2r/value')
        s2r = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s2r = float(s2r)

        file.opendata('/entry/instrument/aperture2/s2l/value')
        s2l = str(file.getdata())
        file.closedata()
        _s2l = float(s2l)

        _s2w = _s2r - _s2l
        self.slit2w.setText(str(_s2w) + ' ' + units)

        #slit3 - height
        file.opendata('/entry/instrument/aperture3/s3t/value')
        s3t = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s3t = float(s3t)

        file.opendata('/entry/instrument/aperture3/s3b/value')
        s3b = str(file.getdata())
        file.closedata()
        _s3b = float(s3b)

        _s3h = _s3t - _s3b
        self.slit3h.setText(str(_s3h) + ' ' + units)

        # width
        file.opendata('/entry/instrument/aperture3/s3r/value')
        s3r = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s3r = float(s3r)

        file.opendata('/entry/instrument/aperture3/s3l/value')
        s3l = str(file.getdata())
        file.closedata()
        _s3l = float(s3l)

        _s3w = _s3r - _s3l
        self.slit3w.setText(str(_s3w) + ' ' + units)

        #slit4 - height
        file.opendata('/entry/instrument/aperture4/s4t/value')
        s4t = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s4t = float(s4t)

        file.opendata('/entry/instrument/aperture4/s4b/value')
        s4b = str(file.getdata())
        file.closedata()
        _s4b = float(s4b)

        _s4h = _s4t - _s4b
        self.slit4h.setText(str(_s4h) + ' ' + units)

        # width
        file.opendata('/entry/instrument/aperture4/s4r/value')
        s4r = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        _s4r = float(s4r)

        file.opendata('/entry/instrument/aperture4/s4l/value')
        s4l = str(file.getdata())
        file.closedata()
        _s4l = float(s4l)

        _s4w = _s4r - _s4l
        self.slit4w.setText(str(_s4w) + ' ' + units)

        #ttheta
        file.opendata('/entry/instrument/bank1/TwoTheta/readback')
        ttheta = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        self.tthetainfo.setText(ttheta + ' ' + units)

        #tthd
        file.opendata('/entry/instrument/bank1/tthd/value')
        tthd = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        self.tthdinfo.setText(tthd + ' ' + units)

        #ths
        file.opendata('/entry/sample/ths/value')
        ths = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        self.thsinfo.setText(ths + ' ' + units)

        #thi
        file.opendata('/entry/instrument/aperture1/thi/value')
        thi = str(file.getdata())
        units = file.getattr('units', 10, 'char')
        file.closedata()
        self.thiinfo.setText(thi + ' ' + units)

        file.close()

        QApplication.restoreOverrideCursor()
Example #47
0
def show_structure(filename):
    f = nxs.open(filename)
    print "Showing contents for: ", f.inquirefile()
    _show(f)
Example #48
0
def check(filename):
    global failures
    failures = 0
    file = nxs.open(filename, 'rw')
    if filename != file.inquirefile(): fail("Files don't match")
    attrs = file.getattrinfo()
    if attrs != 4: fail("Expected 4 root attributes but got %d", attrs)
    for i in range(attrs):
        name, dims, type = file.getnextattr()
        if name not in [
                'file_time', 'HDF_version', 'HDF5_Version', 'XML_version',
                'NeXus_version', 'file_name'
        ]:
            fail("attribute %s unexpected" % (name))

    file.opengroup('entry', 'NXentry')

    expect = dict(hugo='namenlos', cucumber='passion')
    #expect['embedded_null'] = "embedded\000null"
    get = dict((k, v) for k, v in file.attrs())
    same = dicteq(get, expect)
    if not same: fail("/entry attributes are %s" % (get))

    # Check that the numbers are written correctly
    for name,dtype,shape,scale in \
        [('i1','int8',(4),1),
         ('i2','int16',(4),1000),
         ('i4','int32',(4),1000000),
         ('i8','int64',(4),1000000000000),
         ('r4','float32',(5,4),1),
         ('r8','float64',(5,4),1)
         ]:
        n = numpy.prod(shape)
        expected = numpy.arange(n, dtype=dtype).reshape(shape) * scale
        file.opendata(name + '_data')
        get = file.getdata()
        file.closedata()
        if not (get == expected).all():
            fail("%s retrieved %s" % (dtype, get))

    # Check attribute types
    file.opendata('r8_data')
    get = file.getattr("ch_attribute", 5, 'char')
    if not get == "NeXus": fail("ch_attribute retrieved %s" % (get))
    get = file.getattr("i4_attribute", 1, 'int32')
    if not get == numpy.int32(42): fail("i4_attribute retrieved %s" % (get))
    get = file.getattr("r4_attribute", 1, 'float32')
    if not get == numpy.float32(3.14159265):
        fail("r4_attribute retrieved %s" % (get))
    ## Oops... NAPI doesn't support array attributes
    #expect = numpy.array([3,2],dtype='int32')
    #get = file.getattr("i4_array",2,'int32')
    #if not (get==expect).all(): fail('i4_array retrieved %s'%(get))
    #expect = numpy.array([3.14159265,2.718281828],dtype='float32')
    #get = file.getattr("r4_array",2,dtype='float32')
    #if not (get==expect).all(): fail("r4_array retrieved %s"%(get))
    file.closedata()

    # Check reading from compressed datasets
    comp_array = numpy.ones((100, 20), dtype='int32')
    for i in range(100):
        comp_array[i, :] *= i
    expected = comp_array
    file.opengroup('data', 'NXdata')
    file.opendata('comp_data')
    get = file.getdata()
    file.closedata()  #/entry/data/comp_data
    file.closegroup()  #/entry/data
    if not (get == expected).all():
        fail("compressed data differs")
        print get
    file.closegroup()  #/entry

    # Check read slab (e.g., from extensible)

    # Check links
    file.opengroup('entry', 'NXentry')
    file.opengroup('sample', 'NXsample')
    sampleid = file.getgroupID()
    file.closegroup()  #/entry/sample
    file.opengroup('data', 'NXdata')  #/entry/data
    file.opendata('r8_data')  #/entry/data/r8_data
    dataid = file.getdataID()
    file.closedata()  #/entry/data/r8_data
    file.closegroup()  #/entry/data
    file.opendata('r8_data')
    data2id = file.getdataID()
    file.closedata()
    file.closegroup()  #/entry
    if not (file.sameID(dataid, data2id)):
        fail("/entry/data/r8_data not linked to /entry/r8_data")

    # Check openpath and getslab
    file.openpath('/entry/data/comp_data')
    get = file.getslab([4, 4], [5, 3])
    expected = comp_array[4:(4 + 5), 4:(4 + 3)]
    if not (get == expected).all():
        fail("retrieved compressed slabs differ")
        print get
    file.openpath('/entry/data/comp_data')
    get = file.getslab([4, 4], [5, 3])
    expected = comp_array[4:(4 + 5), 4:(4 + 3)]
    if not (get == expected).all():
        fail("after reopen: retrieved compressed slabs differ")
        print get
    file.openpath('../r8_data')
    for k, v in file.attrs():
        if k == 'target' and v != '/entry/r8_data':
            fail("relative openpath was not successful")

    return failures == 0
Example #49
0
    def read(self, filename=None):
        """
            Open and read the data in a file
        
            :param filename: path of the file
        """
        try:
            import nxs
        except:
            msg = "Error reading Nexus file: Nexus package is missing.\n"
            msg += "  Get it from http://http://www.nexusformat.org/"
            raise RuntimeError, msg

        # Instantiate data object
        output = Data2D()
        output.filename = os.path.basename(filename)

        fd = nxs.open(filename, 'rw')

        # Read in the 2D data
        fd.opengroup('mantid_workspace_1')
        fd.opengroup('workspace')
        fd.opendata('values')
        output.data = fd.getdata().copy()
        fd.closedata()

        # Read in the errors
        fd.opendata('errors')
        output.err_data = fd.getdata().copy()
        fd.closedata()

        # Read in the values on each axis
        fd.opendata('axis1')
        output.x_bins = fd.getdata().copy()
        fd.closedata()

        fd.opendata('axis2')
        output.y_bins = fd.getdata().copy()
        fd.closedata()
        fd.closegroup()

        output.xmin = min(output.x_bins)
        output.xmax = max(output.x_bins)
        output.ymin = min(output.y_bins)
        output.ymax = max(output.y_bins)

        output.xaxis("\\rm{Q_{x}}", 'A^{-1}')
        output.yaxis("\\rm{Q_{y}}", 'A^{-1}')
        output.zaxis("\\rm{Intensity}", "cm^{-1}")

        # Meta data
        fd.opendata('title')
        output.title = fd.getdata()
        fd.closedata()

        fd.opengroup('instrument')
        fd.opendata('name')
        output.instrument = fd.getdata()
        fd.closedata()
        fd.closegroup()

        fd.opengroup('logs')
        fd.opengroup('run_number')
        fd.opendata('value')
        output.run = fd.getdata()

        fd.close()

        # Store loading process information
        output.meta_data['loader'] = self.type_name
        return output
Example #50
0
def putArrayData(outf,name, ardata):
    outf.makedata(name,ardata.dtype.name,ardata.shape)
    outf.opendata(name)
    outf.putdata(ardata)
    outf.closedata()

def copyData(infile, inpath, outfile, outname):
    infile.openpath(inpath)
    shape,dtype = infile.getinfo()
    outfile.makedata(outname,dtype,shape)
    outfile.opendata(outname)
    data = infile.getdata()
    outfile.putdata(data)
    outfile.closedata()

inf = nxs.open(sys.argv[1],'r')
outf = nxs.open(sys.argv[2],'w5')
outf.makegroup('entry','NXentry')
outf.opengroup('entry','NXentry')


#--------- Instrument
outf.makegroup('instrument','NXinstrument')
outf.opengroup('instrument','NXinstrument')

#---------- Detector
outf.makegroup('detector','NXdetector')
outf.opengroup('detector','NXdetector')
copyData(inf,'/entry1/TRICS/area_detector2/data',outf,'data')
copyData(inf,'/entry1/TRICS/area_detector2/distance',outf,'distance')
outf.opendata('data')
Example #51
0
#!/usr/bin/python

import sys
import nxs
import numpy

nf = nxs.open("simple3D.h5", "w5")

nf.makegroup("entry", "NXentry")
nf.opengroup("entry", "NXentry")

nf.makegroup("data", "NXdata")
nf.opengroup("data", "NXdata")

a = numpy.zeros((2, 3, 4), dtype=numpy.int)
val = 0
for i in range(2):
    for j in range(3):
        for k in range(4):
            a[i, j, k] = val
            val = val + 1

nf.makedata("test", 'int32', [2, 3, 4])
nf.opendata("test")
nf.putdata(a)
nf.putattr("signal", 1)
nf.closedata()

nf.closegroup()  # NXdata
nf.closegroup()  # NXentry
Example #52
0
def show_structure(filename):
    file = nxs.open(filename)
    file.show()
Example #53
0
def populate(filename,mode):
    c1 = numpy.array(['abcd','efgh','ijkl','mnop','qrst'])
    i1 = numpy.arange(4,dtype='uint8')
    i2 = numpy.arange(4,dtype='int16')*1000
    i4 = numpy.arange(4,dtype='int32')*1000000
    i8 = numpy.arange(4,dtype='int64')*1000000000000
    r4 = numpy.arange(20,dtype='float32').reshape((5,4))
    r8 = numpy.arange(20,dtype='float64').reshape((5,4))
    comp_array=numpy.ones((100,20),dtype='int32')
    for i in range(100): comp_array[i,:] *= i

    file = nxs.open(filename,mode)
    file.setnumberformat('float32','%9.3g')
    file.makegroup("entry","NXentry")
    file.opengroup("entry","NXentry")
    file.putattr("hugo","namenlos")
    file.putattr("cucumber","passion")
    #file.putattr("embedded_null","embedded\000null")

    # Write character data
    file.makedata("ch_data",'char',[10])
    file.opendata("ch_data")
    file.putdata("NeXus data")
    file.closedata()
    file.makedata("c1_data",'char',[5,4])
    file.opendata("c1_data")
    file.putdata(c1)
    file.closedata()
    
    # Write numeric data
    for var in ['i1','i2','i4','i8','r4']:
        if mode == 'w4' and var == 'i8': continue
        name = var+'_data'
        val = locals()[var]
        file.makedata(name,val.dtype,val.shape)
        file.opendata(name)
        file.putdata(val)
        file.closedata()
    
    # Write r8_data
    file.makedata('r8_data','float64',[5,4])
    file.opendata('r8_data')
    file.putslab(r8[4,:],[4,0],[1,4])
    file.putslab(r8[0:4,:],[0,0],[4,4])
    file.putattr("ch_attribute","NeXus")
    file.putattr("i4_attribute",42,dtype='int32')
    file.putattr("r4_attribute",3.14159265,dtype='float32')
    ## Oops... NAPI doesn't support array attributes
    #file.putattr("i4_array",[3,2],dtype='int32')
    #file.putattr("r4_array",[3.14159265,2.718281828],dtype='float32')
    dataID = file.getdataID()
    file.closedata()

    # Create the NXdata group
    file.makegroup("data","NXdata")
    file.opengroup("data","NXdata")
    
    # .. demonstrate linking
    file.makelink(dataID)

    # .. demonstrate compressed data
    file.compmakedata("comp_data",'int32',[100,20],'lzw',[20,20])
    file.opendata('comp_data')
    file.putdata(comp_array)
    file.closedata()
    file.flush()

    # .. demonstrate extensible data
    file.makedata('flush_data','int32',[nxs.UNLIMITED])
    file.opendata('flush_data')
    for i in range(7):
        file.putslab(i,[i],[1])
    file.closedata()
    file.flush()
    file.closegroup()

    # Create NXsample group
    file.makegroup('sample','NXsample')
    file.opengroup('sample','NXsample')
    file.makedata('ch_data','char',[20])
    file.opendata('ch_data')
    file.putdata('NeXus sample')
    file.closedata()
    sampleID = file.getgroupID()
    file.closegroup()
    file.closegroup()

    # Demonstrate named links
    file.makegroup('link','NXentry')
    file.opengroup('link','NXentry')
    file.makelink(sampleID)
    file.makenamedlink('renLinkGroup',sampleID)
    file.makenamedlink('renLinkData',dataID)
    file.closegroup()
    
    file.close()
    return filename
Example #54
0
def show_structure(filename):
    file = nxs.open(filename)
    print("=== File",file.inquirefile())
    _show(file)
Example #55
0
def check(filename, mode):
    global failures
    failures = 0
    file = nxs.open(filename,'rw')
    if filename != file.inquirefile(): fail("Files don't match")

    # check headers
    num_attrs = file.getattrinfo()
    wxattrs = ['xmlns','xmlns:xsi','xsi:schemaLocation', 'XML_version']
    w4attrs = ['HDF_version']
    w5attrs = ['HDF5_Version']
    extras = dict(wx=wxattrs,w4=w4attrs,w5=w5attrs)
    expected_attrs = ['NeXus_version','file_name','file_time']+extras[mode]
    for i in range(num_attrs):
        name,dims,type = file.getnextattr()
        if name not in expected_attrs:
            fail("attribute %s unexpected"%(name))
    if num_attrs != len(expected_attrs): 
        fail("Expected %d root attributes but got %d"
             % (len(expected_attrs),num_attrs))
    
    file.opengroup('entry','NXentry')
    
    expect = dict(hugo='namenlos',cucumber='passion')
    #expect['embedded_null'] = "embedded\000null"
    get = dict((k,v) for k,v in file.attrs())
    same = dicteq(get,expect)
    if not same: fail("/entry attributes are %s"%(get))

    # Check that the numbers are written correctly
    for name,dtype,shape,scale in \
        [('i1','int8',(4),1),
         ('i2','int16',(4),1000),
         ('i4','int32',(4),1000000),
         ('i8','int64',(4),1000000000000),
         ('r4','float32',(5,4),1),
         ('r8','float64',(5,4),1)
         ]:
        if mode == 'w4' and name == 'i8': continue
        n = numpy.prod(shape)
        expected = numpy.arange(n,dtype=dtype).reshape(shape)*scale
        file.opendata(name+'_data')
        get = file.getdata()
        file.closedata()
        if not (get == expected).all(): 
            fail("%s retrieved %s"%(dtype,get))


    # Check attribute types
    file.opendata('r8_data')
    get = file.getattr("ch_attribute",5,'char')
    if not get == "NeXus": fail("ch_attribute retrieved %s"%(get))
    get = file.getattr("i4_attribute",1,'int32')
    if not get == numpy.int32(42): fail("i4_attribute retrieved %s"%(get))
    get = file.getattr("r4_attribute",1,'float32')
    if ((mode=='wx' and not abs(get-3.14159265) < 1e-6) or
        (mode!='wx' and not get == numpy.float32(3.14159265))):
        fail("r4_attribute retrieved %s"%(get))
    ## Oops... NAPI doesn't support array attributes
    #expect = numpy.array([3,2],dtype='int32')
    #get = file.getattr("i4_array",2,'int32')
    #if not (get==expect).all(): fail('i4_array retrieved %s'%(get))
    #expect = numpy.array([3.14159265,2.718281828],dtype='float32')
    #get = file.getattr("r4_array",2,dtype='float32')
    #if not (get==expect).all(): fail("r4_array retrieved %s"%(get))
    file.closedata()

    file.opendata('c1_data')
    rawshape,rawdtype = file.getrawinfo()
    shape,dtype = file.getinfo()
    get = file.getdata()
    file.closedata()
    if not (shape[0]==5 and shape[1]==4 and dtype=='char'):
        fail("returned string array info is incorrect")
    if not (rawshape[0]==5 and rawshape[1]==4 and rawdtype=='char'):
        fail("returned string array storage info is incorrect")
        print(rawshape,dtype)
    if not (get[0]=="abcd" and get[4]=="qrst"):
        fail("returned string is incorrect")
        print(shape,dtype)


    # Check reading from compressed datasets
    comp_array=numpy.ones((100,20),dtype='int32')
    for i in range(100): comp_array[i,:] *= i
    expected = comp_array
    file.opengroup('data','NXdata') #/entry/data
    file.opendata('comp_data')      #/entry/data/comp_data
    get = file.getdata()
    file.closedata()                #/entry/data/comp_data
    file.closegroup()               #/entry/data
    if not (get == expected).all():
        fail("compressed data differs")
        print(get)
        
    # Check strings
    file.opengroup('sample','NXsample') #/entry/sample
    file.opendata('ch_data')            #/entry/sample/ch_data
    rawshape,rawdtype = file.getrawinfo()
    shape,dtype = file.getinfo()
    get = file.getdata()
    file.closedata()                    #/entry/sample/ch_data
    file.closegroup()                   #/entry/sample
    if not (shape[0]==12 and dtype=='char'):
        fail("returned string info is incorrect")
        print(shape,dtype)
    if not (rawshape[0]==20 and rawdtype=='char'):
        fail("returned string storage info is incorrect")
        print(shape,dtype)
    if not (get == "NeXus sample"):
        fail("returned string is incorrect")
        print(shape,dtype)

    file.closegroup() #/entry

    # Check read slab (e.g., from extensible)

    # Check links
    file.opengroup('entry','NXentry')
    file.opengroup('sample','NXsample')
    sampleid = file.getgroupID()
    file.closegroup() #/entry/sample
    file.opengroup('data','NXdata') #/entry/data
    file.opendata('r8_data') #/entry/data/r8_data
    dataid = file.getdataID()
    file.closedata() #/entry/data/r8_data
    file.closegroup() #/entry/data
    file.opendata('r8_data')
    data2id = file.getdataID()
    file.closedata()
    file.closegroup() #/entry
    if not (file.sameID(dataid,data2id)):
        fail("/entry/data/r8_data not linked to /entry/r8_data")
    
    # Check openpath and getslab
    file.openpath('/entry/data/comp_data')
    get = file.getslab([4,4],[5,3])
    expected = comp_array[4:(4+5),4:(4+3)]
    if not (get == expected).all():
        fail("retrieved compressed slabs differ")
        print(get)
    file.openpath('/entry/data/comp_data')
    get = file.getslab([4,4],[5,3])
    expected = comp_array[4:(4+5),4:(4+3)]
    if not (get == expected).all():
        fail("after reopen: retrieved compressed slabs differ")
        print(get)
    file.openpath('../r8_data')
    for k,v in file.attrs():
        if k == 'target' and v != '/entry/r8_data':
            fail("relative openpath was not successful")

    return failures == 0
Example #56
0
def populate(filename,mode):
    c1 = numpy.array(['abcd','efgh','ijkl','mnop','qrst'])
    i1 = numpy.arange(4,dtype='uint8')
    i2 = numpy.arange(4,dtype='int16')*1000
    i4 = numpy.arange(4,dtype='int32')*1000000
    i8 = numpy.arange(4,dtype='int64')*1000000000000
    r4 = numpy.arange(20,dtype='float32').reshape((5,4))
    r8 = numpy.arange(20,dtype='float64').reshape((5,4))
    comp_array=numpy.ones((100,20),dtype='int32')
    for i in range(100): comp_array[i,:] *= i

    file = nxs.open(filename,mode)
    file.setnumberformat('float32','%9.3g')
    file.makegroup("entry","NXentry")
    file.opengroup("entry","NXentry")
    file.putattr("hugo","namenlos")
    file.putattr("cucumber","passion")
    #file.putattr("embedded_null","embedded\000null")

    # Write character data
    file.makedata("ch_data",'char',[10])
    file.opendata("ch_data")
    file.putdata("NeXus data")
    file.closedata()
    file.makedata("c1_data",'char',[5,4])
    file.opendata("c1_data")
    file.putdata(c1)
    file.closedata()
    
    # Write numeric data
    for var in ['i1','i2','i4','i8','r4']:
        if mode == 'w4' and var == 'i8': continue
        name = var+'_data'
        val = locals()[var]
        file.makedata(name,val.dtype,val.shape)
        file.opendata(name)
        file.putdata(val)
        file.closedata()
    
    # Write r8_data
    file.makedata('r8_data','float64',[5,4])
    file.opendata('r8_data')
    file.putslab(r8[4,:],[4,0],[1,4])
    file.putslab(r8[0:4,:],[0,0],[4,4])
    file.putattr("ch_attribute","NeXus")
    file.putattr("i4_attribute",42,dtype='int32')
    file.putattr("r4_attribute",3.14159265,dtype='float32')
    ## Oops... NAPI doesn't support array attributes
    #file.putattr("i4_array",[3,2],dtype='int32')
    #file.putattr("r4_array",[3.14159265,2.718281828],dtype='float32')
    dataID = file.getdataID()
    file.closedata()

    # Create the NXdata group
    file.makegroup("data","NXdata")
    file.opengroup("data","NXdata")
    
    # .. demonstrate linking
    file.makelink(dataID)

    # .. demonstrate compressed data
    file.compmakedata("comp_data",'int32',[100,20],'lzw',[20,20])
    file.opendata('comp_data')
    file.putdata(comp_array)
    file.closedata()
    file.flush()

    # .. demonstrate extensible data
    file.makedata('flush_data','int32',[nxs.UNLIMITED])
    file.opendata('flush_data')
    for i in range(7):
        file.putslab(i,[i],[1])
    file.closedata()
    file.flush()
    file.closegroup()

    # Create NXsample group
    file.makegroup('sample','NXsample')
    file.opengroup('sample','NXsample')
    file.makedata('ch_data','char',[20])
    file.opendata('ch_data')
    file.putdata('NeXus sample')
    file.closedata()
    sampleID = file.getgroupID()
    file.closegroup()
    file.closegroup()

    # Demonstrate named links
    file.makegroup('link','NXentry')
    file.opengroup('link','NXentry')
    file.makelink(sampleID)
    file.makenamedlink('renLinkGroup',sampleID)
    file.makenamedlink('renLinkData',dataID)
    file.closegroup()
    
    file.close()
    return filename
Example #57
0
#!/usr/bin/python

import sys
import nxs
import numpy

a = numpy.zeros((2,3,4),dtype=numpy.int)
val = 0
for i in range(2):
    for j in range(3):
        for k in range(4):
            a[i,j,k] = val
            val = val + 1

nf = nxs.open("simple3D.h5", "w5")

nf.makegroup("entry","NXentry")
nf.opengroup("entry","NXentry")

nf.makegroup("data","NXdata")
nf.opengroup("data","NXdata")
nf.putattr("signal","test")

nf.makedata("test",'int32',[2,3,4])
nf.opendata("test")
nf.putdata(a)
nf.closedata()

nf.closegroup()	# NXdata
nf.closegroup() # NXentry
Example #58
0
def show_structure(filename):
    file = nxs.open(filename)
    print "=== File",file.inquirefile()
    _show(file)
Example #59
0
#!/usr/bin/python

import nxs

nf = nxs.open("hand1.h5", "w5")

nf.makegroup("entry","NXentry")
nf.opengroup("entry","NXentry")

nf.makegroup("data","NXdata")
nf.opengroup("data","NXdata")

nf.closegroup()


nf.makedata("hugo",'int32',[1])
nf.opendata("hugo")
nf.putdata(22.33)
nf.putattr("signal",1)

nxl = nf.getdataID()

nf.closedata()

nf.opengroup("data","NXdata")
nf.makelink(nxl)

nf.close()

exit