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