How to open a GSAS, TOPAS or FullProf refinement and write one back#
Open the other program’s input file with rx.read_project_model, build the structure with to_structure, and after the fit write the result back with that program’s writer.
read_project_model reads a GSAS .EXP, a GSAS-II .gpx, a TOPAS .inp and a FullProf .pcr, and recognises each by its content rather than its extension.
It returns the structure and what the file states about its run.
You build the instrument and read the pattern yourself, from what the file states.
Open the file#
This opens GSAS’s refinement of the fluorapatite pattern, which ships with rietx as FAP.EXP:
import tempfile
from pathlib import Path
import rietx as rx
from rietx.examples import examples_dir
notes = []
model = rx.read_project_model(examples_dir() / "FAP.EXP", diagnostics=notes)
histogram = model.stated.histogram()
print(model.format.name, "| data", histogram.data_file, "| wavelengths", histogram.wavelengths)
print("stated Rwp", model.stated.rwp)
for note in notes:
print(note.code)
gsas_exp | data fap.xra | wavelengths (1.5405, 1.5443)
stated Rwp 0.1005
GSAS_EXP_BACKGROUND_NOT_CARRIED
model.stated holds what the file says about its own run: the wavelengths, the data file it fitted, its R factors and which parameters it refined.
The diagnostics= list gets one entry for each thing the reader read but could not carry over.
Here GSAS fitted a background function rietx does not have, so you choose a rietx background instead.
A construct the reader cannot carry without changing the model, such as a rigid body in TOPAS or an anisotropic displacement block in FullProf, is refused with its name.
Build the models and refine#
structure = model.to_structure()
instrument = rx.Instrument.bragg_brentano(radiation="CuKa")
for line, wavelength in zip(instrument.source.lines, histogram.wavelengths):
line.wavelength.value = wavelength
instrument.background = rx.BackgroundChebyshev.with_terms(6)
data = rx.read_pattern(examples_dir() / "FAP.XRA")
ref = rx.Refinement(structure, instrument)
result = ref.fit(data, plan="mccusker_structural", two_theta_limits=(15, 130))
print(f"rietx Rwp {result.statistics.rwp:.4f}")
rietx Rwp 0.0953
The wavelengths come from the file, so a cell compared with GSAS’s is not shifted by a difference in the declared wavelength. This Rwp is not comparable with GSAS’s stated 0.1005, because the background and peak-shape models differ. To compare the two programs, mirror the other program’s protocol as well, as each comparison in Validation against other codes does.
Write the result for another program#
out = Path(tempfile.mkdtemp()) # or the directory you want the files in
rx.write_topas_inp(ref.fitted_structure, out / "fap.inp", free=ref, scale="topas",
instrument=ref.fitted_instrument, pattern=data)
rx.write_fullprof_pcr(ref.fitted_structure, out / "fap.pcr", instrument=ref.fitted_instrument,
write_zero_shift=True)
rx.write_gsas2_phase_cif(ref.fitted_structure, out / "fap_gsas2.cif")
rx.write_gsas2_instprm(ref.fitted_instrument, out / "fap.instprm")
for name in ("fap.inp", "fap.pcr"):
print(name, rx.identify_project_format(out / name).name)
fap.inp topas_inp
fap.pcr fullprof_pcr
rx.write_topas_inpwrites an input TOPAS runs as it stands, with the data beside it asfap.xye.free=refmakes the file refine the parameters the fit refined, with its ties.rx.write_fullprof_pcrwrites a.pcr. It refuses a non-zero zero shift unlesswrite_zero_shift=True, and refuses any other position correction, because the file would put every peak somewhere else. It also refuses extinction, preferred orientation, texture, absorption and surface roughness away from their off states, because it writes none of them. The fit here can be written because its extinction ended at 0.rx.write_gsas2_phase_cifwrites a CIF that GSAS-II’s phase import reads, andrx.write_gsas2_instprmwrites the instrument as a GSAS-II.instprm. rietx has no writer for a.gpx.
rx.identify_project_format says which format a file is without reading the model.
A writer refuses on the way out what its reader would refuse on the way in, so a file it writes reads back.
See also#
Coming from GSAS-II, Coming from TOPAS and Coming from FullProf list what each reader and writer carries and refuses.
Names in other programs maps each rietx parameter to its name and unit in the other programs.
Refinement files from other programs, Files written for other programs and Models of other programs’ files give each reader, writer and model field by field.