SusMat-SWAXS 2.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,173 @@
1
+ Metadata-Version: 2.3
2
+ Name: SusMat-SWAXS
3
+ Version: 2.1.3
4
+ Summary: A collection of python scripts for uploading analytical data to iRODS
5
+ Author: Samuel Eyley
6
+ Author-email: Samuel Eyley <susmat-swaxspypi.xepsj@simplelogin.com>
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Dist: pyfai>2024.10.0
11
+ Requires-Dist: pyopencl
12
+ Requires-Dist: click
13
+ Requires-Dist: pyside6
14
+ Requires-Dist: h5py
15
+ Requires-Dist: matplotlib
16
+ Requires-Dist: numpy
17
+ Requires-Dist: scipy
18
+ Requires-Dist: sympy
19
+ Requires-Dist: fabio
20
+ Requires-Dist: hdf5plugin
21
+ Requires-Dist: pillow
22
+ Requires-Dist: fairmat-readers-xrd>=0.0.8
23
+ Requires-Python: >=3.12
24
+ Project-URL: Homepage, https://github.com/Sustainable-Materials-Lab/SusMat-SWAXS
25
+ Project-URL: Issues, https://github.com/Sustainable-Materials-Lab/SusMat-SWAXS/issues
26
+ Description-Content-Type: text/markdown
27
+
28
+ # SWAXS tools
29
+
30
+ ## Description
31
+ This is a collection of scripts to perform data conversion and integration on X-ray scattering data, specifically for data collected on the Xenocs Xeuss SWAXS instrument.
32
+
33
+ **Available tools:**
34
+ - `sm-datconv` - Converts Xenocs format 1D data (.dat) to TOPAS compatible .xye format
35
+ - `sm-edfh5` - Convert EDF files to HDF5 format with metadata preservation
36
+ - `sm-saxsint` - Integrate 2D SAXS data from Xenocs Xeuss instruments with background subtraction
37
+ - `sm-waxsint` - Integrate 2D WAXS data from Xenocs Xeuss instruments with absorption correction
38
+ - `sm-spharm` - Visualize spherical harmonics functions from TOPAS coefficients
39
+
40
+ ## Installation
41
+ Install in a virtual environment to avoid dependency conflicts:
42
+
43
+ ```bash
44
+ pip install SusMat-SWAXS
45
+ ```
46
+
47
+
48
+ ## Usage
49
+ All scripts are available as command-line tools after installation. Use the `--help` flag with any command to see detailed usage information.
50
+
51
+ ### sm-datconv
52
+ Converts Xenocs format 1D data (.dat) to TOPAS compatible .xye format.
53
+
54
+ **Input:** `.dat` file with tab-separated columns for q, I(q) (and optionally sigma).
55
+
56
+ **Usage:**
57
+ ```bash
58
+ sm-datconv <filename_without_extension>
59
+ ```
60
+
61
+ **Output:** `.xye` file with tab-separated columns for 2θ, I, and error (if present in input).
62
+
63
+ **Batch processing example (PowerShell):**
64
+ ```powershell
65
+ Get-ChildItem -Filter *.dat | ForEach-Object -Process {sm-datconv $_.BaseName}
66
+ ```
67
+
68
+ ### sm-xrdmlconv
69
+ Converts Malvern PANalytical format 1D data (.xrdml) to TOPAS compatible .xy format.
70
+
71
+ **Input:** `.xrdml` file .
72
+
73
+ **Usage:**
74
+ ```bash
75
+ sm-xrdmlconv <filename_without_extension>
76
+ ```
77
+
78
+ **Output:** `.xy` file with tab-separated columns for 2θ and I and a `.json` file containing metadata.
79
+
80
+ ### sm-edfh5
81
+ Convert multiple EDF files to a single HDF5 file with metadata preservation.
82
+
83
+ **Usage:**
84
+ ```bash
85
+ sm-edfh5 <path to directory containing edf files>
86
+ ```
87
+
88
+ ### sm-saxsint
89
+ Integrate 2D SAXS data from Xenocs Xeuss instruments with background subtraction and absorption correction.
90
+
91
+ **Usage:**
92
+ ```bash
93
+ sm-saxsint <sample.edf> <mask.edf> [OPTIONS]
94
+ ```
95
+
96
+ **Key options:**
97
+ - `--poni <file.poni>` - PONI calibration file
98
+ - `--background <bkg.edf>` - Background file for subtraction
99
+ - `--bkg_factor <float>` - Background scaling factor (default: 1.0)
100
+ - `--unit <q|2th>` - Radial unit for integration (default: q)
101
+ - `--xmin <float>` - Minimum q value (default: 0.004)
102
+ - `--xmax <float>` - Maximum q value (default: 0.7)
103
+ - `--opencl` - Use OpenCL acceleration (requires pyopencl)
104
+ - `--raw2D` - Display 2D image in lab frame
105
+
106
+ **Output:**
107
+ - `*_1D.dat` - Integrated 1D data
108
+ - `*_subtracted.dat` - Background-subtracted data
109
+ - `*.svg` - Visualization plot
110
+
111
+ ### sm-waxsint
112
+ Integrate 2D WAXS data from Xenocs Xeuss instruments with absorption correction.
113
+
114
+ **Usage:**
115
+ ```bash
116
+ sm-waxsint <sample.edf> <mask.edf> [OPTIONS]
117
+ ```
118
+
119
+ **Key options:**
120
+ - `--poni <file.poni>` - PONI calibration file
121
+ - `--background <bkg.edf>` - Background file for subtraction
122
+ - `--bkg_factor <float>` - Background scaling factor (default: 1.0)
123
+ - `--unit <q|2th>` - Radial unit for integration (default: q)
124
+ - `--xmin <float>` - Minimum q value (default: 0.36)
125
+ - `--xmax <float>` - Maximum q value (default: 3.8)
126
+ - `--lac <float>` - Linear absorption coefficient in cm⁻¹ for thickness correction
127
+ - `--thickness <float>` - Sample thickness in cm for transmission correction
128
+ - `--noabs` - Disable absorption correction
129
+ - `--opencl` - Use OpenCL acceleration (requires pyopencl)
130
+ - `--raw2D` - Display 2D image in lab frame
131
+
132
+ **Output:**
133
+ - `*_1D.dat` - Integrated 1D data
134
+ - `*_subtracted.dat` - Background-subtracted, absorption-corrected data
135
+ - `*.svg` - Visualization plot
136
+
137
+ ### sm-spharm
138
+ Visualize spherical harmonics functions using coefficients from TOPAS academic output files.
139
+
140
+ **Usage:**
141
+ ```bash
142
+ sm-spharm C00 C20 C22P C22M C40 C42P C42M C44P C44M C60 C62P C62M C64P C64M C66P C66M C80 C82P C82M C84P C84M C86P C86M C88P C88M [OPTIONS]
143
+ ```
144
+
145
+ **Arguments:** 25 spherical harmonics coefficients (c00 through c88m) from TOPAS output file
146
+
147
+ **Options:**
148
+ - `--output <filename>` / `-o <filename>` - Output filename for the plot (default: sph_harm.svg)
149
+ - `--resolution <int>` / `-r <int>` - Angular resolution for the plot (default: 200)
150
+
151
+ **Example:**
152
+ ```bash
153
+ sm-spharm 1 -0.54889 0.48504 0.74711 0.79916 -0.33178 0.16323 0.45369 \
154
+ -0.29782 -0.29214 -0.43949 0.01524 0.09042 0.85340 0.40316 \
155
+ -0.01881 1.47673 -0.55751 -0.68970 0.02621 -0.06565 -0.56472 \
156
+ 1.00981 -0.12339 -0.25831 --output my_spherical_harmonics.svg
157
+ ```
158
+
159
+ **Output:** 3D visualization plot showing the spherical harmonics function in real space
160
+
161
+ ## Support
162
+ Submit an issue on github: https://github.com/Sustainable-Materials-Lab/SusMat-SWAXS
163
+
164
+ ## Contributing
165
+ Contact Samuel Eyley if you are interested in contributing.
166
+
167
+ ## Authors and acknowledgment
168
+ Originally authored by Samuel Eyley.
169
+
170
+ Significant portions of code were developed with the help of Github Copilot (2025).
171
+
172
+ ## License
173
+ MIT License
@@ -0,0 +1,146 @@
1
+ # SWAXS tools
2
+
3
+ ## Description
4
+ This is a collection of scripts to perform data conversion and integration on X-ray scattering data, specifically for data collected on the Xenocs Xeuss SWAXS instrument.
5
+
6
+ **Available tools:**
7
+ - `sm-datconv` - Converts Xenocs format 1D data (.dat) to TOPAS compatible .xye format
8
+ - `sm-edfh5` - Convert EDF files to HDF5 format with metadata preservation
9
+ - `sm-saxsint` - Integrate 2D SAXS data from Xenocs Xeuss instruments with background subtraction
10
+ - `sm-waxsint` - Integrate 2D WAXS data from Xenocs Xeuss instruments with absorption correction
11
+ - `sm-spharm` - Visualize spherical harmonics functions from TOPAS coefficients
12
+
13
+ ## Installation
14
+ Install in a virtual environment to avoid dependency conflicts:
15
+
16
+ ```bash
17
+ pip install SusMat-SWAXS
18
+ ```
19
+
20
+
21
+ ## Usage
22
+ All scripts are available as command-line tools after installation. Use the `--help` flag with any command to see detailed usage information.
23
+
24
+ ### sm-datconv
25
+ Converts Xenocs format 1D data (.dat) to TOPAS compatible .xye format.
26
+
27
+ **Input:** `.dat` file with tab-separated columns for q, I(q) (and optionally sigma).
28
+
29
+ **Usage:**
30
+ ```bash
31
+ sm-datconv <filename_without_extension>
32
+ ```
33
+
34
+ **Output:** `.xye` file with tab-separated columns for 2θ, I, and error (if present in input).
35
+
36
+ **Batch processing example (PowerShell):**
37
+ ```powershell
38
+ Get-ChildItem -Filter *.dat | ForEach-Object -Process {sm-datconv $_.BaseName}
39
+ ```
40
+
41
+ ### sm-xrdmlconv
42
+ Converts Malvern PANalytical format 1D data (.xrdml) to TOPAS compatible .xy format.
43
+
44
+ **Input:** `.xrdml` file .
45
+
46
+ **Usage:**
47
+ ```bash
48
+ sm-xrdmlconv <filename_without_extension>
49
+ ```
50
+
51
+ **Output:** `.xy` file with tab-separated columns for 2θ and I and a `.json` file containing metadata.
52
+
53
+ ### sm-edfh5
54
+ Convert multiple EDF files to a single HDF5 file with metadata preservation.
55
+
56
+ **Usage:**
57
+ ```bash
58
+ sm-edfh5 <path to directory containing edf files>
59
+ ```
60
+
61
+ ### sm-saxsint
62
+ Integrate 2D SAXS data from Xenocs Xeuss instruments with background subtraction and absorption correction.
63
+
64
+ **Usage:**
65
+ ```bash
66
+ sm-saxsint <sample.edf> <mask.edf> [OPTIONS]
67
+ ```
68
+
69
+ **Key options:**
70
+ - `--poni <file.poni>` - PONI calibration file
71
+ - `--background <bkg.edf>` - Background file for subtraction
72
+ - `--bkg_factor <float>` - Background scaling factor (default: 1.0)
73
+ - `--unit <q|2th>` - Radial unit for integration (default: q)
74
+ - `--xmin <float>` - Minimum q value (default: 0.004)
75
+ - `--xmax <float>` - Maximum q value (default: 0.7)
76
+ - `--opencl` - Use OpenCL acceleration (requires pyopencl)
77
+ - `--raw2D` - Display 2D image in lab frame
78
+
79
+ **Output:**
80
+ - `*_1D.dat` - Integrated 1D data
81
+ - `*_subtracted.dat` - Background-subtracted data
82
+ - `*.svg` - Visualization plot
83
+
84
+ ### sm-waxsint
85
+ Integrate 2D WAXS data from Xenocs Xeuss instruments with absorption correction.
86
+
87
+ **Usage:**
88
+ ```bash
89
+ sm-waxsint <sample.edf> <mask.edf> [OPTIONS]
90
+ ```
91
+
92
+ **Key options:**
93
+ - `--poni <file.poni>` - PONI calibration file
94
+ - `--background <bkg.edf>` - Background file for subtraction
95
+ - `--bkg_factor <float>` - Background scaling factor (default: 1.0)
96
+ - `--unit <q|2th>` - Radial unit for integration (default: q)
97
+ - `--xmin <float>` - Minimum q value (default: 0.36)
98
+ - `--xmax <float>` - Maximum q value (default: 3.8)
99
+ - `--lac <float>` - Linear absorption coefficient in cm⁻¹ for thickness correction
100
+ - `--thickness <float>` - Sample thickness in cm for transmission correction
101
+ - `--noabs` - Disable absorption correction
102
+ - `--opencl` - Use OpenCL acceleration (requires pyopencl)
103
+ - `--raw2D` - Display 2D image in lab frame
104
+
105
+ **Output:**
106
+ - `*_1D.dat` - Integrated 1D data
107
+ - `*_subtracted.dat` - Background-subtracted, absorption-corrected data
108
+ - `*.svg` - Visualization plot
109
+
110
+ ### sm-spharm
111
+ Visualize spherical harmonics functions using coefficients from TOPAS academic output files.
112
+
113
+ **Usage:**
114
+ ```bash
115
+ sm-spharm C00 C20 C22P C22M C40 C42P C42M C44P C44M C60 C62P C62M C64P C64M C66P C66M C80 C82P C82M C84P C84M C86P C86M C88P C88M [OPTIONS]
116
+ ```
117
+
118
+ **Arguments:** 25 spherical harmonics coefficients (c00 through c88m) from TOPAS output file
119
+
120
+ **Options:**
121
+ - `--output <filename>` / `-o <filename>` - Output filename for the plot (default: sph_harm.svg)
122
+ - `--resolution <int>` / `-r <int>` - Angular resolution for the plot (default: 200)
123
+
124
+ **Example:**
125
+ ```bash
126
+ sm-spharm 1 -0.54889 0.48504 0.74711 0.79916 -0.33178 0.16323 0.45369 \
127
+ -0.29782 -0.29214 -0.43949 0.01524 0.09042 0.85340 0.40316 \
128
+ -0.01881 1.47673 -0.55751 -0.68970 0.02621 -0.06565 -0.56472 \
129
+ 1.00981 -0.12339 -0.25831 --output my_spherical_harmonics.svg
130
+ ```
131
+
132
+ **Output:** 3D visualization plot showing the spherical harmonics function in real space
133
+
134
+ ## Support
135
+ Submit an issue on github: https://github.com/Sustainable-Materials-Lab/SusMat-SWAXS
136
+
137
+ ## Contributing
138
+ Contact Samuel Eyley if you are interested in contributing.
139
+
140
+ ## Authors and acknowledgment
141
+ Originally authored by Samuel Eyley.
142
+
143
+ Significant portions of code were developed with the help of Github Copilot (2025).
144
+
145
+ ## License
146
+ MIT License
@@ -0,0 +1,50 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.9.6,<0.10.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "SusMat-SWAXS"
7
+ version = "2.1.3"
8
+ authors = [
9
+ { name="Samuel Eyley", email="susmat-swaxspypi.xepsj@simplelogin.com" },
10
+ ]
11
+ description = "A collection of python scripts for uploading analytical data to iRODS"
12
+ readme = "README.md"
13
+ requires-python = ">=3.12"
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+ dependencies = [
20
+ "pyfai>2024.10.0",
21
+ "pyopencl",
22
+ "click",
23
+ "pyside6",
24
+ "h5py",
25
+ "matplotlib",
26
+ "numpy",
27
+ "scipy",
28
+ "sympy",
29
+ "fabio",
30
+ "hdf5plugin",
31
+ "pillow",
32
+ "fairmat-readers-xrd>=0.0.8",
33
+ ]
34
+
35
+ [project.urls]
36
+ Homepage = "https://github.com/Sustainable-Materials-Lab/SusMat-SWAXS"
37
+ Issues = "https://github.com/Sustainable-Materials-Lab/SusMat-SWAXS/issues"
38
+
39
+ [project.scripts]
40
+ sm-datconv = "smswaxs.dat_conv:cli"
41
+ sm-xrdmlconv = "smswaxs.xrdml_conv:cli"
42
+ sm-topasinit = "smswaxs.topas_init:cli"
43
+ sm-edfh5 = "smswaxs.xeuss_edf_h5_multi:add_edfs_to_hdf5"
44
+ sm-saxsint = "smswaxs.saxs_integration_xeuss:cli"
45
+ sm-waxsint = "smswaxs.waxs_integration_xeuss:cli"
46
+ sm-spharm = "smswaxs.sph_harm:cli"
47
+
48
+
49
+ [tool.uv.build-backend]
50
+ module-name = "smswaxs"
@@ -0,0 +1,32 @@
1
+ """
2
+ SusMat-SWAXS: A collection of python scripts for SWAXS data processing and conversion.
3
+
4
+ This package provides tools for:
5
+ - SAXS/WAXS data integration from Xenocs Xeuss instruments
6
+ - Data format conversions (EDF to HDF5, TIFF, etc.)
7
+ - Background subtraction and absorption corrections
8
+ - Data visualization and analysis
9
+ """
10
+
11
+ __version__ = "2.1.3"
12
+ __author__ = "Samuel Eyley"
13
+ __email__ = "samuel.eyley@kuleuven.be"
14
+
15
+ # Import CLI functions for programmatic access
16
+ from .dat_conv import cli as dat_conv_cli
17
+ from .xeuss_edf_h5_multi import add_edfs_to_hdf5
18
+ from .saxs_integration_xeuss import cli as saxs_integration_cli
19
+ from .waxs_integration_xeuss import cli as waxs_integration_cli
20
+ from .sph_harm import cli as sph_harm_cli
21
+
22
+ # Make CLI functions available at package level
23
+ __all__ = [
24
+ "__version__",
25
+ "__author__",
26
+ "__email__",
27
+ "dat_conv_cli",
28
+ "add_edfs_to_hdf5",
29
+ "saxs_integration_cli",
30
+ "waxs_integration_cli",
31
+ "sph_harm_cli"
32
+ ]
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu May 7 11:44:10 2015
4
+
5
+ @author: u0092172
6
+ """
7
+ import argparse as ap
8
+ import re
9
+ import numpy as np
10
+
11
+ def get_line_number(phrase, file_name):
12
+ """Get the line number containing phrase"""
13
+ with open(file_name, encoding="utf-8") as f:
14
+ for i, line in enumerate(f, 1):
15
+ p = re.compile(phrase)
16
+ if p.match(line) is not None:
17
+ return i
18
+
19
+ def get_number_lines(file_name):
20
+ """Get the number of lines in a file"""
21
+ with open(file_name, encoding="utf-8") as f:
22
+ lines = len(f.readlines())
23
+ return lines
24
+
25
+
26
+ def cli():
27
+ """Convert dat files to xye format for use in TOPAS"""
28
+ parser = ap.ArgumentParser(description='Convert dat files to xye format for use in TOPAS')
29
+ parser.add_argument("input", help="Filename of the .dat (excl. ext.)")
30
+ parser.add_argument(
31
+ "--wavelength", help="Wavelength in Angstroms", default=float(1.54189))
32
+ parser.add_argument(
33
+ "--highscore", help="Output for highscore plus", action="store_true")
34
+ parser.add_argument(
35
+ "--exposure", help="Counting time (convert to counts from CPS)", type=float, default=1)
36
+ # parser.add_argument("--dat", help="convert from dat file", action="store_true")
37
+ parser.add_argument(
38
+ "--SAXS", help="output SAXS data (q and don't cut low angle)", action="store_true")
39
+ parser.add_argument(
40
+ "--renormalize", help="boost counts", action="store_true")
41
+ parser.add_argument("--clip", help="clip range", action="store_true")
42
+ parser.add_argument(
43
+ "--fullprof", help="general fullprof input", action="store_true")
44
+ parser.add_argument(
45
+ "--kratky", help="Generate Kratky plot columns", action="store_true")
46
+ parser.add_argument("--pyfai", help="pyfai input", action="store_true")
47
+ parser.add_argument(
48
+ "--zero", help="set minimum to zero", action="store_true")
49
+ args = parser.parse_args() # map arguments to args
50
+
51
+ fname = args.input
52
+ if args.highscore is True:
53
+ ext = ".asc"
54
+ elif args.fullprof is True:
55
+ ext = "_FP.dat"
56
+ elif args.kratky is True:
57
+ ext = "_kratky.dat"
58
+ else:
59
+ ext = ".xye"
60
+
61
+ # Import the data
62
+
63
+ if args.kratky is True:
64
+ args.SAXS = True
65
+
66
+ if args.pyfai is True:
67
+ data = np.genfromtxt(fname, comments='#',
68
+ usecols=(0, 1, 2), encoding="utf-8")
69
+ else:
70
+ head = get_line_number('.*q\\([AÅ].*', fname)
71
+ data = np.genfromtxt(fname, skip_header=head,
72
+ usecols=(0, 1, 2), encoding="utf-8")
73
+
74
+ data = data[~np.isnan(data).any(axis=1)]
75
+
76
+ q = data[:, 0]
77
+ i = data[:, 1]
78
+ e = data[:, 2]
79
+
80
+ if args.pyfai is True:
81
+ ttheta = q
82
+ if args.clip is True:
83
+ start = np.where(ttheta > 5)[0][0]
84
+ ttheta = ttheta[start:]
85
+ i = i[start:]
86
+ e = e[start:]
87
+ elif args.SAXS is False:
88
+ # Calc 2theta
89
+
90
+ ttheta = 2*np.degrees(np.arcsin((q*args.wavelength)/(4*np.pi)))
91
+
92
+ # Clip the data to start at 5 degrees to remove excessive intensity
93
+ if args.clip is True:
94
+ start = np.where(ttheta > 5)[0][0]
95
+ ttheta = ttheta[start:]
96
+ i = i[start:]
97
+ e = e[start:]
98
+
99
+ # Convert i to counts
100
+ if args.renormalize is True:
101
+ i = i*args.exposure*1e11
102
+ else:
103
+ i = i*args.exposure
104
+
105
+ if min(i) < 0:
106
+ i -= min(i)
107
+
108
+ if args.zero is True:
109
+ i -= min(i)
110
+
111
+ if args.highscore is True:
112
+ out = np.column_stack((ttheta.flatten(), i.flatten()))
113
+ else:
114
+ if args.SAXS is False:
115
+ out = np.column_stack((ttheta.flatten(), i.flatten(), e.flatten()))
116
+ elif args.kratky is False:
117
+ out = np.column_stack((q.flatten(), i.flatten(), e.flatten()))
118
+ else:
119
+ q2i = (q**2)*i
120
+ q2sig = (q**2)*e
121
+ out = np.column_stack(
122
+ (q.flatten(), i.flatten(), q2i.flatten(), e.flatten(), q2sig.flatten()))
123
+
124
+ if args.fullprof is True:
125
+ head2 = 'XYDATA' + '\n' + 'INTER 0 0 2' + 4*'\n'
126
+ elif args.kratky is True:
127
+ head2 = "q I(q) q2I(q) sig(q) q2sig(q)"
128
+ else:
129
+ head2 = ''
130
+
131
+ np.savetxt(fname[:-4]+ext, out, delimiter=' ', fmt='%.10f',
132
+ header=head2, comments='', newline='\n')
133
+
134
+
135
+ if __name__ == '__main__':
136
+ cli()
@@ -0,0 +1,142 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Spyder Editor
4
+
5
+ This is a temporary script file.
6
+ """
7
+
8
+ import h5py
9
+ import hdf5plugin
10
+ import pyFAI
11
+ import fabio
12
+ import numpy as np
13
+ import os
14
+ import glob
15
+ import matplotlib.pyplot as plt
16
+ from matplotlib.colors import LogNorm
17
+ from mpl_toolkits.axes_grid1 import make_axes_locatable
18
+ import matplotlib.ticker as ticker
19
+ import argparse as ap
20
+
21
+ parser = ap.ArgumentParser(description=
22
+ "Perform data reduction on hdf5 data from DESY")
23
+
24
+ parser.add_argument("sample", help="sample file (.nxs)")
25
+
26
+ parser.add_argument("mask", help="mask file (.edf)")
27
+ parser.add_argument("poni", help="PONI configuration file (.poni)")
28
+ parser.add_argument("--empty", help="empty file (.nxs)")
29
+ parser.add_argument("--background", help="background file (.nxs)")
30
+
31
+ parser.add_argument("--bkg_factor", help="background correction factor",type=float,default=float(1))
32
+ parser.add_argument("--noscaling", help="background file (.nxs)",action="store_true")
33
+ parser.add_argument("--autobkg", help="Automatic background scaling",action="store_true")
34
+ args = parser.parse_args()
35
+
36
+ #path = "/home/u0092172/Documents/"
37
+ #folder = "DESY/"
38
+ #sample = "GA5/GA5_2.nxs"
39
+ #background = "water/water_2.nxs"
40
+ mask = fabio.open(args.mask)
41
+ poni = pyFAI.load(args.poni)#"config_saxs_sdetx_1500_12keV.poni")
42
+ file = h5py.File(args.sample,'r')
43
+
44
+ if args.noscaling == True:
45
+ transmission_sample = 1
46
+ else:
47
+ transmission_sample = file['scan']['data']['beamstop_2'][()]/file['scan']['data']['ic1'][()]
48
+ print("Sample transmission:"+str(round(transmission_sample,4)))
49
+
50
+ temperature = str(round(file['scan']['data']['p62']['t95tempproglinkam']['eh.01']['temperature'][()],2))
51
+ print("Sample temperature:"+temperature)
52
+
53
+
54
+ data_1D = poni.integrate1d(file['scan']['data']['saxs_raw'][()][0]/transmission_sample,
55
+ 1000, filename=args.sample+"_"+temperature+'_1D.dat', correctSolidAngle=True,
56
+ method='csr_ocl', radial_range=(None), azimuth_range=(None),
57
+ unit="q_A^-1", mask=mask.data, normalization_factor=1.0,
58
+ metadata=None,error_model="poisson")
59
+
60
+ if args.background != None:
61
+ bkg = h5py.File(args.background,'r')
62
+ if args.noscaling == True:
63
+ transmission_bkg = 1
64
+ else:
65
+ transmission_bkg = bkg['scan']['data']['beamstop_2'][()]/bkg['scan']['data']['ic1'][()]
66
+ try:
67
+ temperature_bkg = str(round(bkg['scan']['data']['p62']['t95tempproglinkam']['eh.01']['temperature'][()],2))
68
+ except KeyError:
69
+ temperature_bkg = str(-1000)
70
+ print("Background transmission:"+str(round(transmission_bkg,4)))
71
+ print("Background temperature:"+temperature_bkg)
72
+
73
+ bkg_1D = poni.integrate1d(bkg['scan']['data']['saxs_raw'][()][0]/transmission_bkg,
74
+ 1000, filename=args.sample+"_"+temperature+'_bkg1D.dat', correctSolidAngle=True,
75
+ method='csr_ocl', radial_range=(None), azimuth_range=(None),
76
+ unit="q_A^-1", mask=mask.data, normalization_factor=1.0,
77
+ metadata=None,error_model="poisson")
78
+ if args.autobkg == True:
79
+ print("Use automated background scaling with caution!")
80
+ if max(bkg_1D[1][:np.where(bkg_1D[0]>0.02)[0][0]]) > max(data_1D[1][:np.where(data_1D[0]>0.02)[0][0]]):
81
+ bkg_1D = [bkg_1D[0],bkg_1D[1],bkg_1D[2]]
82
+ bkg_1D[1] -= max(bkg_1D[1][:np.where(bkg_1D[0]>0.02)[0][0]]) - max(data_1D[1][:np.where(data_1D[0]>0.02)[0][0]])
83
+ else:
84
+ data_I = data_1D[1]
85
+ data_sig = abs(data_1D[2])
86
+ if args.empty != None:
87
+ empty = h5py.File(args.empty,'r')
88
+ if args.noscaling == True:
89
+ transmission_empty = 1
90
+ else:
91
+ transmission_empty = empty['scan']['data']['beamstop_2'][()]/empty['scan']['data']['ic1'][()]
92
+ empty_1D = poni.integrate1d(empty['scan']['data']['saxs_raw'][()][0]/transmission_empty,
93
+ 1000, filename=args.sample+"_"+temperature+'_empty1D.dat', correctSolidAngle=True,
94
+ method='csr_ocl', radial_range=(None), azimuth_range=(None),
95
+ unit="q_A^-1", mask=mask.data, normalization_factor=1.0,
96
+ metadata=None,error_model="poisson")
97
+ if args.autobkg == True:
98
+ if max(empty_1D[1][:np.where(empty_1D[0]>0.02)[0][0]]) > max(data_1D[1][:np.where(data_1D[0]>0.02)[0][0]]):
99
+ empty_1D[1] -= max(empty_1D[1][:np.where(empty_1D[0]>0.02)[0][0]]) - max(data_1D[1][:np.where(data_1D[0]>0.02)[0][0]])
100
+ if args.background != None:
101
+ data_I = (data_1D[1]-empty_1D[1])-(bkg_1D[1]-empty_1D[1])
102
+ data_sig = abs(data_1D[2])+abs(empty_1D[2])+abs(bkg_1D[2])+abs(empty_1D[2])
103
+ else:
104
+ data_I = data_1D[1]-empty_1D[1]
105
+ data_sig = abs(data_1D[2])+abs(empty_1D[2])
106
+ else:
107
+ if args.background == None:
108
+ data_I = data_1D[1]
109
+ data_sig = abs(data_1D[2])
110
+ else:
111
+ data_I = data_1D[1]-bkg_1D[1]
112
+ data_sig = abs(data_1D[2])+abs(bkg_1D[2])
113
+ fig, ax = plt.subplots(figsize=(25,10),constrained_layout=True)
114
+ """fig1 = ax[0].imshow(data_cor,norm=LogNorm(vmin=1e-1, vmax=10**3),cmap=plt.get_cmap('plasma'))
115
+ # fig1 = ax[0].imshow(data_cor,norm=LogNorm(vmin=300, vmax=10**5),cmap=plt.get_cmap('plasma'))
116
+ # create an axes on the right side of ax. The width of cax will be 5%
117
+ # of ax and the padding between cax and ax will be fixed at 0.05 inch.
118
+ divider = make_axes_locatable(ax[0])
119
+ cax = divider.append_axes("right", size="5%", pad=0.05)
120
+
121
+ cbar = plt.colorbar(fig1, cax=cax)
122
+ cbar.ax.tick_params(labelsize=25)
123
+
124
+ ax[0].set_xlabel('pixel',fontsize=25)
125
+ ax[0].set_ylabel('pixel',fontsize=25)
126
+ ax[0].tick_params(labelsize=25)
127
+ """
128
+ # fig2 = ax.plot(q, Intensity)
129
+ #fig2 = ax.plot(data_1D[0],data_I)
130
+ fig = ax.errorbar(data_1D[0],data_I,yerr=data_sig)
131
+ ax.set_xscale('log')
132
+ ax.set_yscale('log')
133
+ ax.set_xlabel(r'$Q\/[\AA^{-1}]$',fontsize=25)
134
+ ax.set_ylabel('intensity [a.u.]',fontsize=25)
135
+ # ax.set_xticks([0.01,0.1,0.4,0.6])
136
+ ax.get_xaxis().set_major_formatter(ticker.ScalarFormatter())
137
+ ax.tick_params(labelsize=25)
138
+ #plt.show()
139
+ plt.savefig(args.sample+"_"+temperature+".png", bbox_inches="tight", dpi=600)
140
+
141
+ data_out = np.column_stack((data_1D[0],data_I,data_sig))
142
+ np.savetxt(args.sample+"_subtracted.dat",data_out,delimiter='\t',fmt='%s')