Source
from taskinit import *
import numpy
import sys
import re
# Writes out regions above threshold to regionfile+'.box'
def boxit(imagename, regionfile, threshold, maskname, chanrange, polrange, minsize, diag, boxstretch, overwrite):
casalog.origin('boxit')
myia = iatool()
try:
if not(regionfile):
regionfile = imagename + '.box'
if not regionfile.endswith('.box'):
regionfile = regionfile + '.box'
if not(overwrite):
if(os.path.exists(regionfile)):
casalog.post('file "' + regionfile + '" already exists.', 'WARN')
return
if(maskname and os.path.exists(maskname)):
casalog.post('output mask "' + maskname + '" already exists.', 'WARN')
return
# If no units, assume mJy for consistency with auto/clean tasks.
# But convert to Jy, because that's what units the images are in.
threshold = qa.getvalue(qa.convert(qa.quantity(threshold,'mJy'),'Jy'))[0]
casalog.post("Setting threshold to " + str(threshold) + "Jy", "INFO")
newIsland = numpy.zeros(1, dtype=[('box','4i4'),('npix','i4')])
# Find all pixels above the threshold
myia.open(imagename)
if len(myia.shape()) != 4:
raise Exception("Only 4D images with direction, spectral, and stokes coordinates are supported")
# CAS-2059 escape characters in image name that will confuse the lattice expression processor
escaped_imagename = imagename
for escapeme in ['-', '+', '*', '/' ]:
escaped_imagename = re.sub("[" + escapeme + "]", "\\" + escapeme, escaped_imagename)
mask = escaped_imagename+'>'+str(threshold)
fullmask = myia.getregion(mask=mask, getmask=True)
if not(fullmask.max()):
casalog.post('Maximum flux in image is below threshold.', 'WARN')
return
writemask = bool(maskname)
csys = myia.coordsys()
shape = fullmask.shape
nx = int(shape[0])
ny = int(shape[1])
n2 = n3 = 1
if len(shape)==3:
n2 = int(shape[2])
if len(shape)==4:
n2 = int(shape[2])
n3 = int(shape[3])
#casa generated images always 4d and in order of [ra, dec, stokes, freq]
#other images can be in the order of [ra, dec, freq, stokes]
nms = csys.names()
chmax=n3
pomax=n2
chmin=0