Download this notebook

Casa-Utils example

Post-processing a ProDimo line sube

Here we post-process a line cube produce by ProDiMo. It is a rather simple example. We subtract the continuum, convolve the cube with some beam, and produce some moment maps.

[18]:
import casatasks as ctasks
import prodimopy.utils_casasim as ucsim
import shutil
import glob
[19]:
# Define project name and file prefix
# This will be the output folder for the produced fits files, and the prefix for all files will also include the proj string
# Be careful, if the folder already exists, the content in that folder will be overwrittten.
proj="test_casasim"
fprefix=proj+"/"+proj
[20]:
# Load a line cube from a ProDiMo model
psim=ucsim.LinePP(proj,"$HOME/MODELS/prodimopy/TWHyaMetis/LINE_3D_002_i003.fits")


make Directorytest_casasim
[21]:
# Continuum subtraction, this will be done directly in the model cube, so is not very realistic, but ok for testing purposes
psim.contsub_model()
# Create some momentmaps directly from the model cube
psim.momentmap(infits=fprefix+".mcube.contsub.fits",outfits=fprefix+".mcube.contsub.mom0.fits")
psim.momentmap(infits=fprefix+".mcube.contsub.fits",outfits=fprefix+".mcube.contsub.mom1.fits",moment=1)
contsub_model ...
2025-10-27 16:44:38     WARN    importfits::::casa      This image has no beam or angular resolution provided, so you will not receive warnings from
2025-10-27 16:44:38     WARN    importfits::::casa+     tasks such as imregrid if your image pixels do not sample the the angular resolution well.
2025-10-27 16:44:38     WARN    importfits::::casa+     (This only affects warnings, not any functionality).
2025-10-27 16:44:38     WARN    importfits::::casa+     Providing a beam and brightness units in an image can also be useful for flux calculations.
2025-10-27 16:44:38     WARN    importfits::::casa+     If you wish to add a beam or brightness units to your image, please use
2025-10-27 16:44:38     WARN    importfits::::casa+     the "beam" parameter or ia.setrestoringbeam() and ia.setbrightnessunit()
2025-10-27 16:44:38     WARN    importfits::::casa      This image has no beam or angular resolution provided, so you will not receive warnings from
2025-10-27 16:44:38     WARN    importfits::::casa+     tasks such as imregrid if your image pixels do not sample the the angular resolution well.
2025-10-27 16:44:38     WARN    importfits::::casa+     (This only affects warnings, not any functionality).
2025-10-27 16:44:38     WARN    importfits::::casa+     Providing a beam and brightness units in an image can also be useful for flux calculations.
2025-10-27 16:44:38     WARN    importfits::::casa+     If you wish to add a beam or brightness units to your image, please use
2025-10-27 16:44:38     WARN    importfits::::casa+     the "beam" parameter or ia.setrestoringbeam() and ia.setbrightnessunit()
2025-10-27 16:44:38     WARN    ImageExprCalculator::compute    Number of axes in input images differs
removed 'test_casasim/cubemcont.im/table.info'
removed 'test_casasim/cubemcont.im/table.lock'
removed 'test_casasim/cubemcont.im/table.f0'
removed 'test_casasim/cubemcont.im/table.f0_TSM0'
removed 'test_casasim/cubemcont.im/table.dat'
removed 'test_casasim/cubemcont.im/logtable/table.info'
removed 'test_casasim/cubemcont.im/logtable/table.lock'
removed 'test_casasim/cubemcont.im/logtable/table.f0'
removed 'test_casasim/cubemcont.im/logtable/table.dat'
removed directory 'test_casasim/cubemcont.im/logtable'
removed directory 'test_casasim/cubemcont.im'
removed 'test_casasim/cont.im/table.info'
removed 'test_casasim/cont.im/table.lock'
removed 'test_casasim/cont.im/table.f0'
removed 'test_casasim/cont.im/mask0/table.info'
removed 'test_casasim/cont.im/mask0/table.lock'
removed 'test_casasim/cont.im/mask0/table.f0'
removed 'test_casasim/cont.im/mask0/table.f0_TSM0'
removed 'test_casasim/cont.im/mask0/table.dat'
removed directory 'test_casasim/cont.im/mask0'
removed 'test_casasim/cont.im/table.f0_TSM0'
removed 'test_casasim/cont.im/table.dat'
removed 'test_casasim/cont.im/logtable/table.info'
removed 'test_casasim/cont.im/logtable/table.lock'
removed 'test_casasim/cont.im/logtable/table.f0'
removed 'test_casasim/cont.im/logtable/table.dat'
removed directory 'test_casasim/cont.im/logtable'
removed directory 'test_casasim/cont.im'
removed 'test_casasim/cube.im/table.info'
removed 'test_casasim/cube.im/table.lock'
removed 'test_casasim/cube.im/table.f0'
removed 'test_casasim/cube.im/mask0/table.info'
removed 'test_casasim/cube.im/mask0/table.lock'
removed 'test_casasim/cube.im/mask0/table.f0'
removed 'test_casasim/cube.im/mask0/table.f0_TSM0'
removed 'test_casasim/cube.im/mask0/table.dat'
removed directory 'test_casasim/cube.im/mask0'
removed 'test_casasim/cube.im/table.f0_TSM0'
removed 'test_casasim/cube.im/table.dat'
removed 'test_casasim/cube.im/logtable/table.info'
removed 'test_casasim/cube.im/logtable/table.lock'
removed 'test_casasim/cube.im/logtable/table.f0'
removed 'test_casasim/cube.im/logtable/table.dat'
removed directory 'test_casasim/cube.im/logtable'
removed directory 'test_casasim/cube.im'
Produced continuum subtraced cube: test_casasim/test_casasim.mcube.contsub.fits
momentmap moment 0 ...
Produced moment 0 map: test_casasim/test_casasim.mcube.contsub.mom0.fits
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/table.info'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/table.lock'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/table.f0'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/mask0/table.info'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/mask0/table.lock'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/mask0/table.f0'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/mask0/table.f0_TSM0'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/mask0/table.dat'
removed directory 'test_casasim/test_casasim.mcube.contsub.mom0.im/mask0'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/table.f0_TSM0'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/table.dat'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/logtable/table.info'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/logtable/table.lock'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/logtable/table.f0'
removed 'test_casasim/test_casasim.mcube.contsub.mom0.im/logtable/table.dat'
removed directory 'test_casasim/test_casasim.mcube.contsub.mom0.im/logtable'
removed directory 'test_casasim/test_casasim.mcube.contsub.mom0.im'
momentmap moment 1 ...
Produced moment 1 map: test_casasim/test_casasim.mcube.contsub.mom1.fits
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/table.info'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/table.lock'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/table.f0'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/mask0/table.info'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/mask0/table.lock'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/mask0/table.f0'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/mask0/table.f0_TSM0'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/mask0/table.dat'
removed directory 'test_casasim/test_casasim.mcube.contsub.mom1.im/mask0'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/table.f0_TSM0'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/table.dat'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/logtable/table.info'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/logtable/table.lock'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/logtable/table.f0'
removed 'test_casasim/test_casasim.mcube.contsub.mom1.im/logtable/table.dat'
removed directory 'test_casasim/test_casasim.mcube.contsub.mom1.im/logtable'
removed directory 'test_casasim/test_casasim.mcube.contsub.mom1.im'
[22]:
# Convolve the continuum subtracted cube to a given beam size (Gaussian beam)
psim.conv_image(fprefix+".mcube.contsub.fits",fprefix+".conv.contsub.fits",beam=["0.03arcsec","0.03arcsec","0.0deg"])
conv_image with beam:  ['0.03arcsec', '0.03arcsec', '0.0deg']  ...
removed 'test_casasim/test_casasim.conv.contsub.im/table.info'
removed 'test_casasim/test_casasim.conv.contsub.im/table.lock'
removed 'test_casasim/test_casasim.conv.contsub.im/table.f0'
removed 'test_casasim/test_casasim.conv.contsub.im/table.f0_TSM0'
removed 'test_casasim/test_casasim.conv.contsub.im/table.dat'
removed 'test_casasim/test_casasim.conv.contsub.im/logtable/table.info'
removed 'test_casasim/test_casasim.conv.contsub.im/logtable/table.lock'
removed 'test_casasim/test_casasim.conv.contsub.im/logtable/table.f0'
removed 'test_casasim/test_casasim.conv.contsub.im/logtable/table.dat'
removed directory 'test_casasim/test_casasim.conv.contsub.im/logtable'
removed directory 'test_casasim/test_casasim.conv.contsub.im'
Produced convolved cube: test_casasim/test_casasim.conv.contsub.fits
[23]:
# Rebin the convolved cube to lower spatial and spectral resolution, create moment maps and spectra
for f in glob.glob(fprefix+"*rebin*"):
  shutil.rmtree(f,ignore_errors=True)

# Here we use directly the casatasks tools to do the rebinning and moment map creation
ctasks.imrebin(imagename=fprefix+".conv.contsub.fits",outfile=fprefix+".contsub.rebin.cube.im",factor=[3,3,3],overwrite=True)
ctasks.immoments(imagename=fprefix+".contsub.rebin.cube.im",outfile=fprefix+".contsub.rebin.cube.integrated.im",moments=[0])
ctasks.immoments(imagename=fprefix+".contsub.rebin.cube.im",outfile=fprefix+".contsub.rebin.cube.mom1.im",moments=[1])
ctasks.specflux(imagename=fprefix+".contsub.rebin.cube.im",overwrite=True,logfile=fprefix+".contsub.rebin.cube.specprof")

ctasks.exportfits(imagename=fprefix+".contsub.rebin.cube.im",fitsimage=fprefix+".contsub.rebin.cube.fits",overwrite=True,dropdeg=True)
ctasks.exportfits(imagename=fprefix+".contsub.rebin.cube.integrated.im",fitsimage=fprefix+".contsub.rebin.cube.integrated.fits",overwrite=True,dropdeg=True)
ctasks.exportfits(imagename=fprefix+".contsub.rebin.cube.mom1.im",fitsimage=fprefix+".contsub.rebin.cube.mom1.fits",overwrite=True,dropdeg=True)

[29]:
# Let's take a look at the produces files
! ls -lh test_casasim/
total 267M
-rw-rw-r-- 1 rab rab 3.3M Oct 27 17:45 test_casasim.contsub.rebin.cube.fits
drwxrwxr-x 3 rab rab 4.0K Oct 27 17:45 test_casasim.contsub.rebin.cube.im
-rw-rw-r-- 1 rab rab  60K Oct 27 17:45 test_casasim.contsub.rebin.cube.integrated.fits
drwxrwxr-x 4 rab rab 4.0K Oct 27 17:45 test_casasim.contsub.rebin.cube.integrated.im
-rw-rw-r-- 1 rab rab  60K Oct 27 17:45 test_casasim.contsub.rebin.cube.mom1.fits
drwxrwxr-x 4 rab rab 4.0K Oct 27 17:45 test_casasim.contsub.rebin.cube.mom1.im
-rw-rw-r-- 1 rab rab 5.9K Oct 27 17:45 test_casasim.contsub.rebin.cube.specprof
-rw-rw-r-- 1 rab rab  88M Oct 27 17:45 test_casasim.conv.contsub.fits
-rw-rw-r-- 1 rab rab  48K Oct 27 17:45 test_casasim.cube.png
-rw-rw-r-- 1 rab rab 450K Oct 27 17:44 test_casasim.mcube.contsubcont.fits
-rw-rw-r-- 1 rab rab  88M Oct 27 17:44 test_casasim.mcube.contsub.fits
-rw-rw-r-- 1 rab rab 450K Oct 27 17:44 test_casasim.mcube.contsub.mom0.fits
-rw-rw-r-- 1 rab rab 450K Oct 27 17:44 test_casasim.mcube.contsub.mom1.fits
-rw-r--r-- 1 rab rab  88M Oct 27 17:44 test_casasim.mcube.fits
-rw-rw-r-- 1 rab rab  91K Oct 27 17:45 test_casasim.plot.pdf

Plotting

If one created the some post-process line cubes with the prodimopy.casautils it is convenient to use also the plotting tools.

Here we show one example for creating some channel map plots.

[30]:
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib as mpl
import numpy as np
import prodimopy.read_casasim as rcsim
import prodimopy.plot_casasim as pcsim
import prodimopy.plot as pplot
pplot.load_style("nb")  # just load some style
INFO: Load prodimopynb mplstyle from package.
[31]:
# Read the Casa "simulation" results
sim=rcsim.CasaSim(directory=proj,fn_prefix=proj+".contsub.rebin")

# Create plots and save them to a multipage pdf
with PdfPages(fprefix+".plot.pdf")  as pdf:
  pc=pcsim.PlotCasasim(pdf,labelspacing=0.2)
  pc.plot_cube(sim.cube,mJy=True,cb_format="%.2f",zlim=[0,np.max(sim.cube.data)*0.9],cb_extend="both",ncol=5)
  pc.plot_integrated(sim.integrated,mJy=False)
  pc.plot_mom1(sim.mom1,mJy=True,cb_format="%.2f",zlim=[-20,20])
  pc.plot_specprof(sim.specprof,mJy=True,xlim=[-50,50])

The produced pdf test_casasim.plot.pdf.

[32]:
# Create some channel map plots and save as png
sim=rcsim.CasaSim(directory=proj,fn_prefix=proj+".contsub.rebin")

mpl.rcParams['figure.figsize'] = np.array([4.0,2.5])*0.65
pc=pcsim.PlotCasasim(None,labelspacing=0.1)
fig=pc.plot_cube(sim.cube,mJy=True,cb_format="%.2f",zlim=[0,np.max(sim.cube.data)*0.9],cb_extend="both",ncol=3,nrow=1,zoomto=0.16,
                vellabel_fontsize=8,vellabel_pos=[0.94,0.14],powerNormGamma=0.7)

ax=fig.axes[2]
props=dict(boxstyle='round',facecolor='0.8',edgecolor="none")
ax.text(0.93,0.93,r"o-H$_2$ 4.694 $\mu m$",color="black",transform=ax.transAxes,fontsize=9,verticalalignment='top',horizontalalignment="right",bbox=props)

axcb=fig.axes[3] # this is the colorbar
axcb.remove()
fig.axes[0].set_xlabel("")

fig.savefig(fprefix+".cube.png")


../_images/notebooks_utils_casasim_13_0.png