@ValidateLarchPlugin
def gsescan_deadtime_correct(fname, channelname, subdir='DT_Corrected',
                             bad=None, _larch=None):
    try:
       sg = gsescan_group(fname, bad=bad, _larch=_larch)
    except:
      print('%s is not a valid ESCAN file' % fname)
      return
    energy =  sg.x
    i0 = sg.get_data('i0')

    ix = -1
    channelname = channelname.lower()
    for ich, ch in enumerate(sg.sums_names):
        if ch.lower().startswith(channelname):
           ix = ich
           break
    if ix < 0:
        print('Cannot find Channel %s in file %s  ' % (channelname, fname))
        return

    chans = list(sg.sums_list[ix])
    chans.pop()
    chans = numpy.array(chans)
    dchans = chans - chans[0]

    fl_raw  = sg.det[chans].sum(axis=0)
    fl_corr = (sg.dt_factor[dchans] * sg.det[chans]).sum(axis=0)
    mufluor = fl_corr / i0
    sg.i0  = i0
    sg.fl_raw = fl_raw
    sg.fl_corr = fl_corr
    sg.mufluor = mufluor
    sg.energy = energy

    npts = len(energy)
    header = GSE_header_IDE[:]
    if 'BM' in sg.scan_prefix:
        header = GSE_header_BMD[:]

    i1, iref = None, None
    ncol, lref = 6, 'iref'
    labels = [l.lower() for l in sg.sums_names]
    if 'i1' in labels:
        i1 = sg.get_data('i1')
    if 'iref' in labels:
        iref = sg.get_data('iref')
    elif 'i2' in labels:
        iref = sg.get_data('i2')

    if i1 is not None:
        header.append('# Column.%i: itrans ' % ncol)
        ncol += 1
    if iref is not None:
        header.append('# Column.%i: irefer ' % ncol)
        ncol += 1
    buff = [l.strip() for l in header]
    buff.append("# Scan.start_time: %s" %
                iso8601_time(os.stat(fname).st_ctime))

    thead = ["# ///",
             "# summed %s fluorescence data from %s" % (channelname, fname),
             "# Dead-time correction applied",
             "#---------------------------------"]

    arrlabel = "# energy     mufluor    i0    fluor_corr   fluor_raw"
    if i1 is not None:
        arrlabel = "%s  itrans"  % arrlabel

    if iref is not None:
        arrlabel = "%s  irefer"  % arrlabel

    thead.append(arrlabel)
    buff.extend(thead)
    fmt = "   %.3f   %.5f   %.1f   %.5f   %.1f"
    for i in range(npts):
        dline = fmt % (energy[i],  mufluor[i], i0[i], fl_corr[i], fl_raw[i])
        if i1 is not None:
            dline = "%s  %.1f" % (dline, i1[i])
        if iref is not None:
            dline = "%s  %.1f" % (dline, iref[i])
        buff.append(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 sg
convert GSE ESCAN fluorescence XAFS scans to dead time corrected files
dataset/ETHPy150Open xraypy/xraylarch/plugins/io/gse_escan.py/gsescan_deadtime_correct