Open in Colab: https://colab.research.google.com/github/casangi/examples/blob/master/community/casa6_1_demo.ipynb


CASA 6.2 Notebook Demo

Original Author: rraba@nrao.edu

Description

This notebook shows how to install the modular CASA 6 tools and tasks with some basic orientation:

  • locate the casadata folder

  • list the available tasks

  • find and print the log file

  • run a simple tclean command

  • view the output images with Astropy

  • view the output images with CARTA

Installation

[1]:
import os

print("installing pre-requisite packages...")
os.system("apt-get install libgfortran3")

print("installing casatools...")
os.system("pip install --index-url https://casa-pip.nrao.edu:443/repository/pypi-group/simple casatasks==6.2.0.106")
os.system("pip install --index-url https://casa-pip.nrao.edu:443/repository/pypi-group/simple casadata")

print("downloading MeasurementSet from CASAguide First Look at Imaging...\n")
os.system("wget https://bulk.cv.nrao.edu/almadata/public/working/sis14_twhya_calibrated_flagged.ms.tar")
os.system("tar -xvf sis14_twhya_calibrated_flagged.ms.tar")

print("make a config file for Google Colab...\n")
os.system("mkdir ~/.casa")
!echo "home     = '/content/'" > ~/.casa/config.py
!echo "datapath = ['`find / -type d -name casadata`']" >> ~/.casa/config.py
!more ~/.casa/config.py
installing pre-requisite packages...
installing casatools...
downloading MeasurementSet from CASAguide First Look at Imaging...

make a config file for Google Colab...

home     = '/content/'
datapath = ['/usr/local/lib/python3.7/dist-packages/casadata']

Getting Started

[2]:
import casatasks
dir(casatasks)
[2]:
['__all__',
 '__builtins__',
 '__cached__',
 '__cwd',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__os',
 '__package__',
 '__path__',
 '__spec__',
 '_clith_spec',
 '_logsink',
 '_pipe_spec',
 '_time',
 'absolute_import',
 'accor',
 'applycal',
 'asdmsummary',
 'bandpass',
 'blcal',
 'calstat',
 'casalog',
 'clearcal',
 'clearstat',
 'concat',
 'config',
 'conjugatevis',
 'cvel',
 'cvel2',
 'delmod',
 'exportasdm',
 'exportfits',
 'exportuvfits',
 'feather',
 'fixplanets',
 'fixvis',
 'flagcmd',
 'flagdata',
 'flagmanager',
 'fluxscale',
 'fringefit',
 'ft',
 'gaincal',
 'gencal',
 'hanningsmooth',
 'imcollapse',
 'imcontsub',
 'imdev',
 'imfit',
 'imhead',
 'imhistory',
 'immath',
 'immoments',
 'impbcor',
 'importasap',
 'importasdm',
 'importatca',
 'importfits',
 'importfitsidi',
 'importgmrt',
 'importlib',
 'importmiriad',
 'importnro',
 'importuvfits',
 'importvla',
 'impv',
 'imrebin',
 'imreframe',
 'imregrid',
 'imsmooth',
 'imstat',
 'imsubimage',
 'imtrans',
 'imval',
 'initweights',
 'listcal',
 'listfits',
 'listhistory',
 'listobs',
 'listpartition',
 'listsdm',
 'listvis',
 'makemask',
 'mpi_env_found',
 'mpi_run_client',
 'mstransform',
 'nompi_or_serial_or_client',
 'nrobeamaverage',
 'os',
 'package_variant',
 'partition',
 'platform',
 'plotants',
 'plotbandpass',
 'plotprofilemap',
 'plotweather',
 'polcal',
 'polfromgain',
 'predictcomp',
 'private',
 'rerefant',
 'rmfit',
 'rmtables',
 'sdatmcor',
 'sdbaseline',
 'sdcal',
 'sdfit',
 'sdfixscan',
 'sdgaincal',
 'sdimaging',
 'sdintimaging',
 'sdpolaverage',
 'sdsidebandsplit',
 'sdsmooth',
 'sdtimeaverage',
 'serial_run',
 'setjy',
 'simalma',
 'simanalyze',
 'simobserve',
 'slsearch',
 'smoothcal',
 'specfit',
 'specflux',
 'specsmooth',
 'splattotable',
 'split',
 'spxfit',
 'statwt',
 'tclean',
 'telemetry_available',
 'telemetry_starttime',
 'tsdimaging',
 'uvcontsub',
 'uvmodelfit',
 'uvsub',
 'version',
 'version_string',
 'virtualconcat',
 'vishead',
 'visstat',
 'widebandpbcor',
 'xml_interface_defs']
[3]:
from casatasks import listobs

rc = listobs(vis='sis14_twhya_calibrated_flagged.ms')

print(os.popen("cat casa-*.log").read())
2021-05-12 17:24:05     INFO    ::casa
2021-05-12 17:24:06     INFO    ::casa  CASA Version MODULAR 6.2.0.106
2021-05-12 17:24:06     INFO    ::casa  Can't import casatelemetry module.
2021-05-12 17:24:06     INFO    listobs::::casa ##########################################
2021-05-12 17:24:06     INFO    listobs::::casa ##### Begin Task: listobs            #####
2021-05-12 17:24:06     INFO    listobs::::casa listobs( vis='sis14_twhya_calibrated_flagged.ms', selectdata=True, spw='', field='', antenna='', uvrange='', timerange='', correlation='', scan='', intent='', feed='', array='', observation='', verbose=True, listfile='', listunfl=False, cachesize=50.0, overwrite=False )
2021-05-12 17:24:06     INFO    listobs::ms::summary    ================================================================================
2021-05-12 17:24:06     INFO    listobs::ms::summary+              MeasurementSet Name:  /content/sis14_twhya_calibrated_flagged.ms      MS Version 2
2021-05-12 17:24:06     INFO    listobs::ms::summary+   ================================================================================
2021-05-12 17:24:06     INFO    listobs::ms::summary+      Observer: cqi     Project: uid://A002/X327408/X6f
2021-05-12 17:24:06     INFO    listobs::ms::summary+   Observation: ALMA
2021-05-12 17:24:06     INFO    listobs::MSMetaData::_computeScanAndSubScanProperties   Computing scan and subscan properties...
2021-05-12 17:24:06     INFO    listobs::ms::summary    Data records: 80563       Total elapsed time = 5647.68 seconds
2021-05-12 17:24:06     INFO    listobs::ms::summary+      Observed from   19-Nov-2012/07:36:57.0   to   19-Nov-2012/09:11:04.7 (UTC)
2021-05-12 17:24:06     INFO    listobs::ms::summary
2021-05-12 17:24:06     INFO    listobs::ms::summary+      ObservationID = 0         ArrayID = 0
2021-05-12 17:24:06     INFO    listobs::ms::summary+     Date        Timerange (UTC)          Scan  FldId FieldName             nRows     SpwIds   Average Interval(s)    ScanIntent
2021-05-12 17:24:06     INFO    listobs::ms::summary+     19-Nov-2012/07:36:57.0 - 07:39:13.1     4      0 J0522-364                 4200  [0]  [6.05] [CALIBRATE_BANDPASS#ON_SOURCE,CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 07:44:45.2 - 07:47:01.2     7      2 Ceres                     3800  [0]  [6.05] [CALIBRATE_AMPLI#ON_SOURCE,CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 07:52:42.0 - 07:53:47.6    10      3 J1037-295                 1900  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 07:56:23.5 - 08:02:11.3    12      5 TW Hya                    8514  [0]  [6.05] [OBSERVE_TARGET#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:04:36.3 - 08:05:41.9    14      3 J1037-295                 1900  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:08:09.6 - 08:13:57.3    16      5 TW Hya                   10360  [0]  [6.05] [OBSERVE_TARGET#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:16:20.6 - 08:17:26.2    18      3 J1037-295                 2100  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:19:53.9 - 08:25:41.7    20      5 TW Hya                   10321  [0]  [6.05] [OBSERVE_TARGET#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:28:17.1 - 08:29:22.6    22      3 J1037-295                 2100  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:32:00.5 - 08:37:48.2    24      5 TW Hya                   10324  [0]  [6.05] [OBSERVE_TARGET#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:40:11.9 - 08:41:17.4    26      3 J1037-295                 2100  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:43:45.6 - 08:49:33.4    28      5 TW Hya                    9462  [0]  [6.05] [OBSERVE_TARGET#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:51:57.1 - 08:53:02.6    30      3 J1037-295                 1900  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 08:58:12.0 - 09:00:28.1    33      6 3c279                     3402  [0]  [6.05] [CALIBRATE_BANDPASS#ON_SOURCE,CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 09:01:35.7 - 09:02:41.2    34      3 J1037-295                 1900  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 09:05:15.6 - 09:07:31.6    36      5 TW Hya                    4180  [0]  [6.05] [OBSERVE_TARGET#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary+                 09:09:59.1 - 09:11:04.7    38      3 J1037-295                 2100  [0]  [6.05] [CALIBRATE_PHASE#ON_SOURCE,CALIBRATE_WVR#ON_SOURCE]
2021-05-12 17:24:06     INFO    listobs::ms::summary               (nRows = Total number of rows per scan)
2021-05-12 17:24:06     INFO    listobs::ms::summary    Fields: 5
2021-05-12 17:24:06     INFO    listobs::ms::summary+     ID   Code Name                RA               Decl           Epoch   SrcId      nRows
2021-05-12 17:24:06     INFO    listobs::ms::summary+     0    none J0522-364           05:22:57.984648 -36.27.30.85128 J2000   0           4200
2021-05-12 17:24:06     INFO    listobs::ms::summary+     2    none Ceres               06:10:15.950590 +23.22.06.90668 J2000   2           3800
2021-05-12 17:24:06     INFO    listobs::ms::summary+     3    none J1037-295           10:37:16.079736 -29.34.02.81316 J2000   3          16000
2021-05-12 17:24:06     INFO    listobs::ms::summary+     5    none TW Hya              11:01:51.796000 -34.42.17.36600 J2000   4          53161
2021-05-12 17:24:06     INFO    listobs::ms::summary+     6    none 3c279               12:56:11.166576 -05.47.21.52464 J2000   5           3402
2021-05-12 17:24:06     INFO    listobs::ms::summary    Spectral Windows:  (1 unique spectral windows and 1 unique polarization setups)
2021-05-12 17:24:06     INFO    listobs::ms::summary+     SpwID  Name                           #Chans   Frame   Ch0(MHz)  ChanWid(kHz)  TotBW(kHz) CtrFreq(MHz) BBC Num  Corrs
2021-05-12 17:24:06     INFO    listobs::ms::summary+     0      ALMA_RB_07#BB_2#SW-01#FULL_RES    384   TOPO  372533.086       610.352    234375.0 372649.9688        2  XX  YY
2021-05-12 17:24:06     INFO    listobs::ms::summary    Sources: 5
2021-05-12 17:24:06     INFO    listobs::ms::summary+     ID   Name                SpwId RestFreq(MHz)  SysVel(km/s)
2021-05-12 17:24:06     INFO    listobs::ms::summary+     0    J0522-364           0     -              -
2021-05-12 17:24:06     INFO    listobs::ms::summary+     1    Ceres               0     -              -
2021-05-12 17:24:06     INFO    listobs::ms::summary+     2    J1037-295           0     -              -
2021-05-12 17:24:06     INFO    listobs::ms::summary+     3    TW Hya              0     -              -
2021-05-12 17:24:06     INFO    listobs::ms::summary+     4    3c279               0     -              -
2021-05-12 17:24:06     INFO    listobs::ms::summary    Antennas: 21:
2021-05-12 17:24:06     INFO    listobs::ms::summary+     ID   Name  Station   Diam.    Long.         Lat.                Offset from array center (m)                ITRF Geocentric coordinates (m)
2021-05-12 17:24:06     INFO    listobs::ms::summary+                                                                        East         North     Elevation               x               y               z
2021-05-12 17:24:06     INFO    listobs::ms::summary+     1    DA42  A050      12.0 m   -067.45.16.2  -22.53.29.3         43.0352     -744.9713       21.6702  2225079.880016 -5440041.377534 -2481724.598031
2021-05-12 17:24:06     INFO    listobs::ms::summary+     2    DA44  A068      12.0 m   -067.45.20.6  -22.53.25.7        -82.4232     -631.7828       23.5810  2224981.097784 -5440131.250387 -2481621.066374
2021-05-12 17:24:06     INFO    listobs::ms::summary+     3    DA45  A070      12.0 m   -067.45.11.9  -22.53.29.3        166.1833     -743.4934       19.8811  2225193.450167 -5439993.764157 -2481722.540534
2021-05-12 17:24:06     INFO    listobs::ms::summary+     4    DA46  A067      12.0 m   -067.45.12.7  -22.53.27.2        142.4097     -678.7318       20.1280  2225181.070532 -5440026.290790 -2481662.975103
2021-05-12 17:24:06     INFO    listobs::ms::summary+     5    DA48  A046      12.0 m   -067.45.17.0  -22.53.29.3         21.4267     -742.7987       21.6757  2225060.202580 -5440050.344436 -2481722.598651
2021-05-12 17:24:06     INFO    listobs::ms::summary+     6    DA49  A029      12.0 m   -067.45.18.2  -22.53.25.8        -12.9134     -636.4552       22.1350  2225044.239583 -5440102.022535 -2481624.808405
2021-05-12 17:24:06     INFO    listobs::ms::summary+     7    DA50  A045      12.0 m   -067.45.17.9  -22.53.30.1         -5.4183     -767.4398       22.6034  2225032.051652 -5440052.426015 -2481745.660003
2021-05-12 17:24:06     INFO    listobs::ms::summary+     9    DV02  A077      12.0 m   -067.45.10.1  -22.53.25.9        217.6299     -637.5333       15.8376  2225255.259272 -5440008.987869 -2481623.352052
2021-05-12 17:24:06     INFO    listobs::ms::summary+     11   DV05  A082      12.0 m   -067.45.08.3  -22.53.29.2        269.0433     -740.9521       15.7832  2225287.593766 -5439952.243679 -2481718.605314
2021-05-12 17:24:06     INFO    listobs::ms::summary+     12   DV06  A037      12.0 m   -067.45.17.5  -22.53.28.8          6.7403     -727.3003       21.2086  2225048.729287 -5440061.085777 -2481708.139136
2021-05-12 17:24:06     INFO    listobs::ms::summary+     14   DV08  A021      12.0 m   -067.45.17.2  -22.53.27.0         14.3196     -672.8108       21.3420  2225063.814715 -5440077.948261 -2481657.992572
2021-05-12 17:24:06     INFO    listobs::ms::summary+     15   DV10  A071      12.0 m   -067.45.19.9  -22.53.23.5        -60.7887     -563.2541       23.3799  2225011.141945 -5440147.560932 -2481557.855663
2021-05-12 17:24:06     INFO    listobs::ms::summary+     16   DV13  A072      12.0 m   -067.45.12.6  -22.53.24.0        147.1742     -580.5887       18.1825  2225199.254375 -5440058.161494 -2481571.803699
2021-05-12 17:24:06     INFO    listobs::ms::summary+     17   DV15  A074      12.0 m   -067.45.12.1  -22.53.32.0        161.8159     -828.6196       18.7688  2225176.483514 -5439963.820451 -2481800.529842
2021-05-12 17:24:06     INFO    listobs::ms::summary+     18   DV16  A069      12.0 m   -067.45.21.3  -22.53.30.2       -101.4797     -770.1047       23.2972  2224942.993176 -5440088.421459 -2481748.384855
2021-05-12 17:24:06     INFO    listobs::ms::summary+     19   DV17  A138      12.0 m   -067.45.17.1  -22.53.34.4         19.1461     -901.2603       26.0137  2225036.269025 -5439997.853009 -2481870.267607
2021-05-12 17:24:06     INFO    listobs::ms::summary+     20   DV18  A053      12.0 m   -067.45.17.3  -22.53.31.2         12.5939     -802.9941       21.5281  2225043.111690 -5440031.889497 -2481777.995870
2021-05-12 17:24:06     INFO    listobs::ms::summary+     21   DV19  A008      12.0 m   -067.45.15.4  -22.53.26.8         67.5592     -667.6872       20.9574  2225113.709955 -5440059.310545 -2481653.122797
2021-05-12 17:24:06     INFO    listobs::ms::summary+     22   DV20  A020      12.0 m   -067.45.17.8  -22.53.28.0         -2.9649     -703.4389       21.6629  2225043.419055 -5440073.737929 -2481686.333574
2021-05-12 17:24:06     INFO    listobs::ms::summary+     24   DV22  A011      12.0 m   -067.45.14.4  -22.53.28.4         95.9131     -716.5005       21.0898  2225132.810230 -5440031.115405 -2481698.143589
2021-05-12 17:24:06     INFO    listobs::ms::summary+     25   DV23  A007      12.0 m   -067.45.15.1  -22.53.27.3         74.0152     -681.2926       21.3231  2225117.809276 -5440052.280005 -2481665.799049
2021-05-12 17:24:06     INFO    listobs::::casa Task listobs complete. Start time: 2021-05-12 17:24:06.176639 End time: 2021-05-12 17:24:06.283321
2021-05-12 17:24:06     INFO    listobs::::casa ##### End Task: listobs              #####
2021-05-12 17:24:06     INFO    listobs::::casa ##########################################

tclean Example

Note: Only non-interactive mode is supported (interactive=False) as the CASA Viewer is not compatible.

[4]:
from casatasks import tclean

print("running tclean, may take a bit...")

tclean(vis='sis14_twhya_calibrated_flagged.ms', imagename='first_image',
       field='5', spw='', specmode='mfs', deconvolver='hogbom', nterms=1,
       gridder='standard', imsize=[250,250], cell=['0.1arcsec'],
       weighting='natural', threshold='0mJy', niter=5000,
       interactive=False, savemodel='modelcolumn')

print("complete")
running tclean, may take a bit...
complete

View Images with Astropy

[5]:
from casatools import image as IA
from astropy.wcs import WCS
import matplotlib.pyplot as plt
import numpy as np

filenames = ['first_image.image', 'first_image.mask', 'first_image.model',
             'first_image.pb', 'first_image.psf', 'first_image.residual']

ia = IA()

def buildImage(imname='',chan=0):
    ia.open(imname)
    pix = ia.getchunk()[:,:,0,chan]
    csys = ia.coordsys()
    ia.close()

    rad_to_deg =  180/np.pi
    w = WCS(naxis=2)
    w.wcs.crpix = csys.referencepixel()['numeric'][0:2]
    w.wcs.cdelt = csys.increment()['numeric'][0:2]*rad_to_deg
    w.wcs.crval = csys.referencevalue()['numeric'][0:2]*rad_to_deg
    w.wcs.ctype = ['RA---SIN', 'DEC--SIN']

    return pix, w

plt.subplots(2,3, figsize=(18,12))
for ii, name in enumerate(filenames):
    pix, w = buildImage(name)
    ax = plt.subplot(2, 3, ii+1, projection=w)
    p1 = int(pix.shape[0]*0.25)
    p2 = int(pix.shape[0]*0.75)

    im = ax.imshow(pix[p1:p2,p1:p2].transpose(), origin='lower',  cmap=plt.cm.viridis)
    plt.colorbar(im, ax=ax)
    ax.set_title(name)
    ax.set_xlabel('Right Ascension')
    ax.set_ylabel('Declination')

plt.subplots_adjust(wspace=0.5, hspace=0.2)
../../_images/examples_community_casa6_1_demo_10_0.png

View Images with CARTA

Consider this very experimental, even a bit magical

[6]:
import os
import subprocess as sp
import socket
import signal
import time
import requests

FRONT_PORT = '39138'  # make something up
BACK_PORT = '41578'
HOST_IP = socket.gethostbyname(socket.gethostname())

print("downloading ngrok...")
os.system("wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip")
os.system("unzip -o ngrok-stable-linux-amd64.zip");

print("downloading CARTA...")
os.system("wget https://github.com/CARTAvis/carta/releases/download/v1.4/CARTA-v1.4-RedHat7.tgz")
os.system("tar -xf CARTA-v1.4-RedHat7.tgz")
os.system("./CARTA.AppImage --appimage-extract")
os.system("rm squashfs-root/resources/app/carta-backend/lib/libstdc++.so.6")

#cp = sp.Popen('CARTA-v1.1-remote/carta --port='+BACK_PORT + ' --fport='+FRONT_PORT, shell=True, preexec_fn=os.setsid, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)
cp = sp.Popen('./squashfs-root/AppRun --remote --port='+BACK_PORT + ' --fport='+FRONT_PORT, shell=True, preexec_fn=os.setsid, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)
gp1 = sp.Popen('./ngrok http ' + HOST_IP+':'+FRONT_PORT, shell=True, preexec_fn=os.setsid, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)
gp2 = sp.Popen('./ngrok http ' + HOST_IP+':'+BACK_PORT, shell=True, preexec_fn=os.setsid, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)

time.sleep(2) # give the processes time to start
fcon = requests.get('http://localhost:4040/api/tunnels').json()['tunnels']
bcon = requests.get('http://localhost:4041/api/tunnels').json()['tunnels']
furl = [fcon[ii]['public_url'] for ii in range(len(fcon)) if fcon[ii]['proto'] == 'https'][0]
burl = [bcon[ii]['public_url'] for ii in range(len(bcon)) if bcon[ii]['proto'] == 'https'][0]
print('click this -> ' + furl +'/?socketUrl=' + burl.replace('https','wss'))
downloading ngrok...
downloading CARTA...
click this -> https://49d90692514c.ngrok.io/?socketUrl=wss://688d91fe9766.ngrok.io
[7]:
# turn it off
os.killpg(os.getpgid(gp1.pid), signal.SIGTERM)
os.killpg(os.getpgid(gp2.pid), signal.SIGTERM)
os.killpg(os.getpgid(cp.pid), signal.SIGTERM)
print("stopped CARTA and the secure tunnels")
stopped CARTA and the secure tunnels