Example #1
0
def cbf_to_dat(lpin):
    lpdir = os.path.dirname(lpin)
    filename = None
    params = {}

    ofs_abs = open(os.path.join(lpdir, "corfac_absorp.dat"), "w")
    ofs_abs.write("file ix xmin xmax ipos posx posy fac\n")
    ofs_mod = open(os.path.join(lpdir, "corfac_modpix.dat"), "w")
    ofs_mod.write("file ix xmin xmax iy ymin ymax fac\n")
    ofs_dec = open(os.path.join(lpdir, "corfac_decay.dat"), "w")
    ofs_dec.write("file ix xmin xmax iy ymin ymax fac\n")

    for l in open(lpin):
        if l.startswith(" CORRECTION FACTORS for visual inspection"):
            filename = l.split()[-1]
        elif l.startswith(" NUMBER OF REFLECTIONS USED FOR DETERMINING"):
            nref = int(l.split()[-1]) # not used now

            # open cbf
            data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(os.path.join(lpdir, filename))
            data = data.reshape(ndimmid, ndimfast)

            filenum = filename[filename.index("_")+1:-4]
            xmin, xmax = float(params["XMIN"][0]), float(params["XMAX"][0])
            nxbin = int(params["NXBIN"][0])
            xstep = (xmax-xmin)/nxbin

            if "ABSORP_" in filename:
                for ix in xrange(nxbin):
                    x1, x2 = xmin + ix*xstep, xmin + (ix+1)*xstep
                    for ipos in xrange(len(params["DETECTOR_SURFACE_POSITION"])):
                        pos = params["DETECTOR_SURFACE_POSITION"][ipos]
                        fac = data[ipos, ix]
                        ofs_abs.write("%s %2d %.2f %.2f %2d %s %5d\n" % (filenum, ix, x1, x2, ipos, pos, fac))
            elif "DECAY_" in filename or "MODPIX_" in filename:
                ymin, ymax = float(params["YMIN"][0]), float(params["YMAX"][0])
                nybin = int(params["NYBIN"][0])
                ystep = (ymax-ymin)/nybin

                for ix in xrange(nxbin):
                    x1, x2 = xmin + ix*xstep, xmin + (ix+1)*xstep
                    for iy in xrange(nybin):
                        y1, y2 = ymin + iy*ystep, ymin + (iy+1)*ystep
                        fac = data[iy, ix]
                        ofs = ofs_mod if "MODPIX_" in filename else ofs_dec
                        ofs.write("%s %2d %.2f %.2f %2d %.5f %.5f %5d\n" % (filenum, ix, x1, x2, iy, y1, y2, fac))
            else:
                print "What is this file!?", filename

            # reset
            filename = None
            params = {}
        elif filename is not None:
            r = xds.re_xds_kwd.findall(l)
            for k, v in r:
                params.setdefault(k, []).append(v)
Example #2
0
def run(cbfin, cbfout, repl=None):
    data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin)
    max_I = max(data)

    if repl is None:
        repl = max_I+100

    data[data<0] = repl
    cbf.save_numpy_data_as_cbf(data, ndimfast, ndimmid, "negative_replaced", cbfout)

    print "Launch:"
    print "adxv -overload %d %s" % (max_I, cbfout)
def convert(cbfin, params):
    if params.byteoffset:
        import yamtbx_byteoffset_h5_ext
        import pyublas

    h5out = os.path.basename(cbfin) + ".h5"

    data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin)
    data = data.reshape((ndimmid, ndimfast))
    if params.decompose:
        data = decompose_panels(data)
    header = XIO.Image(cbfin).header

    of = h5py.File(h5out, "w")

    grp = of.create_group("LCLS")
    dset = grp.create_dataset("photon_energy_eV", (1, ), dtype=numpy.float)
    dset[...] = 12398.4 / header["Wavelength"]
    dset = grp.create_dataset("photon_wavelength_A", (1, ), dtype=numpy.float)
    dset[...] = header["Wavelength"]
    dset = grp.create_dataset("adu_per_eV", (1, ), dtype=numpy.float)
    dset[...] = header["Wavelength"] / 12398.4
    dset = grp.create_dataset("detector_distance_m", (1, ), dtype=numpy.float)
    dset[...] = header["Distance"] / 1000.
    dset = grp.create_dataset("beam_xy_px", (2, ), dtype=numpy.float)
    dset[...] = (header["BeamX"] / header["PixelX"],
                 header["BeamY"] / header["PixelY"])
    dset = grp.create_dataset("osc_step_deg", (1, ), dtype=numpy.float)
    dset[...] = header["PhiWidth"]

    cbfin = os.path.abspath(cbfin)
    dset = grp.create_dataset("original_file", (1, ), "S%d" % len(cbfin))
    dset[...] = cbfin

    grp = of.create_group("data")
    if params.byteoffset:
        assert data.dtype == numpy.int32
        # hid_t group_id, const std::string &name, const pyublas::numpy_vector<int> &data, int width, int height)
        yamtbx_byteoffset_h5_ext.write_byteoffset_data(grp.id.id, "data",
                                                       data.ravel(),
                                                       data.shape[1],
                                                       data.shape[0])
    else:
        dset = grp.create_dataset("data", data.shape,
                                  dtype=data.dtype)  #, compression="CBF")
        dset[...] = data

    of.close()

    print "Processed: %s" % os.path.basename(cbfin)

    return data.shape
def run(cbfin, cbfout, repl=None):
    data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin)
    max_I = max(data)

    if repl is None:
        repl = max_I + 100

    data[data < 0] = repl
    cbf.save_numpy_data_as_cbf(data, ndimfast, ndimmid, "negative_replaced",
                               cbfout)

    print "Launch:"
    print "adxv -overload %d %s" % (max_I, cbfout)
Example #5
0
def run(files, cbfout):
    merged = None
    for i, f in enumerate(files):
        repl = -10 * (i+1)
        print "%s %d" % (f, repl)

        data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(f)
        if i == 0:
            merged = data.copy()
            continue

        merged[data==-10] = 65540 # repl # for adxv visualization. only assuming two files.

    cbf.save_numpy_data_as_cbf(merged, ndimfast, ndimmid, "merged_predictions", cbfout)
Example #6
0
def tst_h5toxds():
    print "Testing H5ToXds.."
    rcode, out, err = util.call("H5ToXds")
    ignore_msg = "(You can ignore this if you don't process hdf5 files which usually mean Eiger data)"

    if rcode == 127:  # 127 is "command not found".
        print "  Not installed. NG %s" % ignore_msg
        return False

    import numpy
    from yamtbx.dataproc.eiger import make_dummy_h5_for_test
    from yamtbx.dataproc import cbf

    tmpdir = util.get_temp_local_dir("h5test")
    data = numpy.random.randint(0, 65535,
                                size=100).astype(numpy.uint32).reshape(
                                    (1, 10, 10))
    master_h5 = make_dummy_h5_for_test(tmpdir, data)
    rcode, out, err = util.call("H5ToXds",
                                "%s 1 1.cbf" % os.path.basename(master_h5),
                                wdir=tmpdir)
    cbfout = os.path.join(tmpdir, "1.cbf")
    if not os.path.isfile(cbfout):
        print "  H5ToXds exists, but not works. Probably Dectris original H5ToXds? Test it with real h5 file. %s" % ignore_msg
        if out.strip():
            print "  -- stdout:"
            print out
        if err.strip():
            print "  -- stderr:"
            print err

        shutil.rmtree(tmpdir)
        return False

    data_read, _, _ = cbf.load_minicbf_as_numpy(cbfout)
    shutil.rmtree(tmpdir)

    if numpy.all(data_read.flatten() == data.flatten()):
        print "  OK"
        return True
    else:
        print "  H5ToXds exists, but not correctly works. Probably Dectris original H5ToXds? Test it with real h5 file. %s" % ignore_msg
        if out.strip():
            print "  -- stdout:"
            print out
        if err.strip():
            print "  -- stderr:"
            print err

        return False
def read_bkgpix_cbf(cbfin, positions):
    data, nx, ny = cbf.load_minicbf_as_numpy(cbfin)
    data = data.reshape(ny, nx)
    npos = len(positions)

    dists = numpy.array((data,)*npos)

    gx, gy = numpy.meshgrid(xrange(nx), xrange(ny))

    for i, pos in enumerate(positions):
        print i, pos
        dists[i,] = (gx-pos[0])**2 + (gy-pos[1])**2 # squared distance to each position
        
    ret = dists.argmin(axis=0) + 1
    ret[data < 0] = -10
    return ret
Example #8
0
def convert(cbfin, params):
    if params.byteoffset:
        import yamtbx_byteoffset_h5_ext
        import pyublas

    h5out = os.path.basename(cbfin) + ".h5"

    data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin)
    data = data.reshape((ndimmid, ndimfast))
    if params.decompose:
        data = decompose_panels(data)
    header = XIO.Image(cbfin).header

    of = h5py.File(h5out, "w")

    grp = of.create_group("LCLS")
    dset = grp.create_dataset("photon_energy_eV", (1,), dtype=numpy.float)
    dset[...] = 12398.4/header["Wavelength"]
    dset = grp.create_dataset("photon_wavelength_A", (1,), dtype=numpy.float)
    dset[...] = header["Wavelength"]
    dset = grp.create_dataset("adu_per_eV", (1,), dtype=numpy.float)
    dset[...] = header["Wavelength"] / 12398.4
    dset = grp.create_dataset("detector_distance_m", (1,), dtype=numpy.float)
    dset[...] = header["Distance"]/1000.
    dset = grp.create_dataset("beam_xy_px", (2,), dtype=numpy.float)
    dset[...] = (header["BeamX"]/header["PixelX"], header["BeamY"]/header["PixelY"])
    dset = grp.create_dataset("osc_step_deg", (1,), dtype=numpy.float)
    dset[...] = header["PhiWidth"]

    cbfin = os.path.abspath(cbfin)
    dset = grp.create_dataset("original_file", (1,), "S%d"%len(cbfin))
    dset[...] = cbfin

    grp = of.create_group("data")
    if params.byteoffset:
        assert data.dtype == numpy.int32
        # hid_t group_id, const std::string &name, const pyublas::numpy_vector<int> &data, int width, int height)
        yamtbx_byteoffset_h5_ext.write_byteoffset_data(grp.id.id, "data", data.ravel(), data.shape[1], data.shape[0])
    else:
        dset = grp.create_dataset("data", data.shape, dtype=data.dtype)#, compression="CBF")
        dset[...] = data

    of.close()

    print "Processed: %s" % os.path.basename(cbfin)

    return data.shape
Example #9
0
def run(files, cbfout):
    merged = None
    for i, f in enumerate(files):
        repl = -10 * (i + 1)
        print "%s %d" % (f, repl)

        data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(f)
        if i == 0:
            merged = data.copy()
            continue

        merged[
            data ==
            -10] = 65540  # repl # for adxv visualization. only assuming two files.

    cbf.save_numpy_data_as_cbf(merged, ndimfast, ndimmid, "merged_predictions",
                               cbfout)
Example #10
0
def run(cbfin, binout):
    # This function only returns signed int.
    arr, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin, quiet=False)
    arr.tofile(binout)
    print "Saved:", binout
    print
    print "Instruction for fit2d"
    print "  Width:", ndimfast
    print " Height:", ndimmid
    print "   Type: Integer (%d byte)" % arr.dtype.itemsize
    print " Signed: Yes" 
    print 
    print """Instruction for R

R
to.read<-file("%(filename)s","rb")
d <- readBin(to.read, integer(), size=%(size)d, signed=%(signed)s, n=%(width)d*%(height)d, endian = "little")
hist(d)
""" % dict(filename=binout, size=arr.dtype.itemsize, signed="TRUE", width=ndimfast, height=ndimmid)
Example #11
0
def run(cbfin, binout):
    # This function only returns signed int.
    arr, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin, quiet=False)
    arr.tofile(binout)
    print "Saved:", binout
    print
    print "Instruction for fit2d"
    print "  Width:", ndimfast
    print " Height:", ndimmid
    print "   Type: Integer (%d byte)" % arr.dtype.itemsize
    print " Signed: Yes"
    print
    print """Instruction for R

R
to.read<-file("%(filename)s","rb")
d <- readBin(to.read, integer(), size=%(size)d, signed=%(signed)s, n=%(width)d*%(height)d, endian = "little")
hist(d)
""" % dict(filename=binout,
           size=arr.dtype.itemsize,
           signed="TRUE",
           width=ndimfast,
           height=ndimmid)
def read_absorp_cbf(cbfin):
    data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(cbfin)
    data = data.reshape(ndimmid, ndimfast)
    #fac = data[ipos, ix]
    return data
Example #13
0
def cbf_to_dat(lpin):
    lpdir = os.path.dirname(lpin)
    filename = None
    params = {}

    ofs_abs = open(os.path.join(lpdir, "corfac_absorp.dat"), "w")
    ofs_abs.write("file ix xmin xmax ipos posx posy fac\n")
    ofs_mod = open(os.path.join(lpdir, "corfac_modpix.dat"), "w")
    ofs_mod.write("file ix xmin xmax iy ymin ymax fac\n")
    ofs_dec = open(os.path.join(lpdir, "corfac_decay.dat"), "w")
    ofs_dec.write("file ix xmin xmax iy ymin ymax fac\n")

    for l in open(lpin):
        if l.startswith(" CORRECTION FACTORS for visual inspection"):
            filename = l.split()[-1]
        elif l.startswith(" NUMBER OF REFLECTIONS USED FOR DETERMINING"):
            nref = int(l.split()[-1])  # not used now

            if "_***" in filename:
                # reset
                filename = None
                params = {}
                continue

            # open cbf
            data, ndimfast, ndimmid = cbf.load_minicbf_as_numpy(
                os.path.join(lpdir, filename))
            data = data.reshape(ndimmid, ndimfast)

            filenum = filename[filename.index("_") + 1:-4]
            xmin, xmax = float(params["XMIN"][0]), float(params["XMAX"][0])
            nxbin = int(params["NXBIN"][0])
            xstep = (xmax - xmin) / nxbin

            if "ABSORP_" in filename:
                for ix in xrange(nxbin):
                    x1, x2 = xmin + ix * xstep, xmin + (ix + 1) * xstep
                    for ipos in xrange(len(
                            params["DETECTOR_SURFACE_POSITION"])):
                        pos = params["DETECTOR_SURFACE_POSITION"][ipos]
                        fac = data[ipos, ix]
                        ofs_abs.write("%s %2d %.2f %.2f %2d %s %5d\n" %
                                      (filenum, ix, x1, x2, ipos, pos, fac))
            elif "DECAY_" in filename or "MODPIX_" in filename:
                ymin, ymax = float(params["YMIN"][0]), float(params["YMAX"][0])
                nybin = int(params["NYBIN"][0])
                ystep = (ymax - ymin) / nybin

                for ix in xrange(nxbin):
                    x1, x2 = xmin + ix * xstep, xmin + (ix + 1) * xstep
                    for iy in xrange(nybin):
                        y1, y2 = ymin + iy * ystep, ymin + (iy + 1) * ystep
                        fac = data[iy, ix]
                        ofs = ofs_mod if "MODPIX_" in filename else ofs_dec
                        ofs.write("%s %2d %.2f %.2f %2d %.5f %.5f %5d\n" %
                                  (filenum, ix, x1, x2, iy, y1, y2, fac))
            else:
                print "What is this file!?", filename

            # reset
            filename = None
            params = {}
        elif filename is not None:
            r = xds.re_xds_kwd.findall(l)
            for k, v in r:
                params.setdefault(k, []).append(v)