예제 #1
0
def mstransform(script, vis_path, out_path, other_transform_args=None,
                overwrite=False):
    """
    Useful for pre-imaging steps of interferometric data reduction.

    Guide:
    http://www.eso.org/~scastro/ALMA/casa/MST/MSTransformDocs/MSTransformDocs.html

    Returns:
        out_path
    """
    vis_path = byteify(vis_path)
    out_path = byteify(out_path)

    if other_transform_args is None:
        other_transform_args = {}
    transform_args = other_transform_args.copy()

    transform_args.update({
        'vis': os.path.abspath(vis_path),
        'outputvis': os.path.abspath(out_path)
    })
    script.append("mstransform(**{})".format(repr(transform_args)))

    if overwrite:
        if os.path.isdir(out_path):
            shutil.rmtree(out_path)

    return out_path
예제 #2
0
def concat(script, vis_paths, out_basename=None, out_dir=None, out_path=None,
           overwrite=False):
    """
    Concatenates multiple visibilities into one.

    By default, output basename is derived by concatenating
    the basenames of the input visibilities, with the prefix `concat_`.
    However, this can result in something very long and unwieldy. Alternatively
    you may specify the exact out_path, or just the out_basename.

    Returns:
        Path to concatenated ms.
    """
    vis_paths = byteify(vis_paths)
    concat_path = byteify(out_path)
    if concat_path is None:
        if out_basename is None:
            concat_path = derive_out_path(vis_paths, out_dir,
                                          out_extension='.ms',
                                          out_prefix='concat_')
        else:
            concnames = out_basename + '.ms'
            concat_path = os.path.join(out_dir, concnames)
    ensure_dir(os.path.dirname(concat_path))
    if overwrite:
        if os.path.isdir(concat_path):
            shutil.rmtree(concat_path)
    abs_vis_paths = [os.path.abspath(v) for v in vis_paths]
    script.append("concat(vis={0}, concatvis='{1}')".format(
        str(abs_vis_paths), os.path.abspath(concat_path))
    )
    return concat_path
예제 #3
0
def concat(script,
           vis_paths,
           out_basename=None,
           out_dir=None,
           out_path=None,
           overwrite=False):
    """
    Concatenates multiple visibilities into one.

    By default, output basename is derived by concatenating
    the basenames of the input visibilities, with the prefix `concat_`.
    However, this can result in something very long and unwieldy. Alternatively
    you may specify the exact out_path, or just the out_basename.

    **Returns:** Path to concatenated ms.
    """
    vis_paths = byteify(vis_paths)
    concat_path = byteify(out_path)
    if concat_path is None:
        if out_basename is None:
            basenames = [
                os.path.splitext(os.path.basename(vp))[0] for vp in vis_paths
            ]
            concnames = 'concat_' + '_'.join(basenames) + '.ms'
        else:
            concnames = out_basename + '.ms'
        concat_path = os.path.join(out_dir, concnames)
    ensure_dir(os.path.dirname(concat_path))
    if overwrite:
        if os.path.isdir(concat_path):
            shutil.rmtree(concat_path)
    abs_vis_paths = [os.path.abspath(v) for v in vis_paths]
    script.append("concat(vis={0}, concatvis='{1}')".format(
        str(abs_vis_paths), os.path.abspath(concat_path)))
    return concat_path
예제 #4
0
def mstransform(script,
                vis_path,
                out_path,
                other_transform_args=None,
                overwrite=False):
    """
    Useful for pre-imaging steps of interferometric data reduction.

    Guide: http://www.eso.org/~scastro/ALMA/casa/MST/MSTransformDocs/MSTransformDocs.html
    """
    vis_path = byteify(vis_path)
    out_path = byteify(out_path)

    if other_transform_args is None:
        other_transform_args = {}
    transform_args = other_transform_args.copy()

    transform_args.update({
        'vis': os.path.abspath(vis_path),
        'outputvis': os.path.abspath(out_path)
    })
    script.append("mstransform(**{})".format(repr(transform_args)))

    if overwrite:
        if os.path.isdir(out_path):
            shutil.rmtree(out_path)
def feather(script, output_basename, highres, lowres, weighting):
    output_name = byteify('{}.feather'.format(output_basename))
    feather_args = {'imagename': output_name,
                    'highres': highres,
                    'lowres': lowres,
                    'sdfactor': weighting}

    script.append('feather(**{})'.format(repr(feather_args)))

    return output_name
def immoments(script, imagename, output_basename, moment, axis='spectral'):
    """Makes a moment of degree moment from imagename.
    """

    output_name = byteify('{}.moment{}'.format(output_basename, moment))
    immoments_args = {'imagename': imagename,
                      'moments': [moment],
                      'axis': axis,
                      'outfile': os.path.abspath(output_name)}

    script.append('immoments(**{})'.format(repr(immoments_args)))

    return output_name
def feather(script, output_basename, highres, lowres, weighting):
    """
    Generate code to feather two measurement sets.
    Returns: path to output (feathered) MS.
    """
    output_name = byteify('{}.feather'.format(output_basename))
    feather_args = {'imagename': os.path.abspath(output_name),
                    'highres': highres,
                    'lowres': lowres,
                    'sdfactor': weighting}

    script.append('feather(**{})'.format(repr(feather_args)))

    return output_name
예제 #8
0
def clean(script,
          vis_paths,
          niter,
          threshold_in_jy,
          mask='',
          modelimage='',
          other_clean_args=None,
          out_dir=None,
          out_path=None,
          overwrite=False
          ):
    """
    Perform clean process to produce an image/map.

    If out_path is None, then the output basename is derived by
    appending a `.clean` or `.dirty` suffix to the input basename. The various
    outputs are then further suffixed by casa, e.g.
    `foo.clean.image`, `foo.clean.psf`, etc. Since multiple outputs are
    generated, this function returns a dictionary detailing the expected paths.

    NB Attempting to run with pre-existing outputs and ``overwrite=False``
    *will not* throw an error, in contrast to most other routines.
    From the CASA cookbook, w.r.t. the outputs:

        "If an image with that name already exists, it will in general be
        overwritten. Beware using names of existing images however. If the clean
        is run using an imagename where <imagename>.residual and
        <imagename>.model already exist then clean will continue starting from
        these (effectively restarting from the end of the previous clean).
        Thus, if multiple runs of clean are run consecutively with the same
        imagename, then the cleaning is incremental (as in the difmap package)."

    You can override this behaviour by specifying ``overwrite=True``, in which
    case all pre-existing outputs will be deleted.

    NB niter = 0 implies create a  'dirty' map, outputs will be named
    accordingly.

    .. warning::

        This function can accept a list of multiple input visibilities. This
        functionality is not extensively tested and should be considered
        experimental - the CASA cookbook is vague on how parameters should be
        passed in this use-case.


    Returns:
        expected_map_paths(:py:class:`.CleanMaps`): namedtuple,
            listing paths for resulting maps.
    """
    vis_paths = byteify(vis_paths)
    vis_paths = listify(vis_paths)
    vis_paths = [os.path.abspath(vp) for vp in vis_paths]
    out_path = byteify(out_path)

    if other_clean_args is None:
        other_clean_args = {}
    clean_args = other_clean_args.copy()

    cleaned_path = out_path
    if cleaned_path is None:
        if niter == 0:
            out_ext = '.dirty'
        else:
            out_ext = '.clean'
        cleaned_path = derive_out_path(vis_paths,
                                       out_dir,
                                       out_extension=out_ext)

    clean_args.update({
        'vis': vis_paths,
        'imagename': os.path.abspath(cleaned_path),
        'niter': niter,
        'threshold': str(threshold_in_jy) + 'Jy',
        'mask': mask,
        'modelimage': modelimage
    })
    script.append("clean(**{})".format(repr(clean_args)))

    ensure_dir(os.path.dirname(cleaned_path))
    expected_map_paths = CleanMaps(
        image=cleaned_path + '.image',
        model=cleaned_path + '.model',
        residual=cleaned_path + '.residual',
        psf=cleaned_path + '.psf',
        mask=cleaned_path + '.mask',
        flux=cleaned_path + '.flux',
    )

    if overwrite:
        for path in expected_map_paths:
            if os.path.isdir(path):
                shutil.rmtree(path)
    return expected_map_paths
예제 #9
0
def clean(script,
          vis_path,
          niter,
          threshold_in_jy,
          mask='',
          modelimage='',
          other_clean_args=None,
          out_dir=None,
          out_path=None,
          overwrite=False):
    """
    Perform clean process to produce an image/map.

    If out_path is None, then the output basename is derived by
    appending a `.clean` or `.dirty` suffix to the input basename. The various
    outputs are then further suffixed by casa, e.g.
    `foo.clean.image`, `foo.clean.psf`, etc. Since multiple outputs are
    generated, this function returns a dictionary detailing the expected paths.

    NB Attempting to run with pre-existing outputs and ``overwrite=False``
    *will not* throw an error, in contrast to most other routines.
    From the CASA cookbook, w.r.t. the outputs:

        "If an image with that name already exists, it will in general be
        overwritten. Beware using names of existing images however. If the clean
        is run using an imagename where <imagename>.residual and
        <imagename>.model already exist then clean will continue starting from
        these (effectively restarting from the end of the previous clean).
        Thus, if multiple runs of clean are run consecutively with the same
        imagename, then the cleaning is incremental (as in the difmap package)."

    You can override this behaviour by specifying ``overwrite=True``, in which
    case all pre-existing outputs will be deleted.

    NB niter = 0 implies create a  'dirty' map, outputs will be named
    accordingly.

    **Returns**:
    :py:class:`.CleanMaps` namedtuple, listing paths for resulting maps.
    """
    vis_path = byteify(vis_path)
    out_path = byteify(out_path)

    if other_clean_args is None:
        other_clean_args = {}
    clean_args = other_clean_args.copy()

    cleaned_path = out_path
    if cleaned_path is None:
        if niter == 0:
            out_ext = '.dirty'
        else:
            out_ext = '.clean'
        cleaned_path = derive_out_path(
            vis_path[0] if type(vis_path) is list else vis_path,
            out_dir,
            out_extension=out_ext)

    clean_args.update({
        'vis':
        vis_path if type(vis_path) is list else os.path.abspath(vis_path),
        'imagename':
        os.path.abspath(cleaned_path),
        'niter':
        niter,
        'threshold':
        str(threshold_in_jy) + 'Jy',
        'mask':
        mask,
        'modelimage':
        modelimage
    })
    script.append("clean(**{})".format(repr(clean_args)))

    ensure_dir(os.path.dirname(cleaned_path))
    expected_map_paths = CleanMaps(
        image=cleaned_path + '.image',
        model=cleaned_path + '.model',
        residual=cleaned_path + '.residual',
        psf=cleaned_path + '.psf',
        mask=cleaned_path + '.mask',
        flux=cleaned_path + '.flux',
    )

    if overwrite:
        for path in expected_map_paths:
            if os.path.isdir(path):
                shutil.rmtree(path)
    return expected_map_paths