Example #1
0
def set_impars(impars, line_name, vis, spwnames=None):
    if line_name not in ('full', ) + spwnames:
        local_impars = {}
        if 'width' in linpars:
            local_impars['width'] = linpars['width']
        else:
            # calculate the channel width
            chanwidths = []
            for vv in vis:
                msmd.open(vv)
                count_spws = len(msmd.spwsforfield(field))
                chanwidth = np.max([np.abs(
                    effectiveResolutionAtFreq(vv,
                                              spw='{0}'.format(i),
                                              freq=u.Quantity(linpars['restfreq']).to(u.GHz),
                                              kms=True)) for i in
                    range(count_spws)])

                # second awful check b/c Todd's script failed for some cases
                for spw in range(count_spws):
                    chanwidths_hz = msmd.chanwidths(int(spw))
                    chanfreqs_hz = msmd.chanfreqs(int(spw))
                    ckms = constants.c.to(u.km/u.s).value
                    if any(chanwidths_hz > (chanwidth / ckms)*chanfreqs_hz):
                        chanwidth = np.max(chanwidths_hz/chanfreqs_hz * ckms)
                msmd.close()

                chanwidths.append(chanwidth)

            # chanwidth: mean? max?
            chanwidth = np.mean(chanwidths)
            logprint("Channel widths were {0}, mean = {1}".format(chanwidths,
                                                                  chanwidth),
                     origin="almaimf_line_imaging")
            if np.any(np.array(chanwidths) - chanwidth > 1e-4):
                raise ValueError("Varying channel widths.")
            local_impars['width'] = '{0:.2f}km/s'.format(np.round(chanwidth, 2))

        local_impars['restfreq'] = linpars['restfreq']
        # calculate vstart
        vstart = u.Quantity(linpars['vlsr'])-u.Quantity(linpars['cubewidth'])/2
        local_impars['start'] = '{0:.1f}km/s'.format(vstart.value)
        local_impars['chanchunks'] = int(chanchunks)

        local_impars['nchan'] = int((u.Quantity(line_parameters[field][line_name]['cubewidth'])
                                    / u.Quantity(local_impars['width'])).value)
        if local_impars['nchan'] < local_impars['chanchunks']:
            local_impars['chanchunks'] = local_impars['nchan']
        impars.update(local_impars)
    else:
        impars['chanchunks'] = int(chanchunks)

    if 'nchan' in impars:
        # apparently you can't have nchan % chanchunks > 0
        cc = impars['chanchunks']
        while impars['nchan'] % cc > 0:
            cc -= 1
        impars['chanchunks'] = cc

    return impars
Example #2
0
def make_custom_mask(fieldname,
                     imname,
                     almaimf_code_path,
                     band_id,
                     rootdir="",
                     suffix=""):

    regfn = os.path.join(
        almaimf_code_path,
        'clean_regions/{0}_{1}{2}.reg'.format(fieldname, band_id, suffix))
    regs = regions.read_ds9(regfn)

    logprint("Using region file {0} to create mask".format(regfn),
             origin='make_custom_mask')

    cube = SpectralCube.read(imname, format='casa_image')
    image = cube[0]
    if image.unit.is_equivalent(u.Jy / u.beam):
        image = image * u.beam
    else:
        assert image.unit.is_equivalent(u.Jy), "Image must be in Jansky/beam."

    mask_array = np.zeros(image.shape, dtype='bool')

    for reg in regs:

        threshold = u.Quantity(reg.meta['label'])
        assert threshold.unit.is_equivalent(
            u.Jy), "Threshold must by in mJy or Jy"

        preg = reg.to_pixel(image.wcs)
        msk = preg.to_mask()

        mask_array[msk.bbox.slices] = (msk.multiply(image) >
                                       threshold) | mask_array[msk.bbox.slices]

    # CASA transposes arrays!!!!!
    mask_array = mask_array.T

    ia.open(imname)
    assert np.all(ia.shape() == mask_array[:, :, None, None].shape
                  ), "Failure: image shape doesn't match mask shape"
    cs = ia.coordsys()
    ia.close()

    maskname = ('{fieldname}_{band_id}{suffix}_mask.mask'.format(
        fieldname=fieldname, band_id=band_id, suffix=suffix))
    # add a root directory if there is one
    # (if rootdir == "", this just returns maskname)
    maskname = os.path.join(rootdir, maskname)

    assert ia.fromarray(outfile=maskname,
                        pixels=mask_array.astype('float')[:, :, None, None],
                        csys=cs.torecord(),
                        overwrite=True), "FAILURE in final mask creation step"
    ia.close()

    return maskname
Example #3
0
if 'only_7m' not in locals():
    if os.getenv('ONLY_7M') is not None:
        only_7m = bool(os.getenv('ONLY_7M').lower() == 'true')
    else:
        only_7m = False

# load the list of continuum MSes from a file
# (this file has one continuum MS full path, e.g. /path/to/file.ms, per line)
with open('continuum_mses.txt', 'r') as fh:
    continuum_mses = [x.strip() for x in fh.readlines()]

if os.getenv(
        'DO_BSENS') is not None and os.getenv('DO_BSENS').lower() != 'false':
    do_bsens = True
    logprint("Using BSENS measurement set")
    continuum_mses += [
        x.replace('_continuum_merged.cal.ms', '_continuum_merged_bsens.cal.ms')
        for x in continuum_mses
    ]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    band = 'B3' if 'B3' in basename else 'B6' if 'B6' in basename else 'ERROR'

    # allow optional cmdline args to skip one or the other band
    if os.getenv('BAND_TO_IMAGE'):
        if band not in os.getenv('BAND_TO_IMAGE'):
Example #4
0
if 'exclude_7m' not in locals():
    if os.getenv('EXCLUDE_7M') is not None:
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
    else:
        exclude_7m = False

if 'only_7m' not in locals():
    if os.getenv('ONLY_7M') is not None:
        only_7m = bool(os.getenv('ONLY_7M').lower() == 'true')
    else:
        only_7m = False

if 'selfcal_field_id' not in locals():
    if os.getenv('SELFCAL_FIELD_ID') is not None:
        selfcal_field_id = list(map(int, os.getenv('SELFCAL_FIELD_ID').split(",")))
        logprint("Using selfcal_field_id = {0}".format(selfcal_field_id),
                 origin='contim_selfcal')
    else:
        selfcal_field_id = None
elif selfcal_field_id is not None:
    if not isinstance(selfcal_field_id, list):
        selfcal_field_id = [selfcal_field_id]
    for entry in selfcal_field_id:
        assert isinstance(entry,int), "{0} is not an int".format(entry)
    logprint("Using selfcal_field_id = {0}".format(selfcal_field_id),
             origin='contim_selfcal')



logprint("Beginning selfcal script with exclude_7m={0} and only_7m={1}".format(exclude_7m, only_7m),
         origin='contim_selfcal')
Example #5
0
for scigoal in science_goal_dirs:
    for group in glob.glob(scigoal + "/*"):
        for member in glob.glob(os.path.join(group, "*")):
            dirpath = member
            scriptsforpi = glob.glob(
                os.path.join(dirpath, "script/*scriptForPI.py"))

            if len(scriptsforpi) == 1:
                scriptforpi = scriptsforpi[0]
            elif len(scriptsforpi) > 1:
                raise ValueError(
                    "Too many scripts for PI in {0}".format(dirpath))
            elif len(scriptsforpi) == 0:
                logprint(
                    "Skipping directory {0} because it has no scriptForPI".
                    format(dirpath))
                continue

            curdir = os.getcwd()

            if os.path.exists(os.path.join(dirpath, 'calibrated')):
                logprint("Skipping script {0} in {1} because calibrated "
                         "exists".format(scriptforpi, dirpath),
                         origin='pipeline_runner')
            elif os.path.exists(os.path.join(dirpath, 'calibration')):
                os.chdir(os.path.join(dirpath, 'script'))

                local_scriptforPI = os.path.basename(scriptforpi)

                logprint("Running script {0} in {1}".format(
from metadata_tools import determine_imsize, determine_phasecenter, logprint
from tasks import tclean, exportfits, plotms
from taskinit import msmdtool
msmd = msmdtool()

imaging_root = "imaging_results"
if not os.path.exists(imaging_root):
    os.mkdir(imaging_root)

if 'exclude_7m' not in locals():
    if os.getenv('EXCLUDE_7M'):
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
    else:
        exclude_7m = False

logprint("exclude_7m={0}".format(exclude_7m), origin='almaimf_contimg_both')

# load the list of continuum MSes from a file
# (this file has one continuum MS full path, e.g. /path/to/file.ms, per line)
with open('continuum_mses.txt', 'r') as fh:
    continuum_mses = [x.strip() for x in fh.readlines()]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    field = basename.split("_")[0]

    if exclude_7m:
        msmd.open(continuum_ms)
Example #7
0
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
    else:
        exclude_7m = False

if 'only_7m' not in locals():
    if os.getenv('ONLY_7M') is not None:
        only_7m = bool(os.getenv('ONLY_7M').lower() == 'true')
    else:
        only_7m = False

if 'selfcal_field_id' not in locals():
    if os.getenv('SELFCAL_FIELD_ID') is not None:
        selfcal_field_id = list(
            map(int,
                os.getenv('SELFCAL_FIELD_ID').split(",")))
        logprint("Using selfcal_field_id = {0}".format(selfcal_field_id),
                 origin='contim_selfcal')
    else:
        selfcal_field_id = None
elif selfcal_field_id is not None:
    if not isinstance(selfcal_field_id, list):
        selfcal_field_id = [selfcal_field_id]
    for entry in selfcal_field_id:
        assert isinstance(entry, int), "{0} is not an int".format(entry)
    logprint("Using selfcal_field_id = {0}".format(selfcal_field_id),
             origin='contim_selfcal')


def sethistory(prefix, selfcalpars=None, impars=None, selfcaliter=None):
    for suffix in ('.image.tt0', '.image.tt0.pbcor', '.residual.tt0'):
        if os.path.exists(prefix + suffix):
            ia.open(prefix + suffix)
Example #8
0
def make_custom_mask(fieldname,
                     imname,
                     almaimf_code_path,
                     band_id,
                     rootdir="",
                     suffix="",
                     do_bsens=False):

    regfn = os.path.join(
        almaimf_code_path,
        'clean_regions/{0}_{1}{2}.reg'.format(fieldname, band_id, suffix))
    if do_bsens:
        regfnbsens = os.path.join(
            almaimf_code_path, 'clean_regions/{0}_{1}{2}_bsens.reg'.format(
                fieldname, band_id, suffix))
        if os.path.exists(regfnbsens):
            logprint('Using BSENS region: {0}'.format(regfn))
            regfn = regfnbsens
            suffix = suffix + '_bsens'
    if not os.path.exists(regfn):
        raise IOError("Region file {0} does not exist".format(regfn))

    regs = regions.read_ds9(regfn)

    logprint("Using region file {0} to create mask from image "
             "{1}".format(regfn, imname),
             origin='make_custom_mask')

    cube = SpectralCube.read(imname, format='casa_image')
    image = cube[0]
    if image.unit.is_equivalent(u.Jy / u.beam):
        image = image * u.beam
    else:
        assert image.unit.is_equivalent(u.Jy), "Image must be in Jansky/beam."

    mask_array = np.zeros(image.shape, dtype='bool')

    for reg in regs:

        threshold = u.Quantity(reg.meta['label'])
        assert threshold.unit.is_equivalent(
            u.Jy), "Threshold must by in mJy or Jy"

        preg = reg.to_pixel(image.wcs)
        msk = preg.to_mask()
        assert hasattr(image,
                       'unit'), "Image {imname} failed to have units".format(
                           imname=imname)
        mimg = msk.multiply(image)
        assert mimg is not None
        assert hasattr(
            mimg, 'unit'), "Masked Image {imname} failed to have units".format(
                imname=imname)
        assert mimg.unit.is_equivalent(u.Jy)

        msk.cutout(mask_array)[:] = (mimg > threshold) | msk.cutout(mask_array)
        # cutout does some extra check-for-overlap work
        #mask_array[msk.bbox.slices] = (msk.multiply(image) > threshold) | mask_array[msk.bbox.slices]

    # CASA transposes arrays!!!!!
    mask_array = mask_array.T

    ia.open(imname)
    assert np.all(ia.shape() == mask_array[:, :, None, None].shape
                  ), "Failure: image shape doesn't match mask shape"
    cs = ia.coordsys()
    ia.close()

    maskname = ('{fieldname}_{band_id}{suffix}_mask.mask'.format(
        fieldname=fieldname, band_id=band_id, suffix=suffix))
    # add a root directory if there is one
    # (if rootdir == "", this just returns maskname)
    maskname = os.path.join(rootdir, maskname)

    assert ia.fromarray(outfile=maskname,
                        pixels=mask_array.astype('float')[:, :, None, None],
                        csys=cs.torecord(),
                        overwrite=True), "FAILURE in final mask creation step"
    ia.close()

    return maskname
Example #9
0
            # load in the line parameter info
            if line_name not in ('full', ) + spwnames:
                linpars = line_parameters[field][line_name]
                restfreq = u.Quantity(linpars['restfreq'])
                vlsr = u.Quantity(linpars['vlsr'])

                # check that the line is in range
                ms.open(vis[0])
                # assume spw is 0 because we're working on split data
                freqs = ms.cvelfreqs(spwids=0, outframe='LSRK') * u.Hz
                targetfreq = restfreq * (1 - vlsr / constants.c)
                if freqs.min() > targetfreq or freqs.max() < targetfreq:
                    # Skip this spw: it is not in range
                    logprint(
                        "Skipped spectral window {0} for line {1} because it's out of range"
                        .format(spw, line_name),
                        origin='almaimf_line_imaging')
                    continue
                else:
                    logprint("Matched spectral window {0} to line {1}".format(
                        spw, line_name),
                             origin='almaimf_line_imaging')
            elif line_name in spwnames and line_name.lstrip("spw") != spw:
                logprint(
                    "Skipped spectral window {0} because it's not {1}".format(
                        spw, line_name),
                    origin='almaimf_line_imaging')
                continue

            if exclude_7m:
                # don't use variable name 'ms' in python2.7!
Example #10
0
if 'exclude_7m' not in locals():
    if os.getenv('EXCLUDE_7M') is not None:
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
    else:
        exclude_7m = False

# load the list of line MSes from a file
# these are all of the individually split windows,.....
with open('to_image.json', 'r') as fh:
    to_image = json.load(fh)

for band in to_image:

    logprint("Imaging fields {0} in band {1}".format(to_image[band].keys(),
                                                     band),
             origin='almaimf_fullcont_imaging')

    for field in to_image[band]:
        # get all spectral windows for a given band (B3 or B6) for a specified
        # field.  These were split to allow for individual line imaging.
        vis = list(
            map(str, [
                x for spw in to_image[band][field]
                for x in to_image[band][field][spw]
            ]))

        logprint("Will image MSes: \n{0}".format("\n".join(vis)),
                 origin='almaimf_fullcont_imaging')

        # strip off .split
Example #11
0
    return impars

if exclude_7m:
    arrayname = '12M'
elif only_7m:
    arrayname = '7M'
else:
    arrayname = '7M12M'


# global default: only do robust 0 for lines
robust = 0

logprint("Initializing line imaging with global parameters"
         " exclude_7m={0}, only_7m={1}, band_list={2}, field_id={3}"
         .format(exclude_7m, only_7m, band_list, field_id),
         origin='almaimf_line_imaging')

for band in band_list:
    for field in to_image[band]:
        spwnames = tuple('spw{0}'.format(x) for x in to_image[band][field])
        logprint("Found spectral windows {0} in band {1}: field {2}"
                 .format(to_image[band][field].keys(), band, field),
                 origin='almaimf_line_imaging')
        if max(int(x) for x in to_image[band][field]) > 7:
            raise ValueError("Found invalid spw numbers; ALMA-IMF data do not include >8 spws")
        for spw in to_image[band][field]:

            # python 2.7 specific hack: force 'field' to be a bytestring
            # instead of a unicode string (CASA's lower-level functions
            # can't accept unicode strings)
Example #12
0
imaging_root = "imaging_results"
if not os.path.exists(imaging_root):
    os.mkdir(imaging_root)

# set the 'chanchunks' parameter globally.
# CASAguides recommend chanchunks=-1, but this resulted in: 2018-09-05 23:16:34     SEVERE  tclean::task_tclean::   Exception from task_tclean : Invalid Gridding/FTM Parameter set : Must have at least 1 chanchunk
chanchunks = os.getenv('CHANCHUNKS') or 16

# global default: only do robust 0 for lines
robust = 0

vis = os.getenv('VIS')
if vis is None:
    raise ValueError("VIS not specified")

logprint("Running on vis {0}".format(vis))

ms.open(vis)
spwinfo = ms.getspectralwindowinfo()
spw_list = spwinfo.keys()
logprint("SPWs are {0}".format(spw_list))

field = 'BrickMaser'

for spw in spw_list:
    if spwinfo[spw]['ChanWidth'] < 0:
        reference_frequency = spwinfo[spw][
            'RefFreq'] - spwinfo[spw]['ChanWidth'] * spwinfo[spw]['NumChan']
    else:
        reference_frequency = spwinfo[spw]['RefFreq']
    freqname = int(reference_frequency / 1e9)
if not os.path.exists(imaging_root):
    os.mkdir(imaging_root)

if 'exclude_7m' not in locals():
    if os.getenv('EXCLUDE_7M') is not None:
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
    else:
        exclude_7m = False

if 'only_7m' not in locals():
    if os.getenv('ONLY_7M') is not None:
        only_7m = bool(os.getenv('ONLY_7M').lower() == 'true')
    else:
        only_7m = False

logprint("Beginning selfcal script with exclude_7m={0} and only_7m={1}".format(exclude_7m, only_7m),
         origin='contim_selfcal')


# load the list of continuum MSes from a file
# (this file has one continuum MS full path, e.g. /path/to/file.ms, per line)
with open('continuum_mses.txt', 'r') as fh:
    continuum_mses = [x.strip() for x in fh.readlines()]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    band = 'B3' if 'B3' in basename else 'B6' if 'B6' in basename else 'ERROR'

    # allow optional cmdline args to skip one or the other band
Example #14
0
        x.replace('_continuum_merged.cal.ms', 'continuum_merged_bsens.cal.ms')
        for x in continuum_mses
    ]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    band = 'B3' if 'B3' in basename else 'B6' if 'B6' in basename else 'ERROR'

    # allow optional cmdline args to skip one or the other band
    if os.getenv('BAND_TO_IMAGE'):
        if band not in os.getenv('BAND_TO_IMAGE'):
            logprint("Skipping band {0} because it is not in {1}".format(
                band, os.getenv('BAND_TO_IMAGE')),
                     origin='almaimf_cont_imaging')
            continue
        logprint("Imaging only band {0}".format(os.getenv('BAND_TO_IMAGE')),
                 origin='almaimf_cont_imaging')

    field = basename.split("_")[0]

    if os.getenv('FIELD_ID'):
        if field not in os.getenv('FIELD_ID'):
            logprint("Skipping {0} because it is not in FIELD_ID={1}".format(
                field, os.getenv('FIELD_ID')))
            continue

    suffix = "_bsens" if "bsens" in continuum_ms else ""
Example #15
0
    line_name = os.getenv('LINE_NAME').lower()
else:
    raise ValueError("line_name was not defined")

# set the 'chanchunks' parameter globally.
# CASAguides recommend chanchunks=-1, but this resulted in: 2018-09-05 23:16:34     SEVERE  tclean::task_tclean::   Exception from task_tclean : Invalid Gridding/FTM Parameter set : Must have at least 1 chanchunk
chanchunks = int(os.getenv('CHANCHUNKS') or 16)

# global default: only do robust 0 for lines
robust = 0

for band in band_list:
    for field in to_image[band]:
        spwnames = tuple('spw{0}'.format(x) for x in to_image[band][field])
        logprint("Found spectral windows {0} in band {1}: field {2}".format(
            to_image[band][field].keys(), band, field),
                 origin='almaimf_line_imaging')
        for spw in to_image[band][field]:

            # python 2.7 specific hack: force 'field' to be a bytestring
            # instead of a unicode string (CASA's lower-level functions
            # can't accept unicode strings)
            field = str(field)
            spw = str(spw)
            band = str(band)

            vis = list(map(str, to_image[band][field][spw]))

            # load in the line parameter info
            if line_name not in ('full', ) + spwnames:
                linpars = line_parameters[field][line_name]
Example #16
0
    for field in to_image[band]:
        for spw in to_image[band][field]:

            vis = list(map(str, to_image[band][field][spw]))

            if exclude_7m:
                vis = [ms for ms in vis if not (is_7m(ms))]
                suffix = '12M'
            else:
                suffix = '7M12M'

            lineimagename = os.path.join(
                imaging_root,
                "{0}_{1}_spw{2}_{3}_lines".format(field, band, spw, suffix))

            logprint(str(vis), origin='almaimf_line_imaging')
            coosys, racen, deccen = determine_phasecenter(ms=vis, field=field)
            phasecenter = "{0} {1}deg {2}deg".format(coosys, racen, deccen)
            (dra, ddec, pixscale) = list(
                determine_imsize(
                    ms=vis[0],
                    field=field,
                    phasecenter=(racen, deccen),
                    spw=0,
                    pixfraction_of_fwhm=1 / 3.,
                    exclude_7m=exclude_7m,
                    min_pixscale=0.1,  # arcsec
                ))
            imsize = [dra, ddec]
            cellsize = ['{0:0.2f}arcsec'.format(pixscale)] * 2
Example #17
0
def create_clean_model(
    cubeimagename,
    contimagename,
    imaging_results_path,
    contmodel_path=None,
):
    #results_path = "./imaging_results/"  # imaging_results frmo the pipeline
    #contmodel_path = "./imaging_results_test_casatools/"  #Path with input and temporary continuum models
    if contmodel_path is None:
        contmodel_path = imaging_results_path

    # Create continuum_model.image.tt0 and .tt1 regridded to the cube spatial frame, but still with 1 spectral pix
    tt0name = "{contmodelpath}/{contimagename}.model.tt0".format(
        contimagename=contimagename, contmodelpath=contmodel_path)
    tt1name = "{contmodelpath}/{contimagename}.model.tt1".format(
        contimagename=contimagename, contmodelpath=contmodel_path)
    if not os.path.exists(tt0name):
        raise IOError(
            "Continuum startmodel file {0} does not exist".format(tt0name))

    temp_dict_cont_tt0 = imregrid(imagename=tt0name, template="get")
    # not needed temp_dict_cont_tt1 = imregrid(imagename=tt1name, template="get")

    # image has to exist, model should not exist!
    # (if you ran tclean with niter=0, no model is created)
    cubeinimagepath = ("{results_path}/{cubeimagename}.image".format(
        results_path=imaging_results_path, cubeimagename=cubeimagename))
    cubeoutmodelpath = ("{results_path}/{cubeimagename}.contcube.model".format(
        results_path=imaging_results_path, cubeimagename=cubeimagename))

    if not os.path.exists(cubeinimagepath):
        raise IOError("Continuum startmodel file {0} does not exist".format(
            cubeinimagepath))

    temp_dict_line = imregrid(imagename=cubeinimagepath, template="get")
    temp_dict_line['shap'][-1] = 1
    temp_dict_line['csys']['spectral2'] = temp_dict_cont_tt0['csys'][
        'spectral2']
    temp_dict_line['csys']['worldreplace2'] = temp_dict_cont_tt0['csys'][
        'worldreplace2']

    tt0model = (
        "{contmodel_path}/{cubeimagename}_continuum_model.image.tt0".format(
            contmodel_path=contmodel_path, cubeimagename=cubeimagename))
    tt1model = (
        "{contmodel_path}/{cubeimagename}_continuum_model.image.tt1".format(
            contmodel_path=contmodel_path, cubeimagename=cubeimagename))

    imregrid(imagename=tt0name,
             output=tt0model,
             template=temp_dict_line,
             overwrite=True)
    imregrid(imagename=tt1name,
             output=tt1model,
             template=temp_dict_line,
             overwrite=True)

    # Use CASA tools to create a model cube from the continuum model
    if os.path.exists(cubeoutmodelpath):
        shutil.rmtree(cubeoutmodelpath)
    shutil.copytree(cubeinimagepath, cubeoutmodelpath)

    dict_line = imregrid(imagename=cubeoutmodelpath, template="get")
    line_im = ia.newimagefromfile(cubeoutmodelpath)
    #print(line_im.shape())

    tt0_im = ia.newimagefromfile(tt0model)
    #print(tt0_im.shape())
    tt0_pixvalues = tt0_im.getchunk()
    tt0_im.close()

    tt1_im = ia.newimagefromfile(tt1model)
    #print(tt1_im.shape())
    tt1_pixvalues = tt1_im.getchunk()
    tt1_im.close()

    # From Eq. 2 of Rau & Cornwell (2011)
    # temp_dict_cont_tt0['csys']['spectral2']['wcs']
    # dict_line['csys']['spectral2']['wcs']
    # dnu_plane: dnu with respect to cube reference freq.
    # dnu: dnu with respect to tt0 continuum reference
    for plane in range(dict_line['shap'][-1]):
        logprint('Calculating continuum model for plane {}'.format(plane))
        dnu_plane = (plane - dict_line['csys']['spectral2']['wcs']['crpix']
                     ) * dict_line['csys']['spectral2']['wcs']['cdelt']
        nu_plane = dict_line['csys']['spectral2']['wcs']['crval'] + dnu_plane
        #print(dnu_plane, nu_plane)
        factor = (nu_plane -
                  temp_dict_cont_tt0['csys']['spectral2']['wcs']['crval']
                  ) / temp_dict_cont_tt0['csys']['spectral2']['wcs']['crval']
        #print(factor)
        plane_pixvalues = tt0_pixvalues + factor * tt1_pixvalues
        blc = [0, 0, 0, plane]
        #trc = [line_im.shape()[0]-1, line_im.shape()[1]-1, 0, plane]
        line_im.putchunk(plane_pixvalues, blc=blc, replicate=False)
    line_im.close()

    return cubeoutmodelpath

    # Commented block is for the case where only tt0 info is used
    '''
Example #18
0

            if exclude_7m:
                vis = [ms for ms in vis if not(is_7m(ms))]
                arrayname = '12M'
            else:
                arrayname = '7M12M'

            lineimagename = os.path.join(imaging_root,
                                         "{0}_{1}_spw{2}_{3}_lines".format(field,
                                                                           band,
                                                                           spw,
                                                                           arrayname))


            logprint(str(vis), origin='almaimf_line_imaging')
            coosys,racen,deccen = determine_phasecenter(ms=vis, field=field)
            phasecenter = "{0} {1}deg {2}deg".format(coosys, racen, deccen)
            (dra,ddec,pixscale) = list(determine_imsizes(mses=vis, field=field,
                                                         phasecenter=(racen,deccen),
                                                         spw=0, pixfraction_of_fwhm=1/3.,
                                                         exclude_7m=exclude_7m,
                                                         min_pixscale=0.1, # arcsec
                                                        ))
            imsize = [int(dra), int(ddec)]
            cellsize = ['{0:0.2f}arcsec'.format(pixscale)] * 2

            dirty_tclean_made_residual = False

            # start with cube imaging
Example #19
0
            # split out the 12M-only data to make further processing slightly
            # faster
            new_continuum_ms = continuum_ms.replace(".cal.ms", "_12M.cal.ms")
            split(vis=continuum_ms, outputvis=new_continuum_ms, antenna=antennae,
                  field=field, datacolumn='data')
            continuum_ms = new_continuum_ms
    else:
        antennae = ""
        arrayname = '7M12M'

    if os.getenv("USE_SELFCAL_MS"):
        selfcal_ms = basename+"_"+arrayname+"_selfcal.ms"
        continuum_ms = selfcal_ms

    logprint("Imaging MS {0} with array {1}".format(continuum_ms, arrayname),
             origin='almaimf_cont_imaging')

    coosys,racen,deccen = determine_phasecenter(ms=continuum_ms, field=field)
    phasecenter = "{0} {1}deg {2}deg".format(coosys, racen, deccen)
    (dra,ddec,pixscale) = list(determine_imsize(ms=continuum_ms, field=field,
                                                phasecenter=(racen,deccen),
                                                exclude_7m=exclude_7m,
                                                spw=0, pixfraction_of_fwhm=1/4.))
    imsize = [dra, ddec]
    cellsize = ['{0:0.2f}arcsec'.format(pixscale)] * 2

    contimagename = os.path.join(imaging_root, basename) + "_" + arrayname

    # SKIP the plot
    #if not os.path.exists(contimagename+".uvwave_vs_amp.png"):
    #    # make a diagnostic plot to show the UV distribution
Example #20
0
import json
from metadata_tools import determine_imsize, determine_phasecenter, logprint
from tasks import tclean, exportfits, plotms, imstat, makemask
from taskinit import msmdtool
from taskinit import iatool
msmd = msmdtool()
ia = iatool()

imaging_root = "imaging_results"
if not os.path.exists(imaging_root):
    os.mkdir(imaging_root)

if 'exclude_7m' not in locals():
    exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')

logprint("exclude_7m={0}".format(exclude_7m),
         origin='almaimf_cont_3sigtemplate')

# load the list of continuum MSes from a file
# (this file has one continuum MS full path, e.g. /path/to/file.ms, per line)
with open('continuum_mses.txt', 'r') as fh:
    continuum_mses = [x.strip() for x in fh.readlines()]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    field = basename.split("_")[0]

    if exclude_7m:
        msmd.open(continuum_ms)
Example #21
0
    os.mkdir(imaging_root)

if 'exclude_7m' not in locals():
    if os.getenv('EXCLUDE_7M') is not None:
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
    else:
        exclude_7m = False

if 'only_7m' not in locals():
    if os.getenv('ONLY_7M') is not None:
        only_7m = bool(os.getenv('ONLY_7M').lower() == 'true')
    else:
        only_7m = False

logprint("Beginning selfcal script with exclude_7m={0} and only_7m={1}".format(
    exclude_7m, only_7m),
         origin='contim_selfcal')

# load the list of continuum MSes from a file
# (this file has one continuum MS full path, e.g. /path/to/file.ms, per line)
with open('continuum_mses.txt', 'r') as fh:
    continuum_mses = [x.strip() for x in fh.readlines()]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    band = 'B3' if 'B3' in basename else 'B6' if 'B6' in basename else 'ERROR'

    # allow optional cmdline args to skip one or the other band
Example #22
0
msmd = msmdtool()
ia = iatool()

imaging_root = "imaging_results"
if not os.path.exists(imaging_root):
    os.mkdir(imaging_root)

if 'exclude_7m' not in locals():
    if os.getenv('EXCLUDE_7M') is not None:
        exclude_7m = bool(os.getenv('EXCLUDE_7M').lower() == 'true')
        arrayname = '12M'
    else:
        exclude_7m = False
        arrayname = '7M12M'

logprint("Beginning selfcal script with exclude_7m={0}".format(exclude_7m),
         origin='contim_selfcal')

# load the list of continuum MSes from a file
# (this file has one continuum MS full path, e.g. /path/to/file.ms, per line)
with open('continuum_mses.txt', 'r') as fh:
    continuum_mses = [x.strip() for x in fh.readlines()]

for continuum_ms in continuum_mses:

    # strip off .cal.ms
    basename = os.path.split(continuum_ms[:-7])[1]

    band = 'B3' if 'B3' in basename else 'B6' if 'B6' in basename else 'ERROR'

    # allow optional cmdline args to skip one or the other band
    if os.getenv('BAND_TO_IMAGE'):