diff --git a/xpdView/azimuthal.py b/xpdView/azimuthal.py index 055d0b0..f528b36 100644 --- a/xpdView/azimuthal.py +++ b/xpdView/azimuthal.py @@ -15,12 +15,71 @@ ''' This file will handle the pyFai integration for quick azimuthal integration of the data ''' +import os + +from tifffile import imread import pyFAI import pyFAI.calibrant - -class Azimuthal(object): +# init at top to save time +ai = pyFAI.AzimuthalIntegrator() + + +def _npt_cal(config_dict): + """ config_dict should be a PyFAI calibration dict """ + try: + x_0, y_0 = (config_dict['centerX'], config_dict['centerY']) + except KeyError: + print("Not a pyFAI calibration dictionary, default " + "beam center will be set") + x_0, y_0 = (1024, 1024) + dist = np.sqrt((2048-x_0)**2 + (2048-y_0)**2) + return dist + + +def xpdView_integrate(input_dir): + """ function to operate on file-based """ + tif_list = [os.path.join(input_dir, f) for f in os.listdir(input_dir) + if os.path.splitext(f)[1] == '.tif'] + # calibration dict + cfg_list = [] + cfg_inuse = None # init + + for tif in tif_list: + stem, ext = os.path.splitext(tif) + cfg_name = os.path.join(input_dir, stem + '.yaml') + if os.path.isfile(cfg_name): + with open(cfg_name, 'r') as f: + cfg_inuse = yaml.load(f) + cfg_list.append(cfg_inuse) + + while any(x is None for x in cfg_list): + ind = cfg_list.index(None) + cfg_list.remove(None) + cfg_list.insert(ind, cfg_name) + print("WARNING: calibration paramters corresponding to {} is " + "missing, use alternative set of paramters from {}\n" + "integration results might be incorrect" + .format(tif_list[ind], cfg_name)) + # use the last one, user's liability + + # integration + if len(cfg_list) == len(tif_list): + for i in range(len(tif_list)): + w_name = tif_list[i].replace('.tif', '.chi') + cfg_dict = cfg_list[i] + npt = _npt_cal(cfg_dict) + img = imread(os.path.join(input_dir, tif_list[i])) + ai.setPyFAI(**config_dict) + integration_dict = {'filename':w_name, + 'polarization_factor': 0.99} + rv = ai.integrate1d(img, npt, **integration_dict) + else: + raise ValueError("Number of images is not equal to number " + "of calibration parameter sets") + +class Azimuthal: """ This class handles all of the azimuthal integration of the data so that it can be done automatically as long as the data comes in as a list of 2D numpy arrays @@ -58,14 +117,18 @@ def get_right_names(self, file_names, data_list): Returns ------- - None - + file_data_dict : dict + a dictionary that mapps file name to 2D numpy array """ - for file in file_names: - if file[-4:] == '.tif': - self.file_names.append(file[:-4] + '.chi') + + file_data_dict = {} + for f in file_names: + stem, ext = os.path.splitext(f) + if ext == '.tif': + self.file_names.append(stem + '.chi') else: - self.file_names.append(file) + #FIXME: need to thinkg if should leave it unchanged + self.file_names.append(f) self.integration_time(data_list) @@ -83,17 +146,11 @@ def integration_time(self, data_list): None """ - # Insert pyFAI calibration parameters, can be changed if better detector parameters found - det = pyFAI.detectors.Perkin() - wl = 0.184320e-10 - ni = pyFAI.calibrant.ALL_CALIBRANTS("Ni") - ni.set_wavelength(wl) - poni1 = .1006793 * 2 - poni2 = .1000774 * 2 - ai = pyFAI.AzimuthalIntegrator(dist=0.2418217, poni1=poni1, poni2=poni2, rot1=0, rot2=0, detector=det) - ai.set_wavelength(wl) + for data in data_list: + ai.setPyFAI(**config_dict) + npt = _npt_cal(config_dict) x, y = ai.integrate1d(data, 1000, unit='q_A^-1') self.x_lists.append(x) self.y_lists.append(y)