def extract_chi(self): #print('chi ing') autobk(self.larch, group=self.larch, _larch=self._larch) self.chi = self.larch.chi self.bkg = self.larch.bkg self.kmin = self.larch.autobk_details.kmin self.kmax = self.larch.autobk_details.kmax
def extract_chi_force(self): if self.verbose: print('chi force reporting') # autobk(self.larch, group=self.larch, _larch=self._larch, e0=self.e0, kmin=self.kmin, kmax=self.kmax) autobk(self.larch, group=self.larch, _larch=self._larch, e0=self.e0, kmin=self.kmin, kmax=self.kmax, nclamp=2, clamp_hi=10) self.k = self.larch.k self.chi = self.larch.chi self.bkg = self.larch.bkg
def extract_chi(self): print('chi reporting') autobk(self.larch, group=self.larch, _larch=self._larch) self.chi = self.larch.chi self.bkg = self.larch.bkg self.kmin = self.larch.autobk_details.kmin self.kmax = self.larch.autobk_details.kmax self.nclamp = 2 self.rbkg = 1
def extract_chi_force(self): #print('chi force reporting') autobk(self.larch, group=self.larch, _larch=self._larch, e0=self.e0, kmin=self.kmin, kmax=self.kmax, rbkg=self.rbkg, clamp_lo=self.clamp_lo, clamp_hi=self.clamp_hi) # autobk(self.larch, group=self.larch, _larch=self._larch, e0=self.e0, kmin=self.kmin, kmax=self.kmax, # nclamp=2, clamp_hi=10) self.k = self.larch.k self.chi = self.larch.chi self.bkg = self.larch.bkg
def EXAFS_EX(self, rbkg=1, nknots=None, e0=None, edge_step=None, kmin=0, kmax=None, kweight=1, dk=0.1, win='hanning', k_std=None, chi_std=None, nfft=2048, kstep=0.05, pre_edge_kws=None, nclamp=4, clamp_lo=1, clamp_hi=1, calc_uncertainties=False, **kws): """Use Autobk algorithm to remove XAFS background from larch Parameters: ----------- rbkg: distance (in Ang) for chi(R) above which the signal is ignored. Default = 1. e0: edge energy, in eV. If None, it will be determined. edge_step: edge step. If None, it will be determined. pre_edge_kws: keyword arguments to pass to pre_edge() nknots: number of knots in spline. If None, it will be determined. kmin: minimum k value [0] kmax: maximum k value [full data range]. kweight: k weight for FFT. [1] dk: FFT window window parameter. [0] win: FFT window function name. ['hanning'] nfft: array size to use for FFT [2048] kstep: k step size to use for FFT [0.05] k_std: optional k array for standard chi(k). chi_std: optional chi array for standard chi(k). nclamp: number of energy end-points for clamp [2] clamp_lo: weight of low-energy clamp [1] clamp_hi: weight of high-energy clamp [1] calc_uncertaintites: Flag to calculate uncertainties in mu_0(E) and chi(k) [False] ----------- # if e0 or edge_step are not specified, get them, either from the # passed-in group or from running pre_edge() Output arrays are written to the provided group""" args = dict(locals()) del args["self"], args["kws"] xafs.autobk(self, _larch=self._larch, **args) return
def run_autobk(Energy,Ut,E0,Rbkg,Kweight,k_min,k_max,fit_s,fit_e,nor_aE0_s,nor_aE0_e,pre_type): exafs = larch_builtins._group(mylarch) ft = larch_builtins._group(mylarch) Pre_edge_kws = {'pre1':fit_s-E0,'pre2':fit_e-E0,'norm1':nor_aE0_s-E0,'norm2':nor_aE0_e-E0} if pre_type == 2: Pre_edge_kws['nvict']=4 else: pass autobk(Energy,mu=Ut,e0=E0,rbkg=Rbkg,kmin=k_min, kmax=k_max,kweight=Kweight,pre_edge_kws=Pre_edge_kws,group=exafs,_larch=mylarch) #print larch_builtins._groupitems(exafs,mylarch) xftf(exafs.k,exafs.chi,group=ft,kweight=3,kmin=3.0,kmax=12.0,_larch=mylarch) k_l = math.sqrt(0.2626*(nor_aE0_s-E0)) k_h = math.sqrt(0.2626*(nor_aE0_e-E0)) mask = np.concatenate([np.zeros(len(exafs.k[0:find_near(exafs.k,k_min)])), np.ones(len(exafs.k[find_near(exafs.k,k_min):find_near(exafs.k,k_max)+1])), np.zeros(len(exafs.k[find_near(exafs.k,k_max)+1:]))]) return exafs.bkg, exafs.pre_edge, exafs.post_edge, exafs.chi, exafs.k, ft.r, ft.chir_mag, ft.chir_im
def EXAFS_EX(self, rbkg=1, nknots=None, e0=None, edge_step=None, kmin=0, kmax=None, kweight=1, dk=0.1, win='hanning', k_std=None, chi_std=None, nfft=2048, kstep=0.05, pre_edge_kws=None, nclamp=4, clamp_lo=1, clamp_hi=1, calc_uncertainties=False, **kws): """Use Autobk algorithm to remove XAFS background from larch Parameters: ----------- rbkg: distance (in Ang) for chi(R) above which the signal is ignored. Default = 1. e0: edge energy, in eV. If None, it will be determined. edge_step: edge step. If None, it will be determined. pre_edge_kws: keyword arguments to pass to pre_edge() nknots: number of knots in spline. If None, it will be determined. kmin: minimum k value [0] kmax: maximum k value [full data range]. kweight: k weight for FFT. [1] dk: FFT window window parameter. [0] win: FFT window function name. ['hanning'] nfft: array size to use for FFT [2048] kstep: k step size to use for FFT [0.05] k_std: optional k array for standard chi(k). chi_std: optional chi array for standard chi(k). nclamp: number of energy end-points for clamp [2] clamp_lo: weight of low-energy clamp [1] clamp_hi: weight of high-energy clamp [1] calc_uncertaintites: Flag to calculate uncertainties in mu_0(E) and chi(k) [False] ----------- # if e0 or edge_step are not specified, get them, either from the # passed-in group or from running pre_edge() Output arrays are written to the provided group""" args=dict(locals()) del args["self"],args["kws"] xafs.autobk(self, _larch=self._larch,**args) return
def read_athena(filename, match=None, do_preedge=True, do_bkg=True, do_fft=True, use_hashkey=False, _larch=None): """read athena project file returns a Group of Groups, one for each Athena Group in the project file Arguments: filename (string): name of Athena Project file match (sring): pattern to use to limit imported groups (see Note 1) do_preedge (bool): whether to do pre-edge subtraction [True] do_bkg (bool): whether to do XAFS background subtraction [True] do_fft (bool): whether to do XAFS Fast Fourier transform [True] use_hashkey (bool): whether to use Athena's hash key as the group name instead of the Athena label [False] Returns: group of groups each named according the label used by Athena. Notes: 1. To limit the imported groups, use the pattern in `match`, using '*' to match 'all' '?' to match any single character, or [sequence] to match any of a sequence of letters. The match will always be insensitive to case. 3. do_preedge, do_bkg, and do_fft will attempt to reproduce the pre-edge, background subtraction, and FFT from Athena by using the parameters saved in the project file. 2. use_hashkey=True will name groups from the internal 5 character string used by Athena, instead of the group label. Example: 1. read in all groups from a project file: cr_data = read_athena('My Cr Project.prj') 2. read in only the "merged" data from a Project, and don't do FFT: zn_data = read_athena('Zn on Stuff.prj', match='*merge*', do_fft=False) """ from larch_plugins.xafs import pre_edge, autobk, xftf if not os.path.exists(filename): raise IOError("%s '%s': cannot find file" % (ERR_MSG, filename)) try: fh = GzipFile(filename) lines = [bytes2str(t) for t in fh.readlines()] fh.close() except: raise ValueError("%s '%s': invalid gzip file" % (ERR_MSG, filename)) athenagroups = [] dat = {"name": ""} Athena_version = None vline = lines.pop(0) if "Athena project file -- Demeter version" not in vline: raise ValueError("%s '%s': invalid Athena File" % (ERR_MSG, filename)) major, minor, fix = "0", "0", "0" try: vs = vline.split("Athena project file -- Demeter version")[1] major, minor, fix = vs.split(".") except: raise ValueError("%s '%s': cannot read version" % (ERR_MSG, filename)) if int(minor) < 9 or int(fix[:2]) < 21: raise ValueError("%s '%s': file is too old to read" % (ERR_MSG, filename)) for t in lines: if t.startswith("#") or len(t) < 2: continue key = t.split(" ")[0].strip() key = key.replace("$", "").replace("@", "") if key == "old_group": dat["name"] = perl2json(t) elif key == "[record]": athenagroups.append(dat) dat = {"name": ""} elif key == "args": dat["args"] = perl2json(t) elif key in ("x", "y", "i0"): dat[key] = np.array([float(x) for x in perl2json(t)]) if match is not None: match = match.lower() out = Group() out.__doc__ = """XAFS Data from Athena Project File %s""" % (filename) for dat in athenagroups: label = dat["name"] this = Group( athena_id=label, energy=dat["x"], mu=dat["y"], bkg_params=Group(), fft_params=Group(), athena_params=Group() ) if "i0" in dat: this.i0 = dat["i0"] if "args" in dat: for i in range(len(dat["args"]) // 2): key = dat["args"][2 * i] val = dat["args"][2 * i + 1] if key.startswith("bkg_"): setattr(this.bkg_params, key[4:], val) elif key.startswith("fft_"): setattr(this.fft_params, key[4:], val) elif key == "label": this.label = val if not use_hashkey: label = this.label else: setattr(this.athena_params, key, val) this.__doc__ = """Athena Group Name %s (key='%s')""" % (label, dat["name"]) olabel = fix_varname(label) if match is not None: if not fnmatch(olabel.lower(), match): continue if do_preedge or do_bkg: pars = this.bkg_params pre_edge( this, _larch=_larch, e0=float(pars.e0), pre1=float(pars.pre1), pre2=float(pars.pre2), norm1=float(pars.nor1), norm2=float(pars.nor2), nnorm=float(pars.nnorm) - 1, make_flat=bool(pars.flatten), ) if do_bkg and hasattr(pars, "rbkg"): autobk( this, _larch=_larch, e0=float(pars.e0), rbkg=float(pars.rbkg), kmin=float(pars.spl1), kmax=float(pars.spl2), kweight=float(pars.kw), dk=float(pars.dk), clamp_lo=float(pars.clamp1), clamp_hi=float(pars.clamp2), ) if do_fft: pars = this.fft_params kweight = 2 if hasattr(pars, "kw"): kweight = float(pars.kw) xftf( this, _larch=_larch, kmin=float(pars.kmin), kmax=float(pars.kmax), kweight=kweight, window=pars.kwindow, dk=float(pars.dk), ) setattr(out, olabel, this) return out
#!/usr/bin/env python ## Autobk (XAFS background subtraction) in pure Python, ## using Python code from Lxsarch. from larch import Interpreter from larch_plugins.xafs import pre_edge, autobk from larch_plugins.io import read_ascii # create plain interpreter, don't load all the plugins _larch = Interpreter(with_plugins=False) fname = '../xafsdata/cu_rt01.xmu' cu = read_ascii(fname, labels='energy mu i0', _larch=_larch) print( 'Read ASCII File:', cu) print( dir(cu)) pre_edge(cu, _larch=_larch) print( 'After pre-edge:') print( dir(cu)) autobk(cu, rbkg=1.0, kweight=1, _larch=_larch) print( 'After autobk:') print( dir(cu))
def larch_init(CSV_sub, params): r""" Larch initialization for data analysis Inputs: CSV_sub (str): files location of the data files (CSV/XMU) params (dics): dicts contain all parameters """ global intervalK global best global KMIN global KMAX global KWEIGHT global g # Kmin = params['Kmin'] Kmax = params['Kmax'] deltak = params['deltak'] BIG = int(Kmax / deltak) SMALL = int(Kmin / deltak) MID = int(BIG - SMALL + 1) RBKG = params['rbkg'] KWEIGHT = params['kweight'] KMIN = Kmin KMAX = Kmax BKGKW = params['bkgkw'] # cu = 1 hfal2 = 2.0 BKGKMAX = params['bkgkmax'] # cu = 25, hfal2 = 15 # print(base) CSV_PATH = os.path.join(base, CSV_sub) g = read_ascii(CSV_PATH) best = read_ascii(CSV_PATH, ) sumgroup = read_ascii(CSV_PATH) # back ground subtraction using autobk # data kweight try: g.chi except AttributeError: autobk(g, rbkg=RBKG, kweight=BKGKW, kmax=BKGKMAX, _larch=mylarch) autobk(best, rbkg=RBKG, _larch=mylarch) autobk(sumgroup, rbkg=RBKG, _larch=mylarch) intervalK = (np.linspace(SMALL, BIG, MID)).tolist() '''chang''' xftf(g.k, g.chi, kmin=KMIN, kmax=KMAX, dk=4, window='hanning', kweight=KWEIGHT, group=g, _larch=mylarch) xftf(best.k, best.chi, kmin=KMIN, kmax=KMAX, dk=4, window='hanning', kweight=KWEIGHT, group=best, _larch=mylarch) xftf(sumgroup.k, sumgroup.chi, kmin=KMIN, kmax=KMAX, dk=4, window='hanning', kweight=KWEIGHT, group=sumgroup, _larch=mylarch) '''chang end''' exp = g.chi # params = {} # params['Kmin'] = Kmin # params['Kmax'] = Kmax params['SMALL'] = SMALL params['BIG'] = BIG params['intervalK'] = intervalK return exp, g, params, mylarch
def read_athena(filename, match=None, do_preedge=True, do_bkg=True, do_fft=True, use_hashkey=False, _larch=None): """read athena project file returns a Group of Groups, one for each Athena Group in the project file Arguments: filename (string): name of Athena Project file match (string): pattern to use to limit imported groups (see Note 1) do_preedge (bool): whether to do pre-edge subtraction [True] do_bkg (bool): whether to do XAFS background subtraction [True] do_fft (bool): whether to do XAFS Fast Fourier transform [True] use_hashkey (bool): whether to use Athena's hash key as the group name instead of the Athena label [False] Returns: group of groups each named according the label used by Athena. Notes: 1. To limit the imported groups, use the pattern in `match`, using '*' to match 'all' '?' to match any single character, or [sequence] to match any of a sequence of letters. The match will always be insensitive to case. 3. do_preedge, do_bkg, and do_fft will attempt to reproduce the pre-edge, background subtraction, and FFT from Athena by using the parameters saved in the project file. 2. use_hashkey=True will name groups from the internal 5 character string used by Athena, instead of the group label. Example: 1. read in all groups from a project file: cr_data = read_athena('My Cr Project.prj') 2. read in only the "merged" data from a Project, and don't do FFT: zn_data = read_athena('Zn on Stuff.prj', match='*merge*', do_fft=False) """ from larch_plugins.xafs import pre_edge, autobk, xftf if not os.path.exists(filename): raise IOError("%s '%s': cannot find file" % (ERR_MSG, filename)) try: fh = GzipFile(filename) lines = [bytes2str(t) for t in fh.readlines()] fh.close() except: raise ValueError("%s '%s': invalid gzip file" % (ERR_MSG, filename)) athenagroups = [] dat = {'name': ''} Athena_version = None vline = lines.pop(0) if "Athena project file -- Demeter version" not in vline: raise ValueError("%s '%s': invalid Athena File" % (ERR_MSG, filename)) major, minor, fix = '0', '0', '0' try: vs = vline.split("Athena project file -- Demeter version")[1] major, minor, fix = vs.split('.') except: raise ValueError("%s '%s': cannot read version" % (ERR_MSG, filename)) if int(minor) < 9 or int(fix[:2]) < 21: raise ValueError("%s '%s': file is too old to read" % (ERR_MSG, filename)) for t in lines: if t.startswith('#') or len(t) < 2 or 'undef' in t: continue key = t.split(' ')[0].strip() key = key.replace('$', '').replace('@', '') if key == 'old_group': dat['name'] = perl2json(t) elif key == '[record]': athenagroups.append(dat) dat = {'name': ''} elif key == 'args': dat['args'] = perl2json(t) elif key in ('x', 'y', 'i0', 'signal'): dat[key] = np.array([float(x) for x in perl2json(t)]) if match is not None: match = match.lower() out = Group() out.__doc__ = """XAFS Data from Athena Project File %s""" % (filename) for dat in athenagroups: label = dat.get('name', 'unknown') this = Group(athena_id=label, energy=dat['x'], mu=dat['y'], bkg_params=Group(), fft_params=Group(), athena_params=Group()) if 'i0' in dat: this.i0 = dat['i0'] if 'args' in dat: for i in range(len(dat['args']) // 2): key = dat['args'][2 * i] val = dat['args'][2 * i + 1] if key.startswith('bkg_'): setattr(this.bkg_params, key[4:], val) elif key.startswith('fft_'): setattr(this.fft_params, key[4:], val) elif key == 'label': this.label = val if not use_hashkey: label = this.label else: setattr(this.athena_params, key, val) this.__doc__ = """Athena Group Name %s (key='%s')""" % (label, dat['name']) olabel = fix_varname(label) if match is not None: if not fnmatch(olabel.lower(), match): continue if do_preedge or do_bkg: pars = this.bkg_params pre_edge(this, _larch=_larch, e0=float(pars.e0), pre1=float(pars.pre1), pre2=float(pars.pre2), norm1=float(pars.nor1), norm2=float(pars.nor2), nnorm=float(pars.nnorm) - 1, make_flat=bool(pars.flatten)) if do_bkg and hasattr(pars, 'rbkg'): autobk(this, _larch=_larch, e0=float(pars.e0), rbkg=float(pars.rbkg), kmin=float(pars.spl1), kmax=float(pars.spl2), kweight=float(pars.kw), dk=float(pars.dk), clamp_lo=float(pars.clamp1), clamp_hi=float(pars.clamp2)) if do_fft: pars = this.fft_params kweight = 2 if hasattr(pars, 'kw'): kweight = float(pars.kw) xftf(this, _larch=_larch, kmin=float(pars.kmin), kmax=float(pars.kmax), kweight=kweight, window=pars.kwindow, dk=float(pars.dk)) setattr(out, olabel, this) return out
10067.28 120488.7 89110.0998902 0.30168329 10073.72 118833.7 88265.1000656 0.29738025 10080.18 118434.7 88372.1004302 0.29280544 10086.66 117995.7 88449.0998063 0.28822094 10093.17 118435.7 89180.0997098 0.28371228 10099.69 117303.7 88720.0998253 0.27927983 10106.22 117929.7 89581.1003571 0.27494432 10112.78 116857.7 89144.1003332 0.27070279 10119.36 115791.7 88718.1000129 0.26632896 10125.96 111467.7 85797.099695 0.26174966 10132.57 110079.7 85128.099834 0.25704747 10139.21 104190.7 80953.0999403 0.2523529 10145.86 93726.7 73074.0996945 0.24890911''' raw_data_lines = raw_data.split('\n') raw_data_table = [] for line in raw_data_lines: raw_data_table.append(list(map(lambda x: float(x), line.strip().split()))) table = np.array(raw_data_table) group.energy = table[:, 0] group.mu = table[:, 3] e0 = find_e0(group, _larch=mylarch) pre_edge(group, _larch=mylarch) autobk(group, _larch=mylarch) xftf(group, _larch=mylarch) xftr(group, _larch=mylarch)
# create an empty Group xafsdat = Group() # read data (here using np.loadtxt), stick into Group # using "expected names" for XAFS data rawdata = np.loadtxt('../xafsdata/fe2o3_rt1.xmu') xafsdat.energy = rawdata[:, 0] xafsdat.mu = rawdata[:, 1] xafsdat.i0 = rawdata[:, 1] # run autobk on the xafsdat Group, including a larch Interpreter.... # note that this expects 'energy' and 'mu' to be in xafsdat, and will # write data for 'k', 'chi', 'kwin', 'e0', ... into xafsdat autobk(xafsdat, rbkg=1.0, kweight=2, _larch=my_larch) # Fourier transform to R space, again passing in a Group (here, # 'k' and 'chi' are expected, and writitng out 'r', 'chir_mag', # and so on xftf(xafsdat, kmin=2, kmax=15, dk=3, kweight=2, _larch=my_larch) # # plot grid of results: # mu + bkg pylab.subplot(2, 2, 1) pylab.plot(xafsdat.energy, xafsdat.bkg, 'r--') pylab.plot(xafsdat.energy, xafsdat.mu) pylab.xlabel('Energy (eV)') # normalized XANES
def read(self, filename=None, match=None, do_preedge=True, do_bkg=True, do_fft=True, use_hashkey=False): """ read Athena project to group of groups, one for each Athena dataset in the project file. This supports both gzipped and unzipped files and old-style perl-like project files and new-style JSON project files Arguments: filename (string): name of Athena Project file match (string): pattern to use to limit imported groups (see Note 1) do_preedge (bool): whether to do pre-edge subtraction [True] do_bkg (bool): whether to do XAFS background subtraction [True] do_fft (bool): whether to do XAFS Fast Fourier transform [True] use_hashkey (bool): whether to use Athena's hash key as the group name instead of the Athena label [False] Returns: None, fills in attributes `header`, `journal`, `filename`, `groups` Notes: 1. To limit the imported groups, use the pattern in `match`, using '*' to match 'all', '?' to match any single character, or [sequence] to match any of a sequence of letters. The match will always be insensitive to case. 3. do_preedge, do_bkg, and do_fft will attempt to reproduce the pre-edge, background subtraction, and FFT from Athena by using the parameters saved in the project file. 2. use_hashkey=True will name groups from the internal 5 character string used by Athena, instead of the group label. Example: 1. read in all groups from a project file: cr_data = read_athena('My Cr Project.prj') 2. read in only the "merged" data from a Project, and don't do FFT: zn_data = read_athena('Zn on Stuff.prj', match='*merge*', do_fft=False) """ if filename is not None: self.filename = filename if not os.path.exists(self.filename): raise IOError("%s '%s': cannot find file" % (ERR_MSG, self.filename)) from larch_plugins.xafs import pre_edge, autobk, xftf if not os.path.exists(filename): raise IOError("file '%s' not found" % filename) text = _read_raw_athena(filename) # failed to read: if text is None: raise OSError(errval) if not _test_athena_text(text): raise ValueError("%s '%s': invalid Athena File" % (ERR_MSG, filename)) # decode JSON or Perl format data = None try: data = parse_jsonathena(text, self.filename) except ValueError: # try as perl format # print("Not json-athena ", sys.exc_info()) try: data = parse_perlathena(text, self.filename) except: # print("Not perl-athena ", sys.exc_info()) pass if data is None: raise ValueError("cannot read file '%s' as Athena Project File" % (self.filename)) self.header = data.header self.journal = data.journal self.group_names = data.group_names for gname in data.group_names: oname = gname if match is not None: if not fnmatch(gname.lower(), match): continue this = getattr(data, gname) if use_hashkey: oname = this.athena_id if (do_preedge or do_bkg) and (self._larch is not None): pars = this.bkg_params pre_edge(this, e0=float(pars.e0), pre1=float(pars.pre1), pre2=float(pars.pre2), norm1=float(pars.nor1), norm2=float(pars.nor2), nnorm=float(pars.nnorm), make_flat=bool(pars.flatten), _larch=self._larch) if do_bkg and hasattr(pars, 'rbkg'): autobk(this, _larch=self._larch, e0=float(pars.e0), rbkg=float(pars.rbkg), kmin=float(pars.spl1), kmax=float(pars.spl2), kweight=float(pars.kw), dk=float(pars.dk), clamp_lo=float(pars.clamp1), clamp_hi=float(pars.clamp2)) if do_fft: pars = this.fft_params kweight=2 if hasattr(pars, 'kw'): kweight = float(pars.kw) xftf(this, _larch=self._larch, kmin=float(pars.kmin), kmax=float(pars.kmax), kweight=kweight, window=pars.kwindow, dk=float(pars.dk)) self.groups[oname] = this
def read(self, filename=None, match=None, do_preedge=True, do_bkg=True, do_fft=True, use_hashkey=False): """ read Athena project to group of groups, one for each Athena dataset in the project file. This supports both gzipped and unzipped files and old-style perl-like project files and new-style JSON project files Arguments: filename (string): name of Athena Project file match (string): pattern to use to limit imported groups (see Note 1) do_preedge (bool): whether to do pre-edge subtraction [True] do_bkg (bool): whether to do XAFS background subtraction [True] do_fft (bool): whether to do XAFS Fast Fourier transform [True] use_hashkey (bool): whether to use Athena's hash key as the group name instead of the Athena label [False] Returns: None, fills in attributes `header`, `journal`, `filename`, `groups` Notes: 1. To limit the imported groups, use the pattern in `match`, using '*' to match 'all', '?' to match any single character, or [sequence] to match any of a sequence of letters. The match will always be insensitive to case. 3. do_preedge, do_bkg, and do_fft will attempt to reproduce the pre-edge, background subtraction, and FFT from Athena by using the parameters saved in the project file. 2. use_hashkey=True will name groups from the internal 5 character string used by Athena, instead of the group label. Example: 1. read in all groups from a project file: cr_data = read_athena('My Cr Project.prj') 2. read in only the "merged" data from a Project, and don't do FFT: zn_data = read_athena('Zn on Stuff.prj', match='*merge*', do_fft=False) """ if filename is not None: self.filename = filename if not os.path.exists(self.filename): raise IOError("%s '%s': cannot find file" % (ERR_MSG, self.filename)) from larch_plugins.xafs import pre_edge, autobk, xftf if not os.path.exists(filename): raise IOError("file '%s' not found" % filename) text = _read_raw_athena(filename) # failed to read: if text is None: raise OSError(errval) if not _test_athena_text(text): raise ValueError("%s '%s': invalid Athena File" % (ERR_MSG, filename)) # decode JSON or Perl format data = None try: data = parse_jsonathena(text, self.filename) except ValueError: # try as perl format # print("Not json-athena ", sys.exc_info()) try: data = parse_perlathena(text, self.filename) except: # print("Not perl-athena ", sys.exc_info()) pass if data is None: raise ValueError("cannot read file '%s' as Athena Project File" % (self.filename)) self.header = data.header self.journal = data.journal self.group_names = data.group_names for gname in data.group_names: oname = gname if match is not None: if not fnmatch(gname.lower(), match): continue this = getattr(data, gname) if use_hashkey: oname = this.athena_id if (do_preedge or do_bkg) and (self._larch is not None): pars = this.bkg_params pre_edge(this, e0=float(pars.e0), pre1=float(pars.pre1), pre2=float(pars.pre2), norm1=float(pars.nor1), norm2=float(pars.nor2), nnorm=float(pars.nnorm), make_flat=bool(pars.flatten), _larch=self._larch) if do_bkg and hasattr(pars, 'rbkg'): autobk(this, _larch=self._larch, e0=float(pars.e0), rbkg=float(pars.rbkg), kmin=float(pars.spl1), kmax=float(pars.spl2), kweight=float(pars.kw), dk=float(pars.dk), clamp_lo=float(pars.clamp1), clamp_hi=float(pars.clamp2)) if do_fft: pars = this.fft_params kweight = 2 if hasattr(pars, 'kw'): kweight = float(pars.kw) xftf(this, _larch=self._larch, kmin=float(pars.kmin), kmax=float(pars.kmax), kweight=kweight, window=pars.kwindow, dk=float(pars.dk)) self.groups[oname] = this
# get mu # get energy # plot groups # merge groups # plot merge # save diagrams # save all as athena project groups = [] for time_stmp, data_ln in zip(group_vals['time'], group_vals['data']): new_group = larch.Group() new_group.mu = data_ln new_group.energy = group_vals['energy'] # run autobk on the xafsdat Group, including a larch Interpreter.... # note that this expects 'energy' and 'mu' to be in xafsdat, and will # write data for 'k', 'chi', 'kwin', 'e0', ... into xafsdat autobk(new_group, rbkg=1.0, kweight=2, _larch=my_larch) # Fourier transform to R space, again passing in a Group (here, # 'k' and 'chi' are expected, and writitng out 'r', 'chir_mag', # and so on xftf(new_group, kmin=2, kmax=15, dk=3, kweight=2, _larch=my_larch) new_group.label = f"Reading at {time_stmp}" # add group to list groups.append(new_group) # plot and save each file in group basic_plot(new_group, save_dir) # save energy v normalised mu