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

"""\
This scripts helps with detecting the plumes as described in D4.
"""

import matplotlib as mpl
mpl.use("Agg")

from multiprocessing import Pool
import os
import pandas
import sys
import matplotlib.pyplot as plt
import numpy as np

import dplume


#TRACER = 'CO2'
#ERROR = float(sys.argv[3])
#USE_EPF = True
#BG_TYPE = 'model'


def main(i):

    # create input filenames
    d = pandas.read_csv("data/pixels_in_Berlin_plumes_clouds.dat", sep='\t')
    fnames = d['filename'][d['XCO2'] > 0].values
    input_path = '/scratch/snx3000/gkuhl/smartcarb/level2/Sentinel_7_CO2/'
    input_fnames = [os.path.join(input_path, fn) for fn in fnames]

    # create output filenames
    if USE_EPF:
        suffix = '_%s_%02d-epf_bg-%s.dat' % (TRACER, 10*ERROR, BG_TYPE)
    else:
        suffix = '_%s_%02d_bg-%s.dat' % (TRACER, 10*ERROR, BG_TYPE)


    output_path = '/users/gkuhl/projects/smartcarb/plume_detection/results'
    output_fnames = [os.path.join(output_path, fn.replace('.nc', suffix))
            for fn in fnames]

    print(fnames[i])
    sc.plume.main(input_fnames[i], output_fnames[i], TRACER, ERROR, USE_EPF,
            cloud_thr=0.01, bg_type=BG_TYPE)



def quick_labels(fnames, output_path):
    for fn in fnames:
        print(os.path.basename(fn))
        res = sc.plume.show_all_ground_truths(fn, output_path=output_path)
        if res:
            plt.close()



if __name__ == '__main__':

    ncpu = 20

    origin = 'Berlin'
    q = 0.99
    use_s5 = False
    make_image = False
    nsat = 6
    nsamples = 20

    for ns in [25]:
        for tracer in []:#['CO2']:
            cloud_thr = 0.01
            for ssp in [0.5]:#, 0.7, 1.0]:
                args = [(fn, tracer, ssp, origin, q, ns, cloud_thr, use_s5,
                         make_image, nsamples)
                        for fn in dplume.get_level2_filenames(origin, nsat=nsat)]

                with Pool(ncpu) as pool:
                    pool.starmap(dplume.main, args)


        for tracer in ['NO2']:
            cloud_thr = 0.30

            for ssp in ['high']:#['low', 'high']:
                args = [(fn, tracer, ssp, origin, q, ns, cloud_thr, use_s5,
                         make_image, nsamples)
                        for fn in dplume.get_level2_filenames(origin, nsat=nsat)]

                with Pool(ncpu) as pool:
                    pool.starmap(dplume.main, args)

                # extract confusion matrix
                #if origin == 'Berlin':
                #    dplume.extract_table(tracer, ssp, q, ns, use_s5)
                #else:
                #    dplume.extract_detection(origin, tracer, ssp, q, ns, use_s5)


    #for ns in [1,5]:
    #    tracer = 'NO2'
    #    ssp = ''
    #    use_s5 = True
    #    cloud_thr = 0.3

    #    args = [(fn, tracer, ssp, origin, q, ns, cloud_thr, use_s5, make_image)
    #            for fn in dplume.get_level2_filenames(origin, nsat=nsat)]

    #    #with Pool(ncpu) as pool:
    #    #    pool.starmap(dplume.main, args)

    #    #extract confusion matrix
    #    if origin == 'Berlin':
    #        dplume.extract_table(tracer, ssp, q, ns, use_s5)


