Ejemplo n.º 1
0
    def write_smv(self, path: str, i: int) -> str:
        """Write the image+header with sequence number `i` to the directory
        `path` in SMV format.

        Returns the path to the written image.
        """
        img = self.data[i]
        h = self.headers[i]

        img = np.ushort(img)
        shape_x, shape_y = img.shape

        phi = self.start_angle + self.osc_angle * (i - 1)

        # TODO: Dials reads the beam_center from the first image and uses that for the whole range
        # For now, use the average beam center and consider it stationary, remove this line later
        mean_beam_center = self.mean_beam_center

        try:
            date = str(datetime.fromtimestamp(h['ImageGetTime']))
        except BaseException:
            date = '0'

        header = collections.OrderedDict()
        header['HEADER_BYTES'] = 512
        header['DIM'] = 2
        header['BYTE_ORDER'] = 'little_endian'
        header['TYPE'] = 'unsigned_short'
        header['SIZE1'] = shape_x
        header['SIZE2'] = shape_y
        header['PIXEL_SIZE'] = self.physical_pixelsize
        header['BIN'] = '1x1'
        header['BIN_TYPE'] = 'HW'
        header['ADC'] = 'fast'
        header['CREV'] = 1
        header['BEAMLINE'] = self.name  # special ID for DIALS
        header['DETECTOR_SN'] = 901  # special ID for DIALS
        header['DATE'] = date
        header['TIME'] = str(h['ImageExposureTime'])
        header['DISTANCE'] = f'{self.distance:.4f}'
        header['TWOTHETA'] = 0.00
        header['PHI'] = '{phi:.4f}'
        header['OSC_START'] = f'{phi:.4f}'
        header['OSC_RANGE'] = f'{self.osc_angle:.4f}'
        header['WAVELENGTH'] = f'{self.wavelength:.4f}'
        # reverse XY coordinates for XDS
        header['BEAM_CENTER_X'] = f'{mean_beam_center[1]:.4f}'
        header['BEAM_CENTER_Y'] = f'{mean_beam_center[0]:.4f}'
        header[
            'DENZO_X_BEAM'] = f'{mean_beam_center[0]*self.physical_pixelsize:.4f}'
        header[
            'DENZO_Y_BEAM'] = f'{mean_beam_center[1]*self.physical_pixelsize:.4f}'
        fn = path / f'{i:05d}.img'
        write_adsc(fn, img, header=header)
        return fn
Ejemplo n.º 2
0
def test_smv(data, header):
    out = 'out.smv'

    formats.write_adsc(out, data, header)

    assert os.path.exists(out)

    img, h = formats.read_image(out)

    assert np.allclose(img, data)
    assert 'value' in h  # changes type to str
    assert h['string'] == header['string']
Ejemplo n.º 3
0
    beam_x, beam_y = find_beam_center_blur(img)
    centers.append((beam_x, beam_y))

    # get image center
    center_x, center_y = (np.array(img.shape) / 2).astype(int)

    shift_x = center_x - beam_x
    shift_y = center_y - beam_y

    # shift the beam to the center of the image
    shifted = ndi.shift(img, (shift_x, shift_y))

    if binning > 1:
        shifted = ndi.zoom(shifted, scale)
        pixel_size = float(header["PIXEL_SIZE"])
        header["PIXEL_SIZE"] = str(pixel_size / binning)
        header["SIZE1"] = str(shifted.shape[0])
        header["SIZE2"] = str(shifted.shape[1])
        header["BEAM_CENTER_X"] = str(center_x / binning)
        header["BEAM_CENTER_Y"] = str(center_y / binning)

    out = fn.stem + "-centered.img"
    write_adsc(fname=str(out), data=shifted, header=header)

# Plot centers of the direct beam

beam_x, beam_y = np.array(centers).T
plt.plot(beam_x, beam_y)
plt.xlabel("X axis (px)")
plt.ylabel("Y axis (px)")
plt.show()