示例#1
0
文件: hzf.py 项目: evizbiz/niceweb
 def __init__(self, filename, mode, timestamp=None, creator=None, compression=zipfile.ZIP_DEFLATED, rootpath=None, attrs={}, **kw):
     fn = tempfile.mkdtemp()
     # os.close(fd) # to be opened by name
     self.fn = fn
     self.filename = filename
     self.mode = mode
     self.compression = compression
     if rootpath is None:
         rootpath = filename.split('.')[0]
     self.path = rootpath
     preexisting = os.path.exists(os.path.join(self.fn, self.path))
     
     if (mode == "a" and not preexisting) or mode == "w":
         os.mkdir(os.path.join(self.fn, self.path))
         if timestamp is None:
             timestr = iso8601.now()
         else:
             # If given a time string, check that it is valid
             try:
                 timestamp = iso8601.parse_date(timestamp)
             except TypeError:
                 pass
             timestr = iso8601.format_date(timestamp)
         attrs['NX_class'] = 'NXroot'
         attrs['file_name'] = filename
         attrs['file_time'] = timestr
         attrs['NeXus_version'] = __version__
         if creator is not None:
             attrs['creator'] = creator
     self.attrs = attrs
示例#2
0
 def __init__(self, filename, mode="r", timestamp=None, creator=None, compression=zipfile.ZIP_DEFLATED, attrs={}, **kw):
     fn = tempfile.mkdtemp()
     self.os_path = fn
     self.root_node = self
     self.filename = filename
     self.mode = mode
     self.compression = compression
     file_exists = os.path.exists(filename)
     if file_exists and (mode == "a" or mode == "r"):
          zipfile.ZipFile(filename).extractall(self.os_path)
     Node.__init__(self, parent_node=self, path="/")        
            
     if mode == "a" or mode == "w":
         #os.mkdir(os.path.join(self.os_path, self.path.lstrip("/")))
         if timestamp is None:
             timestr = iso8601.now()
         else:
             # If given a time string, check that it is valid
             try:
                 timestamp = iso8601.parse_date(timestamp)
             except TypeError:
                 pass
             timestr = iso8601.format_date(timestamp)
         attrs['NX_class'] = 'NXroot'
         attrs['file_name'] = filename
         attrs['file_time'] = timestr
         attrs['NeXus_version'] = __version__
         if creator is not None:
             attrs['creator'] = creator       
     self.attrs.update(attrs)
     self.attrs._write()
示例#3
0
def bt7_ice_to_nice(icedata):
    """
    Convert BT-7 ice data to NeXus.

    The instrument description is stored in bt7.xml.
    """
    # Pull together header and columns
    data = icedata.metadata.copy()
    data.update(icedata.data)
    
    # Expand lattice parameters
    data.update((k.capitalize(),v) for k,v in data['Lattice'].items())
    # Fix up monochromator selection
    if abs(data["MonoSpacing"] - 1.278) < 1e-4:
        data["MonoMaterial"] = "Cu220"
        data["MonoFocus"] = data["FocusCu"]
    else:
        data["MonoMaterial"] = "PG002"
        data["MonoFocus"] = data["FocusPG"]
        
    # Gang monochromator and analyzer blades
    data['MonoBlades'] = numpy.array([data[f] for f in iceformat.BT7_MONOCHROMATOR_BLADES])
    data['AnaBlades'] = numpy.array([data[f] for f in iceformat.BT7_ANALYZER_BLADES])

    # Gather detector data columns    
    data['SDC'] = numpy.array([icedata.data['SDC%d'%d] for d in range(3)])
    data['DDC'] = numpy.array([icedata.data['DDC%d'%d] for d in range(3)])
    data['TDC'] = numpy.array([icedata.data['TDC%02d'%d] for d in range(9)])
    if 'PSDC0' in icedata.data:
        data['PSDC'] = numpy.array([icedata.data['PSDC%02d'%d] for d in range(48)])

    # get counts on the detector
    data['Counts'] = icedata.counts()
    
    # Only include base E for FixedE.  Fixed value is already reported
    if 'FixedE' in icedata.metadata:
        data['FixedE'] = icedata.metadata['FixedE'][0]

    lattice = data['Lattice']
    data['Lattice'] = [lattice[k] for k in 'a', 'b', 'c', 'alpha', 'beta', 'gamma']
    orient = data['Orient1']
    data['Orient1'] = [orient[k] for k in 'h','k','l']
    orient = data['Orient2']
    data['Orient2'] = [orient[k] for k in 'h','k','l']
    data['FilTran'] = data['FilTran'][0]
    data['ScanVarying'] = ", ".join(data['ScanVarying'])
    data['Date'] = iso8601.format_date(data['Date'])
    data['Flip'] = data['Flip'][0]
    #data['Flip'] = [{'A':0,'B':1,'C':2,'D':3}.get(c,0)
    #                for c in data['Flip']]


    nicedata = dict((nicekey, {'value': data[icekey], 
                               'units': icedata.units(icekey), 
                               #'shortname': rename_field(nicekey),
                               })
                    for icekey,nicekey in _ICE_TO_NICE.items()
                    if icekey in data and nicekey)
    return nicedata
示例#4
0
    def list(self, response):
        """
        Returns { experiments: [[id, title, date], ...] }.

        Experiment id, title and date are strings, with date formatted
        like 2007-01-25T07:00:00-05:00.  The experiments are sorted by
        experiment id.
        """
        items = ((exp['id'], exp['title'], iso8601.format_date(exp['creationTimeStamp']*0.001))
                 for exp in self.experiments.values())
        return { 'experiments': list(sorted(items)) }
示例#5
0
    def list(self, response):
        """
        Returns { experiments: [[id, title, date], ...] }.

        Experiment id, title and date are strings, with date formatted
        like 2007-01-25T07:00:00-05:00.  The experiments are sorted by
        experiment id.
        """
        items = ((exp['id'], exp['title'],
                  iso8601.format_date(exp['creationTimeStamp'] * 0.001))
                 for exp in self.experiments.values())
        return {'experiments': list(sorted(items))}
示例#6
0
    def __init__(self,
                 filename,
                 mode="r",
                 timestamp=None,
                 creator=None,
                 compression=zipfile.ZIP_DEFLATED,
                 attrs={},
                 os_path=None,
                 **kw):
        self.readonly = (mode == "r")
        Node.__init__(self, parent_node=None, path="/")
        if self.readonly:
            self.zipfile = zipfile.ZipFile(filename)
        self.attrs = self.makeAttrs()
        self.filename = filename
        self.mode = mode
        self.compression = compression
        file_exists = os.path.exists(filename)

        if not self.readonly:
            if os_path is None:
                fn = tempfile.mkdtemp()
                self.os_path = fn
            else:
                self.os_path = os_path

            if file_exists and (mode == "a"):
                zipfile.ZipFile(filename).extractall(self.os_path)

            if timestamp is None:
                timestr = iso8601.now()
            else:
                # If given a time string, check that it is valid
                try:
                    timestamp = iso8601.parse_date(timestamp)
                except TypeError:
                    pass
                timestr = iso8601.format_date(timestamp)
            attrs['NX_class'] = 'NXroot'
            attrs['file_name'] = filename
            attrs['file_time'] = timestr
            attrs['NeXus_version'] = __version__
            if creator is not None:
                attrs['creator'] = creator
            self.attrs.update(attrs)
            self.attrs._write()
示例#7
0
    def __init__(self,
                 filename,
                 mode,
                 timestamp=None,
                 creator=None,
                 compression=zipfile.ZIP_DEFLATED,
                 rootpath=None,
                 attrs={},
                 **kw):
        fn = tempfile.mkdtemp()
        # os.close(fd) # to be opened by name
        self.fn = fn
        self.filename = filename
        self.mode = mode
        self.compression = compression
        if rootpath is None:
            rootpath = filename.split('.')[0]
        self.path = rootpath
        preexisting = os.path.exists(os.path.join(self.fn, self.path))

        if (mode == "a" and not preexisting) or mode == "w":
            os.mkdir(os.path.join(self.fn, self.path))
            if timestamp is None:
                timestr = iso8601.now()
            else:
                # If given a time string, check that it is valid
                try:
                    timestamp = iso8601.parse_date(timestamp)
                except TypeError:
                    pass
                timestr = iso8601.format_date(timestamp)
            attrs['NX_class'] = 'NXroot'
            attrs['file_name'] = filename
            attrs['file_time'] = timestr
            attrs['NeXus_version'] = __version__
            if creator is not None:
                attrs['creator'] = creator
        self.attrs = attrs
示例#8
0
def bt7_ice_to_nice(icedata):
    """
    Convert BT-7 ice data to NeXus.

    The instrument description is stored in bt7.xml.
    """
    # Pull together header and columns
    data = icedata.metadata.copy()
    data.update(icedata.data)

    # Expand lattice parameters
    data.update((k.capitalize(), v) for k, v in data['Lattice'].items())
    # Fix up monochromator selection
    if abs(data["MonoSpacing"] - 1.278) < 1e-4:
        data["MonoMaterial"] = "Cu220"
        data["MonoFocus"] = data["FocusCu"]
    else:
        data["MonoMaterial"] = "PG002"
        data["MonoFocus"] = data["FocusPG"]

    # Gang monochromator and analyzer blades
    data['MonoBlades'] = numpy.array(
        [data[f] for f in iceformat.BT7_MONOCHROMATOR_BLADES])
    data['AnaBlades'] = numpy.array(
        [data[f] for f in iceformat.BT7_ANALYZER_BLADES])

    # Gather detector data columns
    data['SDC'] = numpy.array([icedata.data['SDC%d' % d] for d in range(3)])
    data['DDC'] = numpy.array([icedata.data['DDC%d' % d] for d in range(3)])
    data['TDC'] = numpy.array([icedata.data['TDC%02d' % d] for d in range(9)])
    if 'PSDC0' in icedata.data:
        data['PSDC'] = numpy.array(
            [icedata.data['PSDC%02d' % d] for d in range(48)])

    # get counts on the detector
    data['Counts'] = icedata.counts()

    # Only include base E for FixedE.  Fixed value is already reported
    if 'FixedE' in icedata.metadata:
        data['FixedE'] = icedata.metadata['FixedE'][0]

    lattice = data['Lattice']
    data['Lattice'] = [
        lattice[k] for k in 'a', 'b', 'c', 'alpha', 'beta', 'gamma'
    ]
    orient = data['Orient1']
    data['Orient1'] = [orient[k] for k in 'h', 'k', 'l']
    orient = data['Orient2']
    data['Orient2'] = [orient[k] for k in 'h', 'k', 'l']
    data['FilTran'] = data['FilTran'][0]
    data['ScanVarying'] = ", ".join(data['ScanVarying'])
    data['Date'] = iso8601.format_date(data['Date'])
    data['Flip'] = data['Flip'][0]
    #data['Flip'] = [{'A':0,'B':1,'C':2,'D':3}.get(c,0)
    #                for c in data['Flip']]

    nicedata = dict((
        nicekey,
        {
            'value': data[icekey],
            'units': icedata.units(icekey),
            #'shortname': rename_field(nicekey),
        }) for icekey, nicekey in _ICE_TO_NICE.items()
                    if icekey in data and nicekey)
    return nicedata