def binning(magic_channel,ylow,yhigh,spm3path,tifpath,fileroot,scannum,*omitfile): # """ # This will rebin tif files using user input parameters. # binning(magic_channel,ylow,yhigh,spm3path,tifpath,fileroot,scannum,*omitfile): # binning(126,50,210,'C:\Users\Slot7_Kourtis','C:\Users\slot7','21Nov2021',338,'C:\Users\omit.txt') # The last input, omit.txt, is optional. # magic_channel: focal channel of the elastic line on the lambda detector # ylow, yhigh: where the summation on the lambda begins and ends in the analyzer chi direction # spm3path: path giving location of spm3 file # tifpath: path giving location of tif files generated by lambda # fileroot: root name of file, is used to generate spm3 and tif file names # scannum: scan number to be rebinned # omitfile: xy coordinates of lambda pixels to be omitted from scan, if any. # If a pixel is permanently on or wonky in any way this is a way to exclude it from the data # Output two columns: Energy and spectra # """ from os import listdir from os.path import isfile, join import matplotlib.pyplot as plt import numpy as np from PIL import Image import re #import matplotlib.pyplot as plt #define region of interest horizontally # Do some calculations # AnalyzerProperties ################## # Read EB from spm3 file also first and last energies of the scan, Ei and Ef tifstring='_scan'+str(scannum)+'_' tiffiles=[] for f in listdir(tifpath): if isfile(join(tifpath,f)) and (tifstring in f) and (fileroot in f): tiffiles.append(f) if len(tiffiles)==0: print('Tif files not found\n') ##################### spm3file=spm3path+'\\'+fileroot+'.spm3' try: f=open(spm3file,'r') except IOError: print("Error: spm3 File does not appear to exist") print("Was looking for "+spm3file) foundscan=0 foundEi=0 for line in f: if line.startswith('#S '+str(scannum)): foundscan=1 print('found the scan '+ spm3file) continue if foundscan: if line.startswith('#C Analyzer EB'): splits=line.split(' '); EB=float(splits[-1]) print('EB is ',EB) continue if line.startswith('#C Rowland radius'): splits=line.split(' '); R=float(splits[-1]) print('R is ',R) continue if line.startswith(tuple('0123456789')) and foundEi==0: splits=line.split(' ') Ei=float(splits[0]) foundEi=1 continue if line=='\n' and foundEi==1: break if foundEi==1: splits=line.split(' ') Ef=float(splits[0]) f.close() ################## EB=EB*1e3 # backscattering energy in eV RefE=1e3*(Ei+Ef)/2 # Energy in eV thetaB=np.arcsin(EB/(RefE)); dEdr=(1/np.tan(thetaB))*RefE/(2*R) #eV/mm micronsperchannel=25 #microns mmperchannel=micronsperchannel/1e3 dEdchan=dEdr*mmperchannel/1e3 #keV/channel xwidth=np.ceil(1.5*1e3/micronsperchannel) #number of channels for a 1.5 mm analyzer # Create the energy array Energy=np.arange(Ei,Ef,(Ef-Ei)/len(tiffiles)) # create bin edges dE=Energy[1]-Energy[0] binedges=Energy-dE/2 binedges=np.append(binedges,Energy[-1]+dE/2) # deltaE is relative energy of the columns on the lambda (strips on mythen) deltaE=(-1)*np.arange(-int(np.floor(xwidth/2)),int(np.floor(xwidth/2)))*dEdchan # scan is initializing a scan in the correct length scan=np.zeros(Energy.shape) A=np.zeros(Energy.shape) xomit=np.empty((0,0),int) yomit=np.empty((0,0),int) if omitfile: print('there is a file of points that should be omitted\n') try: ff=open(omitfile[0]) except IOError: print("Error: file does not appear to exist") print("Was looking for "+omitfile) for line in ff: coordinates=line.split() if len(coordinates)>2: print('More than 2 numbers per line in omit file\n') print('Line 1 is: ',coordinates) xomit=np.append(xomit,np.array([int(coordinates[0])-1])) yomit=np.append(yomit,np.array([int(coordinates[1])-1])) if len(tiffiles)==0: print("Can not find any tif files") print("Was looking here: "+tifpath) # Finally, go through and combine the data for i, file in zip(range(len(tiffiles)),tiffiles): # io.imread returns an nd array # below, reduce size a little to throw out excess data img=plt.imread(tifpath+'\\'+file) img[xomit,yomit]=0 img=img[ylow:yhigh,magic_channel-int(np.floor(xwidth/2)):magic_channel+int(np.floor(xwidth/2))] # below, throw out data with counts above 3000 as noise img[img>3000]=0 img=np.sum(img,axis=0) # below find out what energy each point in the (now 1d array) img corresponds to stripE=deltaE+Energy[i] # Which point on the scan does each element in img belong to. # indexassignments is the bin that each element of img belongs to. indexassignments=np.digitize(stripE,binedges) for j in range(len(indexassignments)): currentbin=indexassignments[j] if currentbin>len(scan)-1: continue scan[currentbin]=scan[currentbin]+img[j] A[currentbin]=A[currentbin]+1 Energy=np.delete(Energy,[0]) scan=np.delete(scan,[0]) A=np.delete(A,[0]) output=np.transpose(np.stack((Energy,scan))) Energy=(Energy)*1e3 # plt.plot(Energy,scan) # plt.show() return output