1
2
3 """
4
5 Authors: Henning O. Sorensen & Erik Knudsen
6 Center for Fundamental Research: Metal Structures in Four Dimensions
7 Risoe National Laboratory
8 Frederiksborgvej 399
9 DK-4000 Roskilde
10 email:erik.knudsen@risoe.dk
11
12 + Jon Wright, ESRF
13 """
14 from fabioutils import filename_object, next_filename
15
16 from openimage import openimage
17
18
20 """
21 Created from a fabio image
22 first and last are file numbers
23 """
24 im = first_object
25 nimages = 0
26
27 if None in (first, last):
28 step = 0
29 total = 1
30 else:
31 total = last - first
32
33 yield im
34 while nimages < total:
35 nimages += step
36 try:
37 newim = im.next()
38 im = newim
39 except:
40 import traceback
41 traceback.print_exc()
42
43
44 print "Got a problem here"
45 try:
46 im.filename = next_filename(im.filename)
47 except:
48
49
50 im.filename = next_filename(im.sequencefilename)
51 yield None
52 yield im
53
54
55
57 """
58 A generator function that creates a file series starting from a a fabioimage.
59 Iterates through all images in a file (if more than 1), then proceeds to
60 the next file as determined by fabio.next_filename.
61
62 first_object: the starting fabioimage, which will be the first one yielded
63 in the sequence
64 nimages: the maximum number of images to consider
65 step: step size, will yield the first and every step'th image until nimages
66 is reached. (e.g. nimages = 5, step = 2 will yield 3 images (0, 2, 4)
67 traceback: if True causes it to print a traceback in the event of an
68 exception (missing image, etc.). Otherwise the calling routine can handle
69 the exception as it chooses
70 yields: the next fabioimage in the series.
71 In the event there is an exception, it yields the sys.exec_info for the
72 exception instead. sys.exec_info is a tuple:
73 ( exceptionType, exceptionValue, exceptionTraceback )
74 from which all the exception information can be obtained.
75 Suggested usage:
76 for obj in new_file_series( ... ):
77 if not isinstance( obj, fabio.fabioimage.fabioimage ):
78 # deal with errors like missing images, non readable files, etc
79 # e.g.
80 traceback.print_exception(obj[0], obj[1], obj[2])
81 """
82 im = first_object
83 nprocessed = 0
84 abort = False
85 if nimages > 0:
86 yield im
87 nprocessed += 1
88 while nprocessed < nimages:
89 try:
90 newim = im.next()
91 im = newim
92 retVal = im
93 except Exception, ex:
94 import sys
95 retVal = sys.exc_info()
96 if(traceback):
97 import traceback
98 traceback.print_exc()
99
100 print "Got a problem here: next() failed"
101
102 try:
103 im.filename = next_filename(im.filename)
104 except:
105 pass
106 if nprocessed % step == 0:
107 yield retVal
108
109 retVal = None
110 if abort: break
111 nprocessed += 1
112
113
114
116 """
117 represents a series of files to iterate
118 has an idea of a current position to do next and prev
119
120 You also get from the list python superclass:
121 append
122 count
123 extend
124 insert
125 pop
126 remove
127 reverse
128 sort
129 """
131 """
132 arg should be a list of strings which are filenames
133 """
134 super(file_series, self).__init__(list_of_strings)
135
136 self._current = 0
137
138
139
140
142 """ first image in series """
143 return self[0]
144
146 """ last in series """
147 return self[-1]
148
150 """ prev in a sequence"""
151 self._current -= 1
152 return self[self._current]
153
155 """ current position in a sequence """
156 return self[self._current]
157
159 """ next in a sequence """
160 self._current += 1
161 return self[self._current]
162
163 - def jump(self, num):
164 """ goto a position in sequence """
165 assert num < len(self) and num > 0, "num out of range"
166 self._current = num
167 return self[self._current]
168
170 """ number of files"""
171 return len(self)
172
173
174
175
177 """ first image in a sequence """
178 return openimage(self.first())
179
181 """ last image in a sequence """
182 return openimage(self.last())
183
185 """ Return the next image """
186 return openimage(self.next())
187
191
193 """ jump to and read image """
194 return openimage(self.jump(num))
195
199
200
201
205
209
213
217
221
225
226
227
228
230 """
231 mydata0001.edf = "mydata" + 0001 + ".edf"
232 mydata0002.edf = "mydata" + 0002 + ".edf"
233 mydata0003.edf = "mydata" + 0003 + ".edf"
234 """
235 - def __init__(self, stem, first, last, extension,
236 digits=4, padding='Y', step=1):
237 """
238 stem - first part of the name
239 step - in case of every nth file
240 padding - possibility for specifying that numbers are not padded
241 with zeroes up to digits
242 """
243 if padding == 'Y':
244 fmt = "%s%0" + str(digits) + "d%s"
245 else:
246 fmt = "%s%i%s"
247
248 super(numbered_file_series, self).__init__(
249 [ fmt % (stem, i, extension) for i in range(first,
250 last + 1,
251 step) ])
252
253
255 """ Much like the others, but created from a string filename """
257 """ create from a filename (String)"""
258 self.obj = filename_object(filename)
259
261 """ increment number """
262 self.obj.num += 1
263 return self.obj.tostring()
264
266 """ decrement number """
267 self.obj.num -= 1
268 return self.obj.tostring()
269
271 """ return current filename string"""
272 return self.obj.tostring()
273
274 - def jump(self, num):
275 """ jump to a specific number """
276 self.obj.num = num
277 return self.obj.tostring()
278
279
281 """ returns the next image as a fabioimage """
282 return openimage(self.next())
284 """ returns the previos image as a fabioimage """
285 return openimage(self.previous())
287 """ returns the current image as a fabioimage"""
288 return openimage(self.current())
290 """ returns the image number as a fabioimage"""
291 return openimage(self.jump(num))
292
294 """ returns the next filename as a fabio.filename_object"""
295 self.obj.num += 1
296 return self.obj
298 """ returns the previous filename as a fabio.filename_object"""
299 self.obj.num -= 1
300 return self.obj
302 """ returns the current filename as a fabio.filename_object"""
303 return self.obj
305 """ returns the filename num as a fabio.filename_object"""
306 self.obj.num = num
307 return self.obj
308