Esempio n. 1
0
def gridmap(specfile, scan_nb, mydetector, region=None, myflatfield=None, myhotpixels=None, reload=0,
            previous_data=None, previous_mask=None, mysetup='ID01', myrocking_angle="outofplane",
            follow_bragg=0, myenergy=None, myoffsets=(0, 0, 0, 0, 0)):
    """
    Load the data, check for saturated pixels, interpolate on an orthogonal grid
    :param specfile:
    :param scan_nb:
    :param mydetector: "Eiger4M" or "Eiger2M" or "Maxipix"
    :param region: roi on the detector
    :param myflatfield: 2D array, flatfield correction for the detector
    :param myhotpixels: 2D array, detector hotpixels to be masked
    :param reload: 1 when you reload the data
    :param previous_data: when you reload the data
    :param previous_mask: when you reload the data
    :param mysetup: 'ID01' or 'SIXS' or 'CRISTAL', different data loading method
    :param myrocking_angle: name of the motor which is tilted during the rocking curve
    :param follow_bragg: for energy_scan, set to 1 if the detector is scanned to follow the Bragg peak (delta @ ID01)
    :param myenergy: energy in eV of the experiment, in case it is not in the spec file
    :param myoffsets: sample and detector offsets for xrayutilities
    :return:
    """
    global sixs_beta, nb_pixel_x, nb_pixel_y
    if region is None:
        if mydetector == "Eiger2M":
            region = [0, nb_pixel_y, 0, nb_pixel_x]
        elif mydetector == "Maxipix":
            region = [0, nb_pixel_y, 0, nb_pixel_x]
        elif mydetector == "Eiger4M":
            region = [0, nb_pixel_y, 0, nb_pixel_x]
        else:
            region = [0, nb_pixel_y, 0, nb_pixel_x]
    if mysetup == 'ID01':
        motor_names = specfile[str(scan_nb) + '.1'].motor_names  # positioners
        motor_positions = specfile[str(scan_nb) + '.1'].motor_positions  # positioners
        labels = specfile[str(scan_nb) + '.1'].labels  # motor scanned
        labels_data = specfile[str(scan_nb) + '.1'].data  # motor scanned
        chi = 0
        delta = motor_positions[motor_names.index('del')]
        nu = motor_positions[motor_names.index('nu')]
        if myrocking_angle == "outofplane":
            eta = labels_data[labels.index('eta'), :]
            phi = motor_positions[motor_names.index('phi')]
            myenergy = motor_positions[motor_names.index('nrj')]  # in kev
        elif myrocking_angle == "inplane":
            phi = labels_data[labels.index('phi'), :]
            eta = motor_positions[motor_names.index('eta')]
            myenergy = motor_positions[motor_names.index('nrj')]  # in kev
        elif myrocking_angle == "energy":
            myenergy = labels_data[labels.index('energy'), :]  # in kev
            if follow_bragg == 1:
                delta = labels_data[labels.index('del'), :]
                # TODO: understand why Qx is positive
            phi = motor_positions[motor_names.index('phi')]
            eta = motor_positions[motor_names.index('eta')]
        else:
            print("Error in rocking angle definition")
            sys.exit()
        myenergy = myenergy * 1000.0  # switch to eV
        if isinstance(myenergy, float) and myenergy < 0:
            print("Energy not correctly defined in spec file, default to 9keV")
            myenergy = 9000.0
    elif mysetup == 'SIXS':
        mydataset = nxsReady.DataSet(datadir + ccdfiletmp % scan_nb, ccdfiletmp % scan_nb, scan="SBS")
        img = mydataset.mfilm[1:, :, :]  # first frame is duplicated
        delta = mydataset.delta[1:].mean()  # not scanned
        gamma = mydataset.gamma[1:].mean()  # not scanned
        mu = mydataset.mu[1:]
    elif mysetup == 'CRISTAL':
        omega = specfile['a']['scan_data']['actuator_1_1'][:] / 1e6
        delta = specfile['a/CRISTAL/I06-C-C07__EX__DIF-DELTA__#1/raw_value'][:]
        nu = specfile['a/CRISTAL/I06-C-C07__EX__DIF-GAMMA__#1/raw_value'][:]
        maxpix_img = specfile['a']['scan_data']['data_04'][:]
    elif mysetup == 'P10':
        # TODO: find motors in specfile
        mu = []
        for index in range(25):  # header
            specfile.readline()
        for index, myline in enumerate(specfile, 0):
            myline = myline.strip()
            mycolumns = myline.split()
            if mycolumns[0] == '!':
                break
            mu.append(mycolumns[0])
        mu = np.asarray(mu, dtype=float)
        nb_img = len(mu)
        specfile.close()

    if reload == 0:
        if mydetector == "Eiger2M":
            counter = 'ei2minr'
            mymask = np.zeros((nb_pixel_y, nb_pixel_x))
        elif mydetector == "Maxipix":
            counter = 'mpx4inr'
            # counter = 'roi7'
            mymask = np.zeros((nb_pixel_y, nb_pixel_x))
        elif mydetector == "Eiger4M":
            mymask = np.zeros((nb_pixel_y, nb_pixel_x))
        else:
            counter = 'mpx4inr'
            mymask = np.zeros((nb_pixel_y, nb_pixel_x))

        if myflatfield is None:
            myflatfield = np.ones(mymask.shape)
        if myhotpixels is None:
            myhotpixels = np.zeros(mymask.shape)
        if mysetup == 'ID01':
            ccdn = labels_data[labels.index(counter), :]
            rawdata = np.zeros((len(ccdn), region[1] - region[0], region[3] - region[2]))
            for index in range(len(ccdn)):
                i = int(ccdn[index])
                e = fabio.open(ccdfiletmp % i)
                ccdraw = e.data
                ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask)
                if mydetector == "Eiger2M":
                    ccdraw, mymask = mask_eiger(ccdraw, mymask)
                elif mydetector == "Maxipix":
                    ccdraw, mymask = mask_maxipix(ccdraw, mymask)
                ccdraw = myflatfield * ccdraw
                ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region)
                rawdata[int(i - ccdn[0]), :, :] = ccd
        elif mysetup == 'SIXS':
            nb_img = img.shape[0]
            rawdata = np.zeros((nb_img, region[1] - region[0], region[3] - region[2]))
            for index in range(nb_img):
                ccdraw = img[index, :, :]  # first image is duplicated
                ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask)
                if mydetector == "Eiger2M":
                    ccdraw, mymask = mask_eiger(ccdraw, mymask)
                elif mydetector == "Maxipix":
                    ccdraw, mymask = mask_maxipix(ccdraw, mymask)
                ccdraw = myflatfield * ccdraw
                ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region)
                rawdata[index, :, :] = ccd
        elif mysetup == 'CRISTAL':
            nb_img = omega.shape[0]
            rawdata = np.zeros((nb_img, region[1] - region[0], region[3] - region[2]))
            for index in range(nb_img):
                ccdraw = maxpix_img[index, :, :]
                ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask)
                ccdraw, mymask = mask_maxipix(ccdraw, mymask)
                ccdraw = myflatfield * ccdraw
                ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region)
                rawdata[index, :, :] = ccd
        elif mysetup == 'P10':
            rawdata = np.zeros((nb_img, region[1] - region[0], region[3] - region[2]))
            for index in range(nb_img):
                h5file = h5py.File(ccdfiletmp % (index + 1), 'r')
                try:
                    ccdraw = h5file['entry']['data']['data'][:].sum(axis=0)
                except OSError:
                    print('hdf5plugin is not installed')
                    sys.exit()
                ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask)
                ccdraw, mymask = mask_eiger4m(ccdraw, mymask)
                ccdraw = myflatfield * ccdraw
                ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region)
                rawdata[index, :, :] = ccd

        mymask = mymask[region[0]:region[1], region[2]:region[3]]
        rawdata, mymask = check_pixels(rawdata, mymask, var_threshold=5, debugging=0)  # additional check for hotpixels
        numz, numy, numx = rawdata.shape
        rawmask3d = np.repeat(mymask[np.newaxis, :, :], numz, axis=0)
        rawmask3d[np.isnan(rawdata)] = 1
        rawdata[np.isnan(rawdata)] = 0
    else:
        rawmask3d = previous_mask
        rawdata = previous_data
        numz, numy, numx = rawdata.shape
    # transform scan angles to reciprocal space coordinates for all detector pixels
    if mysetup == 'ID01':
        myqx, myqy, myqz = hxrd.Ang2Q.area(eta, chi, phi, nu, delta, en=myenergy, delta=myoffsets)
        mygridder = xu.Gridder3D(numz, numy, numx)
        # convert mask to rectangular grid in reciprocal space
        mygridder(myqx, myqz, myqy, rawmask3d)
        mymask3d = np.copy(mygridder.data)
        # convert data to rectangular grid in reciprocal space
        mygridder(myqx, myqz, myqy, rawdata)
        return mygridder.xaxis, mygridder.yaxis, mygridder.zaxis, rawdata, mygridder.data, rawmask3d, mymask3d

    elif mysetup == 'SIXS':
        if myenergy is None:
            print('Defaulting energy to 8.5keV')
            myenergy = 8500
        myqx, myqy, myqz = hxrd.Ang2Q.area(sixs_beta, mu, sixs_beta, gamma, delta, en=myenergy, delta=myoffsets)
        mygridder = xu.Gridder3D(numz, numy, numx)
        # convert mask to rectangular grid in reciprocal space
        mygridder(myqx, myqz, myqy, rawmask3d)
        mymask3d = np.copy(mygridder.data)
        # convert data to rectangular grid in reciprocal space
        mygridder(myqx, myqz, myqy, rawdata)
        return mygridder.xaxis, mygridder.yaxis, mygridder.zaxis, rawdata, mygridder.data, rawmask3d, mymask3d

    elif mysetup == 'CRISTAL':
        # TODO: implement this for CRISTAL
        print('Gridder not yet implemented for CRISTAL setup')
        return 0, 0, 0, rawdata, 0, rawmask3d, 0
    elif mysetup == 'P10':
        # TODO: implement this for P10
        print('Gridder not yet implemented for P10 setup')
        return 0, 0, 0, rawdata, 0, rawmask3d, 0
    else:
        print('Wrong setup')
        sys.exit()
Esempio n. 2
0
    return mydata, mymask


###########################################################################
scanlist = np.arange(start_scan, stop_scan + 1, 1)
if flatfield_file != "" and flatfield_file != '':
    flatfield = np.load(flatfield_file)
    npz_key = flatfield.keys()
    flatfield = flatfield[npz_key[0]]
else:
    flatfield = np.ones((roi[1] - roi[0], roi[3] - roi[2]))
mask = np.zeros((516, 516))

# load first scan to get the data size
dataset = nxsReady.DataSet(datadir + ccdfiletmp % start_scan,
                           ccdfiletmp % start_scan,
                           scan="SBS")
img_per_scan = dataset.mfilm[1:, :, :].shape[0]  # first image is repeated
nb_img = img_per_scan * len(scanlist)
raw_gamma = np.zeros(nb_img)
raw_delta = np.zeros(nb_img)

rawdata = np.zeros((nb_img, roi[1] - roi[0], roi[3] - roi[2]))
data = np.zeros(
    (nb_img - len(frames_to_exclude), roi[1] - roi[0], roi[3] - roi[2]))
eta = np.zeros(nb_img - len(frames_to_exclude))
delta = np.zeros(nb_img - len(frames_to_exclude))
gamma = np.zeros(nb_img - len(frames_to_exclude))
sum_data = np.zeros((roi[1] - roi[0], roi[3] - roi[2]))

for index in range(len(scanlist)):