-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
921122f
commit 5edd4c5
Showing
3 changed files
with
635 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from pdfstream.transformation.cli import transform | ||
import numpy as np | ||
import os | ||
import glob | ||
import matplotlib.pyplot as plt | ||
from scipy.signal import savgol_filter | ||
from pdfstream.io import load_array | ||
from pdfstream.transformation.io import load_pdfconfig, write_pdfgetter | ||
from pdfstream.transformation.main import get_pdf | ||
from pathlib import Path | ||
from diffpy.pdfgetx import PDFConfig | ||
|
||
def globfilename(path, search_item='*.tiff'): | ||
filenames = glob.glob(path+search_item) | ||
inputnames=[filename.replace('\\','/') for filename in filenames] | ||
return(inputnames) | ||
|
||
def transform_bkg( | ||
cfg_file, | ||
data_file: str, | ||
output_dir: str = ".", | ||
plot_setting: typing.Union[str, dict] = None, | ||
test: bool = False, | ||
) -> typing.Dict[str, str]: | ||
"""Transform the data.""" | ||
if isinstance(cfg_file,str): | ||
pdfconfig = load_pdfconfig(cfg_file) | ||
else: | ||
pdfconfig = cfg_file | ||
|
||
chi = load_array(data_file) | ||
pdfgetter = get_pdf(pdfconfig, chi, plot_setting=plot_setting) | ||
filename = Path(data_file).stem | ||
dct = write_pdfgetter(output_dir, filename, pdfgetter) | ||
if not test: | ||
plt.show() | ||
return dct | ||
|
||
|
||
def count_header(filename): | ||
# count header | ||
with open (filename,'r') as myfile: | ||
data = myfile.read().splitlines() | ||
count = 0 | ||
for i, lines in enumerate(data): | ||
if lines == '#### start data': | ||
count =i | ||
return count+2 | ||
|
||
def find_qmax(filename, ave_cutoff=8e-03, window_length=21): | ||
|
||
header_count=count_header(filename) | ||
q, f = np.loadtxt(filename, skiprows= header_count, unpack=True) | ||
qmax = q[-1] | ||
fhat = savgol_filter(f, window_length, 3) | ||
fdiff=f-fhat | ||
zero_cross = np.where(np.diff(np.signbit(fhat)))[0] | ||
z=zero_cross[::-1] | ||
for i, i_zero in enumerate(z): | ||
ave=np.average(abs(fdiff[i_zero:])) | ||
|
||
if ave > ave_cutoff: | ||
print(i_zero, q[i_zero], ave) | ||
else: | ||
qmax = np.round(q[z[i-1]],1) | ||
print(i_zero, q[i_zero], ave) | ||
print(f'qmax ={qmax}') | ||
break | ||
plt.figure(figsize=(16,8)) | ||
plt.plot(q,f) | ||
plt.plot(q, fhat, 'r') | ||
|
||
plt.plot(q, (f-fhat)) | ||
print(qmax) | ||
return qmax | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
homepath='/nsls2/users/hzhong/pdfstream/' | ||
testpath = './chi_files/' | ||
output_dir=homepath | ||
testfiles = globfilename(testpath, "*.chi") | ||
testfile =testfiles[1] | ||
bkg_file = testfiles[0] | ||
|
||
pdfconfig = PDFConfig() | ||
#pdfconfig.readConfig(cfg_file) | ||
config_file= globfilename('./','*.cfg')[0] | ||
pdfconfig.readConfig(config_file) | ||
pdfconfig.backgroundfiles = bkg_file | ||
pdfconfig.bgscale=0.9 | ||
print(pdfconfig) | ||
|
||
transform('pdfconfig.cfg', testfile, output_dir=output_dir, plot_setting={'marker':'.','color':'green'} ) | ||
transform_bkg('pdfconfig.cfg', testfile, output_dir=output_dir, plot_setting={'marker':'.','color':'green'} ) | ||
transform_bkg(pdfconfig, testfile, output_dir=output_dir, plot_setting={'marker':'.','color':'green'} ) | ||
|
||
pdfconfig = PDFConfig() | ||
config_file= globfilename('./','*.cfg')[0] | ||
pdfconfig.readConfig(config_file) | ||
pdfconfig.qmaxinst = 24.0 | ||
pdfconfig.qmin=1.0 | ||
pdfconfig.qmax=20 | ||
pdfconfig.rpoly=0.9 | ||
pdfconfig.rmin=0.1 | ||
pdfconfig.rmax=15 | ||
# pdfconfig.rpoly=0.9 | ||
pdfconfig.bgscale=1 | ||
pdfconfig.outputtypes=['sq','fq','gr'] | ||
print(pdfconfig) | ||
|
||
transform_bkg(pdfconfig, testfile, output_dir=output_dir, plot_setting={'marker':'.','color':'green'} ) | ||
|
||
fqfile = globfilename(output_dir, '*/*.fq')[1] | ||
print(fqfile) | ||
|
||
qmax=find_qmax(fqfile, ave_cutoff=8e-03, window_length=101) | ||
|
||
pdfconfig.qmax=qmax | ||
print(pdfconfig) | ||
transform_bkg(pdfconfig, testfile, output_dir=output_dir, plot_setting={'marker':'.','color':'green'} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.