from __future__ import print_function,division import os import collections # prepare logging import logging logfname = os.path.splitext(__file__)[0] + ".log" logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%y-%m-%d %H:%M:%S', filename=logfname, filemode='w') import numpy as np import pylab as plt import mcutils as mc import mcutils.xray as xray from mcutils.xray import id9 #id9 = xray.id9 # use npz files (they can handle more stuff (list of arrays,unicode) than h5py) id9.default_extension = '.npz' #id9.default_extension = '.h5' g_default_mask = '../masks/fesalen1_run8_careful.edf' g_default_folder = "../fesalen/fesalen1/run%d" runsE = collections.OrderedDict() runsT = collections.OrderedDict() runsE[7] = 60; runsT[7] = 135 runsE[8] = 60; runsT[8] = 135 runsE[9] = 120; runsT[9] = 135 runsE[10] = 240; runsT[10] = 135 runsE[11] = 30; runsT[11] = 135 runsE[12] = 45; runsT[12] = 135 runsE[13] = 60; runsT[13] = 120 runsE[14] = 120; runsT[14] = 120 runsE[15] = 30; runsT[15] = 120 runs135=(11,12,8,9,10) runs120=(15,13,14) def prepareLog(): """ It allows printing to terminal on top of logfile """ # define a Handler which writes INFO messages or higher to the sys.stderr console = logging.StreamHandler() console.setLevel(logging.INFO) # set a format which is simpler for console use formatter = logging.Formatter('%(message)s') # tell the handler to use this format console.setFormatter(formatter) # add the handler to the root logger (if needed) if len(logging.getLogger('').handlers)==1: logging.getLogger('').addHandler(console) prepareLog() def defineCalibrants(dmin=3): c120 = xray.cell.Triclinic(a=10.297,b=10.716,c=13.955,alpha=72.22,beta=70.45,gamma=74.13) p120,_ = c120.reflection_list() c300 = xray.cell.Triclinic(a=10.485,b=11.015,c=14.280,alpha=74.61,beta=69.29,gamma=75.29) p300,_ = c300.reflection_list() return xray.storage.DataStorage( p120=p120, c120=c120,p300=p300,c300=c300) def readCalculations(folder="../fesalen/cif",force=False,plot=False): npz = folder + "/fesalen_calc.npz" if not os.path.isfile(npz) or force: ret = dict() for T in ("120LS","200HS","300HS"): # calculated with lambda = 1 Ang theta,i = np.loadtxt( folder+"/fesalen_%s_fwhm0.1.tsv"%T,unpack=True ) theta = theta/180*np.pi q = 4*np.pi/1*np.sin(theta/2) key = "T%s"%T ret[ key ] = dict( q = q, i = i ) ret = xray.storage.DataStorage(ret) ret.save(folder + "/fesalen_calc.npz" ) ret = xray.storage.read(npz) if plot: T = list(ret.keys()) T.sort() for i,t in enumerate(T): plt.plot( ret[t].q,ret[t].i, label=t)#,color=plt.cm.Blues((0.3+0.7*i/2)) ) plt.grid() plt.legend() plt.xlabel(r"q ($\AA^{-1}$)") return ret def findCenter(cen=(484,479.5),dist=0.251): files = xray.utils.getEdfFiles("../fesalen/fesalen1/run17/",nFiles=100) img = xray.azav.read(files).mean(axis=0) - 10. wavelength=12.398/18.*1e-10 # 1,0,0 peak are the one close to azimuth 0 and are at 0.6596Ang-1 at 120K # and 0.65098 at 300K (run 17 is @ RT) xray.azav.find_center(img,dist=dist,psize=0.0001770834,center=cen, reference=0.65098,wavelength=wavelength) def removeBaseline(folder,qlims=(0.5,2.5),max_iter=30,force=False): if isinstance(folder,int): folder = g_default_folder%folder fname = folder + "/pyfai_1d_nobaseline"+id9.default_extension if not os.path.isfile(fname) or force: log.info("Calling azav with default parameters") data = azav(folder,withBaseline=False) # first peak (around 0.6) is weak and requires special care ... data.q,data.data = xray.utils.removeBackground(data.q,data.data, xlims=qlims,max_iter=max_iter,background_regions=[ (0,0.58) ]) data.save(fname) else: data = xray.storage.read(fname) return data def azav(folder,nQ=3000,force=False,saveChi=True,mask=g_default_mask, withBaseline=True): if withBaseline: data=removeBaseline(folder) else: data = id9.doFolder_azav(folder,nQ=nQ,force=force,mask=mask, saveChi=saveChi) return data def datared(folder,monitor=(0.5,4),showPlot=True,errMask=5,chi2Mask=2, qlims=(0.5,3),withBaseline=False,forceDatared=False, select=slice(None),**kw): if isinstance(folder,int): folder = g_default_folder%folder data = azav(folder,withBaseline=withBaseline) extension = def_ext if withBaseline else "_nobaseline" + def_ext # storage files to use diffFile = folder + "/diffs" + extension if not os.path.isfile(diffFile) or forceDatared: data,diffs = id9.doFolder_dataRed(data,monitor=monitor, errMask=errMask,chi2Mask=chi2Mask,qlims=qlims, outStorageFile=diffFile) else: data = xray.storage.read(azavFile) diffs = xray.storage.read(diffFile) if showPlot: xray.utils.plotdiffs(diffs.q,diffs.data,t=diffs.scan,select=select, absSignal=diffs.dataAbsAvNeg,absSignalScale=10) plt.title(folder + " norm %s" % str(monitor)) plt.figure() xray.utils.plotdiffs(diffs.q,diffs.dataAsAbs,t=diffs.scan, select=select) plt.title(folder + " norm %s" % str(monitor)) return data,diffs def doall(folder,force=False,removeBaseline=True): azav(folder,force=force) return datared(folder) def _getAmplitudeFromSVD(i,comp=0): u,s,v = np.linalg.svd(i) return u[:,comp]*s[comp] def _getAmplitudeFromLinFit(i,ref=1): amp = [mc.linFit(i[ref],ii) for ii in i] return np.asarray(amp) def _getAmplitudeFromAbs(i,e): amp = np.abs(i).mean(axis=1) baseline = np.median(e) return amp-baseline def _getAmplitudeFromPeak(i): """ get max-min (to be less sensitive to baseline change """ amp = np.max(i,axis=1)-np.min(i,axis=1) return amp def anaAmplitue(run=12,plot=False,scaleWithE=False,withBaseline=False): folder = g_default_folder % run pars = dict( chi2Mask=1.5,errMask=3,monitor=(0.4,2.5),showPlot=False ) data,diffs=datared(folder,**pars,withBaseline=withBaseline) ranges = ( (0.66,0.72), (0.74,0.8), (0.82,0.88) ) #, (0.96,1.02 ) ) nPlot = len(ranges) if plot: fig,ax = plt.subplots(nPlot,1,sharex=True) amp = collections.OrderedDict() for num,r in enumerate(ranges): idx = (diffs.q>r[0]) & (diffs.qr[0]) & (diffs.q