def ft(dset, complist=None, model=None, start=200, stop=900, script='ft', delete=False): """ Fourier transforming model (complist) and writing the resulting visibilities into the MODEL column Parameters ---------- dset : string Name of input measurement set (MS) file containing visibilities for all baselines and the corresponding metadata complist : string Name of the component list containing list of model sources model : string Name of model image(s) scripts : string Name of casapy script that will be create on-the-fly. Default is exportfits delete: boolean Deletes the casapy script that was created on-the-fly after execution """ casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') if complist is None and model is None: task_opt = casawrapper.create_casa_options(vis="'{}'".format(dset), spw="'0:{}~{}'".format(start, stop), usescratch=True) elif not complist is None: task_opt = casawrapper.create_casa_options(vis="'{}'".format(dset), complist="'{}'".format(complist), spw="'0:{}~{}'".format(start, stop), usescratch=True) elif not model is None: task_opt = casawrapper.create_casa_options(vis="'{}'".format(dset), model="'{}'".format(model), spw="'0:{}~{}'".format(start, stop), usescratch=True) else: raise ValueError('Need to specify model input.') casawrapper.call_casa_task(task='ft', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def imaging(dset, imagename, antenna='', cellsize='8arcmin', npix=512, niter=0, threshold='0Jy', weighting='uniform', start=200, stop=900, uvlength=0, uvsign='>', phasecenter='', gridmode='', wprojplanes=1024, script='clean', delete=True): """ Generates images using all antenna or specific antennas using visibilities from the measurement set (MS) Parameters ---------- dset: string or list Input measurement sets (MS) file containing visibilities for all baselines and the corresponding metadata imagename: string Output casa image antenna: string Antenna(s) or baseline(s) to used for imaging. e.g antenna='0' uses data from antenna 0 only antenna='0,4,5' uses data from antennas 0,4 and 5 antenna='0&3' uses data from baseline 0-3 antenna='0&3; 4&3' uses data from baselines 0-3 and 4-3 Default is ''(all) which uses all the baselines cellsize: string Degrees to be contained in one pixel of the image. Default is 3 arcmins. npix: int Number of pixel the output image is along x(l) and y(m) axis. Default is 512. niter: integer Number of iterations to use for deconvolution. Default is 0. Hence, dirty images are generated by default. threshold: string Cleaning threshold in Jy or mJy. Default is 0 Jy. weighting: string Weighting to be using for gridding of uv points. Default is uniform. start: int Starting frequency channel. Default is 200. stop: int Stopping/endign frequency channel. Default is 900. uvlength: float UV length in metres equal to or smaller to exclude while generating the image. Default is 0. uvsign: string either '>' or '<' for cut in uvlength. For example if '>30' all baselines greater than 30 m will be used for imaging phasecenter: string Pointing center of the image gridmode: string Gridding kernel for FFT-based transforms wprojplanes : int Number of w-projection planes for convolution; -1 => automatic determination script: string Name of casapy script that will be create on-the-fly. Default is clean delete: boolean Deletes the casapy script that was created on-the-fly after execution """ vis = "{}".format(dset) if type(dset) is list else "'{}'".format(dset) antenna_out = 'all' if antenna == '' else antenna print ('Imaging using antenna(s) {}'.format(antenna_out)) casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(vis=vis, imagename="'{}'".format(imagename), antenna="'{}'".format(antenna), cell="'{}'".format(cellsize), imsize=[npix,npix], threshold="'{}'".format(threshold), niter="{}".format(niter), spw="'0:{}~{}'".format(start, stop), uvrange="'{}{}'".format(uvsign, uvlength), weighting="'{}'".format(weighting), phasecenter="'{}'".format(phasecenter) ,gridmode="'{}'".format(gridmode), wprojplanes="{}".format(wprojplanes), usescratch=True) casawrapper.call_casa_task(task='clean', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def create_complist(infile, outfile='component.cl', script='create_cl', delete=False): """ Creates component list of sources Parameters: ----------- infile : Input text file containing the required parameters (see generate_complist_input function for the input format) outfile : Output name of the component list Returns ------- CASA Component with all the components """ if os.path.exists(outfile): print ('WARNING: overwritting existed file') os.system('rm -rf {}'.format(outfile)) stdout = open(script + '.py', 'wb') sources = numpy.loadtxt(infile, dtype='str', delimiter=':') if sources.ndim == 1: sources = sources.reshape((1, len(sources))) for src in sources: task_opt = "dir='{}', flux={}, fluxunit='Jy', shape='point', spectrumtype='spectral index', index={}, freq='{}MHz'".format(src[0], float(src[1]), float(src[2]), float(src[3])) text = ('cl.addcomponent({})\n'.format(task_opt)).encode() stdout.write(text) text = ("cl.rename('{}')\ncl.close()".format(outfile)).encode() stdout.write(text) stdout.close() casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') casawrapper.call_casa_script(script + '.py', casa_opts=casa_opt, delete=delete)
def subtract_model_ant(dset, ant, script='subtract_mod_ant', operation='subtract', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "import numpy as np; import copy\n" task_opt += "operation='{}'\n".format(operation) task_opt += "ms = casac.table()\n" task_opt += "ms.open('{}', nomodify=False)\n".format(dset) task_opt += "datacol = 'CORRECTED_DATA' if 'CORRECTED_DATA' in ms.colnames() else 'DATA'\n" task_opt += "data = ms.getcol(datacol)\n" task_opt += "model = ms.getcol('MODEL_DATA')\n" task_opt += "A1 = ms.getcol('ANTENNA1')\n" task_opt += "inds = np.where(A1=={})\n".format(ant) task_opt += "data_bl = data[:, :, inds[0]]\n" task_opt += "mod_bl = model[:, :, inds[0]]\n" task_opt += "new_data = copy.deepcopy(data)\n" task_opt += "if operation == 'add':\n" task_opt += " new_data[:, :, inds[0]] = data_bl + mod_bl\n" task_opt += " ms.putcol(datacol, new_data)\n" task_opt += "else:\n" task_opt += " new_data[:, :, inds[0]] = data_bl - mod_bl\n" task_opt += " ms.putcol(datacol, new_data)\n" task_opt += "ms.close()" stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def flag_antenna(dset, antenna, script='flag_a', delete=True): """ Flags all the visibilities corresponding to one/more antennas Parameters ---------- dset: string Name of input measurement set (MS) file containing visibilities for all baselines and the corresponding metadata antenna: string Antenna number to be flagged, e.g '0' or list of antennas e.g '0, 1, 5, 6' script: string Name of casapy script that will be create on-the-fly. Default is flag_a delete: boolean Deletes the casapy script that was created on-the-fly after execution """ print ('Flagging antenna(s) {}'.format(antenna)) casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(vis="'{}'".format(dset), antenna="'{}'".format(antenna)) casawrapper.call_casa_task(task='flagdata', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def test_call_casa_task(): casa_opts = cw.create_casa_options(nologger='0', nogui='0', nologfile='0') cw.call_casa_task(task="importuvfits", script="execute", task_options={'vis':"'test.ms'"}, casa_options=casa_opts, delete=False, verbose=True) nt.assert_true(os.path.exists(scriptname)) fl = open(scriptname) lines = [] for line in fl.readlines(): lines.append(line) answer = ['default(importuvfits)\n', "vis='test.ms'\n", 'go()'] nt.assert_equal(lines, answer)
def writeto(dset, data_array, column_to, script='writeto', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "ms = casac.table()\n" task_opt += "ms.open('{}', nomodify=False)\n".format(dset) task_opt += "if '{}' in ms.colnames():\n".format(column_to) task_opt += " ms.putcol('{}', {})\n".format(column_to, data_array) task_opt += "ms.close()" stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def change_pc(dset, script='pc', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "import numpy as np\n" task_opt += "ms = casac.table()\n" task_opt += "ms.open('{}/FIELD', nomodify=False)\n".format(dset) task_opt += "pc =np.array([[[ 0.87707669]],[[-0.53722607]]])\n" task_opt += "ms.putcol('PHASE_DIR', pc)\n" task_opt += "ms.close()" stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def get_freq(dset, outname, script='get_freqs', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "import numpy as np\n" task_opt += "ms = casac.table()\n" task_opt += "ms.open('{}/SPECTRAL_WINDOW', nomodify=False)\n".format(dset) task_opt += "freqs = ms.getcol('CHAN_FREQ')\n" task_opt += "ms.close()\n" task_opt += "np.save('{}', freqs)".format(outname) stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def set_to_unflag(dset, script='unflag', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "import numpy as np\n" task_opt += "ms = casac.table()\n" task_opt += "ms.open('{}', nomodify=False)\n".format(dset) task_opt += "flags = ms.getcol('FLAG')\n" task_opt += "new_flags = np.zeros(flags.shape, dtype=np.bool)\n" task_opt += "ms.putcol('FLAG', new_flags)\n" task_opt += "ms.close()" stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def uvfits2ms(uvfits, outfile=None, script='uvfits2ms', overwrite=False, delete=True): """ Converts measurement sets to uvfits files Parameters ---------- dset: string Measurement sets (MS) containing visibilities and the corresponding metadata. outfile: string Name of the output uvfits file. Default is <dset>.uvfits. script: string Name of casapy script that will be create on-the-fly. Default is ms2uvfits. overwrite : boolean Overwrites any existing file. Default is False. delete: boolean Deletes the casapy script that was created on-the-fly after execution. """ if outfile is None: outfile = uvfits.replace('.uvfits', '.ms') print ('Converting {} to {}'.format(uvfits, outfile)) casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(fitsfile="'{}'".format(uvfits), vis="'{}'".format(outfile)) casawrapper.call_casa_task(task='importuvfits', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def read_col(dset, column, outfile='data', script='read_col', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "ms = casac.table()\n" task_opt += "ms.open('{}', nomodify=False)\n".format(dset) task_opt += "if '{}' in ms.colnames():\n".format(column) task_opt += " data = ms.getcol('{}')\n".format(column) task_opt += "ms.close()\n" task_opt += "import numpy as np\n" task_opt += "np.save('{}', data)".format(outfile) stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def importfits(fitsname, imagename=None, overwrite=False, script='importfits', delete=True): """ Converts fitsfile to CASA image format Paramters --------- fitsname: string Name of input fits file. imagename: string Name of the output casa image.Default is <fitsname>.fits. overwrite : boolean If True, overwrites the existing image with the new one. Default is False. script: string Name of casapy script that will be create on-the-fly. Default is exportfits delete: boolean Deletes the casapy script that was created on-the-fly after execution """ if imagename is None: imagename = fitsname.replace('.fits', '.image') casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(fitsimage="'{}'".format(fitsname), imagename="'{}'".format(imagename), overwrite=overwrite) casawrapper.call_casa_task(task='importfits', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def subtract_model(dset, script='subtract_mod', operation='subtract', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = "operation='{}'\n".format(operation) task_opt += "ms = casac.table()\n" task_opt += "ms.open('{}', nomodify=False)\n".format(dset) task_opt += "datacol = 'CORRECTED_DATA' if 'CORRECTED_DATA' in ms.colnames() else 'DATA'\n" task_opt += "data = ms.getcol(datacol)\n" task_opt += "if 'MODEL_DATA' in ms.colnames():\n" #task_opt += "data = ms.getcol('CORRECTED_DATA') if 'CORRECTED_DATA' in ms.colnames() else ms.getcol('DATA')\n" task_opt += " if operation == 'add':\n" task_opt += " ms.putcol(datacol, data + ms.getcol('MODEL_DATA'))\n" task_opt += " else:\n" task_opt += " ms.putcol(datacol, data - ms.getcol('MODEL_DATA'))\n" task_opt += "ms.close()" stdout = open(script + ".py", "w") stdout.write(task_opt) stdout.close() casawrapper.call_casa_script(script + ".py", casa_opt, delete=delete)
def concat(dsets, outfile, script='concat', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(vis="{}".format(dsets), concatvis="'{}'".format(outfile)) casawrapper.call_casa_task(task='concat', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def gaincal(dset, caltable, solint='30s', gaintype='G', usescratch=True, script='gaincal', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(vis="'{}'".format(dset), gaintype="'{}'".format(gaintype), solint="'{}'".format(solint), caltable="'{}'".format(caltable), usescratch="'{}'".format(usescratch)) casawrapper.call_casa_task(task='gaincal', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)
def test_create_casa_options(): kwargs = cw.create_casa_options(nologger='0', nogui='0', nologfile='0') nt.assert_equal(kwargs, {'nologger': '0', 'nogui': '0', 'nologfile': '0'})
def applycal(dset, gaintable, script='applycal', delete=False): casa_opt = casawrapper.create_casa_options(nologger='0', nogui='0', nologfile='0') task_opt = casawrapper.create_casa_options(vis="'{}'".format(dset), gaintable="'{}'".format(gaintable)) casawrapper.call_casa_task(task='applycal', script=script, task_options=task_opt, casa_options=casa_opt, delete=delete)