From f9f3c1e7fd6ea68f8ed22d024ed66b455a4e34ba Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 10:52:38 +0100 Subject: [PATCH 01/12] [gitignore] Add .DS_Store files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 93151cc..89fb6fa 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ ENV/ # JetBrains .idea/ + +# MacOS +*.DS_Store From 4ca69a6df88a7e5ef89d5f5ca60c4e6d19f7e866 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 10:55:05 +0100 Subject: [PATCH 02/12] [uvtable] Rename format uvtable->ascii --- uvplot/uvtable.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uvplot/uvtable.py b/uvplot/uvtable.py index 0176db8..3a1f069 100644 --- a/uvplot/uvtable.py +++ b/uvplot/uvtable.py @@ -37,7 +37,7 @@ def __init__(self, uvtable=None, filename="", wle=1, **kwargs): if filename != "": self.filename = filename - uvdata = self.read_uvtable(self.filename, kwargs.get('format', 'uvtable')) + uvdata = self.read_uvtable(self.filename, kwargs.get('format', 'ascii')) u = uvdata[:, 0] v = uvdata[:, 1] @@ -61,7 +61,7 @@ def __init__(self, uvtable=None, filename="", wle=1, **kwargs): @staticmethod def read_uvtable(filename, format): """ Read uvtable from file, given a specific format. """ - if format == 'uvtable': + if format == 'ascii': uvdata = np.loadtxt(filename) else: From f2e3495f07b8e6e19f6f3d6f602c98581818a8d4 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 16:58:35 +0100 Subject: [PATCH 03/12] [setup] Lower numpy requirement to make it compatible with CASA --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 114a4f8..f7c36b3 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ author_email="mtazzari@ast.cam.ac.uk", description="Utilities for plotting interferometric visibilities.", long_description=open('README.rst').read(), - install_requires=["numpy>1.11", "matplotlib"], + install_requires=["numpy>=1.9", "matplotlib"], license="LGPLv3", url="tbd", classifiers=[ From 0159fb9b3f4e899fb3155a642ff03b8d4d6f114c Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 16:59:37 +0100 Subject: [PATCH 04/12] [uvtable] Add export_uvtable functionality. Must be executed in CASA. --- uvplot/__init__.py | 2 +- uvplot/constants.py | 3 +- uvplot/uvtable.py | 144 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 3 deletions(-) diff --git a/uvplot/__init__.py b/uvplot/__init__.py index e149267..2515f25 100644 --- a/uvplot/__init__.py +++ b/uvplot/__init__.py @@ -15,5 +15,5 @@ if matplotlib.get_backend().lower() == 'macosx': matplotlib.use('TkAgg') -from .uvtable import UVTable +from .uvtable import UVTable, export_uvtable from .constants import arcsec diff --git a/uvplot/constants.py b/uvplot/constants.py index cda5076..1c9cb10 100644 --- a/uvplot/constants.py +++ b/uvplot/constants.py @@ -4,5 +4,6 @@ from __future__ import (division, print_function, absolute_import, unicode_literals) -arcsec = 4.84813681109536e-06 # radians +arcsec = 4.84813681109536e-06 # radians +clight = 2.99792458e+8 # [m/s] Speed of light diff --git a/uvplot/uvtable.py b/uvplot/uvtable.py index 3a1f069..5df897a 100644 --- a/uvplot/uvtable.py +++ b/uvplot/uvtable.py @@ -8,7 +8,9 @@ import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec -__all__ = ["UVTable"] +from .constants import clight + +__all__ = ["UVTable", "export_uvtable"] class UVTable(object): @@ -383,3 +385,143 @@ def plot(self, fig_filename=None, color='k', linestyle='.', label='', plt.savefig(fig_filename) else: return axes + + +def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, + dualpol=True, fmt='%10.6e', keepms=False, verbose=False): + """ + Export visibilities from an MS Table to a uvtable. + Currently the only uvtable format supported is ASCII. + + Typicall call signature:: + + export_uvtable('uvtable_new.txt', tb, split=split, + split_args={'vis': 'sample.ms', datacolumn': 'data', spw='0,1}, verbose=True)" + + Parameters + ---------- + uvtable_filename : str + Filename of the output uvtable, e.g. "uvtable.txt" + tb : CASA tb object + vis : str, optional + MS Table filename, e.g. mstable.ms + split_args : dict, optional + Default is None. If provided, perform a split before exporting the uvtable. + The split_args dictionary is passed to the CASA::split task. + The CASA::split task must be provided in input as split. + split : optional + CASA split task + dualpol : bool, optional + If the MS Table contains dual polarisation data. Default is True. + fmt : str, optional + Format of the output ASCII uvtable. + keepms : bool, optional + If True, keeps the outputvis created by the split command. + verbose: bool, optional + If True, print informative messages. + + Note + ---- + By default, only the 1st spectral window (spw) is exported. + To export all the spws in an MS table provide split_args, e.g.:: + + split_args = {'vis': 'input.ms', 'outputvis':'input_tmp.ms', spw:'*'} + + """ + if vis != "": + MStb_name = vis + + if split_args: + if split is None: + raise RuntimeError("Missing split parameter: provide the CASA split object in input. See typical call signature.") + if vis != "" and vis != split_args['vis']: + # raise RuntimeError("extract_uvtable: either provide `vis` or `split_args` as input parameters, not both.") + raise RuntimeError( + "extract_uvtable: vis={} input parameter doesn't match with split_args['vis']={}".format( + vis, split_args['vis'])) + + if not 'outputvis' in split_args.keys(): + split_args.update(outputvis='mstable_tmp.ms'.encode()) + + MStb_name = split_args['outputvis'] + + if verbose: + print("Applying split. Creating temporary MS table {} from original MS table {}".format(MStb_name, split_args['vis'])) + + split(**split_args) + else: + if vis == "": + raise RuntimeError("Missing vis parameter: provide a valid MS table filename.") + + if verbose: print("Reading {}".format(MStb_name)) + + tb.open(MStb_name) + + # get coordinates + uvw = tb.getcol("UVW".encode()) + u, v, w = [uvw[i, :] for i in range(3)] + + # get weights + weights_orig = tb.getcol("WEIGHT".encode()) + + # get visibilities + tb_columns = tb.colnames() + if "CORRECTED_DATA" in tb_columns: + if verbose: print("Reading CORRECTED_DATA column") + data = tb.getcol("CORRECTED_DATA".encode()) + else: + if verbose: print("CORRECTED_DATA column not found. Reading DATA column") + data = tb.getcol("DATA".encode()) + + spw = tb.getcol("DATA_DESC_ID".encode()) + nspw = len(np.unique(spw)) + if nspw > 1: + if split_args is None or (split_args is not None and 'spw' not in split_args.keys()): + print( + "Warning: the MS table {} has {} spectral windows. By default all of them are exported." + " To choose which spws to export, provide split_args with the spw parameter.".format( + MStb_name, nspw)) + + ispw = 0 + + if dualpol: + # dual polarisation: extract the polarised visibilities and weights + V_XX = data[0, ispw, :] + V_YY = data[1, ispw, :] + weights_XX = weights_orig[0, :] + weights_YY = weights_orig[1, :] + + # compute weighted average of the visibilities and weights + V = (V_XX * weights_XX + V_YY * weights_YY) / (weights_XX + weights_YY) + weights = 0.5 * (weights_XX + weights_YY) + + else: + # single polarisation + V = data[0, ispw, :] + weigths = weights_orig + + spw_path = tb.getkeyword('SPECTRAL_WINDOW'.encode()).split()[-1] + tb.close() + + # get the mean observing frequency + tb.open(spw_path) + freqs = tb.getcol('CHAN_FREQ'.encode()) # [GHz] + tb.close() + + wle = clight / freqs.mean() # [m] + + # export to file as ascii + if verbose: print( + "Exporting visibilities to {}".format(uvtable_filename)) + np.savetxt(uvtable_filename, + np.column_stack([u, v, V.real, V.imag, weights]), fmt=fmt.encode(), + delimiter='\t', + header='Extracted from {}.\nwavelength[m] = {}\nColumns:\tu[m]\tv[m]\tRe(V)[Jy]\tIm(V)[Jy]\tweight'.format( + MStb_name, wle)) + + if split_args: + if not keepms: + from subprocess import call + if verbose: + print("Removing temporary MS table {}".format(split_args['outputvis'])) + call("rm -rf {}".format(split_args['outputvis']), shell=True) \ No newline at end of file From bd44c68ba502698a78cd901e51d1bbe243f37155 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:03:17 +0100 Subject: [PATCH 05/12] [uvtable] Better docs of export_uvtable --- uvplot/uvtable.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/uvplot/uvtable.py b/uvplot/uvtable.py index 5df897a..cd42cbd 100644 --- a/uvplot/uvtable.py +++ b/uvplot/uvtable.py @@ -390,7 +390,7 @@ def plot(self, fig_filename=None, color='k', linestyle='.', label='', def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, dualpol=True, fmt='%10.6e', keepms=False, verbose=False): """ - Export visibilities from an MS Table to a uvtable. + Export visibilities from an MS Table to a uvtable. Requires execution inside CASA. Currently the only uvtable format supported is ASCII. Typicall call signature:: @@ -427,6 +427,20 @@ def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, split_args = {'vis': 'input.ms', 'outputvis':'input_tmp.ms', spw:'*'} + Example + ------- + To extract all the visibilities from an MS table:: + + casa --nologger --nogui -c + "from uvplot import export_uvtable; export_uvtable('uvtable.txt', tb, vis='sample.ms')" + + To extract the visibilities in spectral windows 0 and 2 pass the `split_args` + parameter and the CASA `split` task + + casa --nologger --nogui -c + "from uvplot import export_uvtable; export_uvtable('uvtable.txt', tb, split=split, + split_args={'vis': 'sample.ms' , 'datacolumn': 'data', 'spw':'0,2'})" + """ if vis != "": MStb_name = vis From eeaaebb6c64d923cb5077ee8b5c142ddc9618109 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:16:43 +0100 Subject: [PATCH 06/12] [setup,readme] Bump version to 0.2.0 --- README.rst | 4 +++- setup.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 0b3fc6f..e4b6cae 100644 --- a/README.rst +++ b/README.rst @@ -12,10 +12,12 @@ A simple package to make nice plots of deprojected interferometric visibilities, | -The current version should work on both Python >2.7 and >3.6 and implements the basic plotting functionality. More features are to come in the future. +The current version works on both Python >2.7 and >3.6 and implements the basic plotting functionality. More features are to come in the future. If you are interested, or have feature requests, or encounter issues, consider creating an Issue or writing me an `email `_. +What's new in **v0.2.0**: a new `export_uvtable` function to export visibilities from an MS to an ASCII table. + This is an example plot: .. image:: static/uvplot.png diff --git a/setup.py b/setup.py index f7c36b3..103d4e4 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="uvplot", - version="0.1.0", + version="0.2.0", packages=find_packages(), author="Marco Tazzari", author_email="mtazzari@ast.cam.ac.uk", From db2e67a4f7903391e9de65a271807ffc88e167c0 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:19:22 +0100 Subject: [PATCH 07/12] [readme] Add installation instructions with pip and casa-pip. --- README.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.rst b/README.rst index e4b6cae..d1afddb 100644 --- a/README.rst +++ b/README.rst @@ -18,6 +18,22 @@ If you are interested, or have feature requests, or encounter issues, consider c What's new in **v0.2.0**: a new `export_uvtable` function to export visibilities from an MS to an ASCII table. + +Installation +------------ +It can be installed with:: + + pip install git+https://github.com/mtazzari/uvplot.git + +and, inside CASA, with:: + + casa-pip install git+https://github.com/mtazzari/uvplot.git + +where `casa-pip` is a tool that can be downloaded `here `_ + + +An Example +---------- This is an example plot: .. image:: static/uvplot.png From 8ef46ba2473976fe6b4dacfb0da1d3df96da7f94 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:32:19 +0100 Subject: [PATCH 08/12] [readme] Fix wrong conflict resolution --- README.rst | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/README.rst b/README.rst index cbd7690..7e54d3e 100644 --- a/README.rst +++ b/README.rst @@ -22,6 +22,7 @@ The current version implements the basic plotting functionality. More features a If you are interested, or have feature requests, or encounter issues, consider creating an Issue or writing me an `email `_. I am happy to have your feedback! +What's new in **v0.2.0**: a new `export_uvtable` function to export visibilities from an MS to an ASCII table. Installation ------------ @@ -32,27 +33,17 @@ Installation pip install git+https://github.com/mtazzari/uvplot.git -Example -------- - -What's new in **v0.2.0**: a new `export_uvtable` function to export visibilities from an MS to an ASCII table. - - -Installation ------------- -It can be installed with:: - - pip install git+https://github.com/mtazzari/uvplot.git +and, inside CASA, with: -and, inside CASA, with:: +.. code-block :: bash casa-pip install git+https://github.com/mtazzari/uvplot.git where `casa-pip` is a tool that can be downloaded `here `_ -An Example ----------- +Example +------- This is an example plot: .. image:: static/uvplot.png From 53bdbe08e249e126941efee0776fe6b8e873d20d Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:33:59 +0100 Subject: [PATCH 09/12] [readme] Fix docs how to install in CASA --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 7e54d3e..4b3b918 100644 --- a/README.rst +++ b/README.rst @@ -33,7 +33,7 @@ Installation pip install git+https://github.com/mtazzari/uvplot.git -and, inside CASA, with: +To make **uvplot** available in CASA, run from the shell: .. code-block :: bash From 0c4fde46be47a3c957c6f5d645fcbda806dbe964 Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:55:00 +0100 Subject: [PATCH 10/12] [uvtable] Better docs for usage in CASA --- uvplot/uvtable.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/uvplot/uvtable.py b/uvplot/uvtable.py index cd42cbd..aad9b1d 100644 --- a/uvplot/uvtable.py +++ b/uvplot/uvtable.py @@ -429,17 +429,21 @@ def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, Example ------- - To extract all the visibilities from an MS table:: + From within CASA, to extract all the visibilities from an MS table:: - casa --nologger --nogui -c - "from uvplot import export_uvtable; export_uvtable('uvtable.txt', tb, vis='sample.ms')" + export_uvtable('uvtable.txt', tb, vis='sample.ms') - To extract the visibilities in spectral windows 0 and 2 pass the `split_args` - parameter and the CASA `split` task + From within CASA, to extract the visibilities in spectral windows 0 and 2 use + the `split_args` parameter and the CASA `split` task:: - casa --nologger --nogui -c - "from uvplot import export_uvtable; export_uvtable('uvtable.txt', tb, split=split, - split_args={'vis': 'sample.ms' , 'datacolumn': 'data', 'spw':'0,2'})" + export_uvtable('uvtable.txt', tb, split=split, + split_args={'vis': 'sample.ms' , 'datacolumn': 'data', 'spw':'0,2'}) + + To perform these operations without running CASA interactively:: + + casa --nologger --nogui -c "from uvplot import export_uvtable; export_uvtable(...)" + + ensuring that the strings are inside the '..' characters and not the "..." one. """ if vis != "": From 50b37526d632a546c11e7dc1b3612d0793aa62fe Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Wed, 11 Oct 2017 17:56:11 +0100 Subject: [PATCH 11/12] [readme] Add CASA version --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 4b3b918..8c086aa 100644 --- a/README.rst +++ b/README.rst @@ -39,8 +39,9 @@ To make **uvplot** available in CASA, run from the shell: casa-pip install git+https://github.com/mtazzari/uvplot.git -where `casa-pip` is a tool that can be downloaded `here `_ +where `casa-pip` is a tool that can be downloaded `here `_ . +**uvplot** has been tested on CASA versions > 4.7.0. Example ------- From 3714ce0129f183ca71c292cb8ada46f01021a1dd Mon Sep 17 00:00:00 2001 From: Marco Tazzari Date: Thu, 12 Oct 2017 14:42:45 +0100 Subject: [PATCH 12/12] [uvtable] Better docs export_uvtable Clarify need for CASA tb table object. --- uvplot/uvtable.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/uvplot/uvtable.py b/uvplot/uvtable.py index aad9b1d..02c83d4 100644 --- a/uvplot/uvtable.py +++ b/uvplot/uvtable.py @@ -402,7 +402,10 @@ def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, ---------- uvtable_filename : str Filename of the output uvtable, e.g. "uvtable.txt" - tb : CASA tb object + tb : CASA `tb` object + As tb parameter you **must** pass the tb object that is defined in the CASA shell. + Since tb cannot be accessed outside CASA, export_uvtable('uvtable.txt', tb, ...) + can be executed only inside CASA. vis : str, optional MS Table filename, e.g. mstable.ms split_args : dict, optional @@ -433,6 +436,9 @@ def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, export_uvtable('uvtable.txt', tb, vis='sample.ms') + where `tb` is the CASA tb object (to inspect it type `tb` in the CASA shell). + For more information on `tb` see ``_ + From within CASA, to extract the visibilities in spectral windows 0 and 2 use the `split_args` parameter and the CASA `split` task:: @@ -542,4 +548,4 @@ def export_uvtable(uvtable_filename, tb, vis="", split_args=None, split=None, from subprocess import call if verbose: print("Removing temporary MS table {}".format(split_args['outputvis'])) - call("rm -rf {}".format(split_args['outputvis']), shell=True) \ No newline at end of file + call("rm -rf {}".format(split_args['outputvis']), shell=True)