|
1 | 1 | *GSAS-II Export Modules* |
2 | 2 | ==================================== |
3 | 3 |
|
| 4 | +Many of the data files written by GSAS-II for use by other software is |
| 5 | +written using a layer of routines called exporters. Exporters usually require quite |
| 6 | +simple code, so they can be written quickly to allow GSAS-II to |
| 7 | +provide output in new data formats. |
| 8 | +The interface to the exporters |
| 9 | +is self-configuring, so all supplied exporters are available once the |
| 10 | +exporter is added to the code base. This allows GSAS-II to be quite |
| 11 | +flexible in adapting to use many data formats without need for |
| 12 | +extensive coding. |
| 13 | + |
4 | 14 | Exports are implemented by deriving a class from |
5 | 15 | :class:`~GSASII.GSASIIfiles.ExportBaseclass` in module |
6 | | -:mod:`~GSASII.GSASIIfiles`. Initialization of |
7 | | -``self.exporttype`` determines the type of export that will be performed |
8 | | -('project', 'phase', 'single', 'powder', 'image', 'map' or (someday) |
9 | | -'pdf') and of ``self.multiple`` |
| 16 | +:mod:`~GSASII.GSASIIfiles`. |
| 17 | +Export routines commonly access the GUI to determine information on |
| 18 | +the contents of the file(s) to be written, but for powder diffraction |
| 19 | +and phase data export, a routine named ``Writer()`` may be defined. If this is |
| 20 | +present, the exporter can be used from: mod:`~GSASII.GSASIIscriptable` |
| 21 | +without GUI access. Note that the arguments for the``Writer()`` method |
| 22 | +include a histogram tree name as well as a file name to |
| 23 | +be written. |
| 24 | + |
| 25 | +A file containing one or more export routine can be placed |
| 26 | +either in the ``GSASII/exports`` directory (which requires |
| 27 | +modification of the ``__init__.py`` file or the file can be placed in |
| 28 | +the ``~/.GSASII/exports`` directory. |
| 29 | +The next time GSAS-II is started, |
| 30 | +the file will loaded when the GSAS-II files are read by the Python and |
| 31 | +the new data format will appear in the appropriate exporter menu. |
| 32 | + |
| 33 | +.. _export_routines: |
| 34 | + |
| 35 | +====================================== |
| 36 | + Writing an Exporter Routine |
| 37 | +====================================== |
| 38 | + |
| 39 | +When writing a exporter routine, one should create a new class derived |
| 40 | +from class `~GSASII.GSASIIfiles.ExportBaseclass`. |
| 41 | +The name of the class is arbitrary, but if more than one class is |
| 42 | +placed in file, each class must have a different name. The same name |
| 43 | +can be repeated if it is in different files. |
| 44 | +As described below, this class will implement |
| 45 | +an ``__init__()`` and an ``Exporter()`` method, and many will supply a |
| 46 | +``Writer()`` method, too. The purpose of each of these |
| 47 | +routines is described below. The easiest way to craft a new exporter |
| 48 | +will be to use the other exporters of the same data type as a model |
| 49 | +for where to find the data values that will be written, but the documentation |
| 50 | +for the parent class (`~GSASII.GSASIIfiles.ExportBaseclass`) provides |
| 51 | +useful information on support routines that pull information from the |
| 52 | +GSAS-II data structures into the exporter. |
| 53 | + |
| 54 | +__init__() |
| 55 | +-------------- |
| 56 | + |
| 57 | +The ``__init__`` method will follow standard boilerplate largely independent |
| 58 | +of the data type: |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + def __init__(self): |
| 63 | + super(self.__class__,self).__init__( # fancy way to self-reference |
| 64 | + G2frame=G2frame, |
| 65 | + formatName = 'Format name for menu', |
| 66 | + extension='.extn', |
| 67 | + longFormatName = 'Longer more detailed format name for status line' |
| 68 | + ) |
| 69 | +
|
| 70 | +The first line in the ``__init__`` method calls the parent class |
| 71 | +``__init__`` method with the following parameters: |
| 72 | + |
| 73 | + * ``G2frame``: a reference to the main GSAS-II GUI window or None |
| 74 | + when run scripted. |
| 75 | + * ``formatName``: a string to be used in the menu. Should be short. |
| 76 | + * ``extension``: a string to be used in the file name. All files |
| 77 | + produced by the exporter will have this extension. |
| 78 | + * ``longFormatName``: a longer string to be used to describe the |
| 79 | + format in help. |
| 80 | + |
| 81 | +In addition, one instance variables must be defined: |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + self.exporttype = ['phase'] |
| 86 | +
|
| 87 | +The value for ``self.exporttype`` determines the type of export that will be performed |
| 88 | +('project', 'phase', 'single', 'powder', 'image', 'map', 'sasd', 'refd' or (someday) |
| 89 | +'pdf') and the menu where the exporter will be placed. Note that |
| 90 | +'project' exports are those that include all data from a |
| 91 | +.gpx file (all phases, histograms, etc.) |
| 92 | + |
| 93 | +Another item is optional. |
| 94 | + |
| 95 | +.. code-block:: python |
| 96 | +
|
| 97 | + self.multiple = True |
| 98 | +
|
| 99 | +The value specified for ``self.multiple`` |
10 | 100 | determines if only a single phase, data set, etc. can be exported at a |
11 | | -time (when False) or more than one can be selected. |
| 101 | +time (when False) or when True, a file can be produced with multiple |
| 102 | +histograms, phases, etc. |
| 103 | + |
| 104 | +Exporter() |
| 105 | +-------------- |
| 106 | + |
| 107 | +The class must supply a ``Exporter`` method that will write a |
| 108 | +file. Depending on the settings for ``self.exporttype`` and |
| 109 | +``self.multiple`` and the contents of the Data Tree, will dictate |
| 110 | +what dialogs will be presented to the user to select what will be |
| 111 | +written. |
| 112 | + |
| 113 | +Writer() |
| 114 | +-------------- |
| 115 | + |
| 116 | +For powder and phase exports, if the class supplies a ``Writer()`` |
| 117 | +module, then the export format |
| 118 | +will be available for scripted output (with |
| 119 | +:mod:`GSASIIscriptable`). These modules are supplied a histogram name |
| 120 | +or a phase name and should not attempt to access the GUI. It is not |
| 121 | +required that that this method be supplied, but usually it is not hard to do, |
| 122 | +unless information from the user is required. |
| 123 | + |
| 124 | +Note that for phase exports the ``Writer()`` should be declared as follows: |
| 125 | + |
| 126 | +.. code-block:: python |
| 127 | +
|
| 128 | + def Writer(self,hist,phasenam,mode='w'): |
| 129 | +
|
| 130 | +while for histogram exports the ``Writer()`` should be declared as follows: |
| 131 | + |
| 132 | +.. code-block:: python |
12 | 133 |
|
13 | | -Powder export routines may optionally define a ``Writer()`` |
14 | | -method that accepts the histogram tree name as well as a file name to |
15 | | -be written. This allows :mod:`~GSASII.GSASIIscriptable` to use the exporters |
16 | | -independent of the GUI. |
| 134 | + def Writer(self,hist,filename=None,mode='w'): |
17 | 135 |
|
18 | 136 | ------------------------------------------- |
19 | 137 | *Module G2export_examples: Examples* |
|
0 commit comments