Exemple #1
0
def integrate_them(o):
    """ 
    Process a series of files
    o = options object 
    o.parfile gives name of parameter file (fit2d or poni format)
    o.dark overrides to supply dark filename
    o.flood overrides to supply flood filename
    o.mask  overrides to supply mask filename
    o.backcalc asks for the back computation of the image
    o.npts gives number of output points
    """
    # pyFAI.load( ponifile )
    integrator = AzimuthalIntegrator()
    #integrator.tth = integrator.newtth
    # integrator.setChiDiscAtZero()
    ptype = determineparfile(o.parfile)
    if ptype == "pyfai":
        integrator.load(o.parfile)
        if o.dark is not None:
            print("Using dark from command line", o.dark)
        if o.flood is not None:
            print("Using dark from command line", o.flood)
    elif ptype == "fit2d":
        f2d = fit2dcakepars(o.parfile)
        if f2d["SPATIAL DIS."][0] not in ["Y", "y"]:
            # Set to None. Spatial is from parfile
            f2d["SD FILE"] = None
        integrator.setFit2D(float(f2d["DISTANCE"]),
                            float(f2d["X-BEAM CENTRE"]),
                            float(f2d["Y-BEAM CENTRE"]),
                            tilt=float(f2d["ANGLE OF TILT"]),
                            tiltPlanRotation=float(f2d["TILT ROTATION"]),
                            pixelX=float(f2d["X-PIXEL SIZE"]),
                            pixelY=float(f2d["Y-BEAM CENTRE"]),
                            splineFile=f2d["SD FILE"])
        integrator.rot3 = 0
        integrator.reset()
        print(integrator.param, integrator.detector.pixel1)
        # First choice is command line. Then from pars if supplied
        if o.dark is None:
            if f2d["DARK CURRENT"][0] in ["Y", "y"]:
                o.dark = f2d["DC FILE"]
                print("Using dark from fit2d parameter file", o.dark)
        else:
            print("Using dark from command line", o.dark)
        if o.flood is None:
            if f2d["FLAT-FIELD"][0] in ["Y", "y"]:
                o.flood = f2d["FF FILE"]
                print("Using flood from fit2d parameter file", o.flood)
        else:
            print("Using flood from command line", o.flood)
    # Should be in fabio utilities
    df = darkflood(o.dark, o.flood)
    # Should be in fabio
    fs = edffilenameseries(o.stem, o.first, o.last, o.glob, o.extn)
    #    integrator.polarization( factor = 1, shape=(2048,2048) )
    # Command line is first priority for make
    if o.mask is not None:
        mask = fabio.open(o.mask).data
        print("Using mask", o.mask)
        # assume poni file deals with this independently?
    elif ptype == "fit2d":  # try in fit2d parfile
        if f2d["USE MASK"][0] in ['y', 'Y']:
            mask = fabio.open(f2d["MASK FILE"]).data
            print("Using mask", f2d["MASK FILE"])
        else:
            mask = None
    if mask is not None:
        print("mask mean:", mask.mean())
    integrator.write(os.path.splitext(o.parfile)[0] + ".poni")
    for f in fs:
        print("Processing", f, end=' ')
        try:
            fo = df.correct(fabio.open(f))
        except:

            continue
        if ptype == "fit2d":
            outFile = f.replace(f2d["input_extn"], f2d["output_extn"])
        else:
            outFile = f.replace(o.extn, ".dat")
        global SOLID_ANGLE
        if 0:
            from matplotlib.pylab import imshow, figure, show, log, plot
            #imshow(log(fo.data))
            #figure()
            if mask is not None:
                imshow(log(fo.data * (1 - mask)),
                       vmin=log(10),
                       vmax=log(30000))
            else:
                imshow(log(fo.data), vmin=log(100), vmax=log(3000))
            #        show()
        if o.npts is None:
            npts = min(fo.data.shape)
        else:
            npts = int(o.npts)
        tth, I = integrator.integrate1d(
            fo.data,
            nbPt=npts,
            filename=outFile,
            correctSolidAngle=SOLID_ANGLE,
            mask=mask,  # 1 for valid
            unit="q_A^-1",
            #dummy=dummy, # mask pixels == dummy
            #delta_dummy=delta_dummy # precision of dummy
        )

        print("wrote", outFile)
        if o.backcalc:
            calcimage = calcfrom1d(integrator, tth, I,
                                   fo.data.shape) * integrator._polarization
            err = (calcimage - fo.data) * (1 - mask) / (calcimage + mask)
            e = fabio.edfimage.edfimage(data=err.astype(numpy.float32))
            e.write(outFile + ".edf")
            fitcen(fo.data, calcimage, (1 - mask))


#            from matplotlib.pylab import imshow, show
#            imshow( integrator._polarization )
#            show()

    if o.display:
        if mask is not None:
            display(tth, I,
                    (calcimage - fo.data) * (1 - mask) / (calcimage + 1))
        else:
            display(tth, I, (calcimage - fo.data) / (calcimage + 1))