In [40]:
import os, re, datetime, sys
from subprocess import call, check_output

run_in_ipython = 'IPython' in sys.modules

if run_in_ipython:
    basedir ='/mnt/volume_1/DynamicTopography/Models/'
else:
    basedir = os.path.dirname(os.path.abspath(__file__))+'/'
    
    
model_names = [
    'gld106',
    'gld107',
    'gld108', 
    'gld112',
    'gld115',
    'gld118',
    'gld119',
    'gld123',
    'gld130',
    'gld134',
    'gld136',
    'gld162',
    'gld179',
    'gld214',
    'gld215',
    'gld216',
    'gld217',
    'gld224',
    'gld225',
    'gld226',
    'gld230',
    'gld89',
    'gld91',
    'gld95',
    'gld98',
    'gld241',
    ]

if not run_in_ipython and  len(sys.argv) > 2:
    model_names =  sys.argv[1:]

plate_frame_folder = 'PlateFrame'
mantle_frame_folder = 'MantleFrame'

model_names = get_models(basedir)

def main():
    for model_name in model_names:
        model_root = basedir+model_name
        if not os.path.isdir(model_root):
            print 'Unable to find folder: '+ model_root

        plate_frame_root = basedir+model_name+'/'+plate_frame_folder
        if not os.path.isdir(plate_frame_root):
            print 'Unable to find folder: '+ plate_frame_root

        mantle_frame_root = basedir+model_name+'/'+mantle_frame_folder
        if not os.path.isdir(mantle_frame_root):
            print 'Unable to find folder: '+ mantle_frame_root

        time_list = []

        for f in os.listdir(plate_frame_root):
            if not f.endswith(".nc"):
                #print f
                continue
            time_list.append(get_time(f, model_name))

        time_list = sorted(set(time_list),key=lambda x: float(x))

        print time_list
        
        lines = ['#!/bin/bash\n', '#'+str(datetime.datetime.now())+'\n', '#'+str(time_list)+'\n']
        fo = open(model_root+"/rename.sh", "w+")

        lines.append('#Plate Frame: .nc \n')
        lines += generate_cmd_lines(plate_frame_root, 'nc', time_list, model_name)

        lines.append('#Plate Frame: .jpg \n')
        lines += generate_cmd_lines(plate_frame_root, 'jpg', time_list, model_name)


        lines.append('#Mantle Frame \n')
        lines += generate_cmd_lines(mantle_frame_root, 'jpg', time_list, model_name, True)

        fo.writelines(lines)
        fo.close()
        check_output(["chmod","+x", model_root+"/rename.sh"])
        check_output([model_root+"/rename.sh"])
    print str(datetime.datetime.now()) +': done!'

def get_time(f,model_name):
    f = re.sub(model_name, '', f)#remove model name
    #print f, model_name
    numbers = re.findall(r'\d*\.\d+|\d+',f)
    #print "numbers: "
    #print numbers
    if len(numbers) == 1:
        return numbers[0]
    elif len(numbers) > 1:
        return numbers[-1]
    else:
        print 'No time found in the file name: ' + f
        return None

    
def get_models(basedir):
    ret = []
    for d in os.listdir(basedir):
        if (os.path.isdir(basedir+d) and 
            os.path.isdir(basedir+d+'/PlateFrame') and 
            os.path.isdir(basedir+d+'/MantleFrame')):
            
            ret.append(d)
    return ret
    
    
def generate_cmd_lines(folder, ext, time_list, model_name, mantle_frame=False):
    lines_ = []
    for f in os.listdir(folder):
        time = get_time(f,model_name)
        if time not in time_list:
            print 'Invalid time: ' + str(time), model_name, f
        if 'Masked' in f:
            new_name = 'Masked_' + time + '.' + ext
        else:
            new_name = time+'.'+ext
            
        if f.endswith('.'+ext) and f != new_name:
            lines_.append(
                (time,
                 'mv '+folder+'/'+f+' '+ folder+'/'+new_name+'\n'))
        else:
            continue
       
    lines_.sort(key=lambda tup: tup[0])    
    return [x[1] for x in lines_]

if __name__ == "__main__":
    main()
    #print get_models(basedir)


['0', '8', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '159', '169', '179', '190', '200', '210', '220', '230']
['0', '6', '9', '14', '19', '30', '40', '45', '50', '60', '71', '74', '79', '90', '100', '104', '111', '119', '125', '129', '140', '150', '159', '169', '181', '190', '199', '211', '222', '230']
['0', '4', '11', '15', '19', '31', '40', '46', '51', '57', '60', '70', '80', '91', '97', '101', '106', '111', '119', '130', '141', '151', '161', '170', '181', '191', '200']
['0', '2', '6', '10', '16', '21', '30', '41', '50', '60', '69', '80', '89', '100', '110', '121', '131', '141', '150']
['0', '3', '7', '10', '15', '19', '31', '39', '51']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120.00', '130', '140.00']
['0', '8', '18', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '159', '169', '180', '190', '200', '210', '220', '230']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
['0', '6', '10', '14', '19', '31', '40', '50', '60', '70', '81', '90', '101', '111', '119', '131', '140', '150']
['0', '9', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '160', '170', '180', '190', '200', '210', '220', '230']
['0', '5', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55', '60', '65', '70', '75', '80', '85', '90', '95', '100', '105', '110', '115', '120', '125', '130', '135', '140', '145', '150', '155', '160', '165', '170', '175', '180', '185', '190', '195', '200']
['0', '4', '8', '15', '20', '29', '40', '49', '61', '70', '80', '89', '99', '105', '109', '121', '131', '141', '150', '159', '172', '179', '191', '201', '211', '220', '230']
['0', '5', '8', '12', '15', '20', '29', '40', '49', '61', '70', '80', '91', '99', '111', '120', '130', '141', '150', '158', '171', '179', '190', '201', '211', '220', '230']
['0', '9', '19', '30', '39', '50', '60', '70', '80', '90', '95', '100', '106', '110', '121', '130', '141', '151', '161', '170', '181', '190', '201', '211', '219', '230']
['0', '5', '9', '11']
['0', '4', '9', '14', '19', '24', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140', '150']
['0', '5', '10', '15', '20', '30', '40', '45', '50', '60', '70', '75', '80', '90', '100', '105', '110', '120', '125', '130', '140', '150', '160', '170', '180', '190', '200', '210', '220', '230']
['0', '6', '9', '15', '20', '29', '40', '44', '49', '59', '70', '75', '79', '90', '101', '105', '109', '120', '126', '130', '141', '151', '159', '170', '180', '189', '200', '221', '230']
['0', '4', '9', '14', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '160', '170', '180', '190', '200', '210', '220', '230']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90']
Invalid time: 4 M1 .gmtcommands4
Invalid time: 4 M1 .gmtcommands4
['0', '4', '10', '14', '21', '30', '41', '46', '50', '60', '69', '75', '81', '91', '100', '106', '111', '121', '130', '141', '150', '160', '170', '181', '190', '199', '210', '220', '230']
['0', '6', '10', '15', '21', '29', '41', '49', '61', '70', '79', '91', '101', '111', '119', '131', '139', '150', '159', '171', '179', '190', '201', '220', '230']
['0', '4', '9', '14', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '160', '170', '180', '190', '200', '210', '220', '230']
['0', '9', '19', '29', '39', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140', '150']
['0', '9', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '159', '170', '180', '190', '200', '210', '220', '230']
['0', '5', '9', '15', '20', '29', '41', '49', '59', '70', '80', '89', '99', '110', '121', '131', '141', '150', '159', '172', '179', '191', '202', '211', '220', '230']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
['0', '10', '20', '30', '40', '50']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
['0', '11', '20', '30', '41', '49', '61', '70', '81', '90', '100', '111', '119', '130', '139', '151', '161', '170', '180', '189', '200']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
['0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '110', '120', '130', '140']
['0', '9', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '159', '169', '180', '190', '200', '210', '220', '230']
['0', '3', '8', '13', '18', '28', '38', '48', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '159', '169', '179', '190', '200', '210', '220', '230']
['0', '10']
['0', '5', '10', '15', '20', '30', '40', '45', '50', '60', '70', '75', '80', '90', '100', '105', '109', '121', '125', '130', '141', '151', '160', '170', '179', '189', '201', '209', '221', '230']
['0', '8', '18', '28', '38', '48', '58', '68', '78', '88', '98', '109', '119', '129', '139', '149', '159', '169', '179', '189', '199', '209', '219', '230', '240', '250', '260', '270', '280', '290', '300']
['0', '5', '10', '15', '20', '30', '40', '45', '50', '60', '71', '75', '79', '90', '100', '104', '111', '120', '125', '131', '140', '150', '161', '170', '179', '191', '200', '211', '219', '230']
['0', '4', '9', '15', '20', '31', '40', '51', '61', '70', '79', '90', '100', '111', '119', '131', '139', '150', '159', '171', '178', '191', '201', '211', '220', '230']
['0', '4', '11', '16', '20', '30', '41', '50', '60', '70', '79', '89', '99', '109', '120', '130', '141', '150']
['0', '9', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '120', '130', '140', '150', '160', '170', '180', '190', '200']
['0', '9', '19', '30', '40', '50', '60', '70', '80', '90', '100']
['0', '6', '9', '15', '20', '30', '40', '44', '51', '60', '70', '75', '81', '83', '87', '91', '97', '100', '105', '109', '118', '121', '126', '130', '135', '140', '143', '150', '160', '170', '179', '191', '201', '211', '219', '230']
['0', '4', '9', '14', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '160', '170', '180', '190', '200', '210', '220', '230']
['0', '3', '7', '11', '15', '20', '31', '40', '51', '60', '71', '80', '89', '100', '110', '120', '130', '141', '150']
['0', '4', '11', '14', '21', '30', '40', '49', '61', '69', '81', '89', '100', '110', '121', '129', '139', '150', '161', '170', '180', '191', '200', '210', '221', '230']
['0', '9', '19', '29', '39', '49', '59', '69', '79', '89', '99', '109', '119', '129', '139', '149', '159', '169', '180', '190', '200', '210', '220', '230']
['0', '5', '9', '11', '15', '21', '29', '39']
['0', '2', '6', '10', '16', '21', '30', '41', '50', '60', '69', '80', '89', '100', '110', '121', '131', '141', '150']
['0', '9', '14', '19', '29', '39', '49', '59', '70', '80', '90', '100', '110', '120', '130', '140', '150']
2017-03-15 07:02:42.521987: done!

In [2]:
print 'test'


test

In [ ]: