[2027] | 1 | *GSAS-II Import Modules* |
---|
| 2 | ==================================== |
---|
| 3 | |
---|
| 4 | Imports are implemented by deriving a class from |
---|
[2068] | 5 | :class:`GSASIIIO.ImportPhase`, :class:`GSASIIIO.ImportStructFactor`, |
---|
| 6 | :class:`GSASIIIO.ImportPowderData` |
---|
[2027] | 7 | or :class:`GSASIIIO.ImportPowderData` (which are in turn |
---|
| 8 | derived from :class:`GSASIIIO.ImportBaseclass`) |
---|
| 9 | to implement import of |
---|
| 10 | a phase, a single crystal or a powder dataset, respectively. |
---|
| 11 | Module file names (`G2phase_`, `G2pwd_` and `G2sfact_`, etc.) are used to |
---|
| 12 | determine which menu an import routine should be placed into. (N.B. this |
---|
| 13 | was an unnecessary choice; this could be done from the class used.) |
---|
| 14 | |
---|
| 15 | This list may not include all currently defined formats, since modules |
---|
| 16 | may be loaded from anywhere in the path. |
---|
| 17 | |
---|
| 18 | Writing an Import Routine |
---|
| 19 | -------------------------- |
---|
| 20 | |
---|
| 21 | .. _Import_routines: |
---|
| 22 | |
---|
| 23 | When writing a import routine, one should create a new class derived |
---|
| 24 | from :class:`GSASIIIO.ImportPhase`, :class:`GSASIIIO.ImportStructFactor` |
---|
| 25 | or :class:`GSASIIIO.ImportPowderData`. As described below, |
---|
| 26 | all these classes will implement |
---|
| 27 | an ``__init__()`` and a ``Reader()`` method, and most will supply a |
---|
| 28 | ``ContentsValidator()`` method, too. |
---|
[2068] | 29 | See the :class:`~GSASIIIO.ImportPhase`, |
---|
| 30 | :class:`~GSASIIIO.ImportStructFactor`, |
---|
| 31 | :class:`~GSASIIIO.ImportPowderData` |
---|
| 32 | or :class:`~GSASIIIO.ImportImage` class documentation |
---|
[2027] | 33 | for details on what values each type of ``Reader()`` should set. |
---|
| 34 | |
---|
| 35 | __init__() |
---|
| 36 | ~~~~~~~~~~~~~~ |
---|
| 37 | |
---|
[2068] | 38 | The ``__init__`` method will follow standard boilerplate: |
---|
[2027] | 39 | |
---|
[2068] | 40 | .. code-block:: python |
---|
| 41 | |
---|
| 42 | def __init__(self): |
---|
| 43 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
| 44 | extensionlist=('.ext1','ext2'), |
---|
| 45 | strictExtension=True, |
---|
| 46 | formatName = 'example image', |
---|
| 47 | longFormatName = 'A longer description that this is an example image format' |
---|
| 48 | ) |
---|
| 49 | |
---|
| 50 | The first line in the ``__init__`` method calls the parent class |
---|
| 51 | ``__init__`` method with the following parameters: |
---|
| 52 | |
---|
| 53 | * ``extensionlist``: a list of extensions that may be used for this type of file. |
---|
| 54 | * ``strictExtension``: Should be True if only files with extensions in |
---|
| 55 | ``extensionlist`` are allows; False if all file types should be offered |
---|
[2027] | 56 | in the file browser. Also if False, the import class will be |
---|
| 57 | used on all files when "guess from format" is tried, though |
---|
[2068] | 58 | readers with matching extensions will be tried first. |
---|
| 59 | It is a very good idea to supply a :ref:`ContentsValidator <ContentsValidator>` |
---|
| 60 | method when ``strictExtension`` is False. |
---|
| 61 | * ``formatName``: a string to be used in the menu. Should be short. |
---|
| 62 | * ``longFormatName``: a longer string to be used to describe the format in help. |
---|
[2027] | 63 | |
---|
| 64 | Reader() |
---|
| 65 | ~~~~~~~~~~~~~~ |
---|
| 66 | |
---|
| 67 | The class must supply a ``Reader`` method that actually performs the |
---|
| 68 | reading. All readers must have at a minimum these arguments:: |
---|
| 69 | |
---|
| 70 | def Reader(self, filename, filepointer, ParentFrame, **unused): |
---|
| 71 | |
---|
| 72 | where the arguments have the following uses: |
---|
| 73 | |
---|
[2068] | 74 | * ``filename``: a string with the name of the file being read |
---|
| 75 | * ``filepointer``: a file object (created by :func:`open`) that accesses |
---|
[2027] | 76 | the file and is points to the beginning of the file when Reader is |
---|
| 77 | called. |
---|
[2068] | 78 | * ``ParentFrame``: a reference to the main GSAS-II (tree) windows, for |
---|
[2027] | 79 | the unusual ``Reader`` routines that will create GUI windows to ask |
---|
[2068] | 80 | questions. The Reader should do something reasonable such as take a |
---|
| 81 | reasonable default if ``ParentFrame`` is None, which indicates that |
---|
| 82 | GUI should not be accessed. |
---|
[2027] | 83 | |
---|
| 84 | In addition, the following keyword parameters are defined that ``Reader`` |
---|
| 85 | routines may optionally use: |
---|
| 86 | |
---|
[2068] | 87 | * ``buffer``: a dict that can be used to retain information between repeated calls of the routine |
---|
| 88 | * ``blocknum``: counts the number of times that a reader is called, to |
---|
| 89 | be used with files that contain more than one set of data (e.g. GSAS |
---|
| 90 | .gsa/.fxye files with multiple banks or image files with multiple images.) |
---|
| 91 | * ``usedRanIdList``: a list of previously used random Id values that can be checked to determine that a value is unique. |
---|
[2027] | 92 | |
---|
[2068] | 93 | As an example, the ``buffer`` dict is used in CIF reading to hold the parsed CIF file |
---|
[2027] | 94 | so that it can be reused without having to reread the file from |
---|
| 95 | scratch. |
---|
| 96 | |
---|
| 97 | Reader return values |
---|
| 98 | ______________________ |
---|
| 99 | |
---|
| 100 | The ``Reader`` routine should return the value of True if the file has been |
---|
| 101 | read successfully. Optionally, use `self.warnings` to indicate any |
---|
| 102 | problems. |
---|
| 103 | |
---|
| 104 | If the file cannot be read, the ``Reader`` routine should |
---|
| 105 | return False or raise an :class:`GSASIIIO.ImportBaseclass.ImportException` |
---|
| 106 | exception. (Why either? Sometimes an exception is the easiest way to |
---|
| 107 | bail out of a called routine.) Place text in `self.errors` and/or use:: |
---|
| 108 | |
---|
| 109 | ImportException('Error message') |
---|
| 110 | |
---|
| 111 | to give the user information on what went wrong during the reading. |
---|
| 112 | |
---|
| 113 | self.warnings |
---|
| 114 | _____________________ |
---|
| 115 | |
---|
| 116 | Use `self.warnings` to indicate any information |
---|
| 117 | that should be displayed to the user if the file is read successfully, |
---|
| 118 | but perhaps not completely or additional settings will need to be |
---|
| 119 | made. |
---|
| 120 | |
---|
| 121 | self.errors |
---|
| 122 | _____________________ |
---|
| 123 | |
---|
| 124 | Use `self.errors` to give the user information on where and why a read |
---|
| 125 | error occurs in the file. Note that text supplied with the ``raise`` |
---|
| 126 | statement will be appended to ``self.errors``. |
---|
| 127 | |
---|
| 128 | self.repeat |
---|
| 129 | _____________________ |
---|
| 130 | |
---|
| 131 | Set `self.repeat` to True (the default is False) if a Reader should be |
---|
[2068] | 132 | called again to after reading to indicate that more data may exist in |
---|
| 133 | the file to be read. This is used for reading multiple powder |
---|
| 134 | histograms or multiple images from a single file. Variable |
---|
| 135 | `self.repeatcount` is used to keep track of the block numbers. |
---|
[2027] | 136 | |
---|
| 137 | *support routines* |
---|
| 138 | _________________________ |
---|
| 139 | |
---|
| 140 | Note that the base class (:class:`GSASIIIO.ImportBaseclass`) supplies two routines, |
---|
| 141 | :meth:`~GSASIIIO.ImportBaseclass.BlockSelector` and |
---|
| 142 | :meth:`~GSASIIIO.ImportBaseclass.MultipleBlockSelector` that are useful for |
---|
| 143 | selecting amongst one or more datasets (and perhaps phases) for |
---|
| 144 | ``Reader()`` routines that may encounter more than one set of information |
---|
| 145 | in a file. |
---|
| 146 | Likewise, when an operation will take some time to complete, |
---|
| 147 | use :meth:`~GSASIIIO.ImportBaseclass.ShowBusy` and |
---|
| 148 | :meth:`~GSASIIIO.ImportBaseclass.DoneBusy` to show the user |
---|
| 149 | that something is happening. |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | ContentsValidator() |
---|
| 153 | ~~~~~~~~~~~~~~~~~~~~ |
---|
| 154 | |
---|
[2068] | 155 | .. _ContentsValidator: |
---|
| 156 | |
---|
[2027] | 157 | Defining a ``ContentsValidator`` method is optional, but is usually a |
---|
| 158 | good idea, particularly if the file extension is not a reliable |
---|
| 159 | identifier for the file type. The intent of this routine is to take a |
---|
| 160 | superficial look at the file to see if it has the expected |
---|
| 161 | characteristics of the expected file type. For example, are there |
---|
| 162 | numbers in the expected places? |
---|
| 163 | |
---|
| 164 | This routine is passed a single argument: |
---|
| 165 | |
---|
| 166 | * `filepointer`: a file object (created by :func:`open`) that accesses |
---|
| 167 | the file and is points to the beginning of the file when ContentsValidator is |
---|
| 168 | called. |
---|
| 169 | |
---|
| 170 | Note that :meth:`GSASIIIO.ImportBaseclass.CIFValidator` is a ContentsValidator |
---|
| 171 | for validating CIF files. |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | ReInitialize() |
---|
| 175 | ~~~~~~~~~~~~~~~~~~~~ |
---|
| 176 | |
---|
| 177 | Import classes are substantiated only once and are used as needed. |
---|
| 178 | This means that if something needs to be initialized before the |
---|
| 179 | ``Reader()`` will be called to read a new file, it must be coded. The |
---|
| 180 | ``ReInitialize()`` method is provided for this and it is always called |
---|
| 181 | before the ``ContentsValidator`` method is called. Use care to call |
---|
| 182 | the parent class ``ReInitialize()`` method, if this is overridden. |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | ContentsValidator return values |
---|
| 186 | ________________________________ |
---|
| 187 | |
---|
| 188 | The ``ContentsValidator`` routine should return the value of True if |
---|
| 189 | the file appears to match the type expected for the class. |
---|
| 190 | |
---|
| 191 | If the file cannot be read by this class, the routine should |
---|
| 192 | return False. Preferably one will also place text in `self.errors` |
---|
| 193 | to give the user information on what went wrong during the reading. |
---|
| 194 | |
---|
| 195 | Phase Import Routines |
---|
| 196 | ---------------------------------------- |
---|
| 197 | Phase import routines are classes derived from |
---|
| 198 | :class:`GSASIIIO.ImportPhase`. |
---|
| 199 | They must be found in files named `G2phase*.py` that are in the Python path |
---|
| 200 | and the class must override the ``__init__`` method and add a ``Reader`` method. |
---|
| 201 | The distributed routines are: |
---|
| 202 | |
---|
| 203 | .. automodule:: G2phase |
---|
| 204 | :members: |
---|
| 205 | :synopsis: Uses previously implemented code: PDB and GSAS .EXP |
---|
| 206 | |
---|
| 207 | .. automodule:: G2phase_GPX |
---|
| 208 | :members: |
---|
| 209 | :synopsis: Reads phase information from a GSAS-II project (.gpx) file |
---|
| 210 | a text file. |
---|
| 211 | |
---|
| 212 | .. automodule:: G2phase_CIF |
---|
| 213 | :members: |
---|
| 214 | :synopsis: Reads phase information from a CIF |
---|
| 215 | |
---|
| 216 | Powder Data Import Routines |
---|
| 217 | --------------------------------------------- |
---|
| 218 | Powder data import routines are classes derived from |
---|
| 219 | :class:`GSASIIIO.ImportPowderData`. |
---|
| 220 | They must be found in files named `G2pwd*.py` that are in the Python path |
---|
| 221 | and the class must override the ``__init__`` method and add a |
---|
| 222 | ``Reader`` method. |
---|
| 223 | |
---|
| 224 | The distributed routines are: |
---|
| 225 | |
---|
| 226 | .. automodule:: G2pwd_GPX |
---|
| 227 | :members: |
---|
| 228 | :synopsis: Reads powder data from from a GSAS-II project (.gpx) file |
---|
| 229 | |
---|
| 230 | .. automodule:: G2pwd_fxye |
---|
| 231 | :members: |
---|
| 232 | :synopsis: Reads powder data in all of the GSAS formats |
---|
| 233 | |
---|
| 234 | .. automodule:: G2pwd_xye |
---|
| 235 | :members: |
---|
| 236 | :synopsis: Reads powder data from a Topas format file |
---|
| 237 | |
---|
| 238 | .. automodule:: G2pwd_CIF |
---|
| 239 | :members: |
---|
| 240 | :synopsis: Reads powder data from a CIF |
---|
| 241 | |
---|
| 242 | Single Crystal Data Import Routines |
---|
| 243 | ----------------------------------------------------- |
---|
| 244 | Single crystal data import routines are classes derived from |
---|
| 245 | , :class:`GSASIIIO.ImportStructFactor`. |
---|
| 246 | They must be found in files named `G2sfact*.py` that are in the Python path |
---|
| 247 | and the class must override the ``__init__`` method and add a ``Reader`` method. |
---|
| 248 | The distributed routines are: |
---|
| 249 | |
---|
| 250 | .. automodule:: G2sfact |
---|
| 251 | :members: |
---|
| 252 | :synopsis: Reads single crystal data from simple hkl files |
---|
| 253 | |
---|
| 254 | .. automodule:: G2sfact_CIF |
---|
| 255 | :members: |
---|
| 256 | :synopsis: Reads single crystal data from CIF files |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | Small Angle Scattering Data Import Routines |
---|
| 260 | ----------------------------------------------------- |
---|
| 261 | Small angle scattering data import routines are classes derived from |
---|
| 262 | , :class:`GSASIIIO.ImportSmallAngle`. |
---|
| 263 | They must be found in files named `G2sad*.py` that are in the Python path |
---|
| 264 | and the class must override the ``__init__`` method and add a ``Reader`` method. |
---|
| 265 | The distributed routines are: |
---|
| 266 | |
---|
| 267 | .. automodule:: G2sad_xye |
---|
| 268 | :members: |
---|
| 269 | :synopsis: Reads small angle scattering data from simple files |
---|
| 270 | |
---|
| 271 | Image Import Routines |
---|
| 272 | ----------------------------------------------------- |
---|
| 273 | Image import routines are classes derived from |
---|
[2068] | 274 | :class:`GSASIIIO.ImportImage`. |
---|
| 275 | See :ref:`Writing a Import Routine<Import_Routines>` for general |
---|
| 276 | information on importers and |
---|
| 277 | :ref:`the ImportImage docstring<Image_import_routines>` |
---|
| 278 | for what a reader should define. |
---|
| 279 | Image importers must be found in files named `G2img*.py` that are in the Python path |
---|
| 280 | and the class must override the ``__init__`` method and add a |
---|
| 281 | ``Reader`` method. |
---|
| 282 | |
---|
[2027] | 283 | The distributed routines are: |
---|
| 284 | |
---|
| 285 | .. automodule:: G2img_ADSC |
---|
| 286 | :members: |
---|
| 287 | |
---|
| 288 | .. automodule:: G2img_EDF |
---|
| 289 | :members: |
---|
| 290 | |
---|
| 291 | .. automodule:: G2img_SumG2 |
---|
| 292 | :members: |
---|
| 293 | |
---|
| 294 | .. automodule:: G2img_GE |
---|
| 295 | :members: |
---|
| 296 | |
---|
| 297 | .. automodule:: G2img_MAR |
---|
| 298 | :members: |
---|
| 299 | |
---|
| 300 | .. automodule:: G2img_Rigaku |
---|
| 301 | :members: |
---|
| 302 | |
---|
| 303 | .. automodule:: G2img_1TIF |
---|
| 304 | :members: |
---|
| 305 | |
---|
| 306 | .. automodule:: G2img_CheMin |
---|
| 307 | :members: |
---|