
'''
Split the HI Arecibo mask for 14B-088 into individual
channels using the undilated mask for certain channels.

These channels began to diverge with the new mask, but worked fine
with the old one.

Requires the results csv produced by channel_clean_results.csv
'''

from astropy.table import Table
import numpy as np

from casa_tools import image_split_by_channel


# Load the results csv file
t = Table.read("/home/eric/Dropbox/code_development/VLA_Lband/14B-088/HI/channel_clean_results.csv")

bad_chans = []

for i, row in enumerate(t):
    if row['Reached Threshold'] == "True":
        continue
    if np.isnan(row['Max Residual']):
        continue

    chan = int(row["Name"].split("/")[-2].split("_")[-1])
    bad_chans.append(chan-670)

print "Splitting now..."

mask = "M33_14B-088_HI_mask.image"
output_dir = "old_mask_channels/"

nchan = 1

for start_chan in bad_chans:
    print start_chan
    image_split_by_channel(mask, nchan=nchan, start=start_chan,
                           output_dir=output_dir)

@@ -263,16 +263,35 @@
 sv%0A'''%0A%0A
+import numpy as np%0A
 from ast
@@ -321,27 +321,18 @@
 %0Aimport 
-numpy as np
+os
 %0A%0Afrom c
@@ -371,16 +371,43 @@
 channel%0A
+from tasks import rmtables%0A
 %0A%0A# Load
@@ -501,16 +501,24 @@
 -088/HI/
+imaging/
 channel_
@@ -534,24 +534,165 @@
 ults
-.csv%22)%0A%0Abad_chan
+_1016_4.4.csv%22)%0A%0A# Do you want to delete the image products on Jasper, and copy the changed%0A# mask?%0Areplace_mask_and_clean = True%0A%0Abad_chans = %5B%5D%0Abad_chans_m
 s = 
@@ -926,13 +926,45 @@
 chan
--
+ - 
 670)
+%0A    bad_chans_ms.append(chan)
 %0A%0Apr
@@ -991,18 +991,112 @@
 .%22%0A%0Amask
- =
+_path = os.path.expanduser(%22~/Data_3/M33/Arecibo/14B-088_items/%22)%0Amask = os.path.join(mask_path,
  %22M33_14
@@ -1115,16 +1115,17 @@
 k.image%22
+)
 %0Aoutput_
@@ -1129,16 +1129,40 @@
 ut_dir =
+ os.path.join(mask_path,
  %22old_ma
@@ -1174,16 +1174,17 @@
 annels/%22
+)
 %0A%0Anchan 
@@ -1352,8 +1352,1123 @@
 ut_dir)%0A
+%0Anp.save(%22bad_chans.npy%22, np.array(bad_chans_ms))%0A%0Aif replace_mask_and_clean:%0A%0A    import shutil%0A%0A    if not os.path.exists(os.path.expanduser(%22~/Jasper/m33%22)):%0A        raise Exception(%22Jasper doesn't seem to be mounted.%22)%0A%0A    data_path = os.path.expanduser(%22~/Jasper/m33/14B-088/single_channels%22)%0A%0A    for chan, chan_mask in zip(bad_chans_ms, bad_chans):%0A        print(%22Channel %7B%7D%22.format(chan))%0A        channel_path = os.path.join(data_path, %22channel_%7B%7D%22.format(chan))%0A%0A        # Remove old clean outputs%0A        rmtables(os.path.join(channel_path,%0A                              %2214B-088_HI_LSRK.ms.contsub_channel_%7B%7D.clean.*%22.format(chan)))%0A%0A        # log files, job outputs%0A        os.system(%22rm %7B0%7D/*.log %7B0%7D/*.last, %7B0%7D/*.sub.*%22.format(channel_path))%0A%0A        # Now delete the old mask and copy over the new one.%0A        mask_name = os.path.join(channel_path,%0A                                 %22M33_14B-088_HI_mask_channel_%7B%7D.image%22.format(chan_mask))%0A        rmtables(mask_name)%0A%0A        new_mask_name = os.path.join(output_dir, os.path.basename(mask_name))%0A        shutil.copytree(new_mask_name, mask_name)%0A

Update for failures of new clean