def _process_data(self): for sect, opts in conf_sects.items(): # if sect == 'scan': print( opts) if not self.cp.has_section(sect): continue bools = opts.get('bools', []) floats = opts.get('floats', []) ints = opts.get('ints', []) thissect = {} is_ordered = False if 'ordered' in opts: thissect = OrderedDict() is_ordered = True for opt in self.cp.options(sect): get = self.cp.get if opt in bools: get = self.cp.getboolean elif opt in floats: get = self.cp.getfloat elif opt in ints: get = self.cp.getint val = get(sect, opt) if is_ordered and '|' in val: opt, val = val.split('|', 1) opt = opt.strip() val = val.strip() thissect[opt] = val self.config[sect] = thissect
def read_stageconfig(filename=None, _larch=None): """read Sample Stage configurattion file, saving results Parameters ---------- filename: None or string, name of file holding sample positions (see Note) Examples -------- read_stageconfig() Notes ------ 1. If filename is None (the default) 'SampleStage_autosave.ini' from the current working folder is used. 2. the file timestamp is always tested to find new position names 3. Generally, you don't need to do this explicitly, as move_samplestage will run this when necessary. """ if filename is None: filename = DEF_SampleStageFile if not os.path.exists(filename): print 'No stageconfig found!' return None #endif stages = [] positions = OrderedDict() ini = ConfigParser() ini.read(filename) for item in ini.options('stages'): line = ini.get('stages', item) words = [i.strip() for i in line.split('||')] stages.append(words[0]) #endfor for item in ini.options('positions'): line = ini.get('positions', item) words = [i.strip() for i in line.split('||')] name = words[0].lower() vals = [float(i) for i in words[2].split(',')] positions[name] = vals #endfor this = Group(pos=positions, stages=stages, _modtime_=os.stat(filename).st_mtime) if _larch is not None: if not _larch.symtable.has_group('macros'): _larch.symtable.new_group('macros') _larch.symtable.macros.stage_conf = this print(" Read %d positions from stage configuration." % len(positions)) return this
def read_gsexdi(fname, _larch=None, nmca=4, bad=None, **kws): """Read GSE XDI Scan Data to larch group, summing ROI data for MCAs and apply deadtime corrections """ group = _larch.symtable.create_group() group.__name__ = 'GSE XDI Data file %s' % fname xdi = XDIFile(str(fname)) group._xdi = xdi group.filename = fname group.npts = xdi.npts group.bad_channels = bad group.dtc_taus = XSPRESS3_TAUS if _larch.symtable.has_symbol('_sys.gsecars.xspress3_taus'): group.dtc_taus = _larch.symtable._sys.gsecars.xspress3_taus for family in ('scan', 'mono', 'facility'): for key, val in xdi.attrs.get(family, {}).items(): if '||' in val: val, addr = val.split('||') try: val = float(val) except: pass setattr(group, "%s_%s" % (family, key), val) ocrs, icrs = [], [] ctime = None for attrname in dir(xdi): if attrname.lower() == 'counttime': ctime = getattr(xdi, attrname) if ctime is None: try: ctime = xdi.TSCALER / 5.e7 except AttributeError: ctime = 1.0 is_xspress3 = any(['13QX4' in a[1] for a in xdi.attrs['column'].items()]) group.with_xspress3 = is_xspress3 for i in range(nmca): ocr = getattr(xdi, 'OutputCounts_mca%i' % (i + 1), None) if ocr is None: ocr = ctime ocr = ocr / ctime icr = getattr(xdi, 'InputCounts_mca%i' % (i + 1), None) if icr is not None: icr = icr / ctime else: icr = 1.0 * ocr if is_xspress3: tau = group.dtc_taus[i] icr = estimate_icr(ocr * 1.00, tau, niter=7) ocrs.append(ocr) icrs.append(icr) labels = [] sums = OrderedDict() for i, arrname in enumerate(xdi.array_labels): dat = getattr(xdi, arrname) aname = sumname = rawname = arrname.lower() if ('_mca' in aname and 'outputcounts' not in aname and 'clock' not in aname): sumname, imca = sumname.split('_mca') imca = int(imca) - 1 datraw = dat * 1.0 rawname = sumname + '_nodtc' dat = dat * icrs[imca] / ocrs[imca] if any(np.isnan(dat)): nan_pts = np.where(np.isnan(dat))[0] dat[nan_pts] = datraw[nan_pts] setattr(group, aname, dat) if sumname not in labels: labels.append(sumname) sums[sumname] = dat if rawname != sumname: sums[rawname] = datraw if rawname not in labels: labels.append(rawname) else: sums[sumname] = sums[sumname] + dat if rawname != sumname: sums[rawname] = sums[rawname] + datraw for name, dat in sums.items(): if not hasattr(group, name): setattr(group, name, dat) for arrname in xdi.array_labels: sname = arrname.lower() if sname not in labels: labels.append(sname) for imca in range(nmca): setattr(group, 'ocr_mca%i' % (imca + 1), ocrs[imca]) setattr(group, 'icr_mca%i' % (imca + 1), icrs[imca]) group.array_labels = labels return group
def gsexdi_deadtime_correct(fname, channelname, subdir='DT_Corrected', bad=None, _larch=None): """convert GSE XDI fluorescence XAFS scans to dead time corrected files""" if not is_GSEXDI(fname): print("'%s' is not a GSE XDI scan file\n" % fname) return out = Group() out.orig_filename = fname try: xdi = read_gsexdi(fname, bad=bad, _larch=_larch) except: print('Could not read XDI file ', fname) return for attr in ('energy', 'i0', 'i1', 'i2', 'tscaler', 'counttime', 'scan_start_time', 'scan_end_time'): if hasattr(xdi, attr): setattr(out, attr, getattr(xdi, attr)) # some scans may not record separate counttime, but TSCALER # is clock ticks for a 50MHz clock if not hasattr(out, 'counttime'): out.counttime = xdi.tscaler * 2.e-8 if hasattr(xdi, 'energy_readback'): out.energy = xdi.energy_readback arrname = None channelname = channelname.lower().replace(' ', '_') for arr in xdi.array_labels: if arr.lower().startswith(channelname): arrname = arr break if arrname is None: print('Cannot find Channel %s in file %s ' % (channelname, fname)) return out.ifluor = getattr(xdi, arrname) out.ifluor_raw = getattr(xdi, arrname) arrname_raw = arrname + '_nodtc' if arrname_raw in xdi.array_labels: out.ifluor_raw = getattr(xdi, arrname_raw) out.mufluor = out.ifluor / out.i0 if hasattr(out, 'i1') or hasattr(out, 'itrans'): i1 = getattr(out, 'i1', None) if i1 is None: i1 = getattr(out, 'itrans', None) if i1 is not None: i1 = i1 / out.i0 i1[np.where(i1 < 2.0e-20)] = 2.0e-20 out.mutrans = -np.log(i1) npts = len(out.energy) buff = ['# XDI/1.0 GSE/1.0'] header = OrderedDict() hgroups = [ 'beamline', 'facility', 'mono', 'undulator', 'detectors', 'scaler', 'detectorstage', 'samplestage', 'scan', 'scanparameters' ] hskip = ['scanparameters.end', 'scanparameters.start'] for agroup in hgroups: attrs = xdi._xdi.attrs.get(agroup, {}) if agroup == 'mono': agroup = 'monochromator' header[agroup] = OrderedDict() for sname in sorted(attrs.keys()): if "%s.%s" % (agroup, sname) not in hskip: header[agroup][sname] = attrs[sname] header['facility']['name'] = 'APS' header['facility']['xray_source'] = '3.6 cm undulator' header['beamline']['name'] = '13-ID-E, GSECARS' header['detectors']['i0'] = '20cm ion chamber, He' header['detectors']['ifluor'] = 'Si SDD Vortex ME-4, 4 elements' header['detectors']['ifluor_electronics'] = 'Quantum Xspress3 3.1.10' mono_cut = 'Si(111)' if xdi.mono_dspacing < 2: mono_cut = 'Si(311)' header['monochromator']['name'] = "%s, LN2 cooled" % mono_cut out_arrays = OrderedDict() out_arrays['energy'] = ('energy', 'eV') out_arrays['mufluor'] = ('mufluor', None) if hasattr(out, 'i1'): out_arrays['mutrans'] = ('mutrans', None) out_arrays['ifluor'] = ('ifluor', '# deadtime-corrected') out_arrays['ifluor_raw'] = ('ifluor_raw', '# not deadtime-corrected') out_arrays['i0'] = ('i0', None) if hasattr(out, 'i1'): out_arrays['itrans'] = ('i1', None) if hasattr(out, 'i2'): out_arrays['irefer'] = ('i2', None) if hasattr(out, 'counttime'): out_arrays['counttime'] = ('counttime', 'sec') arrlabel = [] for iarr, aname in enumerate(out_arrays): lab = "%12s " % aname if iarr == 0: lab = "%11s " % aname arrlabel.append(lab) extra = out_arrays[aname][1] if extra is None: extra = '' buff.append("# Column.%i: %s %s" % (iarr + 1, aname, extra)) arrlabel = '#%s' % (' '.join(arrlabel)) ncol = len(out_arrays) for family, fval in header.items(): for attr, val in fval.items(): buff.append("# %s.%s: %s" % (family.title(), attr, val)) buff.append("# ///") for comment in xdi._xdi.comments.split('\n'): c = comment.strip() if len(c) > 0: buff.append('# %s' % c) buff.extend([ "# summed %s fluorescence data from %s" % (channelname, fname), "# Dead-time correction applied", "#" + "-" * 78, arrlabel ]) efmt = "%11.4f" ffmt = "%13.7f" gfmt = "%13.7g" for i in range(npts): dline = ["", efmt % out.energy[i], ffmt % out.mufluor[i]] if hasattr(out, 'i1'): dline.append(ffmt % out.mutrans[i]) dline.extend( [gfmt % out.ifluor[i], gfmt % out.ifluor_raw[i], gfmt % out.i0[i]]) if hasattr(out, 'i1'): dline.append(gfmt % out.i1[i]) if hasattr(out, 'i2'): dline.append(gfmt % out.i2[i]) if hasattr(out, 'counttime'): dline.append(gfmt % out.counttime[i]) buff.append(" ".join(dline)) ofile = fname[:] if ofile.startswith('..'): ofile = ofile[3:] ofile = ofile.replace('.', '_') + '.dat' ofile = os.path.join(subdir, ofile) if not os.path.exists(subdir): os.mkdir(subdir) try: fout = open(ofile, 'w') fout.write("\n".join(buff)) fout.close() print("wrote %s, npts=%i, channel='%s'" % (ofile, npts, channelname)) except: print("could not open / write to output file %s" % ofile) return out
'scan': { 'ints': ('dimension', ), 'floats': ('start1', 'stop1', 'step1', 'time1', 'start2', 'stop2', 'step2') } } __c = (('general', ('mapdb', 'struck', 'scaler', 'xmap', 'mono', 'fileplugin', 'basedir', 'scandir', 'envfile')), ('xps', ('host', 'user', 'passwd', 'group', 'positioners')), ('scan', ('filename', 'dimension', 'comments', 'pos1', 'start1', 'stop1', 'step1', 'time1', 'pos2', 'start2', 'stop2', 'step2')), ('xrf', ('use', 'type', 'prefix', 'plugin')), ('fast_positioners', None), ('slow_positioners', None)) conf_objs = OrderedDict(__c) conf_files = ('MapDefault.ini', '//cars5/Data/xas_user/config/FastMap/Default.ini') ##struck = 13IDC:str: ##scaler = 13IDC:scaler2 default_conf = """# FastMap configuration file (default) [general] mapdb = 13XRM:map: mono = 13IDA: struck = 13IDE:SIS1 scaler = 13IDE:scaler1 xmap = dxpMercury: fileplugin = netCDF1:
def read_gsexdi(fname, _larch=None, nmca=4, bad=None, **kws): """Read GSE XDI Scan Data to larch group, summing ROI data for MCAs and apply deadtime corrections """ group = _larch.symtable.create_group() group.__name__ = 'GSE XDI Data file %s' % fname xdi = XDIFile(str(fname)) group._xdi = xdi group.filename = fname group.npts = xdi.npts group.bad_channels = bad for family in ('scan', 'mono', 'facility'): for key, val in xdi.attrs.get(family, {}).items(): if '||' in val: val, addr = val.split('||') try: val = float(val) except: pass setattr(group, "%s_%s" % (family, key), val) ocrs, icrs = [], [] ctime = None for attrname in dir(xdi): if attrname.lower() == 'counttime': ctime = getattr(xdi, attrname) if ctime is None: try: ctime = xdi.TSCALER / 5.e7 except AttributeError: ctime = 1.0 is_xspress3 = any(['13QX4' in a[1] for a in xdi.attrs['column'].items()]) group.with_xspress3 = is_xspress3 dtc_taus = XSPRESS3_TAUS if _larch.symtable.has_symbol('_sys.gsecars.xspress3_taus'): dtc_taus = _larch.symtable._sys.gsecars.xspress3_taus dtc_mode = 'icr/ocr' for i in range(nmca): mca = "mca%i" % (i + 1) ocr = getattr(xdi, 'OutputCounts_%s' % mca, None) clock = getattr(xdi, 'Clock_%s' % mca, None) icr = getattr(xdi, 'InputCounts_%s' % mca, None) dtfact = getattr(xdi, 'DTFactor_%s' % mca, None) resets = getattr(xdi, 'ResetTicks_%s' % mca, None) allevt = getattr(xdi, 'AllEvent_%s' % mca, None) if ocr is None: ocr = ctime ocr = ocr / ctime if icr is not None: icr = icr / ctime # Get ICR from one of several alternatives: # 1. InputCounts_mca was given # 2. DTFactor_mca was given # 3. ResetTicks_mca and AllEvets_mca were given # 4. Use "known values" for tau if icr is None: if dtfact is not None: icr = ocr * dtfact dtc_mode = 'dtfactor' elif (clock is not None and resets is not None and allevt is not None): dtfact = clock / (clock - (6 * allevt + resets)) icr = ocr * dtfact dtc_mode = 'resets' # finally estimate from measured values of tau: if icr is None: icr = 1.0 * ocr dtc_mode = 'none' if is_xspress3: tau = dtc_taus[i] icr = estimate_icr(ocr * 1.00, tau, niter=7) dtc_mode = 'saved_taus' ocrs.append(ocr) icrs.append(icr) group.dtc_mode = dtc_mode if dtc_mode == 'saved_taus': group.dtc_taus = dtc_taus labels = [] sums = OrderedDict() for i, arrname in enumerate(xdi.array_labels): dat = getattr(xdi, arrname) if arrname.lower() == 'data': arrname = '_data' aname = sumname = rawname = arrname.lower() if ('_mca' in aname and 'outputcounts' not in aname and 'dtfactor' not in aname and 'clock' not in aname): sumname, imca = sumname.split('_mca') imca = int(imca) - 1 datraw = dat * 1.0 rawname = sumname + '_nodtc' dat = dat * icrs[imca] / ocrs[imca] if any(np.isnan(dat)): nan_pts = np.where(np.isnan(dat))[0] dat[nan_pts] = datraw[nan_pts] setattr(group, aname, dat) if sumname not in labels: labels.append(sumname) sums[sumname] = dat if rawname != sumname: sums[rawname] = datraw if rawname not in labels: labels.append(rawname) else: sums[sumname] = sums[sumname] + dat if rawname != sumname: sums[rawname] = sums[rawname] + datraw for name, dat in sums.items(): if not hasattr(group, name): setattr(group, name, dat) for arrname in xdi.array_labels: sname = arrname.lower() if sname not in labels: labels.append(sname) data = [] for name in labels: data.append(getattr(group, name)) group.data = np.array(data) for imca in range(nmca): setattr(group, 'ocr_mca%i' % (imca + 1), ocrs[imca]) setattr(group, 'icr_mca%i' % (imca + 1), icrs[imca]) group.array_labels = labels return group