11from __future__ import print_function
2+
23import os
4+ import shutil
5+
6+ import astropy .units as u
37import drivecasa
48import drivecasa .commands .simulation as sim
5- from drivecasa .utils import ensure_dir
6- import astropy .units as u
9+ import drivecasa .commands .subroutines
710from astropy .time import Time
11+ from drivecasa .utils import ensure_dir
812from fastimgproto .pipeline .data import vla_c_antennalist_path
913
10- def simulate_vis_with_casa (pointing_centre , source_list , output_dir ):
14+
15+ def simulate_vis_with_casa (pointing_centre , source_list , vis_path ,
16+ overwrite = True , echo = False ):
1117 """
1218 Use casapy to simulate a visibility measurementset with noise.
1319
@@ -16,28 +22,41 @@ def simulate_vis_with_casa(pointing_centre, source_list, output_dir):
1622 Args:
1723 pointing_centre (:class:`astropy.coordinates.SkyCoord`)
1824 source_list: list of :class:`fastimgproto.skymodel.helpers.SkySource`
19- output_dir (str): Output directory which will contain `vis.ms`
25+ vis_path (str): Path to visibilities generated.
26+ echo (bool): Echo the CASA output to terminal during processing.
2027
2128 Returns (str): Full path to `vis.ms`.
2229 """
2330
24- ensure_dir (output_dir )
2531
26- commands_logfile = os .path .join (output_dir , "./casa-visibilities_for_point_source-commands.log" )
27- component_list_path = os .path .join (output_dir , './sources.cl' )
28- output_visibility = os .path .abspath (os .path .join (output_dir , './vis.ms' ))
32+ vis_abspath = os .path .abspath (vis_path )
33+ commands_logfile = vis_abspath + "_casa_commands.log"
34+ casa_logfile = vis_abspath + "_casa_log.log"
35+ component_list_path = vis_abspath + "_sources.cl"
2936
37+ for outdir in vis_abspath , component_list_path :
38+ if os .path .isdir (outdir ):
39+ if overwrite :
40+ shutil .rmtree (outdir )
41+ else :
42+ raise IOError ("{} already present and overwrite==False." .format (
43+ outdir
44+ ))
3045 if os .path .isfile (commands_logfile ):
31- os .unlink (commands_logfile )
32- casa = drivecasa .Casapy (commands_logfile = commands_logfile )
46+ os .remove (commands_logfile )
47+ ensure_dir (os .path .dirname (vis_abspath ))
48+
49+ casa = drivecasa .Casapy (commands_logfile = commands_logfile ,
50+ casa_logfile = casa_logfile ,
51+ echo_to_stdout = echo )
3352 script = []
53+ # Add subroutine definition, for manual reproduction with CASA:
54+ script .append (drivecasa .drivecasa .commands .subroutines .def_load_antennalist )
3455
56+ # Define some observation parameters...
3557 # For VLA reference numbers, see:
3658 # https://science.nrao.edu/facilities/vla/docs/manuals/oss/performance/fov
3759 # https://science.nrao.edu/facilities/vla/docs/manuals/oss/performance/resolution
38-
39-
40- # Define some observation parameters:
4160 obs_central_frequency = 3. * u .GHz
4261 obs_frequency_bandwidth = 0.125 * u .GHz
4362 primary_beam_fwhm = (45. * u .GHz / obs_central_frequency ) * u .arcmin
@@ -49,7 +68,7 @@ def simulate_vis_with_casa(pointing_centre, source_list, output_dir):
4968 out_path = component_list_path )
5069
5170 # Open the visibility file
52- sim .open_sim (script , output_visibility )
71+ sim .open_sim (script , vis_abspath )
5372
5473 # Configure the virtual telescope
5574 # sim.setpb(script,
@@ -81,5 +100,5 @@ def simulate_vis_with_casa(pointing_centre, source_list, output_dir):
81100 sim .corrupt (script )
82101 sim .close_sim (script )
83102
84- casa .run_script (script )
85- return output_visibility
103+ casa_output = casa .run_script (script )
104+ return casa_output
0 commit comments