M51 at z = 0.1 and z = 0.3 (CASA 3.3)

From CASA Guides
Revision as of 11:00, 28 October 2011 by Jcrossle (talk | contribs) (sim_observe Output)
Jump to: navigation, search

Simulating Observations in CASA

This guide is applicable to CASA version 3.3. For older versions of CASA, see M51 at z = 0.1 and z = 0.3 (CASA 3.2).

Follow this process, Extracting scripts from these tutorials, to extract a casapy script from this web page. Also notice that this page uses global Python variables (e.g. fluxScale), not to be confused with task inputs (e.g. inbright).


CO 1-0 Moment maps of the original BIMA SONG measurements of M51. Left: Moment 0 (integrated intensity). Right: Moment 1 (velocity field).

This tutorial presents a simulation of ALMA observations of the well-known galaxy M51 in the CO 1-0 transition. This galaxy is located relatively nearby (luminosity distance = 9 Mpc), but, in this simulation, we will model how it would appear at redshift z = 0.1 (luminosity distance = 460 Mpc).

The goal of this tutorial is to provide a complete run-through of a relatively simple simulation. Included in this simulation are the effects of (u, v) sampling of a 50-antenna ALMA, the primary beam of the ALMA, and thermal noise. Neither calibration overheads nor errors are included, and so this simulation should be viewed as optimistic.

For this tutorial, we'll use the BIMA SONG (Helfer et al. 2003) observations of M51 as the basis for the model. Grab the file NGC5194.bima12m.cm.fits.gz and uncompress it in a working directory.

# in bash (or other unix shell)
gunzip NGC5194.bima.12m.cm.fits.gz

Load data and Initialize sim_observe

For convenience, we store the name of the resulting measurement set into the Python global cubeName. The task importfits is needed to convert the image FITS file into a CASA image. Here we invoke importfits as a function, where the task inputs are set as function input parameters. The task will produce a new directory with the name designated by imagename (in this case m51-song.image) in the current working directory.

# in CASA
cubeName = 'm51-song.image'
importfits(fitsimage='NGC5194.bima12m.cm.fits', imagename=cubeName)

The task sim_observe will generate the simulated measurement set using the input model. We initialize the task like so.

# in CASA

Noise in the Input Model

The input model is actually an observation and, as such, certainly contains noise. We're OK as long as the noise in the model falls below the expected thermal noise of the ALMA observation. Here's a quick, back-of-the-envelope calculation.

The noise on the BIMA SONG channels measures roughly 0.1 Jy/beam, which scales to 0.04 mJy/beam at z = 0.1 (to within factors of powers of 1+z for cosmology and root-small-integer for the to-be-degraded beam; see below). For comparison, the expected thermal noise of the ALMA observation is 0.1 mJy / beam (using the ALMA Sensitivity Calculator, assuming 8 hrs integration, and, to match roughly the BIMA SONG observation, a 4 MHz channel width), a factor of 2.5 greater than the anticipated contribution of noise from the model. Added in quadrature, the noise components total 0.108 mJy, and so we can expect that the input model noise degrades the simulation noise by about 8%.

Cosmology Calculations

Next we'll set up some python globals to handle the scaling of the model coordinates and flux densities appropriate for new redshift. We'll primarily need the angular size and luminosity distances for a given cosmology. To keep things simple, we'll use Ned Wright's CosmoCalc (2006) with the default cosmology; redshifts were collected from NED.

# Distinguish between z_lsrk, which sets the observed frequency for scaling, from z_cmb, 
#      which is needed to get cosmological distances.
z_old_cmb = 0.002122 # CMB-referenced z for cosmological distances from NED
z_old_lsrk = 0.001544 # from NED
z_new = 0.1
# angular size distances from CosmoCalc
da_old = 9.0
da_new = 375.9
# luminosity distances from CosmoCalc
dl_old = 8.937
dl_new = 454.8

The convention is old refers to M51 as observed at its proper redshift, and new refers to the new, higher redshift for our model.

Preparing the Model

The next step is to scale the M51 data cube into a model cube appropriate for sim_observe. First, we establish the file naming convention by setting the task parameter project.

# in CASA
# z = 0.1, or point-1; useful to distinguish from repeated simulations at different z's
suffix = "-p1"
# project ID to assign output filenames
project = "M51-ATZ" + suffix

Flux Density Scaling

Our goal in this step is to set the inbright parameter of sim_observe, which will scale the data cube appropriately for its new luminosity distance. The task wants models in units of Jy / pixel, but the BIMA SONG cube has units of Jy / beam. That's an easy conversion.

# in CASA
# Get the major and minor axes of the model clean beam
bmaj = imhead(imagename=cubeName,mode='get',hdkey='beammajor')
bmin = imhead(imagename=cubeName,mode='get',hdkey='beamminor')
# Convert radians to 1-arcsecond pixels using the qa tool
bmaj = qa.convert(bmaj,'arcsec')['value']
bmin = qa.convert(bmin,'arcsec')['value']
# Gaussian beam conversion = beams / pixel
toJyPerPix = 1.0 / (1.1331 * bmaj * bmin)

Next, scale the flux for the new luminosity distance. Make the approximation that each pixel is a point source, and use the inverse square law to scale. sim_observe can scale the peak surface brightness using the parameter inbright.

# in CASA
# Correct flux density for luminosity distance
fluxScale = (dl_old/dl_new)**2 * (1.0 + z_new) / (1.0 + z_old_cmb)
# Current peak flux (Jy / pixel)
peak = imstat(cubeName)['max'][0] * toJyPerPix
# Desired peak flux (using Python formatting convention)
inbright = "%fJy/pixel" % (peak*fluxScale)

Notice that there is an additional (1+z) correction because we are scaling flux densities rather than bolometric fluxes.

Angular Size Scaling

The sky coordinates axes of the model need to be adjusted (1) to place M51 in the southern hemisphere and (2) to correct for the new angular size distance. These tasks can be accomplished by the sim_observe parameters indirection (to change the location of M51 on the sky) and incell (to change the angular scale of an input pixel). To perform task (1), we'll just flip the sign of the declination using imhead. We'll use the qa tool to convert radians to sexagesimal units.

# in CASA
# For clarity, build up "indirection" string one term at a time.
indirection = "J2000 " # Epoch
# RA
crval1 = imhead(imagename=cubeName,mode='get',hdkey='crval1')['value']
indirection += qa.formxxx(str(crval1)+'rad',format='hms') + " " 
# Dec * -1
crval2 = imhead(imagename=cubeName,mode='get',hdkey='crval2')['value']
indirection += qa.formxxx('%frad' % (-1*float(crval2)),format='dms')

Next, adjust the pixel scale for the new angular size distance. To perform this adjustment, we'll use imhead with mode = "get" to read in the original pixel scale, then use the sim_observe input parameter incell to designate the new pixel size.

# in CASA
# Scale pixel size: imhead returns radians; convert to arcsec
cdelt2 = imhead(imagename=cubeName,mode='get',hdkey='cdelt2')['value']
oldCell = cdelt2 * 206265
# Scale for new angular size distance
newCell = oldCell * da_old / da_new 
# Format the new pixel size for input to sim_observe
incell = "%farcsec" % (newCell)

Adjusting the Frequency Axis

Changing the frequency axis of the model header is just a straightforward (1+z) correction. sim_observe adjusts the channelwidth using the inwidth parameter and the observing frequency using incenter.

Notice that the absolute value of the BIMA SONG channel width is used. The noise calculation in sim_observe needs positive channel widths, but the input cube is ordered in increasing velocity rather than increasing frequency. We could transpose the cube; on the other hand, the only penalty of changing the sign is that the sense of rotation will be flipped. Since this simulation is a simple detection experiment, the rotation sense is irrelevant, and so we'll take the easy way out.

# in CASA
# Move freq to z_new
oldFreq = imhead(imagename=cubeName,mode='get',hdkey='crval3')['value']
newFreq = oldFreq * (1.0 + z_old_lsrk) / (1.0 + z_new)
nchan = imstat("NGC5194.bima12m.cm.fits")['trc'][2]
# Adjust frequency channelwidth for new z
oldDnu = imhead(imagename=cubeName,mode='get',hdkey='cdelt3')['value']
newDnu = abs((1.0+z_old_lsrk) /(1.0+z_new)*oldDnu) # make channel widths positive
inwidth = "%fHz" % newDnu
# Specify the observing frequency at the center of the observing band:
incenter = "%fHz" % (newFreq + 0.5*nchan*newDnu)


The original BIMA SONG image is about 480 arcseconds across; scale this image size to the new redshift. We will use this value later.

# in CASA
imSize = 480.0 * (da_old / da_new) # arcseconds

For relatively high redshifts, there should be no need to mosaic the observations. We'll nevertheless allow for mosaicking in case we want to repeat the simulation for lower redshift. sim_observe needs the spacing between pointings in the mosaic; we'll require pointings spaced by half of the primary beam. The ALMA primary beam is 17 arcseconds at 300 GHz.

# in CASA
primaryBeam = 17.0 * (300e9 / newFreq) # arcseconds
pointingspacing = "%farcsec" % (primaryBeam / 2.0) 
mapsize = "%farcsec" % imSize

We also need to estimate the desired synthesized beam size. We don't want the new beam to be so large so that we cannot resolve a rotation curve, but we also don't want it to be so small that we effectively resolve out the BIMA SONG data. The BIMA SONG beam was 5 arcsec, and so as a reasonable guess we'll adopt the equivalent of a 15" beam at its true distance(3 times coarser than the BIMA SONG beam) and then scale appropriately to z = 0.1.

# in CASA
beamNew = 15.0 * (da_old / da_new)

We want pixels that sample the beam at least 3 times for stable deconvolution; we'll use 4 times sampling, rounded off to the nearest milliarcsec.

# in CASA
pixelSize = round(beamNew * 1000.0 / 4.0) / 1000.0

Now, to guard against undersampling the beam as a result of rounding error, reset the desired beam to 4 times the pixel size.

# in CASA
beamNew = 4.0 * pixelSize

Now we know both the image size and pixel size in arcseconds, but sim_analyze wants the ratio: the number of pixels along the RA or Dec axis. To keep the image from becoming too small, set the minimum image size to be 256 pixels.

# in CASA
imSizePix = int(round(imSize / pixelSize))
if imSizePix < 256: imSizePix = 256

Let sim_observe decide on an appropriate ALMA configuration based on the desired beam size. Set the parameter antennalist as follows (but see Other Antenna Configurations below).

# in CASA
antennalist = "alma;%farcsec" % beamNew

We will set the integration time to 100 seconds and the total integration time to 8 hours. For a real observation, 10 seconds may be a more appropriate integration time. However, we can decrease the simulation CPU time by increasing the integration interval (See Etime study for details]]). Whenever simulating a new data set, we recommend beginning with a large integration time for initial testing and then lowering the integration time to generate a more realistic measurement set.

# in CASA
integration = '100s'
# 8 hours of total integration
totaltime = '28800s'

We will add thermal noise to the simulated data by setting parameter thermalnoise to tsys-atm. We will keep the default values of precipitable water vapor (1 mm) and ambient temperature (269 K).

# in CASA

Finally, we set graphics to "both" to send plots to the screen and disk; we set verbose to True to print additional information to the logger and terminal; and we set overwrite to True to overwrite existing files in the project subdirectory. Now we are ready to run sim_observe.

# in CASA
verbose = True
overwrite = True

sim_observe Output

Outputs from sim_observe will be written to a directory specified by the project parameter, in this case "M51-ATZ-p1". sim_observe will generate these files:

Filename Description
M51-ATZ-p1.alma_0.360000arcsec.ms Model measurement set sans noise
M51-ATZ-p1.alma_0.360000arcsec.noisy.ms Model measurement set with thermal noise
M51-ATZ-p1.alma_0.360000arcsec.quick.psf CASA image of the point spread function
M51-ATZ-p1.alma_0.360000arcsec.skymodel CASA image of the input sky model rescaled according to the skymodel sub-parameters
M51-ATZ-p1.alma_0.360000arcsec.skymodel.flat flattened CASA image of the input sky model rescaled
M51-ATZ-p1.alma_0.360000arcsec.skymodel.png flattened PNG image of the input sky model rescaled and overlaid with the mosaic pattern specified by the setpointings sub-parameters
M51-ATZ-p1.alma_0.360000arcsec.observe.png 2x2 PNG summary plot (sim.observe.png) showing: source elevation vs. time, antenna position, uv coverage, and the point spread function
M51-ATZ-p1.alma_0.360000arcsec.ptg.txt ASCII text listing of mosaic pointings

And there are plenty of other auxiliary files. The image at the right can be created by running the task viewer and loading the appropriate image file (e.g. viewer(infile = 'M51-ATZ-p1/M51-ATZ-p1.image', channel = 16)).


# in CASA
modelimage = cubeName
# make simulated images/cubes using noisy data
image=True # SIM_ANALYZE
vis = '$project.noisy.ms' # SIM_ANALYZE
cell = "%farcsec" % pixelSize
imsize = [imSizePix,imSizePix]
threshold = "1.0mJy"
weighting = "natural"
stokes = 'I'
verbose = True
overwrite = True

Other Antenna Configurations

ALMA synthetic beam size as a function of array configuration number

Finally, we need to know which ALMA configuration number based on the desired angular resolution. Simdata makes this easy by allowing users to specify the desired angular resolution in the parameter antennalist.

This tutorial is somewhat automated to produce a decent cube of M51 as viewed at z = 0.1. The selection of the ALMA antenna configuration is automated, but, for other simulations (or this one, for that matter), it will be worth playing with the configurations, or perhaps evaluating the possibility of detections in CSV or early science.

Notice from the simdata inputs that CASA comes with stock antenna configurations in the directory $CASAPATH/data/alma/simmos/ (the python task os.getenv is used to look up CASAPATH automatically). For CASA 3.2, here is a list of included ALMA antenna configurations.

ALMA Configuration Files

There are also configuration data for the ACA, EVLA, and SMA.

Take a Break

If you have got this far, you've earned it. Simdata will be running for a while, and coffee sure sounds good right now.


Channel 16 of the M51 at z=0.1 simulation. The rms noise is about 0.25 mJy/beam, and the peak flux density on this channel is about 2 mJy/beam.

Here is an inventory of some of the simdata products. They will appear in the directory "M51-ATZ-p1", a sub-directory of your working directory.

Filename Description
M51-ATZ-p1.ms Model measurement set sans noise
M51-ATZ-p1.noisy.ms Model measurement set with thermal noise
M51-ATZ-p1.image CLEAN-deconvolved cube of M51-ATZ-p1.noisy.ms

And there are plenty of other auxiliary files. The image at the right can be created by running the task viewer and loading the appropriate image file (e.g. viewer(infile = 'M51-ATZ-p1/M51-ATZ-p1.image', channel = 16)).

The rest frequency will have been lost in the simulation, and it's worth restoring to the header.

imhead(imagename="M51-ATZ-p1/M51-ATZ-p1.image", mode="put", hdkey="restfreq", 

The simulated data cube can be analyzed just like any other CASA image -- examples are given in the CARMA tutorial and the VLA 21cm tutorial.

Moment Maps

Moment maps of the M51 CO 1-0 at z=0.1 simulation. Left: Moment 0 (integrated intensity) map. Right: Moment 1 (velocity field) map. Note that the rotation sense has been flipped, exactly as expected.

Use immoments to calculate the integrated intensity and velocity field maps from the simulated cube. The excludepix option applies a 3σ cut.


The results are shown at right.

You can view these results by running the task viewer, then selecting the moments maps found in the directory M51-ATZ-p1 (M51-ATZ-p1.moments.integrated and M51-ATZ-p1.moments.weighted_coord)

Pushing M51 out to z = 0.3

We'll leave it as an exercise how to tune this simulation to push M51 all the way out to z = 0.3 (luminosity distance = 1540 Mpc). Simdata produces a reasonable detection in 8 hrs integration, but you'll need to consider more carefully the antenna configuration needed to produce the requisite sensitivity. To conclude, here are the moment maps for the z = 0.3 simulation.

Simulation of M51 CO 1-0 at z = 0.3. Left: integrated intensity map. Right: (marginally resolved) velocity field.

Simulating Observations in CASA

Last checked on CASA Version 3.2.0.

--Jack Gallimore 15:55, 30 April 2010 (UTC), updated for CASA 3.2 by A. Kimball, 28th April 2011