From 55a44039713a41bde03de421f4e4863c7cc8a595 Mon Sep 17 00:00:00 2001 From: Marco Cammarata Date: Fri, 13 Jan 2017 14:51:48 +0100 Subject: [PATCH] update salen example --- xray/example_main_salen.py | 346 ++++++++++++++++++++++++++++++++----- 1 file changed, 300 insertions(+), 46 deletions(-) diff --git a/xray/example_main_salen.py b/xray/example_main_salen.py index e130607..bbc41df 100644 --- a/xray/example_main_salen.py +++ b/xray/example_main_salen.py @@ -1,6 +1,6 @@ from __future__ import print_function,division import os - +import collections # prepare logging import logging @@ -17,15 +17,28 @@ import mcutils.xray as xray from mcutils.xray import id9 #id9 = xray.id9 -from dualtree import dualtree - # 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 @@ -35,56 +48,110 @@ def prepareLog(): formatter = logging.Formatter('%(message)s') # tell the handler to use this format console.setFormatter(formatter) - # add the handler to the root logger - logging.getLogger('').addHandler(console) + # add the handler to the root logger (if needed) + if len(logging.getLogger('').handlers)==1: + logging.getLogger('').addHandler(console) prepareLog() -def findCenter(): - files = xray.utils.getEdfFiles("../fesalen/fesalen1/run8/",nFiles=100) +def defineCalibrants(dmin=3): + from pyFAI import calibrant + c120 = calibrant.Cell(a=10.297,b=10.716,c=13.955,alpha=72.22,beta=70.45,gamma=74.13) + p120 = np.pi*2/ np.asarray( list(c120.d_spacing(dmin=dmin).keys()) ).astype(float) + p120.sort() + c300 = calibrant.Cell(a=10.485,b=11.015,c=14.280,alpha=74.61,beta=69.29,gamma=75.29) + p300 = np.pi*2/ np.asarray( list(c300.d_spacing(dmin=dmin).keys()) ).astype(float) + p300.sort() + return xray.storage.DataStorage( dict(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.exists(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. - xray.azav.find_center(img) + 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 azav(folder,nQ=1500,force=False,saveChi=True,mask=g_default_mask): + + +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.exists(fname) or force: + data = azav(folder,doRemoveBaseline=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, + doRemoveBaseline=True): + if isinstance(folder,int): folder = g_default_folder%folder if isinstance(mask,int): files = xray.utils.getFiles(folder,"*.edf*") img = xray.azav.pyFAIread(files[0]) temp = np.ones_like(img,dtype=bool) temp[:mask] = False mask = temp - return id9.doFolder_azav(folder,nQ=nQ,force=force,mask=mask,saveChi=saveChi) - - -def removeBaseline(folder,qlims=(0.6,3),max_iter=30): - data = azav(folder) - idx = (data.q>qlims[0]) & (data.qr[0]) & (data.qr[0]) & (diffs.qr[0]) & (diffs.q