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