def main():
    parser = createOptionParser()
    (opts, pargs) = parser.parse_args()

    if len(pargs) != 2:
        parser.error("You must supply FILE1 and FILE2")

    # Get the PDFs
    xobj, yobj = getPDFFromFile(pargs[0])
    xref, yref = getPDFFromFile(pargs[1])

    # Get configuration values
    config = {}
    config["rmin"] = opts.rmin
    config["rmax"] = opts.rmax
    config["rstep"] = None
    if opts.rmin is not None and opts.rmax is not None and \
            opts.rmax <= opts.rmin:
        e = "rmin must be less than rmax"
        parser.error(e)

    # Set up the morphs
    chain = morphs.MorphChain(config)
    # Add the r-range morph, we will remove it when saving and plotting
    chain.append(morphs.MorphRGrid())
    refpars = []

    ## Scale
    if opts.scale is not None:
        chain.append(morphs.MorphScale())
        config["scale"] = opts.scale
        refpars.append("scale")
    ## Stretch
    if opts.stretch is not None:
        chain.append(morphs.MorphStretch())
        config["stretch"] = opts.stretch
        refpars.append("stretch")
    ## Smear
    if opts.smear is not None:
        chain.append(morphs.MorphXtalPDFtoRDF())
        chain.append(morphs.MorphSmear())
        chain.append(morphs.MorphXtalRDFtoPDF())
        refpars.append("smear")
        config["smear"] = opts.smear
        config["baselineslope"] = opts.baselineslope
        if opts.baselineslope is None:
            refpars.append("baselineslope")
            config["baselineslope"] = -0.5
    ## Size
    radii = [opts.radius, opts.pradius]
    nrad = 2 - radii.count(None)
    if nrad == 1:
        radii.remove(None)
        config["radius"] = radii[0]
        chain.append(morphs.MorphSphere())
        refpars.append("radius")
    elif nrad == 2:
        config["radius"] = radii[0]
        refpars.append("radius")
        config["pradius"] = radii[1]
        refpars.append("pradius")
        chain.append(morphs.MorphSpheroid())
    iradii = [opts.iradius, opts.ipradius]
    inrad = 2 - iradii.count(None)
    if inrad == 1:
        iradii.remove(None)
        config["iradius"] = iradii[0]
        chain.append(morphs.MorphISphere())
        refpars.append("iradius")
    elif inrad == 2:
        config["iradius"] = iradii[0]
        refpars.append("iradius")
        config["ipradius"] = iradii[1]
        refpars.append("ipradius")
        chain.append(morphs.MorphISpheroid())

    ## Resolution
    if opts.qdamp is not None:
        chain.append(morphs.MorphResolutionDamping())
        refpars.append("qdamp")
        config["qdamp"] = opts.qdamp

    # Now remove non-refinable parameters
    if opts.exclude is not None:
        refpars = set(refpars) - set(opts.exclude)
        refpars = list(refpars)

    # Refine or execute the morph
    refiner = refine.Refiner(chain, xobj, yobj, xref, yref)
    if opts.pearson:
        refiner.residual = refiner._pearson
    if opts.addpearson:
        refiner.residual = refiner._addpearson
    if opts.refine and refpars:
        try:
            # This works better when we adjust scale and smear first.
            if "smear" in refpars:
                rptemp = ["smear"]
                if "scale" in refpars:
                    rptemp.append("scale")
                refiner.refine(*rptemp)
            refiner.refine(*refpars)
        except ValueError as e:
            parser.error(str(e))
    elif "smear" in refpars and opts.baselineslope is None:
        try:
            refiner.refine("baselineslope", baselineslope=-0.5)
        except ValueError as e:
            parser.error(str(e))
    else:
        chain(xobj, yobj, xref, yref)

    # Get Rw for the morph range
    rw = tools.getRw(chain)
    pcc = tools.getPearson(chain)
    # Replace the MorphRGrid with Morph identity
    chain[0] = morphs.Morph()
    chain(xobj, yobj, xref, yref)

    items = list(config.items())
    items.sort()
    output = "\n".join("# %s = %f" % i for i in items)
    output += "\n# Rw = %f" % rw
    output += "\n# Pearson = %f" % pcc
    print(output)
    if opts.savefile is not None:
        header = "# PDF created by pdfmorph\n"
        header += "# from %s\n" % os.path.abspath(pargs[0])

        header += output
        if opts.savefile == "-":
            outfile = sys.stdout
        else:
            outfile = open(opts.savefile, 'w')
        print(header, file=outfile)
        import numpy
        numpy.savetxt(outfile, zip(chain.xobjout, chain.yobjout))
        outfile.close()

    if opts.plot:
        pairlist = [chain.xyobjout, chain.xyrefout]
        labels = ["objective", "reference"]
        # Plot extent defaults to calculation extent
        pmin = opts.pmin if opts.pmin is not None else opts.rmin
        pmax = opts.pmax if opts.pmax is not None else opts.rmax
        maglim = opts.maglim
        mag = opts.mag
        pdfplot.comparePDFs(pairlist,
                            labels,
                            rmin=pmin,
                            rmax=pmax,
                            maglim=maglim,
                            mag=mag,
                            rw=rw)

    return
Exemple #2
0
def main():
    parser = createOptionParser()
    (opts, pargs) = parser.parse_args()

    if len(pargs) != 2:
        parser.error("You must supply FILE1 and FILE2")

    # Get the PDFs
    xobj, yobj = getPDFFromFile(pargs[0])
    xref, yref = getPDFFromFile(pargs[1])

    # Get configuration values
    config = {}
    config["rmin"] = opts.rmin
    config["rmax"] = opts.rmax
    config["rstep"] = None
    if opts.rmin is not None and opts.rmax is not None and \
            opts.rmax <= opts.rmin:
        e = "rmin must be less than rmax"
        parser.error(e)

    # Set up the morphs
    chain = morphs.MorphChain(config)
    # Add the r-range morph, we will remove it when saving and plotting
    chain.append( morphs.MorphRGrid() )
    refpars = []

    ## Scale
    if opts.scale is not None:
        chain.append( morphs.MorphScale() )
        config["scale"] = opts.scale
        refpars.append("scale")
    ## Stretch
    if opts.stretch is not None:
        chain.append( morphs.MorphStretch() )
        config["stretch"] = opts.stretch
        refpars.append("stretch")
    ## Smear
    if opts.smear is not None:
        chain.append( morphs.MorphXtalPDFtoRDF() )
        chain.append( morphs.MorphSmear() )
        chain.append( morphs.MorphXtalRDFtoPDF() )
        refpars.append("smear")
        config["smear"] = opts.smear
        config["baselineslope"] = opts.baselineslope
        if opts.baselineslope is None:
            refpars.append("baselineslope")
            config["baselineslope"] = -0.5
    ## Size
    radii = [opts.radius, opts.pradius]
    nrad = 2 - radii.count(None)
    if nrad == 1:
        radii.remove(None)
        config["radius"] = radii[0]
        chain.append( morphs.MorphSphere() )
        refpars.append("radius")
    elif nrad == 2:
        config["radius"] = radii[0]
        refpars.append("radius")
        config["pradius"] = radii[1]
        refpars.append("pradius")
        chain.append( morphs.MorphSpheroid() )
    iradii = [opts.iradius, opts.ipradius]
    inrad = 2 - iradii.count(None)
    if inrad == 1:
        iradii.remove(None)
        config["iradius"] = iradii[0]
        chain.append( morphs.MorphISphere() )
        refpars.append("iradius")
    elif inrad == 2:
        config["iradius"] = iradii[0]
        refpars.append("iradius")
        config["ipradius"] = iradii[1]
        refpars.append("ipradius")
        chain.append( morphs.MorphISpheroid() )

    ## Resolution
    if opts.qdamp is not None:
        chain.append( morphs.MorphResolutionDamping() )
        refpars.append("qdamp")
        config["qdamp"] = opts.qdamp

    # Now remove non-refinable parameters
    if opts.exclude is not None:
        refpars = set(refpars) - set(opts.exclude)
        refpars = list(refpars)

    # Refine or execute the morph
    refiner = refine.Refiner(chain, xobj, yobj, xref, yref)
    if opts.pearson:
        refiner.residual = refiner._pearson
    if opts.addpearson:
        refiner.residual = refiner._addpearson
    if opts.refine and refpars:
        try:
            # This works better when we adjust scale and smear first.
            if "smear" in refpars:
                rptemp = ["smear"]
                if "scale" in refpars:
                    rptemp.append("scale")
                refiner.refine(*rptemp)
            refiner.refine(*refpars)
        except ValueError, e:
            parser.error(str(e))