
import os
import pandas
import xarray

from smartcarb import misc
from smartcarb import io


def read_overpasses(origin, nsat=None):

    path = "/project/kug/smartcarb/plume_detection/"
    filename = os.path.join(path, origin, "%sPlume.dat" % origin)
    data = pandas.read_csv(filename, sep='\t', index_col=2, parse_dates=True)

    if nsat is not None:
        data = misc.filter_dataset(data, nsat=nsat)

    return data



def get_product(tracer, ssp, q, ns, use_s5=False):
    if use_s5:
        ssp = 'S5'

    if tracer == 'CO2':
        product = '%s_%.1f_q%.3f_ns%03d' % (tracer, ssp, q, ns)
    else:
        product = '%s_%s_q%.3f_ns%03d' % (tracer, ssp, q, ns)

    return product



def read_results(tracer, ssp, ns, q=0.99, nsat=6, origin='Berlin', nmax=None):

    path = '/project/kug/smartcarb/plume_detection/%s/plume_detection' % origin
    product = get_product(tracer, ssp, q, ns)
    filename = os.path.join(path, '%s.dat' % product)

    # read and filter by constellation
    data = pandas.read_csv(filename, sep='\t', index_col=0, parse_dates=True)
    data = misc.filter_dataset(data, nsat=nsat)

    # remove plumes with less than nmax CO2 pixels
    if nmax is not None:
        if origin == 'Berlin':
            data = data[data.TP1 >= nmax]
        else:
            data = data[data.CO2_pixels >= nmax]

    # add filenames
    data['filename'] = [get_output_filename(v.satellite, v.orbit, v.lon_eq, time,
                                            tracer, ssp, ns, origin)
                        for time, v in data.iterrows()]
    return data


def output2input_filename(filename):
    sat, orbit, time, lon_eq = io.process_filename(filename)
    return io.create_filename(time, input_files='level2',
                              instrument=sat, orbit=orbit,
                              lon_eq=lon_eq)



def read_output(filename, ssp=None):
    data = xarray.open_dataset(filename)
    data.attrs['time'] = pandas.Timestamp(data.time)

    # replace CO2 and CO2 noise field with different noise scenario 
    if ssp is not None:
        if data.attrs['tracer'] == 'CO2':
            print("""\
Warning: CO2 will be overwritten but CO2 was used for plume detection.
         Derived data arrays might be inconsistent."""
            )

        l2filename = output2input_filename(filename)
        level2 = io.read_level2(l2filename, co2_noise=ssp, co2_thr=0.01)

        data['CO2'] = level2['CO2']
        data['CO2_noise'] = level2['CO2_noise']


    # add noise level (if it's missing)
    for noise in [0.5, 0.7, 1.0]:
        if '%.1f' % noise in data.CO2_noise.attrs['standard name'] \
           and 'noise_level' not in data.CO2.attrs:
            data.CO2.attrs['noise_level'] = noise

    return data


def get_output_filename(sat, orbit, lon_eq, time, tracer, ssp, ns, origin):
    product = get_product(tracer, ssp, q=0.99, ns=ns)
    path = '/project/kug/smartcarb/plume_detection/%s/plume_detection' % origin

    time = time.strftime('%Y%m%d%H')
    filename = '%s_%s_o%04d_l%04d_%s.nc' % (sat, time, orbit, lon_eq, product)
    filename = os.path.join(path, product, filename)

    return filename


def iter_level2(data, tracer, ssp, ns, q=0.99, origin='Berlin'):
    for time, v in data.iterrows():
        filename = get_output_filename(v.satellite, v.orbit, v.lon_eq, time, tracer,
                                       ssp, ns, origin)

        d = read_output()

        yield d








