
'''
Generate a pbs script for job submission, submit the job, be happy
'''

import glob
import os
import shutil
import time


def return_template(ms_name, model_name, mask_name):

    template = \
        '''
        #!/bin/bash

        #PBS -S /bin/bash
        #PBS -l pmem=2000m
        #PBS -l feature=X5675
        #PBS -l nodes=1:ppn=12
        #PBS -l walltime=72:00:00
        #PBS -m bea
        #PBS -M koch.eric.w@gmail.com
        #PBS -l epilogue=/home/ekoch/code_repos/simscript/epilogue.sh

        source /home/ekoch/.bashrc

        cd /home/ekoch/m33/14B-088/

        echo "Starting at: `date`"
        casa -c /home/ekoch/code_repos/VLA_Lband/14B-088/HI/HI_channel_clean.py X1 X2 X3
        echo "Exited with code $? at: `date`"
        '''

    template = template.replace("X1", ms_name)
    template = template.replace("X2", model_name)
    template = template.replace("X3", mask_name)

    return template

# Set the directory to look in.
ms_channel = "/home/ekoch/m33/14B-088/channel_ms/"
model_channels = "/home/ekoch/m33/14B-088/model_channels/M33_14B-088_HI_model_channel_"
mask_channels = "/home/ekoch/m33/14B-088/mask_channels/M33_14B-088_HI_mask_channel_"
output_direc = "/home/ekoch/m33/14B-088/single_channels/"

# Use mask and model? Disable when continuing to clean.
use_mask_model = True

while True:
    channel_ms = glob.glob(os.path.join(ms_channel, "*.ms"))

    # If there aren't any more split ms in the path, break and exit
    if len(channel_ms) == 0:
        break

    # Now loop through the existing channel ms
    for chan in channel_ms:
        chan_num = int(chan.split("_")[-1][:-3])

        channel_direc = os.path.join(output_direc, "channel_"+str(chan_num))

        # Check if that channel has been imaged already
        if os.path.isdir(channel_direc):
            print("Already imaged "+str(chan_num)+". Skipping")
            continue

        os.mkdir(channel_direc)
        shutil.move(chan, channel_direc)
        shutil.move(model_channels+str(chan_num)+".image", channel_direc)
        shutil.move(mask_channels+str(chan_num)+".image", channel_direc)

        chan_ms = os.path.join(channel_direc, chan.split("/")[-1])

        model_name = os.path.join(channel_direc,
                                  "M33_14B-088_HI_model_channel_"
                                  + str(chan_num) + ".image")
        mask_name = os.path.join(channel_direc,
                                 "M33_14B-088_HI_mask_channel_"
                                 + str(chan_num) + ".image")

        chan_template = return_template(chan_ms, model_name, mask_name)

        # Write to file
        sub_file = os.path.join(channel_direc, "channel_"+str(chan_num)+".sub")
        with open(sub_file, 'w') as f:
            f.write(chan_template)

        # Now submit!
        os.system("qsub " + sub_file)

    # Wait an hour, then check again for new channel ms
    time.sleep(3600)

@@ -203,24 +203,16 @@
     '''%0A
-        
 #!/bin/b
@@ -216,24 +216,16 @@
 n/bash%0A%0A
-        
 #PBS -S 
@@ -234,24 +234,16 @@
 in/bash%0A
-        
 #PBS -l 
@@ -253,24 +253,16 @@
 m=2000m%0A
-        
 #PBS -l 
@@ -275,24 +275,16 @@
 e=X5675%0A
-        
 #PBS -l 
@@ -298,24 +298,16 @@
 :ppn=12%0A
-        
 #PBS -l 
@@ -324,24 +324,16 @@
 2:00:00%0A
-        
 #PBS -m 
@@ -336,24 +336,16 @@
  -m bea%0A
-        
 #PBS -M 
@@ -366,24 +366,16 @@
 ail.com%0A
-        
 #PBS -l 
@@ -429,24 +429,16 @@
 gue.sh%0A%0A
-        
 source /
@@ -457,24 +457,16 @@
 bashrc%0A%0A
-        
 cd /home
@@ -486,24 +486,16 @@
 B-088/%0A%0A
-        
 echo %22St
@@ -513,24 +513,16 @@
 %60date%60%22%0A
-        
 casa -c 
@@ -594,24 +594,16 @@
 1 X2 X3%0A
-        
 echo %22Ex
@@ -645,16 +645,49 @@
    '''%0A%0A
+    template = template.strip()%0A%0A
     temp

Fix spacing issues