casa
$Rev:20696$
|
00001 # sd task for image processing (press or basket) 00002 import os 00003 import numpy 00004 import numpy.fft as npfft 00005 import pylab as pl 00006 import asap as sd 00007 from taskinit import * 00008 import time 00009 import sdutil 00010 00011 def sdimprocess(infiles, mode, numpoly, beamsize, smoothsize, direction, masklist, tmax, tmin, outfile, overwrite): 00012 00013 casalog.origin('sdimprocess') 00014 00015 try: 00016 worker = sdimprocess_worker(**locals()) 00017 worker.initialize() 00018 worker.execute() 00019 worker.finalize() 00020 00021 except Exception, instance: 00022 sdutil.process_exception(instance) 00023 casalog.post( str(instance), priority = 'ERROR' ) 00024 raise Exception, instance 00025 00026 class sdimprocess_worker(sdutil.sdtask_interface): 00027 def __init__(self, **kwargs): 00028 super(sdimprocess_worker,self).__init__(**kwargs) 00029 00030 def __del__(self): 00031 # cleanup method must be called when the instance is 00032 # deleted 00033 self.cleanup() 00034 00035 def initialize(self): 00036 self.parameter_check() 00037 00038 # temporary filename 00039 tmpstr = time.ctime().replace( ' ', '_' ).replace( ':', '_' ) 00040 self.tmpmskname = 'masked.'+tmpstr+'.im' 00041 self.tmpconvname = 'convolve2d.'+tmpstr+'.im' 00042 self.tmppolyname = 'polyfit.'+tmpstr+'.im' 00043 # set tempolary filename 00044 self.tmprealname = [] 00045 self.tmpimagname = [] 00046 self.image = None 00047 self.convimage = None 00048 self.polyimage = None 00049 self.imageorg = None 00050 self.realimage = None 00051 self.imagimage = None 00052 if type(self.infiles) == str: 00053 self.tmprealname.append( 'fft.'+tmpstr+'.real..0.im' ) 00054 self.tmpimagname.append( 'fft.'+tmpstr+'.imag.0.im' ) 00055 else: 00056 for i in range(len(self.infiles)): 00057 self.tmprealname.append( 'fft.%s.%s.real.im' % (tmpstr,i) ) 00058 self.tmpimagname.append( 'fft.%s.%s.imag.im' % (tmpstr,i) ) 00059 00060 # default output filename 00061 if self.outfile == '': 00062 self.outfile = 'sdimprocess.out.im' 00063 casalog.post( 'outfile=%s' % self.outfile ) 00064 00065 # threshold 00066 self.nolimit = 'nolimit' 00067 if self.tmin == 0.0 and self.tmax == 0.0: 00068 self.thresh = [] 00069 elif self.tmin > self.tmax: 00070 casalog.post('tmin > tmax. Swapped.' ) 00071 self.thresh = [self.tmax, self.tmin] 00072 elif self.tmin == self.tmax: 00073 if self.tmin > 0.0: 00074 casalog.post( 'tmin == tmax. Use tmin as minumum threshold.' ) 00075 self.thresh = [self.tmin, self.nolimit] 00076 else: 00077 casalog.post( 'tmin == tmax. Use tmax as maximum threshold.' ) 00078 self.thresh = [self.nolimit, self.tmin] 00079 else: 00080 self.thresh = [self.tmin, self.tmax] 00081 00082 def parameter_check(self): 00083 if self.mode.lower() == 'press': 00084 # Pressed-out method 00085 # check input file 00086 if type(self.infiles) == list: 00087 if len(self.infiles) != 1: 00088 raise Exception, "infiles allows only one input file for pressed-out method." 00089 else: 00090 self.infiles = self.infiles[0] 00091 # check direction 00092 if type(self.direction) == list: 00093 if len(self.direction) != 1: 00094 raise Exception, "direction allows only one direction for pressed-out method." 00095 else: 00096 self.direction = self.direction[0] 00097 elif self.mode.lower() == 'basket': 00098 # FFT-based basket-weaving method 00099 # check input file 00100 if type(self.infiles) == str or \ 00101 (type(self.infiles) == list and len(self.infiles) < 2): 00102 raise Exception, "infiles should be a list of input images for Basket-Weaving." 00103 00104 # check direction 00105 if type(self.direction) == float: 00106 raise Exception, 'direction must have at least two different direction.' 00107 else: 00108 if len(self.direction) < 2: 00109 raise Exception, 'direction must have at least two different direction.' 00110 else: 00111 raise Exception, 'Unsupported processing mode: %s'%(self.mode) 00112 00113 def execute(self): 00114 if self.mode.lower() == 'press': 00115 self.__execute_press() 00116 elif self.mode.lower() == 'basket': 00117 self.__execute_basket_weaving() 00118 00119 def __execute_press(self): 00120 ### 00121 # Pressed-out method (Sofue & Reich 1979) 00122 ### 00123 casalog.post( 'Apply Pressed-out method' ) 00124 00125 # mask 00126 self.image = ia.newimagefromimage(infile=self.infiles,outfile=self.tmpmskname) 00127 imshape = self.image.shape() 00128 nx = imshape[0] 00129 ny = imshape[1] 00130 nchan = imshape[2] 00131 if len(self.thresh) == 0: 00132 casalog.post( 'Use whole region' ) 00133 else: 00134 if len(imshape) == 4: 00135 # with polarization axis 00136 npol = imshape[3] 00137 for ichan in range(nchan): 00138 for ipol in range(npol): 00139 pixmsk = self.image.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol]) 00140 for ix in range(pixmsk.shape[0]): 00141 for iy in range(pixmsk.shape[1]): 00142 if self.thresh[0] == self.nolimit: 00143 if pixmsk[ix][iy] > self.thresh[1]: 00144 pixmsk[ix][iy] = 0.0 00145 elif self.thresh[1] == self.nolimit: 00146 if pixmsk[ix][iy] < self.thresh[0]: 00147 pixmsk[ix][iy] = 0.0 00148 else: 00149 if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]: 00150 pixmsk[ix][iy] = 0.0 00151 self.image.putchunk( pixmsk, [0,0,ichan,ipol] ) 00152 elif len(imshape) == 3: 00153 # no polarization axis 00154 for ichan in range(nchan): 00155 pixmsk = self.image.getchunk( [0,0,ichan], [nx-1,ny-1,ichan]) 00156 for ix in range(pixmsk.shape[0]): 00157 for iy in range(pixmsk.shape[1]): 00158 if self.thresh[0] == self.nolimit: 00159 if pixmsk[ix][iy] > self.thresh[1]: 00160 pixmsk[ix][iy] = 0.0 00161 elif self.thresh[1] == self.nolimit: 00162 if pixmsk[ix][iy] < self.thresh[0]: 00163 pixmsk[ix][iy] = 0.0 00164 else: 00165 if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]: 00166 pixmsk[ix][iy] = 0.0 00167 self.image.putchunk( pixmsk, [0,0,ichan] ) 00168 00169 00170 # smoothing 00171 #bmajor = 0.0 00172 #bminor = 0.0 00173 if type(self.beamsize) == str: 00174 qbeamsize = qa.quantity(self.beamsize) 00175 else: 00176 qbeamsize = qa.quantity(self.beamsize,'arcsec') 00177 if type(self.smoothsize) == str: 00178 #bmajor = smoothsize 00179 #bminor = smoothsize 00180 qsmoothsize = qa.quantity(self.smoothsize) 00181 else: 00182 #bmajor = '%sarcsec' % (beamsize*smoothsize) 00183 #bminor = '%sarcsec' % (beamsize*smoothsize) 00184 qsmoothsize = qa.mul(qbeamsize,self.smoothsize) 00185 bmajor = qsmoothsize 00186 bminor = qsmoothsize 00187 self.convimage = self.image.convolve2d( outfile=self.tmpconvname, major=bmajor, minor=bminor, overwrite=True ) 00188 self.convimage.done() 00189 self.convimage = ia.newimage( self.tmpconvname ) 00190 00191 # get dTij (original - smoothed) 00192 if len(imshape) == 4: 00193 # with polarization axis 00194 npol = imshape[3] 00195 for ichan in range(nchan): 00196 for ipol in range(npol): 00197 pixmsk = self.image.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol]) 00198 pixsmo = self.convimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan,ipol] ) 00199 pixsub = pixmsk - pixsmo 00200 self.convimage.putchunk( pixsub, [0,0,ichan,ipol] ) 00201 elif len(imshape) == 3: 00202 for ichan in range(nchan): 00203 # no polarization axis 00204 pixmsk = self.image.getchunk( [0,0,ichan], [nx-1,ny-1,ichan]) 00205 pixsmo = self.convimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan] ) 00206 pixsub = pixmsk - pixsmo 00207 self.convimage.putchunk( pixsub, [0,0,ichan] ) 00208 00209 # polynomial fit 00210 fitaxis = 0 00211 if self.direction == 0.0: 00212 fitaxis = 0 00213 elif self.direction == 90.0: 00214 fitaxis = 1 00215 else: 00216 raise Exception, "Sorry, the task don't support inclined scan with respect to horizontal or vertical axis, right now." 00217 # Replace duplicated method ia.fitpolynomial with 00218 # ia.fitprofile 00219 #polyimage = convimage.fitpolynomial( fitfile=tmppolyname, axis=fitaxis, order=numpoly, overwrite=True ) 00220 #polyimage.done() 00221 if os.path.exists( self.tmppolyname ): 00222 cu.removetable([self.tmppolyname]) 00223 self.convimage.setbrightnessunit('K') 00224 resultdic = self.convimage.fitprofile( model=self.tmppolyname, axis=fitaxis, poly=self.numpoly, ngauss=0, multifit=True ) 00225 polyimage = ia.newimage( self.tmppolyname ) 00226 00227 # subtract fitted image from original map 00228 imageorg = ia.newimage( self.infiles ) 00229 if len(imshape) == 4: 00230 # with polarization axis 00231 npol = imshape[3] 00232 for ichan in range(nchan): 00233 for ipol in range(npol): 00234 pixorg = imageorg.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol]) 00235 pixpol = polyimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol] ) 00236 pixsub = pixorg - pixpol 00237 polyimage.putchunk( pixsub, [0,0,ichan,ipol] ) 00238 elif len(imshape) == 3: 00239 # no polarization axis 00240 for ichan in range(nchan): 00241 pixorg = imageorg.getchunk( [0,0,ichan], [nx-1,ny-1,ichan]) 00242 pixpol = polyimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan] ) 00243 pixsub = pixorg - pixpol 00244 polyimage.putchunk( pixsub, [0,0,ichan] ) 00245 00246 # output 00247 polyimage.rename( self.outfile, overwrite=self.overwrite ) 00248 00249 polyimage.done() 00250 self.convimage.done( remove=True ) 00251 self.image.done() 00252 imageorg.done() 00253 00254 def __execute_basket_weaving(self): 00255 ### 00256 # Basket-Weaving (Emerson & Grave 1988) 00257 ### 00258 casalog.post( 'Apply Basket-Weaving' ) 00259 00260 # initial setup 00261 outimage = ia.newimagefromimage( infile=self.infiles[0], outfile=self.outfile, overwrite=self.overwrite ) 00262 imshape = outimage.shape() 00263 nx = imshape[0] 00264 ny = imshape[1] 00265 nchan = imshape[2] 00266 tmp=[] 00267 nfile = len(self.infiles) 00268 for i in xrange(nfile): 00269 tmp.append(numpy.zeros(imshape,dtype=float)) 00270 maskedpixel=numpy.array(tmp) 00271 del tmp 00272 00273 # direction 00274 dirs = [] 00275 if len(self.direction) == nfile: 00276 dirs = self.direction 00277 else: 00278 casalog.post( 'direction information is extrapolated.' ) 00279 for i in range(nfile): 00280 dirs.append(self.direction[i%len(direction)]) 00281 00282 # masklist 00283 masks = [] 00284 if type(self.masklist) == float: 00285 for i in range(nfile): 00286 masks.append( self.masklist ) 00287 elif type(self.masklist) == list and nfile != len(self.masklist): 00288 for i in range(nfile): 00289 masks.append( self.masklist[i%len(self.masklist)] ) 00290 for i in range(len(masks)): 00291 masks[i] = 0.01 * masks[i] 00292 00293 # mask 00294 for i in range(nfile): 00295 self.realimage = ia.newimagefromimage( infile=self.infiles[i], outfile=self.tmprealname[i] ) 00296 self.imagimage = ia.newimagefromimage( infile=self.infiles[i], outfile=self.tmpimagname[i] ) 00297 self.realimage.close() 00298 self.imagimage.close() 00299 if len(self.thresh) == 0: 00300 casalog.post( 'Use whole region' ) 00301 else: 00302 if len(imshape) == 4: 00303 # with polarization axis 00304 npol = imshape[3] 00305 for i in range(nfile): 00306 self.realimage = ia.newimage( self.tmprealname[i] ) 00307 for ichan in range(nchan): 00308 for ipol in range(npol): 00309 pixmsk = self.realimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol]) 00310 for ix in range(pixmsk.shape[0]): 00311 for iy in range(pixmsk.shape[1]): 00312 if self.thresh[0] == self.nolimit: 00313 if pixmsk[ix][iy] > self.thresh[1]: 00314 maskedpixel[i][ix][iy][ichan][ipol]=pixmsk[ix][iy] 00315 pixmsk[ix][iy] = 0.0 00316 elif self.thresh[1] == self.nolimit: 00317 if pixmsk[ix][iy] < self.thresh[0]: 00318 maskedpixel[i][ix][iy][ichan][ipol]=pixmsk[ix][iy] 00319 pixmsk[ix][iy] = 0.0 00320 else: 00321 if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]: 00322 maskedpixel[i][ix][iy][ichan][ipol]=pixmsk[ix][iy] 00323 pixmsk[ix][iy] = 0.0 00324 self.realimage.putchunk( pixmsk, [0,0,ichan,ipol] ) 00325 self.realimage.close() 00326 elif len(imshape) == 3: 00327 # no polarization axis 00328 for i in range(nfile): 00329 self.realimage = ia.newimage( self.tmprealname[i] ) 00330 for ichan in range(nchan): 00331 pixmsk = self.realimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan]) 00332 for ix in range(pixmsk.shape[0]): 00333 for iy in range(pixmsk.shape[1]): 00334 if self.thresh[0] == self.nolimit: 00335 if pixmsk[ix][iy] > self.thresh[1]: 00336 maskedpixel[i][ix][iy][ichan]=pixmsk[ix][iy] 00337 pixmsk[ix][iy] = 0.0 00338 elif self.thresh[1] == self.nolimit: 00339 if pixmsk[ix][iy] < self.thresh[0]: 00340 maskedpixel[i][ix][iy][ichan]=pixmsk[ix][iy] 00341 pixmsk[ix][iy] = 0.0 00342 else: 00343 if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]: 00344 maskedpixel[i][ix][iy][ichan]=pixmsk[ix][iy] 00345 pixmsk[ix][iy] = 0.0 00346 self.realimage.putchunk( pixmsk, [0,0,ichan] ) 00347 self.realimage.close() 00348 maskedvalue=None 00349 if any(maskedpixel.flatten()!=0.0): 00350 maskedvalue=maskedpixel.mean(axis=0) 00351 del maskedpixel 00352 00353 # set weight factor 00354 weights = numpy.ones( shape=(nfile,nx,ny), dtype=float ) 00355 eps = 1.0e-5 00356 dtor = numpy.pi / 180.0 00357 for i in range(nfile): 00358 if abs(numpy.sin(dirs[i]*dtor)) < eps: 00359 # direction is around 0 deg 00360 maskw = 0.5 * nx * masks[i] 00361 for ix in range(nx): 00362 for iy in range(ny): 00363 dd = abs( float(ix) - 0.5 * (nx-1) ) 00364 if dd < maskw: 00365 cosd = numpy.cos(0.5*numpy.pi*dd/maskw) 00366 weights[i][ix][iy] = 1.0 - cosd * cosd 00367 if weights[i][ix][iy] == 0.0: 00368 weights[i][ix][iy] += eps*0.01 00369 elif abs(numpy.cos(dirs[i]*dtor)) < eps: 00370 # direction is around 90 deg 00371 maskw = 0.5 * ny * masks[i] 00372 for ix in range(nx): 00373 for iy in range(ny): 00374 dd = abs( float(iy) - 0.5 * (ny-1) ) 00375 if dd < maskw: 00376 cosd = numpy.cos(0.5*numpy.pi*dd/maskw) 00377 weights[i][ix][iy] = 1.0 - cosd * cosd 00378 if weights[i][ix][iy] == 0.0: 00379 weights[i][ix][iy] += eps*0.01 00380 else: 00381 maskw = 0.5 * sqrt( nx * ny ) * masks[i] 00382 for ix in range(nx): 00383 for iy in range(ny): 00384 tand = numpy.tan((dirs[i]-90.0)*dtor) 00385 dd = abs( ix * tand - iy - 0.5 * (nx-1) * tand + 0.5 * (ny-1) ) 00386 dd = dd / sqrt( 1.0 + tand * tand ) 00387 if dd < maskw: 00388 cosd = numpy.cos(0.5*numpy.pi*dd/maskw) 00389 weights[i][ix][iy] = 1.0 - cosd * cosd 00390 if weights[i][ix][iy] == 0.0: 00391 weights[i][ix][iy] += eps*0.01 00392 # shift 00393 xshift = -((ny-1)/2) 00394 yshift = -((nx-1)/2) 00395 for ix in range(xshift,0,1): 00396 tmp = weights[i,:,0].copy() 00397 weights[i,:,0:ny-1] = weights[i,:,1:ny].copy() 00398 weights[i,:,ny-1] = tmp 00399 for iy in range(yshift,0,1): 00400 tmp = weights[i,0:1].copy() 00401 weights[i,0:nx-1] = weights[i,1:nx].copy() 00402 weights[i,nx-1:nx] = tmp 00403 00404 # FFT 00405 if len(imshape) == 4: 00406 # with polarization axis 00407 npol = imshape[3] 00408 for i in range(nfile): 00409 self.realimage = ia.newimage( self.tmprealname[i] ) 00410 self.imagimage = ia.newimage( self.tmpimagname[i] ) 00411 for ichan in range(nchan): 00412 for ipol in range(npol): 00413 pixval = self.realimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol] ) 00414 pixval = pixval.reshape((nx,ny)) 00415 pixfft = npfft.fft2( pixval ) 00416 pixfft = pixfft.reshape((nx,ny,1,1)) 00417 self.realimage.putchunk( pixfft.real, [0,0,ichan,ipol] ) 00418 self.imagimage.putchunk( pixfft.imag, [0,0,ichan,ipol] ) 00419 del pixval, pixfft 00420 self.realimage.close() 00421 self.imagimage.close() 00422 elif len(imshape) == 3: 00423 # no polarization axis 00424 for i in range(nfile): 00425 self.realimage = ia.newimage( self.tmprealname[i] ) 00426 self.imagimage = ia.newimage( self.tmpimagname[i] ) 00427 for ichan in range(nchan): 00428 pixval = self.realimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan] ) 00429 pixval = pixval.reshape((nx,ny)) 00430 pixfft = npfft.fft2( pixval ) 00431 pixfft = pixfft.reshape((nx,ny,1)) 00432 self.realimage.putchunk( pixfft.real, [0,0,ichan] ) 00433 self.imagimage.putchunk( pixfft.imag, [0,0,ichan] ) 00434 del pixval, pixfft 00435 self.realimage.close() 00436 self.imagimage.close() 00437 00438 # weighted mean 00439 if len(imshape) == 4: 00440 npol = imshape[3] 00441 for ichan in range(nchan): 00442 for ipol in range(npol): 00443 pixout = numpy.zeros( shape=(nx,ny), dtype=complex ) 00444 denom = numpy.zeros( shape=(nx,ny), dtype=float ) 00445 for i in range(nfile): 00446 self.realimage = ia.newimage( self.tmprealname[i] ) 00447 self.imagimage = ia.newimage( self.tmpimagname[i] ) 00448 pixval = self.realimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol] ) + self.imagimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol] ) * 1.0j 00449 pixval = pixval.reshape((nx,ny)) 00450 pixout = pixout + pixval * weights[i] 00451 denom = denom + weights[i] 00452 self.realimage.close() 00453 self.imagimage.close() 00454 pixout = pixout / denom 00455 pixout = pixout.reshape((nx,ny,1,1)) 00456 self.realimage = ia.newimage( self.tmprealname[0] ) 00457 self.imagimage = ia.newimage( self.tmpimagname[0] ) 00458 self.realimage.putchunk( pixout.real, [0,0,ichan,ipol] ) 00459 self.imagimage.putchunk( pixout.imag, [0,0,ichan,ipol] ) 00460 self.realimage.close() 00461 self.imagimage.close() 00462 elif len(imshape) == 3: 00463 for ichan in range(nchan): 00464 pixout = numpy.zeros( shape=(nx,ny), dtype=complex ) 00465 denom = numpy.zeros( shape=(nx,ny), dtype=float ) 00466 for i in range(nfile): 00467 self.realimage = ia.newimage( self.tmprealname[i] ) 00468 self.imagimage = ia.newimage( self.tmpimagname[i] ) 00469 pixval = self.realimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan] ) 00470 pixval = pixval.reshape((nx,ny)) 00471 pixout = pixout + pixval * weights[i] 00472 denom = denom + weights[i] 00473 self.realimage.close() 00474 self.imagimage.close() 00475 pixout = pixout / denom 00476 pixout = pixout.reshape((nx,ny,1)) 00477 self.realimage = ia.newimage( self.tmprealname[0] ) 00478 self.imagimage = ia.newimage( self.tmpimagname[0] ) 00479 self.realimage.putchunk( pixout.real, [0,0,ichan] ) 00480 self.imagimage.putchunk( pixout.imag, [0,0,ichan] ) 00481 self.realimage.close() 00482 self.imagimage.close() 00483 00484 # inverse FFT 00485 self.realimage = ia.newimage( self.tmprealname[0] ) 00486 self.imagimage = ia.newimage( self.tmpimagname[0] ) 00487 if len(imshape) == 4: 00488 npol = imshape[3] 00489 for ichan in range(nchan): 00490 for ipol in range(npol): 00491 pixval = self.realimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol] ) + self.imagimage.getchunk( [0,0,ichan,ipol], [nx-1,ny-1,ichan,ipol] ) * 1.0j 00492 pixval = pixval.reshape((nx,ny)) 00493 pixifft = npfft.ifft2( pixval ) 00494 pixifft = pixifft.reshape((nx,ny,1,1)) 00495 outimage.putchunk( pixifft.real, [0,0,ichan,ipol] ) 00496 del pixval, pixifft 00497 elif len(imshape) == 3: 00498 for ichan in range(nchan): 00499 pixval = self.realimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan] ) + self.imagimage.getchunk( [0,0,ichan], [nx-1,ny-1,ichan] ) * 1.0j 00500 pixval = pixval.reshape((nx,ny)) 00501 pixifft = npfft.ifft2( pixval ) 00502 pixifft = pixifft.reshape((nx,ny,1)) 00503 outimage.putchunk( pixifft.real, [0,0,ichan] ) 00504 del pixval, pixifft 00505 if maskedvalue is not None: 00506 outimage.putchunk(outimage.getchunk()+maskedvalue) 00507 self.realimage.close() 00508 self.imagimage.close() 00509 outimage.close() 00510 00511 def finalize(self): 00512 self.cleanup() 00513 00514 def cleanup(self): 00515 # finalize image analysis tool 00516 if self.image is not None: 00517 if self.image.isopen(): self.image.done() 00518 tools = [self.convimage, self.imageorg, 00519 self.realimage, self.imagimage] 00520 for t in tools: 00521 if t and t.isopen(): t.done(remove=True) 00522 00523 # remove tempolary files 00524 filelist = [self.tmpmskname, self.tmpconvname, self.tmppolyname] \ 00525 + self.tmprealname + self.tmpimagname 00526 existing_files = [] 00527 for f in filelist: 00528 if os.path.exists(f): 00529 existing_files.append(f) 00530 cu.removetable(existing_files) 00531