#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Track eddy with Identification file produce with EddyIdentification
"""
from py_eddy_tracker import EddyParser
from py_eddy_tracker.tracking import Correspondances
from os.path import exists, dirname
from os import mkdir
import logging
import datetime as dt

logger = logging.getLogger("pet")


def usage():
    """Usage
    """
    # Run using:
    parser = EddyParser(
        "Tool to use identification step to compute tracking")
    parser.add_argument('nc_file',
                        nargs='+',
                        help='File of correspondances to reload link '
                             'without tracking computation')
    parser.add_argument('path_out', help='Path, where to write file')
    return parser.parse_args()


if __name__ == '__main__':
    CONFIG = usage()

    # Create output directory
    if not exists(dirname(CONFIG.path_out)):
        mkdir(dirname(CONFIG.path_out))

    START_TIME = dt.datetime.now()
    CORRESPONDANCES = Correspondances.load(CONFIG.nc_file[0])
    logger.info('Start merging')
    for i in CONFIG.nc_file[1:]:
        CORRESPONDANCES.merge_correspondance(Correspondances.load(i))

    CORRESPONDANCES.save(CONFIG.path_out)
