def _traj_file_load(fname, traj_axis='x'): #MR20160725 nLinesHead = 1 hlp = [] with open(fname, 'r') as f: for i in range(nLinesHead): hlp.append(f.readline()) data = uti_io.read_ascii_data_cols(fname, '\t', _i_col_start=0, _i_col_end=10, _n_line_skip=nLinesHead) mode = 3 allrange = [ data[5][0], # z-coordinate begin data[5][-1], # z-coordinate end len(data[0]), # number of points min(data[1]), # x-coordinate begin max(data[1]), # x-coordinate end 1, min(data[3]), # y-coordinate begin max(data[3]), # y-coordinate end 1, ] arLabels = ['Longitudinal Position', 'Horizontal Position', 'Vertical Position'] arUnits = ['m', 'm', 'm', 'm'] if traj_axis == 'x': data = data[1] arLabels.append('Horizontal Coordinate') elif traj_axis == 'y': data = data[3] arLabels.append('Vertical Coordinate') else: raise ValueError('Parameter "axis" has wrong value: {}. Allowed values are "x" and "y"'.format(traj_axis)) return data, mode, allrange, arLabels, arUnits
def _file_load(_fname, _read_labels=1): #MR20160725 nLinesHead = 11 hlp = [] #with open(_fname,'r') as f: hlp = f.readlines(nLinesHead) with open(_fname,'r') as f: for i in range(nLinesHead): hlp.append(f.readline()) #ne, nx, ny, ns = [ int(hlp[i].replace('#','').split()[0]) for i in [3,6,9,10] ] ne, nx, ny = [int(hlp[i].replace('#','').split()[0]) for i in [3,6,9]] ns = 1 testStr = hlp[nLinesHead - 1] if testStr[0] == '#': ns = int(testStr.replace('#','').split()[0]) else: nLinesHead -= 1 e0,e1,x0,x1,y0,y1 = [float(hlp[i].replace('#','').split()[0]) for i in [1,2,4,5,7,8]] #data = np.squeeze(np.loadtxt(_fname, dtype=np.float64)) #get data from file (C-aligned flat) data = uti_io.read_ascii_data_cols(_fname, '\t', _i_col_start=0, _i_col_end=0, _n_line_skip=nLinesHead)[0] allrange = e0, e1, ne, x0, x1, nx, y0, y1, ny arLabels = ['Photon Energy', 'Horizontal Position', 'Vertical Position', 'Intensity'] arUnits = ['eV', 'm', 'm', 'ph/s/.1%bw/mm^2'] if _read_labels: arTokens = hlp[0].split(' [') arLabels[3] = arTokens[0].replace('#','') arUnits[3] = ''; if len(arTokens) > 1: arUnits[3] = arTokens[1].split('] ')[0] #print(arLabels[3], arUnits[3]) for i in range(3): arTokens = hlp[i*3 + 1].split() nTokens = len(arTokens) nTokensLabel = nTokens - 3 nTokensLabel_mi_1 = nTokensLabel - 1 strLabel = '' for j in range(nTokensLabel): strLabel += arTokens[j + 2] if j < nTokensLabel_mi_1: strLabel += ' ' arLabels[i] = strLabel arUnits[i] = arTokens[nTokens - 1].replace('[','').replace(']','') m = _enum('T','V','H','E','HV','EV','EH','EHV') if ne==1 and nx==1 and ny==1 : mode = m.T if ne==1 and nx==1 and ny>1 : mode = m.V if ne==1 and nx>1 and ny==1 : mode = m.H if ne>1 and nx==1 and ny==1 : mode = m.E if ne==1 and nx>1 and ny>1 : mode = m.HV if ne>1 and nx==1 and ny>1 : mode = m.EV if ne>1 and nx>1 and ny==1 : mode = m.EH if ne>1 and nx>1 and ny>1 : mode = m.EHV #print(mode) return data, mode, allrange, arLabels, arUnits
def file_load(_fname, _read_labels=1): nLinesHead = 11 hlp = [] #with open(_fname,'r') as f: hlp = f.readlines(nLinesHead) with open(_fname,'r') as f: for i in range(nLinesHead): hlp.append(f.readline()) #ne, nx, ny, ns = [ int(hlp[i].replace('#','').split()[0]) for i in [3,6,9,10] ] ne, nx, ny = [int(hlp[i].replace('#','').split()[0]) for i in [3,6,9]] ns = 1 testStr = hlp[nLinesHead - 1] if testStr[0] == '#': ns = int(testStr.replace('#','').split()[0]) else: nLinesHead -= 1 e0,e1,x0,x1,y0,y1 = [float(hlp[i].replace('#','').split()[0]) for i in [1,2,4,5,7,8]] #data = np.squeeze(np.loadtxt(_fname, dtype=np.float64)) #get data from file (C-aligned flat) data = uti_io.read_ascii_data_cols(_fname, '\t', _i_col_start=0, _i_col_end=0, _n_line_skip=nLinesHead)[0] allrange = e0, e1, ne, x0, x1, nx, y0, y1, ny arLabels = ['Photon Energy', 'Horizontal Position', 'Vertical Position', 'Intensity'] arUnits = ['eV', 'm', 'm', 'ph/s/.1%bw/mm^2'] if _read_labels: arTokens = hlp[0].split(' [') arLabels[3] = arTokens[0].replace('#','') arUnits[3] = ''; if len(arTokens) > 1: arUnits[3] = arTokens[1].split('] ')[0] #print(arLabels[3], arUnits[3]) for i in range(3): arTokens = hlp[i*3 + 1].split() nTokens = len(arTokens) nTokensLabel = nTokens - 3 nTokensLabel_mi_1 = nTokensLabel - 1 strLabel = '' for j in range(nTokensLabel): strLabel += arTokens[j + 2] if j < nTokensLabel_mi_1: strLabel += ' ' arLabels[i] = strLabel arUnits[i] = arTokens[nTokens - 1].replace('[','').replace(']','') m = _enum('T','V','H','E','HV','EV','EH','EHV') if ne==1 and nx==1 and ny==1 : mode = m.T if ne==1 and nx==1 and ny>1 : mode = m.V if ne==1 and nx>1 and ny==1 : mode = m.H if ne>1 and nx==1 and ny==1 : mode = m.E if ne==1 and nx>1 and ny>1 : mode = m.HV if ne>1 and nx==1 and ny>1 : mode = m.EV if ne>1 and nx>1 and ny==1 : mode = m.EH if ne>1 and nx>1 and ny>1 : mode = m.EHV #print(mode) return data, mode, allrange, arLabels, arUnits
def _check_finite(file_path): try: import numpy as np NUMPY = True except ImportError: NUMPY = False if NUMPY: with open(file_path) as f: data = np.loadtxt(f) condition = np.isfinite(data).all() else: import math import uti_io data = uti_io.read_ascii_data_cols(file_path, '\t', _i_col_start=0, _i_col_end=0, _n_line_skip=10)[0] first_value = data[0] condition = math.isinf(first_value) and not math.isnan(first_value) assert condition, \ 'data is not finite: [{}, ...]'.format(', '.join([str(x) for x in data[:10]])) return data
def get_att_len(material, E=8000): """ by LW 07/04/2011 function gets the attenuation length from stored data file, attenuation length is a .dat file from http://henke.lbl.gov/optical_constants/getdb2.html (energy range: 2-30keV,delete the header lines or comment with '%', name the file n_material.dat) calling sequence: mu=get_mu(material,E) where mu [meter] is the 1/e attenuation length, E: X-ray energy in eV, default: 8000eV' """ #get list_of supported materials from data file directory: #xdatafiles = [ f for f in listdir(datapath) if isfile(join(datapath,f)) ] xdatafiles = [ f for f in os.listdir(datapath) if os.path.isfile(os.path.join(datapath, f)) ] #OC150715 name = [] #for i in range(0, np.size(xdatafiles)): for i in range(0, len(xdatafiles)): #OC100915 mm = re.search('(?<=mu_)\w+', xdatafiles[i]) if mm is not None: name.append(mm.group(0)) #E=np.array(E) if material in name: #loadn=datapath+'mu_'+material+'.dat' loadm = os.path.join(datapath, 'mu_' + material + '.dat') #OC100915 #m=pl.loadtxt(loadn,comments='%') m = uti_io.read_ascii_data_cols(loadm, ' ', _i_col_start=0, _i_col_end=1, _n_line_skip=2) #OC100915 #if np.min(E)>=np.min(m[:,0]) and np.max(E)<=np.max(m[:,0]): # mu=np.interp(E,m[:,0],m[:,1]) # return mu*1e-6 #else: raise xfuncs_Exception ('error: energy '+"%3.4f" %E +'[keV] out of range ('+"%3.4f" % np.min(m[:,0])+'=<E<='+"%3.4f" % np.max(m[:,0])+'eV)') arE = m[0] arAttLen = m[1] attLen = (1e-6) * uti_math.interp_1d_var(E, arE, arAttLen) nE_mi_1 = len(arE) - 1 if ((E < arE[0]) or (E > arE[nE_mi_1])): print('Warning: energy ' + "%3.4f" % E + '[keV] out of range (' + "%3.4f" % arE[0] + '=<E<=' + "%3.4f" % arE[nE_mi_1] + 'eV)') return attLen elif material == 'material?': #print 'list of supported materials (based on data files in directory '+datapath+':' #print name print( 'list of supported materials (based on data files in directory ' + datapath + ':') print(name) else: raise xfuncs_Exception( 'error: non recognized material, please create index of refraction file first. Type get_mu("?") for instructions; type get_n("material?") for list of supported materials' )
def get_ac(material, E=8000): """ by LW 10/03/2010 function calculates the critical angle for total external reflection as a function of the material and the X-ray energy according to ac=sqrt(2*delta) index of refraction is a .dat file from http://henke.lbl.gov/optical_constants/getdb2.html (energy range: 2-30keV,delete the header % lines, name the file n_material.dat) % calling sequence: ac=get_ac(material,E) where ac: critial angle in degrees, E [eV] (default: 8000eV) type get_ac(\'materilal?\') to show list of supported materials" """ #get list_of supported materials from data file directory: #xdatafiles = [ f for f in listdir(datapath) if isfile(join(datapath,f)) ] xdatafiles = [ f for f in os.listdir(datapath) if os.path.isfile(os.path.join(datapath, f)) ] #OC150715 name = [] #for i in range(0, np.size(xdatafiles)): for i in range(0, len(xdatafiles)): #OC150715 m = re.search('(?<=n_)\w+', xdatafiles[i]) if m is not None: name.append(m.group(0)) #E=np.array(E) if material in name: #loadn=datapath+'n_'+material+'.dat' #n=pl.loadtxt(loadn,comments='%') loadn = os.path.join(datapath, 'n_' + material + '.dat') #OC150715 n = uti_io.read_ascii_data_cols(loadn, ' ', _i_col_start=0, _i_col_end=1, _n_line_skip=2) #OC150715 #if np.min(E)>=np.min(n[:,0]) and np.max(E)<=np.max(n[:,0]): # d=np.interp(E,n[:,0],n[:,1]) # return np.degrees(np.sqrt(2*d)) #else: raise xfuncs_Exception ('error: energy '+"%3.4f" %E +'[eV] out of range ('+"%3.4f" % np.min(n[:,0])+'=<E<='+"%3.4f" % np.max(n[:,0])+'eV)') arE = n[0] arDelta = n[1] #################################################### elif material == 'material?': #print 'list of supported materials (based on data files in directory '+datapath+':' #print name print( 'list of supported materials (based on data files in directory ' + datapath + ':') print(name) else: raise xfuncs_Exception( 'error: non recognized material, please create index of refraction file first. Type "get_ac?" for instructions; type get_ac("material?") for list of supported materials' )
def _multicolumn_file_load(fname): #MR31102017 with open(fname, 'r') as f: header = f.readline().split(',') header = [x.replace('#', '').strip() for x in header] data = uti_io.read_ascii_data_cols(fname, '\t', _i_col_start=0, _i_col_end=-1, _n_line_skip=1) d = {} for i, k in enumerate(header): k_no_units = k.split('[')[0].strip() units = k.split('[')[1].split(']')[0].strip() d[k_no_units] = { 'data': data[i], 'label': k, 'units': units, } # data, mode, allrange, arLabels, arUnits return d, None, [], [], []
def get_refr(material, E=8000): """ "by LW 07/04/2011 function get the complex index of refraction from stored data file, index of refraction is a .dat file from http://henke.lbl.gov/optical_constants/getdb2.html (energy range: 2-30keV,delete the header lines, name the file n_material.dat) calling sequence: n=get_n(material,E) where n is the complex refractive index detlta-i*beta, E: X-ray energy in eV, default: 8000eV" """ #get list_of supported materials from data file directory: #xdatafiles = [ f for f in listdir(datapath) if isfile(join(datapath,f)) ] xdatafiles = [ f for f in os.listdir(datapath) if os.path.isfile(os.path.join(datapath, f)) ] #OC150715 name = [] #for i in range(0, np.size(xdatafiles)): for i in range(0, len(xdatafiles)): #OC160715 m = re.search('(?<=n_)\w+', xdatafiles[i]) if m is not None: name.append(m.group(0)) #E=np.array(E) if material in name: #loadn=datapath+'n_'+material+'.dat' #n=pl.loadtxt(loadn,comments='%') loadn = os.path.join(datapath, 'n_' + material + '.dat') #OC160715 n = uti_io.read_ascii_data_cols(loadn, ' ', _i_col_start=0, _i_col_end=2, _n_line_skip=2) #OC160715 #if np.min(E)>=np.min(n[:,0]) and np.max(E)<=np.max(n[:,0]): # d=np.interp(E,n[:,0],n[:,1]) # b=np.interp(E,n[:,0],n[:,2]) # return d-1j*b #else: raise xfuncs_Exception ('error: energy '+"%3.4f" %E +'[keV] out of range ('+"%3.4f" % np.min(n[:,0])+'=<E<='+"%3.4f" % np.max(n[:,0])+'eV)') arE = n[0] arDelta = n[1] arBeta = n[2] d = uti_math.interp_1d_var(E, arE, arDelta) b = uti_math.interp_1d_var(E, arE, arBeta) nE_mi_1 = len(arE) - 1 if ((E < arE[0]) or (E > arE[nE_mi_1])): print('Warning: energy ' + "%3.4f" % E + '[keV] out of range (' + "%3.4f" % arE[0] + '=<E<=' + "%3.4f" % arE[nE_mi_1] + 'eV)') return d - 1j * b #Check the sign of imaginary part! elif material == 'material?': #print 'list of supported materials (based on data files in directory '+datapath+':' #print name print( 'list of supported materials (based on data files in directory ' + datapath + ':') print(name) #else: raise xfuncs_Exception ( 'error: non recognized material, please create index of refraction file first. Type "get_n?" for instructions; type get_n("material?") for list of supported materials') else: raise uti_xray_mat_Exception( 'error: non recognized material, please create index of refraction file first. Type "get_n?" for instructions; type get_n("material?") for list of supported materials' )