import logging log = logging.getLogger(__name__) import os import collections import numpy as np from . import azav from . import dataReduction from . import utils from . import storage from . import filters default_extension = ".npz" def _conv(x): try: x = float(x) except: x = np.nan return x def _readDiagnostic(fname,retry=3): ntry = 0 while ntry1: log.warn("Found more than one *.log file that is not diagnostics.log: %s"%files) return logfile def readLogFile(fnameOrFolder,subtractDark=False): """ read id9 style logfile """ if os.path.isdir(fnameOrFolder): fname = findLogFile(fnameOrFolder) else: fname = fnameOrFolder f = open(fname,"r") lines = f.readlines() f.close() lines = [line.strip() for line in lines] darks = {} for line in lines: if line.find("pd1 dark/sec")>=0: darks['pd1ic'] = _findDark(line) if line.find("pd2 dark/sec")>=0: darks['pd2ic'] = _findDark(line) if line.find("pd3 dark/sec")>=0: darks['pd3ic'] = _findDark(line) if line.find("pd4 dark/sec")>=0: darks['pd4ic'] = _findDark(line) for iline,line in enumerate(lines): if line.lstrip()[0] != "#": break data=np.genfromtxt(fname,skip_header=iline-1,names=True,comments="%",dtype=None,converters = {'delay': lambda s: _delayToNum(s)}) if subtractDark: for diode in ['pd1ic','pd2ic','pd3ic','pd4ic']: if diode in darks: data[diode]=data[diode]-darks[diode]*data['timeic'] return data def doFolder_azav(folder,nQ=1500,files='*.edf*',force=False,mask=None, saveChi=True,poni='pyfai.poni',storageFile='auto',dark=9.9,zingerFilter=30,qlims=(0,10), removeBack=False,removeBack_kw=dict()): """ very small wrapper around azav.doFolder, essentially just reading the diagnostics.log """ diag = dict( delays = readDelayFromDiagnostic(folder) ) if storageFile == 'auto' : storageFile = folder + "/" + "pyfai_1d" + default_extension data = azav.doFolder(folder,files=files,nQ=nQ,force=force,mask=mask, saveChi=saveChi,poni=poni,storageFile=storageFile,diagnostic=diag,dark=dark,save=False) #try: # if removeBack is not None: # _,data.data = azav.removeBackground(data,qlims=qlims,**removeBack_kw) #except Exception as e: # log.error("Could not remove background, error was %s"%(str(e))) if zingerFilter > 0: data.data = filters.removeZingers(data.data,threshold=zingerFilter) #data.save(storageFile); it does not save err ? # idx = utils.findSlice(data.q,qlims) # n = np.nanmean(data.data[:,idx],axis=1) # data.norm_range = qlims # data.norm = n # n = utils.reshapeToBroadcast(n,data.data) # data.data_norm = data.data/n data.save(storageFile) return data def doFolder_dataRed(azavStorage,monitor=None,funcForAveraging=np.nanmean, qlims=None,outStorageFile='auto',reference='min'): """ azavStorage if a DataStorage instance or the filename to read """ if isinstance(azavStorage,storage.DataStorage): data = azavStorage folder = azavStorage.folder elif os.path.isfile(azavStorage): folder = os.path.dirname(azavStorage) data = storage.DataStorage(azavStorage) else: # assume is just a folder name folder = azavStorage azavStorage = folder + "/pyfai_1d" + default_extension data = storage.DataStorage(azavStorage) #assert data.q.shape[0] == data.data.shape[1] == data.err.shape[1] if qlims is not None: idx = (data.q>qlims[0]) & (data.q