#!/usr/bin/env python
# coding: utf-8

from datetime import datetime, timedelta

import glob
import os
import subprocess

import smartcarb as sc

#script = 'create_2d_fields.py'
#script = 'create_2d_field_maps.py'
script = 'plume_detection.py'


os.environ['script'] = script

if script == 'create_2d_fields.py':

    start_date = datetime(2015,7,1)
    stop_date = datetime(2015,12,26)
    delta = timedelta(days=3)
    date = '2015-07-01'

    for start in sc.misc.iter_times(start_date, stop_date, delta):
        print(start)
        logfile = start.strftime('logs/2d_fields_%Y%m%d.log')

        stop = start + delta
        subprocess.call(['sbatch', '--output=%s' % logfile, 'run.job',
            date, start.strftime("%Y-%m-%d"), stop.strftime("%Y-%m-%d")]
        )

if script == 'create_2d_field_maps.py':

    start_date = datetime(2015,1,1)
    stop_date = datetime(2015,12,25)
    delta = timedelta(days=10)

    for start in sc.misc.iter_times(start_date, stop_date, delta):

        logfile = start.strftime('logs/2d_maps_%Y%m%d.log')

        filenames = []

        for dt in sc.misc.iter_times(start, start + delta):
            if dt < datetime(2015,7,1):
                path = '/scratch/snx3000/gkuhl/smartcarb/cosmo2D_2015-01/'
            else:
                path = '/scratch/snx3000/gkuhl/smartcarb/cosmo2D_2015-07/'

            filenames.append(
                    os.path.join(path, dt.strftime('cosmo_2d_%Y%m%d%H.nc'))
            )

        subprocess.call(['sbatch', '--output=%s' % logfile, 'run.job'] + filenames)


if script == 'plume_detection.py':

    for error in [0.5, 0.7, 1.0]:
        logfile = 'logs/plume_detect_0_34.log'
        subprocess.call(['sbatch', '--output=%s' % logfile, 'run.job', '0', '34', str(error)])

        logfile = 'logs/plume_detect_34_68.log'
        subprocess.call(['sbatch', '--output=%s' % logfile, 'run.job', '34', '68', str(error)])




















