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