diffpy / pyobjcryst

Python bindings to ObjCryst++ Object-Oriented Crystallographic Library
Other
15 stars 17 forks source link

Add access to the linear polarisation rate #38

Open vincefn opened 1 year ago

vincefn commented 1 year ago

@kif The linear polarisation rate is not interfaced in python ; it would also be useful to add a parameter for an azimuthal angle, in case the pattern is not recorded in the plane perpendicular to the polarisation plane. Both parameters (linear polarisation and azimuthal angle) should also be listed as RefinablePar.

See https://gsas-ii.readthedocs.io/en/latest/GSASIIpwd.html#GSASIIpwd.Polarization and J.Appl.Cryst.(2020).53, 1559–1561

kif commented 1 year ago

Here is a quick & dirty monkey patch:

import io
import xml.etree.ElementTree as et
from pyobjcryst.powderpattern import PowderPattern

def get_polarization(powder_pattern):
    with io.StringIO() as file:
        powder_pattern.XMLOutput(file, 2)
        file.seek(0)
        xml_root = et.fromstring(file.read())
    xml_pol = xml_root.find("Radiation").find("LinearPolarRate")
    return float(xml_pol.text)
PowderPattern.LinearPolarRate=get_polarization
vincefn commented 1 year ago

A much less dirty approach would be to use the Radiation object (accessible through powder_pattern.GetRadiation()) XML string - using XMLInput on the entire PowderPattern has plenty of side effects (re-creating the powder pattern components), whereas re-importing just the radiation object is almost inconsequential.

In fact, you can input a modified value for just the linear polarisation rate with a stripped-down XML string:

s = "<Radiation>  <LinearPolarRate>0.9</LinearPolarRate> </Radiation>"
r.XMLInput(s)

Note however that after changing the linear polarisation rate, you'll need to change another parameter (such as the wavelength) to trigger an actual re-calculation of the powder pattern (because of the cached calculations and the fact that the linear polarisation rate is not a RefinablePar and thus its change does not invalidate stored computed values IIRC)....