create_fmask4_launch.py 3.13 KB
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys 
import os
import os.path as op
import glob
import numpy as np
import json
import csv
import shutil



def get_L1C_dir(location, date, display = False):
    '''
    Get the path of the L1C directory
    '''
    paths_configuration = json.load(open(op.join('..', 'paths_configuration.json')))
    L1C_dir = paths_configuration["global_chains_paths"]["L1C"]
    location_dir = op.join(L1C_dir, location)

    with_date = glob.glob(op.join(location_dir, 'S2*_{}*.SAFE'.format(date)))
    granule = op.join(with_date[0], 'GRANULE')
    
    final = glob.glob(op.join(granule, '*', 'IMG_DATA'))[0]
    final = op.abspath(glob.glob(op.join(final, '..'))[0])
    if display == True:
        print('----- L1C directory -----')
        print(final)
        print(date)

    return final
    



def open_csv(csv_path):
    locations = []
    clear_dates = []
    cloudy_dates = []
    with open(csv_path, 'rb') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        spamreader.next()
        for row in spamreader:
            locations.append(row[1])
            clear_dates.append(row[2])
            cloudy_dates.append(row[3])
    print(locations)
    print(clear_dates)
    print(cloudy_dates)
    return locations, clear_dates, cloudy_dates
    

    
def create_fmask_launch(locations, cloudy_dates, out_file):
    paths_configuration = json.load(open(op.join('..', 'paths_configuration.json')))
      
    commands = []
    for i in range(len(locations)):
        L1C_dir = get_L1C_dir(locations[i], cloudy_dates[i], display = False)
        commands.append("cd {}\n".format(L1C_dir))
        commands.append("pwd\n".format(L1C_dir))
        commands.append("nice /mnt/data/PROG/FMASK/application/run_Fmask_4_0.sh /mnt/data/PROG/MATLAB_Runtime/v93/\n")

        
        
        tile = paths_configuration["tile_location"][locations[i]]
        src_dir = op.join(L1C_dir, 'FMASK_DATA')
        dst_dir = op.join('/mnt/data/home/baetensl/Programs/Fmask4_output', '{}_{}_{}'.format(locations[i], tile, cloudy_dates[i]))
        commands.append("mkdir {}\n".format(dst_dir))
        #~ commands.append("mv {}{}{}\n".format(src_dir, ' ', dst_dir))
        commands.append("cp {}/*{}{}/\n".format(src_dir, ' ', dst_dir))
        commands.append("rm -rf {}\n".format(src_dir))
        commands.append("\n")        
        

    
        # write shell files to launch the PEPS download
    nb_commands = 7*4 #3 lines, 4 scenes
    
    start = 0
    end = nb_commands
    for k in range(1+int(float(len(commands))/nb_commands)):
        outf = open('{}_{}.sh'.format(out_file, k), 'w')
        for line in commands[start:end]:
            outf.write(line)
        outf.close()
        start += nb_commands
        end += nb_commands     



def main():    
    csv_path = '/mnt/data/home/baetensl/clouds_detection_git/Various_data/all_scenes.csv'
    locations, clear_dates, cloudy_dates = open_csv(csv_path)
    out_file = '/mnt/data/home/baetensl/clouds_detection_git/Tools/fmask_launch/fmask_launch'
    
    create_fmask_launch(locations, cloudy_dates, out_file)



    
if __name__ == '__main__':
    main()