#!/usr/bin/env python

import argparse
import numpy as np
import os
import sys

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

parser.add_argument('file', type=str, help='file name')
parser.add_argument('--rot', help='apStar file format?',action="store_true")
parser.add_argument('--elem', help='add fake holes?',action="store_true")
args=parser.parse_args()

opt=''
if args.rot : opt+=' --rot '
if args.elem : opt+=' --elem '
n=16
npix=int(np.ceil(8575./n))
fitfrac=0.75
order=4

for i in range(n) :
    suffix='_{:d}'.format(i+1)
    out='nn'+suffix
    f=open(out,'w')
    f.write('#!/bin/csh\n')
    f.write('#SBATCH --account=sdss-kp\n')
    f.write('#SBATCH --partition=sdss-kp\n')
    f.write('#SBATCH --time=24:00:00\n')
    f.write('#SBATCH --ntasks=16\n')
    f.write('#SBATCH --nodes=1\n')
    f.write('#SBATCH -o '+out+'.out\n')
    f.write('#SBATCH -e '+out+'.out\n')
    #f.write('cd /uufs/chpc.utah.edu/common/home/sdss06/apogeework/apogee/spectro/speclib/synth/turbospec/marcs/nn/'+args.file+'\n')
    f.write('cd '+os.getcwd()+'\n')

    f.write('nn {:s} --pixels {:d} {:d} 1 --suffix {:s} --fitfrac {:f} --order {:d} {:s}\n'.format(
            args.file, i*npix,np.min([8575,(i+1)*npix]),suffix,fitfrac,order,opt))

    f.write('wait\n')
    f.write('echo DONE\n')
    f.close()

