Skip to content

Commit 399d07e

Browse files
committed
Finalizing changes to chunk loading for SST1RSoXSLoader after testing
1 parent 0bc289b commit 399d07e

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/PyHyperScattering/SST1RSoXSLoader.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212
import dask.array as da
1313

14+
1415
class SST1RSoXSLoader(FileLoader):
1516
'''
1617
Loader for TIFF files from NSLS-II SST1 RSoXS instrument
@@ -29,7 +30,6 @@ def __init__(self,corr_mode=None,user_corr_func=None,dark_pedestal=100,exposure_
2930
dark_pedestal (numeric): value to subtract(/add, if negative) to the whole image. this should match the instrument setting for suitcased tiffs, typically 100.
3031
exposure_offset (numeric): value to add to the exposure time. Measured at 2ms with the piezo shutter in Dec 2019 by Jacob Thelen, NIST
3132
constant_md (dict): values to insert into every metadata load.
32-
use_chunked_loading (bool): flag to use chunked loading with dask or not.
3333
'''
3434

3535
if corr_mode == None:
@@ -39,13 +39,14 @@ def __init__(self,corr_mode=None,user_corr_func=None,dark_pedestal=100,exposure_
3939
else:
4040
self.corr_mode = corr_mode
4141

42+
4243
self.constant_md = constant_md
44+
4345
self.dark_pedestal = dark_pedestal
4446
self.user_corr_func = user_corr_func
4547
self.exposure_offset = exposure_offset
4648
self.use_chunked_loading = use_chunked_loading
4749
# self.darks = {}
48-
4950
# def loadFileSeries(self,basepath):
5051
# try:
5152
# flist = list(basepath.glob('*primary*.tiff'))
@@ -61,6 +62,8 @@ def __init__(self,corr_mode=None,user_corr_func=None,dark_pedestal=100,exposure_
6162
#
6263
# return out
6364

65+
66+
6467
def loadSingleImage(self,filepath,coords=None, return_q=False,image_slice=None,use_cached_md=False,**kwargs):
6568
'''
6669
HELPER FUNCTION that loads a single image and returns an xarray with either pix_x / pix_y dimensions (if return_q == False) or qx / qy (if return_q == True)
@@ -79,12 +82,11 @@ def loadSingleImage(self,filepath,coords=None, return_q=False,image_slice=None,u
7982
raise NotImplementedError('Image slicing is not supported for SST1')
8083
if use_cached_md != False:
8184
raise NotImplementedError('Caching of metadata is not supported for SST1')
82-
85+
8386
img = np.array(Image.open(filepath))
8487

8588
if self.use_chunked_loading:
8689
img = da.from_array(img, chunks='auto')
87-
8890

8991
headerdict = self.loadMd(filepath)
9092
# two steps in this pre-processing stage:
@@ -117,7 +119,6 @@ def loadSingleImage(self,filepath,coords=None, return_q=False,image_slice=None,u
117119
# # step 2: dark subtraction
118120
# this is already done in the suitcase, but we offer the option to add/subtract a pedestal.
119121
image_data = (img-self.dark_pedestal)/corr
120-
image_data = img
121122
if return_q:
122123
qpx = 2*np.pi*60e-6/(headerdict['sdd']/1000)/(headerdict['wavelength']*1e10)
123124
qx = (np.arange(1,img.size[0]+1)-headerdict['beamcenter_y'])*qpx
@@ -130,10 +131,19 @@ def loadSingleImage(self,filepath,coords=None, return_q=False,image_slice=None,u
130131

131132
def read_json(self,jsonfile):
132133
json_dict = {}
133-
with open(jsonfile) as f:
134-
data = json.load(f)
135-
meas_time =datetime.datetime.fromtimestamp(data[1]['time'])
136-
json_dict['sample_name'] = data[1]['sample_name']
134+
135+
# Try / except statment to be compatible with local rsoxs data before
136+
# and after SST1 cycle 2 2023
137+
try: # For earlier cyles
138+
with open(jsonfile) as f:
139+
data = json.load(f)
140+
meas_time = datetime.datetime.fromtimestamp(data[1]['time'])
141+
except KeyError: # For later cycles (confirmed working for cycle 2 2023)
142+
with open(jsonfile) as f:
143+
data = [0, json.load(f)] # Quick fix to load the json in a list
144+
meas_time = datetime.datetime.fromtimestamp(data[1]['time'])
145+
146+
json_dict['sample_name'] = data[1]['sample_name']
137147
if data[1]['RSoXS_Main_DET'] == 'SAXS':
138148
json_dict['rsoxs_config'] = 'saxs'
139149
# discrepency between what is in .json and actual
@@ -155,7 +165,7 @@ def read_json(self,jsonfile):
155165
json_dict['beamcenter_y'] = data[1]['RSoXS_SAXS_BCY']
156166
json_dict['sdd'] = data[1]['RSoXS_SAXS_SDD']
157167

158-
elif data[1]['RSoXS_Main_DET'] == 'WAXS':
168+
elif (data[1]['RSoXS_Main_DET'] == 'WAXS') | (data[1]['RSoXS_Main_DET'] == 'waxs_det'): # additional name for for cycle 2 2023
159169
json_dict['rsoxs_config'] = 'waxs'
160170
if (meas_time > datetime.datetime(2020,11,16)) and (meas_time < datetime.datetime(2021,1,15)):
161171
json_dict['beamcenter_x'] = 400.46
@@ -172,7 +182,7 @@ def read_json(self,jsonfile):
172182
json_dict['sdd'] = data[1]['RSoXS_WAXS_SDD']
173183

174184
else:
175-
json_dict['rsoxs_config'] == 'unknown'
185+
json_dict['rsoxs_config'] = 'unknown'
176186
warnings.warn('RSoXS_Config is neither SAXS or WAXS. Check json file',stacklevel=2)
177187

178188
if json_dict['sdd'] == None:
@@ -252,9 +262,15 @@ def loadMd(self,filepath):
252262
else:
253263
cwd = pathlib.Path(dirPath)
254264

255-
json_fname = list(cwd.glob('*.jsonl'))
256-
json_dict = self.read_json(json_fname[0])
257-
265+
# Another try/except statement to be compatible with local data pre and
266+
# post SST1 cycle 2 2023
267+
try: # For earlier data
268+
json_fname = list(cwd.glob('*.jsonl'))
269+
json_dict = self.read_json(json_fname[0])
270+
except IndexError: # For later data (works for cycle 2 2023)
271+
json_fname = list(cwd.glob('*.json')) # Changed '*.jsonl' to '*.json'
272+
json_dict = self.read_json(json_fname[0])
273+
258274
baseline_fname = list(cwd.glob('*baseline.csv'))
259275
baseline_dict = self.read_baseline(baseline_fname[0])
260276

0 commit comments

Comments
 (0)