Exemple #1
0
def extract_to_minicbf(h5master, frameno_or_path, cbfout, binning=1):
    from yamtbx.dataproc import cbf
    from yamtbx.dataproc.XIO.plugins import eiger_hdf5_interpreter

    if type(frameno_or_path) in (tuple, list):
        data = extract_data_range_sum(h5master, frameno_or_path)
        nframes = len(frameno_or_path)
    elif type(frameno_or_path) is int:
        data = extract_data(h5master, frameno_or_path)
        nframes = 1
    else:
        data = extract_data_path(h5master, frameno_or_path)
        nframes = 1

    if data is None:
        raise RuntimeError("Cannot extract frame %s from %s" %
                           (frameno_or_path, h5master))

    h = eiger_hdf5_interpreter.Interpreter().getRawHeadDict(h5master)
    h5 = h5py.File(h5master, "r")

    if binning > 1:
        beamxy = h["BeamX"], h["BeamY"]
        data, (h["BeamX"],
               h["BeamY"]) = software_binning(data, binning, beamxy)

    h["Detector"] = "Unknown"
    if "/entry/instrument/detector/description" in h:  # EIGER2 does not have this field?
        h["Detector"] = h5["/entry/instrument/detector/description"].value

    h["ExposurePeriod"] = h5["/entry/instrument/detector/frame_time"].value
    h["PhiWidth"] *= nframes
    cbf.save_numpy_data_as_cbf(data.flatten(),
                               size1=data.shape[1],
                               size2=data.shape[0],
                               title="",
                               cbfout=cbfout,
                               pilatus_header="""\
# Detector: %(Detector)s, S/N %(SerialNumber)s
# Pixel_size %(PixelX)e m x %(PixelY)e m
# %(SensorMaterial)s sensor, thickness %(SensorThickness).3e m
# Exposure_time %(ExposureTime).6f s
# Exposure_period %(ExposurePeriod).6f s
# Count_cutoff %(Overload)d counts
# Wavelength %(Wavelength).6f A
# Detector_distance %(Distance).3e m
# Beam_xy (%(BeamX).1f, %(BeamY).1f) pixels
# Start_angle %(PhiStart).6f deg.
# Angle_increment %(PhiWidth).6f deg.
""" % h)
Exemple #2
0
    def read_h5file(self):
        self.cmbInfile.Clear()
        for f in sorted(
                glob.glob(
                    os.path.join(os.path.dirname(self.h5file),
                                 "*_master.h5"))):
            self.cmbInfile.Append(f)
        self.cmbInfile.SetStringSelection(self.h5file)

        try:
            h5 = h5py.File(self.h5file, "r")
            h = eiger_hdf5_interpreter.Interpreter().getRawHeadDict(
                self.h5file)
            self.n_images = h["Nimages"]

            self.txtInfo.SetValue("""\
         Filename: %s
 Number of frames: %d
             Date: %s
 Phi start, width: %.6f, %.6f deg
  Camera distance: %.2f mm
       Wavelength: %.6f A
    Exposure time: %.6f sec
      Beam center: (%d, %d) px
    """ % (os.path.basename(self.h5file), self.n_images, h["DateStr"].replace(
                "T", " "), h["PhiStart"], h["PhiWidth"], h["Distance"] * 1.e3,
            h["Wavelength"], h["ExposureTime"], h["BeamX"], h["BeamY"]))
        except:
            self.n_images = 0
            self.txtInfo.SetValue(traceback.format_exc())

        self.cmbFrame.Clear()
        values = filter(
            lambda x: x > 0,
            sorted(
                set([
                    1,
                ] + map(lambda x: int(self.n_images * x), (3 / 4., 1 / 4.,
                                                           1 / 2., 1)))))
        for v in values:
            self.cmbFrame.Append(str(v))

        self.change_frameno([1])
Exemple #3
0
    def read_h5file(self):
        self.cmbInfile.Clear()
        glob_f = lambda x: glob.glob(os.path.join(os.path.dirname(self.h5file), x))
        for f in sorted(glob_f("*_master.h5")+glob_f("*_onlyhits.h5")): self.cmbInfile.Append(f)
        self.cmbInfile.SetStringSelection(self.h5file)
        self.lstTrigger.ClearAll()

        widgets = self.spnFrame, self.chkSum, self.spnSumFrame, self.spnSumDeg, self.chkKeepFrame
        
        is_onlyhits = self.h5file.endswith("_onlyhits.h5")
        try:
            h5 = h5py.File(self.h5file, "r")
            h = eiger_hdf5_interpreter.Interpreter().getRawHeadDict(self.h5file)
            self.n_images = h["Nimages"]
            self.n_images_each = h["Nimages_each"]
        except:
            self.n_images = self.n_images_each = 0
            self.txtInfo.SetValue(traceback.format_exc())
            return

        try:
            safe_val = lambda x, k, a: x[k].value if k in x else a
            yes_or_no = lambda x, k: ("no", "yes", "?")[safe_val(x, k, 2)]
            self.txtInfo.SetValue("""\
         Filename: %s
         Detector: %s (%s)
 Number of frames: %d (%d * %d trigger)
             Date: %s
       Omega step: %.4f deg
  Camera distance: %.2f mm
       Wavelength: %.5f A
       Frame time: %.6f sec 
      Beam center: (%d, %d) px
      Corrections: countrate=%s 
                   flatfield=%s
                   efficiency=%s
         ROI mode: %s
           Sensor: %s %.1f um
     Eiger fw ver: %s
    """ % (os.path.basename(self.h5file),
           safe_val(h5["/entry/instrument/detector"], "description", "?"), safe_val(h5["/entry/instrument/detector"], "detector_number", "?"),
           self.n_images, self.n_images_each, h["Ntrigger"],
           h["DateStr"].replace("T", " "),
           h["PhiWidth"],
           h["Distance"]*1.e3,
           h["Wavelength"],
           safe_val(h5["/entry/instrument/detector"], "frame_time", float("nan")),
           h["BeamX"], h["BeamY"],
           yes_or_no(h5["/entry/instrument/detector"], "countrate_correction_applied"),
           yes_or_no(h5["/entry/instrument/detector"], "flatfield_correction_applied"),
           yes_or_no(h5["/entry/instrument/detector"], "efficiency_correction_applied"),
           safe_val(h5["/entry/instrument/detector/detectorSpecific"], "roi_mode", "?"),
           h.get("SensorMaterial", "?"), h.get("SensorThickness", float("nan"))*1.e6,
           safe_val(h5["/entry/instrument/detector/detectorSpecific"], "eiger_fw_version", "?")
    ))
        except:
            self.txtInfo.SetValue(traceback.format_exc())

        if is_onlyhits:
            for w in widgets: w.Disable()
            self.stFrameRange.SetLabel("")
            self.lstTrigger.InsertColumn(0, "Frame", width=55)
            self.lstTrigger.InsertColumn(1, "Spots", width=55)
            self.onlyhits_keys = sorted(h5["/entry/data"].keys())
            if not self.onlyhits_keys: return
            for i, v in enumerate(self.onlyhits_keys):
                nspots = h5["/entry/data"][v]["data"].attrs.get("n_spots")
                nspots = "%3d" % nspots if nspots is not None else ""
                frame = int(v[v.rindex("_")+1:])
                self.lstTrigger.InsertStringItem(i, str(frame))
                self.lstTrigger.SetStringItem(i, 1, nspots)
            self.lstTrigger.Select(0) # this calls an event to load image

        else:
            for w in widgets: w.Enable()
            self.onlyhits_keys = []
            self.stFrameRange.SetLabel(" (from 1 to %d)"%self.n_images_each)
            self.spnFrame.SetRange(1, self.n_images_each)
            self.spnFrame.SetIncrement(1)
            self.spnSumFrame.SetRange(1, self.n_images_each)
            self.spnSumDeg.SetIncrement(h["PhiWidth"])
            self.spnSumDeg.SetRange(h["PhiWidth"], h["PhiWidth"]*self.n_images_each)
            if h["PhiWidth"] > 1e-5:
                self.spnSumDeg.Enable()
                self.spnSumFrame.SetValue(int(1./h["PhiWidth"]+.5))
                self.onSpnSumFrame(None)
            else:
                self.spnSumFrame.SetValue(1)
                self.spnSumDeg.Disable()
                
            self.lstTrigger.InsertColumn(0, "Trigger")
            self.lstTrigger.InsertColumn(1, "Omega")
            omega = h5["/entry/sample/goniometer/omega"]
            for i in xrange(h["Ntrigger"]):
                self.lstTrigger.InsertStringItem(i, "%d"%(i+1))
                try:
                    oms = omega[i*self.n_images_each]
                    self.lstTrigger.SetStringItem(i, 1, "%.3f"%oms)
                except: pass

            self.lstTrigger.Select(0) # this calls an event to load image