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

# driver routine to do RBF filling of synthetic grids via rbf.fill

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 datetime
import time
import pdb

import matplotlib
matplotlib.use('Agg')

from apogee.speclib import rbf

if __name__ == '__main__' :

    # command line arguments
    parser = argparse.ArgumentParser(
        prog=os.path.basename(sys.argv[0]),
        description='Fills holes in a synthetic spectral grid with RBF interpolation')
    parser.add_argument('planfile', type=str, help='Plan file')
    parser.add_argument('--dir', type=str, help='directory')
    parser.add_argument('--grid', type=str, help='grid')
    parser.add_argument('--out', type=str, help='output prefix', default='rbf_')
    parser.add_argument('--threads', type=int, help='number of threads', default=30)
    parser.add_argument('--r0', type=float, help='r0 value', default=1.0)
    parser.add_argument('--apstar', help='apStar file format?',action="store_true")
    parser.add_argument('--fakehole', help='add fake holes?',action="store_true")
    parser.add_argument('--nofill', help='add fake holes?',action="store_true")
    parser.add_argument('--cmrange', type=float, nargs='+', help='list of [C/M]')
    parser.add_argument('--nmrange', type=float, nargs='+', help='list of [N/M]')
    parser.add_argument('--vtrange', type=float, nargs='+', help='list of vmicro')
    parser.add_argument('--digits', type=int, default=2, help='digits for file names')
    parser.add_argument('--cmnear', help='use nearest [C/M] per C/O issue',action="store_true")
    parser.add_argument("--flag",default='11111')
    parser.add_argument("--done")
    parser.add_argument("--host")
    args=parser.parse_args()

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

    if not args.nofill :
        rbf.fill(args.planfile,grid=args.grid,out=args.out,dir=args.dir,r0=args.r0,apstar=args.apstar,fakehole=args.fakehole,threads=args.threads,
                 cmrange=args.cmrange,nmrange=args.nmrange,vtrange=args.vtrange,digits=args.digits)
    rbf.mergeholes(args.planfile,grid=args.grid,out=args.out,dir=args.dir,apstar=args.apstar,
                   cmrange=args.cmrange,nmrange=args.nmrange,vtrange=args.vtrange,digits=args.digits)
    # if we're doing fake holes, generate plots for results
    if args.fakehole :
        rbf.comp(args.planfile,out=args.out,grid=args.grid,dir=args.dir,fakehole=True,hard=True,
                 cmrange=args.cmrange,nmrange=args.nmrange,vtrange=args.vtrange,digits=args.digits)

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

    # handling of batch queue if requested
    if args.done is not None :
        subprocess.call(['setdone',args.done])
        try: 
            subprocess.call(['setdone',done])
        except: pass
        print('host', args.host)
        if args.host is not None :
            try: os.remove(args.done+'.'+args.host)
            except: pass
