# -*- coding: utf-8 -*-
import os
from datetime import datetime
# from track_functions import get_roms, get_timcom
from track_computation import get_model

def cal_trajectory(lat, lon, track_way, track_method, dataset_type, start, end, archive_path, depth, radius, test=False, algorithm='bgigps'):
    st_run_time = datetime.now() # Caculate execution time with en_run_time

    points = {'lats':[],'lons':[], 'time': []}  # collect all points we've gained

    print 'You added point: %f, %f' % (lat,lon)
    print 'track way: '+track_way
    print 'track method: '+track_method
    print 'h-criteria: %f' % depth
    print 'r-criteria: %f' % radius
        
    __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
    try:
        if dataset_type == 'roms':
            if test:
                inputfile = os.path.join(__location__, archive_path+'ROMS_72hr.nc')
            else:
                # inputfile = os.path.join(__location__, 'ROMS_72hr.nc')
                inputfile = '/cluster/TIMCOM/DATA/forecast/partaicle_tracking/' + archive_path + '/ROMS_72hr.nc'
            print 'roms: '+inputfile
        elif dataset_type == 'timcom':
            if test:
                inputfile = os.path.join(__location__, archive_path+'TIMCOM_72hr.nc')
            else:
                # inputfile = os.path.join(__location__, 'TIMCOM_72hr.nc')
                inputfile = '/cluster/TIMCOM/DATA/forecast/partaicle_tracking/' + archive_path + '/TIMCOM_72hr.nc'
            print 'timcom: '+inputfile
        else:
            raise Exception('No such dataset type.')
        
        model = get_model(inputfile)
        if algorithm == 'bgigps':
            point = model.get_track_nbi_gps(lon,lat,depth, radius,track_way, start, end)
        elif algorithm == 'idwutm':
            point = model.get_track_idw_utm(lon,lat,depth, radius, track_method, track_way, start, end)
        points['lons'].extend(point['lon'])
        points['lats'].extend(point['lat'])
        points['time'].extend(point['time'])
        print 'Points quantity: ',len(points['lons'])
    except Exception as e:
        message = e.args[0]["message"]
        raise Exception(message)

    en_run_time = datetime.now()
    print 'Take '+str(en_run_time-st_run_time)+' running the code. End at '+str(en_run_time)

    return {'lats': points['lats'], 'lons': points['lons'], 'time': points['time']}