#!/usr/bin/env python
# encoding: utf-8
#
# @Author: Jon Holtzman
# @Date: March 2018
# @Filename: apred
# @License: BSD 3-Clause
# @Copyright: Jon Holtzman


from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals

import argparse
import os
import sys
import subprocess
import pdb
import time
import datetime

from apogee.apred import rv
from apogee.utils import finish

if __name__ == '__main__' :

    parser = argparse.ArgumentParser(
        prog=os.path.basename(sys.argv[0]),
        description='Runs RVs')

    parser.add_argument('field', type=str, help='Plan file')
    parser.add_argument("--threads",type=int,default=4)
    parser.add_argument("--nobj",type=int,default=0)
    parser.add_argument("--nres",type=float,nargs=3,help='list of sinc widths',default=[5,4.25,3.5])
    parser.add_argument('--tweak', help='do normalization tweak?',action="store_true")
    parser.add_argument("--done")
    parser.add_argument("--host")
    parser.add_argument("--flag",type=str,default='11111')
    parser.add_argument('--save', help='save intermediate pkl files?',action="store_true")
    parser.add_argument('--clobber', help='Overwrite files?',action="store_true")
    parser.add_argument('--rvclobber', help='Overwrite RV files?',action="store_true")
    parser.add_argument('--vcclobber', help='Overwrite visitcomb files?',action="store_true")
    args=parser.parse_args()

    now = datetime.datetime.now()
    start = time.time()
    print("Start: ",now.strftime("%Y-%m-%d %H:%M:%S"))

    try:
        rv.doppler_rv(args.field,threads=args.threads,clobber=args.clobber,rvclobber=args.rvclobber,vcclobber=args.vcclobber,
                      nobj=args.nobj,tweak=args.tweak,nres=args.nres,save=args.save)
        print('rv completed successfully for {:s}'.format(args.field))
        finish.done(args.done,args.host,start)
    except:
        print('rv failed for {:s}'.format(args.field))
        finish.done(args.done,args.host,start)
        raise
