The exercise can be performed by placing all of the shell commands into a script, (which can also be downloaded here), but it is also possible to use copy and paste to execute the commands into a terminal window on a Mac or Linux computer (this likely works but has not been tried with Windows Subsystem for Linux).
Finally, download from https://subversion.xray.aps.anl.gov/pyGSAS/Tutorials/PythonScript/data/ the following files: "PBSO4.XRA", "INST_XRY.PRM", "PBSO4.CWN", "inst_d1a.prm" and "PbSO4-Wyckoff.cif" and make a note of their location for the next step.
To simplify the subsequent commands, let's define variables with the path of the data files and the path to where GSAS-II is installed: Note that we will run the Python found in the path. If Python needs to be executed from a different location, it might be useful to also define a variable that points to that.
The first step in this exercise is to create the GSAS-II project and then add this two powder datasets and a phase to the project. Note that in this example, it is possible to do all this in a single step, but this is done here with two commands, in part to demonstrate command options only available with the "add" command (-hf, -pf and -l, which select formats for the histogram and phase files, as well as, designate which histograms are linked to the phase, respectively.) Note since the "add" command offers the "-l" (or "--histlist") option and "create" does not, if the added phase(s) will be linked to only some histograms, the "add" command is required.
The following single command creates a JSON file. This contains a representation of Python dict structure. JSON syntax is quite close to Python, except that the boolean key words (true and false) are all lower case and that while use of extra commas at ends of dicts and lists is allowed in Python, it is not in JSON. This code example requires a fair amount of explaination. Note that the first and last lines (cat and EOF) are used to create the file (example.json) from the shell and could be eliminated if the file were to be created directly in an editor.
There are two entries in the dict, refinements, which defines the steps to be followed in the refinement, and code, which defines Python code to be executed after the GSAS-II project is read in, but before the refinement is performed.
- Performs the same actions as are described in steps 5-10 in the Scripting GSAS-II tutorial. The Single Step section shows these steps collapsed to a list of dicts.
- Note that since JSON does not offer a method to quote multiline strings, this dict entry is defined as a list of strings, which are concatenated as separate Python lines. This Python code will be evaluated [using expr()] inside function refine where variable proj is defined as the current G2Project.
In this example, the function HistStats is defined as before, but now a global statement is needed so that this function is visible where it will be used. Note that the two statements used to set GSAS-II parameters directly (from steps 4 and 9 in the Scripting GSAS-II tutorial) are moved here and are referenced with the variable proj rather than gpx.
The final command executes the contents of the JSON file: