copy_to_local4sklearn.py 2.51 KB
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys 
import os
import os.path as op
import glob
import shutil
import json
import datetime

def list_available_dirs(algo):
	if algo == 'alcd':
		all_dirs = glob.glob(op.join('/mnt/data/home/baetensl/clouds_detection_git/Data_ALCD', '*_*'))
	elif algo == 'pcc':
		all_dirs = glob.glob(op.join('/mnt/data/home/baetensl/clouds_detection_git/Data_PCC', '*_*'))		
	
	return all_dirs


def copy_scene_to_tmp(scene_dir):
    print(scene_dir)
    scene_name = op.basename(scene_dir)
    files_to_copy = ['In_data/Image/*_bands.tif', 'In_data/Image/*bands_bands.txt',
    'Samples/training_samples_extracted.sqlite', 'Out/colorized_classif.png']
    print(scene_dir)
    
    files_paths = [glob.glob(op.join(scene_dir, f))[0] for f in files_to_copy]
    #~ files_paths = []
    #~ for f in files_to_copy:
        #~ print(f)
        #~ files_paths.append(glob.glob(op.join(scene_dir, f))[0])
        
        
    files_names = [op.basename(f) for f in files_paths]
    
    main_dst_dir = '/home/baetensl/Documents/ALCD_data'
    main_dst_dir = '/mnt/data/home/baetensl/clouds_detection_git/Tools/tmp/ALCD_data'
    dst_dir = op.join(main_dst_dir, scene_name, 'OTB')
    
    commands = []
    commands.append('mkdir -p {} \n'.format(dst_dir))
    for i, f in enumerate(files_paths):
        src = f
        dst = op.join(dst_dir, files_names[i])
        c = 'scp baetensl@s2calc.cesbio.cnes.fr://{} {} \n'.format(src, dst)
        c = 'cp -r {} {} \n'.format(src, dst)
        commands.append(c)
    commands.append('\n')
        
    
    print(commands)
    return commands
    
def copy2local():

    src_dir = '/mnt/data/home/baetensl/clouds_detection_git/Tools/tmp/ALCD_data'
    dst_dir = '/home/baetensl/Documents/'

    commands = []
    #~ commands.append('mkdir -p {} \n'.format(dst_dir))
    commands.append('scp -rp baetensl@s2calc.cesbio.cnes.fr://{} {} \n'.format(src_dir, dst_dir))
    commands.append('\n')
    print(commands)
    return commands    

def write_commands2file(commands, txt_file):
    outf = open(txt_file, 'w')
    
    for line in commands:
        outf.write(line)
    outf.close()


	
def main():
    
    algo = 'alcd'
    all_dirs = list_available_dirs(algo)

    commands = []
    for d in all_dirs:
        commands.extend(copy_scene_to_tmp(d))
    
    txt_file = 'tmp/copy2local'
    write_commands2file(commands, txt_file)

    local_commands = copy2local()
    txt_file = 'tmp/copy2local_scp'
    write_commands2file(local_commands, txt_file)


	
if __name__=='__main__':
	main()