#!/usr/bin/env python
# encoding: utf-8
#
# @Author: Jon Holtzman
# @Date: March 2018
# @Filename: mkgridlsf
# @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 datetime
import time
import pdb
from apogee.speclib import synth

if __name__ == '__main__' :

    parser = argparse.ArgumentParser(
        prog=os.path.basename(sys.argv[0]),
        description='Convolves a synthetic spectral grid with LSF')

    parser.add_argument('planfile', type=str, help='Plan file')
    parser.add_argument("--prefix", type=str, default=None, help='Prefix for input files (default=None)')
    parser.add_argument("--apred", type=str, default=None, help='apred version for lsf/wave (default=None)')
    parser.add_argument("--telescope", type=str, default=None, help='telescope for lsf/wave (default=None)')
    parser.add_argument("--flag",default='11111')
    parser.add_argument("--done")
    parser.add_argument("--host")
    parser.add_argument('--clobber', help='Overwrite files?',action="store_true")
    args=parser.parse_args()

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

    #if args.clobber: clobber=',/clobber'
    #else : clobber=''
    #subprocess.call(["idl","-e","speclib_mkgrid,'"+args.planfile+"'"+clobber])
    synth.mkgridlsf(args.planfile,prefix=args.prefix,apred=args.apred,telescope=args.telescope)
    now = datetime.datetime.now()
    print("End: ",now.strftime("%Y-%m-%d %H:%M:%S"))
    print("elapsed: ",time.time()-start)

    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
