def cleanup(self): # finalize image analysis tool if hasattr(self, "image") and self.image is not None: if self.image.isopen(): self.image.done() tools = ["convimage", "imageorg", "realimage", "imagimage"] for t in tools: if hasattr(self, t): v = getattr(self, t) if v and v.isopen(): v.done(remove=True) # remove tempolary files filelist = ["tmpmskname", "tmpconvname", "tmppolyname", "tmprealname", "tmpimagname"] existing_files = [] for s in filelist: if hasattr(self, s): f = getattr(self, s) if isinstance(f, list): for g in f: if os.path.exists(g): existing_files.append(g) else: if os.path.exists(f): existing_files.append(f) # CAS-5410 Use private tools inside task scripts if len(existing_files) > 0: cu = utilstool() cu.removetable(existing_files)
def cleanup(self): # finalize image analysis tool if hasattr(self,'image') and self.image is not None: if self.image.isopen(): self.image.done() tools = ['convimage', 'imageorg', 'realimage', 'imagimage'] for t in tools: if hasattr(self,t): v = getattr(self,t) if v and v.isopen(): v.done(remove=True) # remove tempolary files filelist = ['tmpmskname', 'tmpconvname', 'tmppolyname', 'tmprealname', 'tmpimagname'] existing_files = [] for s in filelist: if hasattr(self, s): f = getattr(self, s) if isinstance(f,list): for g in f: if os.path.exists(g): existing_files.append(g) else: if os.path.exists(f): existing_files.append(f) # CAS-5410 Use private tools inside task scripts if len(existing_files) > 0: cu = utilstool() cu.removetable(existing_files)
def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2): if not image or not os.path.exists(image): raise RuntimeError, "No image found to fit." if os.path.exists(model): # CAS-5410 Use private tools inside task scripts cu = utilstool() cu.removetable([model]) tmpia = gentools(['ia'])[0] modelimg = tmpia.newimagefromimage(infile=image, outfile=model) try: if tmpia.isopen(): tmpia.close() imshape = modelimg.shape() # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task. ndim = len(imshape) nx = imshape[0] ny = imshape[1] # an xy-plane can be fit simultaneously (doing per plane to save memory) if ndim == 3: get_blc = lambda i, j: [0, 0, i] get_trc = lambda i, j: [nx - 1, ny - 1, i] imshape2 = imshape[2] imshape3 = 1 elif ndim == 4: get_blc = lambda i, j: [0, 0, i, j] get_trc = lambda i, j: [nx - 1, ny - 1, i, j] imshape2 = imshape[2] imshape3 = imshape[3] else: # ndim == 2 get_blc = lambda i, j: [0, 0] get_trc = lambda i, j: [nx - 1, ny - 1] imshape2 = 1 imshape3 = 1 for i3 in range(imshape3): for i2 in range(imshape2): blc = get_blc(i2, i3) trc = get_trc(i2, i3) dslice = modelimg.getchunk(blc, trc) mslice = modelimg.getchunk(blc, trc, getmask=True) model = self._get_polyfit_model_array( dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order) modelimg.putchunk(model, blc) # the fit model image itself is free from invalid pixels modelimg.calcmask('T', asdefault=True) except: raise finally: modelimg.close()
def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2): if not image or not os.path.exists(image): raise RuntimeError, "No image found to fit." if os.path.exists(model): # CAS-5410 Use private tools inside task scripts cu = utilstool() cu.removetable([model]) tmpia = gentools(['ia'])[0] modelimg = tmpia.newimagefromimage(infile=image, outfile=model) try: if tmpia.isopen(): tmpia.close() imshape = modelimg.shape() # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task. ndim = len(imshape) nx = imshape[0] ny = imshape[1] # an xy-plane can be fit simultaneously (doing per plane to save memory) if ndim == 3: for ichan in range(imshape[2]): dslice = modelimg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) mslice = modelimg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan], getmask=True) model = self._get_polyfit_model_array( dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order) modelimg.putchunk(model, [0, 0, ichan, ipol]) elif ndim == 4: for ipol in range(imshape[3]): for ichan in range(imshape[2]): dslice = modelimg.getchunk( [0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) mslice = modelimg.getchunk( [0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol], getmask=True) model = self._get_polyfit_model_array( dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order) modelimg.putchunk(model, [0, 0, ichan, ipol]) else: # ndim=2 is not supported in this task. raise RuntimeError, "image dimension should be 3 or 4" # the fit model image itself is free from invalid pixels modelimg.calcmask('T', asdefault=True) except: raise finally: modelimg.close()
def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2): if not image or not os.path.exists(image): raise RuntimeError, "No image found to fit." if os.path.exists( model ): # CAS-5410 Use private tools inside task scripts cu = utilstool() cu.removetable([model]) tmpia = gentools(['ia'])[0] modelimg = tmpia.newimagefromimage(infile=image,outfile=model) try: if tmpia.isopen(): tmpia.close() imshape = modelimg.shape() # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task. ndim = len(imshape) nx = imshape[0] ny = imshape[1] # an xy-plane can be fit simultaneously (doing per plane to save memory) if ndim == 3: get_blc = lambda i, j: [0, 0, i] get_trc = lambda i, j: [nx-1, ny-1, i] imshape2 = imshape[2] imshape3 = 1 elif ndim == 4: get_blc = lambda i, j: [0, 0, i, j] get_trc = lambda i, j: [nx-1, ny-1, i, j] imshape2 = imshape[2] imshape3 = imshape[3] else: # ndim == 2 get_blc = lambda i, j: [0,0] get_trc = lambda i, j: [nx-1, ny-1] imshape2 = 1 imshape3 = 1 for i3 in range(imshape3): for i2 in range(imshape2): blc = get_blc(i2, i3) trc = get_trc(i2, i3) dslice = modelimg.getchunk(blc, trc) mslice = modelimg.getchunk(blc, trc, getmask=True) model = self._get_polyfit_model_array(dslice.reshape(nx,ny), mslice.reshape(nx,ny), axis, order) modelimg.putchunk(model, blc) # the fit model image itself is free from invalid pixels modelimg.calcmask('T', asdefault=True) except: raise finally: modelimg.close()
def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2): if not image or not os.path.exists(image): raise RuntimeError, "No image found to fit." if os.path.exists(model): # CAS-5410 Use private tools inside task scripts cu = utilstool() cu.removetable([model]) tmpia = gentools(["ia"])[0] modelimg = tmpia.newimagefromimage(infile=image, outfile=model) try: if tmpia.isopen(): tmpia.close() imshape = modelimg.shape() # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task. ndim = len(imshape) nx = imshape[0] ny = imshape[1] # an xy-plane can be fit simultaneously (doing per plane to save memory) if ndim == 3: for ichan in range(imshape[2]): dslice = modelimg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) mslice = modelimg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan], getmask=True) model = self._get_polyfit_model_array(dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order) modelimg.putchunk(model, [0, 0, ichan, ipol]) elif ndim == 4: for ipol in range(imshape[3]): for ichan in range(imshape[2]): dslice = modelimg.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) mslice = modelimg.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol], getmask=True) model = self._get_polyfit_model_array( dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order ) modelimg.putchunk(model, [0, 0, ichan, ipol]) else: # ndim=2 is not supported in this task. raise RuntimeError, "image dimension should be 3 or 4" # the fit model image itself is free from invalid pixels modelimg.calcmask("T", asdefault=True) except: raise finally: modelimg.close()
def __execute_press(self): ### # Pressed-out method (Sofue & Reich 1979) ### casalog.post("Apply Pressed-out method") # CAS-5410 Use private tools inside task scripts ia = gentools(["ia"])[0] # mask self.image = ia.newimagefromimage(infile=self.infiles, outfile=self.tmpmskname) # back-up original mask name is_initial_mask = self.image.maskhandler("default")[0] != "" temp_maskname = "temporal" imshape = self.image.shape() nx = imshape[0] ny = imshape[1] nchan = imshape[2] if len(self.thresh) == 0: casalog.post("Use whole region") else: # mask pixels beyond thresholds maskstr = "mask('%s')" % self.tmpmskname if self.thresh[0] != self.nolimit: maskstr += " && '%s'>=%f" % (self.tmpmskname, self.thresh[0]) if self.thresh[1] != self.nolimit: maskstr += " && '%s'<=%f" % (self.tmpmskname, self.thresh[1]) # Need to flush to image once to calcmask ... sigh self.image.done() self.image = ia.newimage(self.tmpmskname) self.image.calcmask(mask=maskstr, name=temp_maskname, asdefault=True) # smoothing # bmajor = 0.0 # bminor = 0.0 # CAS-5410 Use private tools inside task scripts qa = qatool() if type(self.beamsize) == str: qbeamsize = qa.quantity(self.beamsize) else: qbeamsize = qa.quantity(self.beamsize, "arcsec") if type(self.smoothsize) == str: # bmajor = smoothsize # bminor = smoothsize qsmoothsize = qa.quantity(self.smoothsize) else: # bmajor = '%sarcsec' % (beamsize*smoothsize) # bminor = '%sarcsec' % (beamsize*smoothsize) qsmoothsize = qa.mul(qbeamsize, self.smoothsize) bmajor = qsmoothsize bminor = qsmoothsize # masked channels are replaced by zero and convolved here. self.convimage = self.image.convolve2d(outfile=self.tmpconvname, major=bmajor, minor=bminor, overwrite=True) self.convimage.done() self.convimage = ia.newimage(self.tmpconvname) # get dTij (original - smoothed) if len(imshape) == 4: # with polarization axis npol = imshape[3] for ichan in range(nchan): for ipol in range(npol): pixmsk = self.image.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) pixsmo = self.convimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) pixsub = pixmsk - pixsmo self.convimage.putchunk(pixsub, [0, 0, ichan, ipol]) elif len(imshape) == 3: for ichan in range(nchan): # no polarization axis pixmsk = self.image.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) pixsmo = self.convimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) pixsub = pixmsk - pixsmo self.convimage.putchunk(pixsub, [0, 0, ichan]) # polynomial fit fitaxis = 0 if self.direction == 0.0: fitaxis = 0 elif self.direction == 90.0: fitaxis = 1 else: raise Exception, "Sorry, the task don't support inclined scan with respect to horizontal or vertical axis, right now." # Replace duplicated method ia.fitpolynomial with # ia.fitprofile # polyimage = convimage.fitpolynomial( fitfile=tmppolyname, axis=fitaxis, order=numpoly, overwrite=True ) # polyimage.done() if os.path.exists(self.tmppolyname): # CAS-5410 Use private tools inside task scripts cu = utilstool() cu.removetable([self.tmppolyname]) self.convimage.setbrightnessunit("K") # Unfortunately, ia.fitprofile is very fragile. # Using numpy instead for fitting with masked pixels (KS, 2014/07/02) # resultdic = self.convimage.fitprofile( model=self.tmppolyname, axis=fitaxis, poly=self.numpoly, ngauss=0, multifit=True, gmncomps=0 ) self.__polynomial_fit_model(image=self.tmpmskname, model=self.tmppolyname, axis=fitaxis, order=self.numpoly) polyimage = ia.newimage(self.tmppolyname) # set back defalut mask (need to get from self.image) avail_mask = polyimage.maskhandler("get") if is_initial_mask: casalog.post("copying mask from %s" % (self.infiles)) polyimage.calcmask("mask('%s')" % self.infiles, asdefault=True) else: # no mask in the original image polyimage.calcmask("T", asdefault=True) if temp_maskname in avail_mask: polyimage.maskhandler("delete", name=temp_maskname) # subtract fitted image from original map imageorg = ia.newimage(self.infiles) if len(imshape) == 4: # with polarization axis npol = imshape[3] for ichan in range(nchan): for ipol in range(npol): pixorg = imageorg.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) pixpol = polyimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) pixsub = pixorg - pixpol polyimage.putchunk(pixsub, [0, 0, ichan, ipol]) elif len(imshape) == 3: # no polarization axis for ichan in range(nchan): pixorg = imageorg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) pixpol = polyimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) pixsub = pixorg - pixpol polyimage.putchunk(pixsub, [0, 0, ichan]) # output polyimage.rename(self.outfile, overwrite=self.overwrite) polyimage.done() self.convimage.done(remove=True) self.image.done() imageorg.done()
def __execute_press(self): ### # Pressed-out method (Sofue & Reich 1979) ### casalog.post( 'Apply Pressed-out method' ) # CAS-5410 Use private tools inside task scripts ia = gentools(['ia'])[0] # mask self.image = ia.newimagefromimage(infile=self.infiles,outfile=self.tmpmskname) # back-up original mask name is_initial_mask = (self.image.maskhandler('default')[0] != '') temp_maskname = "temporal" imshape = self.image.shape() ndim = len(imshape) nx = imshape[0] ny = imshape[1] if len(self.thresh) == 0: casalog.post( 'Use whole region' ) else: # mask pixels beyond thresholds maskstr = ("mask('%s')" % self.tmpmskname) if self.thresh[0] != self.nolimit: maskstr += (" && '%s'>=%f" % (self.tmpmskname, self.thresh[0])) if self.thresh[1] != self.nolimit: maskstr += (" && '%s'<=%f" % (self.tmpmskname, self.thresh[1])) # Need to flush to image once to calcmask ... sigh self.image.done() self.image = ia.newimage( self.tmpmskname ) self.image.calcmask(mask=maskstr, name=temp_maskname, asdefault=True) # smoothing #bmajor = 0.0 #bminor = 0.0 # CAS-5410 Use private tools inside task scripts qa = qatool() if type(self.beamsize) == str: qbeamsize = qa.quantity(self.beamsize) else: qbeamsize = qa.quantity(self.beamsize,'arcsec') if type(self.smoothsize) == str: #bmajor = smoothsize #bminor = smoothsize qsmoothsize = qa.quantity(self.smoothsize) else: #bmajor = '%sarcsec' % (beamsize*smoothsize) #bminor = '%sarcsec' % (beamsize*smoothsize) qsmoothsize = qa.mul(qbeamsize,self.smoothsize) bmajor = qsmoothsize bminor = qsmoothsize pa = qa.quantity(0.0, 'deg') # masked channels are replaced by zero and convolved here. self.convimage = self.image.convolve2d( outfile=self.tmppolyname, major=bmajor, minor=bminor, pa=pa, overwrite=True ) self.convimage.done() # get dTij (original - smoothed) self.convimage = ia.imagecalc(outfile=self.tmpconvname, pixels='"{org}" - "{conv}"'.format(org=self.tmpmskname, conv=self.tmppolyname), overwrite=True) # polynomial fit fitaxis = 0 if self.direction == 0.0: fitaxis = 0 elif self.direction == 90.0: fitaxis = 1 else: raise Exception, "Sorry, the task don't support inclined scan with respect to horizontal or vertical axis, right now." # Replace duplicated method ia.fitpolynomial with # ia.fitprofile #polyimage = convimage.fitpolynomial( fitfile=tmppolyname, axis=fitaxis, order=numpoly, overwrite=True ) #polyimage.done() if os.path.exists( self.tmppolyname ): # CAS-5410 Use private tools inside task scripts cu = utilstool() cu.removetable([self.tmppolyname]) self.convimage.setbrightnessunit('K') # Unfortunately, ia.fitprofile is very fragile. # Using numpy instead for fitting with masked pixels (KS, 2014/07/02) #resultdic = self.convimage.fitprofile( model=self.tmppolyname, axis=fitaxis, poly=self.numpoly, ngauss=0, multifit=True, gmncomps=0 ) self.__polynomial_fit_model(image=self.tmpmskname, model=self.tmppolyname,axis=fitaxis, order=self.numpoly) polyimage = ia.newimage( self.tmppolyname ) # set back defalut mask (need to get from self.image) avail_mask = polyimage.maskhandler('get') if is_initial_mask: casalog.post("copying mask from %s"%(self.infiles)) polyimage.calcmask("mask('%s')"%self.infiles,asdefault=True) else: #no mask in the original image polyimage.calcmask('T', asdefault=True) if temp_maskname in avail_mask: polyimage.maskhandler('delete', name=temp_maskname) # subtract fitted image from original map subtracted = ia.imagecalc(outfile=self.outfile, pixels='"{org}" - "{fit}"'.format(org=self.infiles, fit=self.tmppolyname), overwrite=self.overwrite) subtracted.done() # finalization polyimage.done(remove=True) self.convimage.done(remove=True) self.image.done()