#!/usr/bin/env python

from os.path import join, isfile
from glob import glob
import numpy as np
import sys
import getpass
import argparse

from k2crank.run_pipeline import run

parser = argparse.ArgumentParser(description="""
                read, reduce and detrend K2 photometry""",
                usage='use "%(prog)s --help" for more information',
                formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-i', help='path to file',
    type=str, default='./')
parser.add_argument('-o', help='output directory (default: output/)',
    type=str, default='output/')
parser.add_argument('-cutoff_limit', help='parameter for aperture size (default: 1.2)',
    type=float, default=1.2)
parser.add_argument('-showfig', help='show all plots',
    type=bool, default=False)

args = parser.parse_args()

if isfile(args.i):
    inputpath = args.i
else:
    print('{} does not exist'.format(args.i))
    sys.exit()

outputpath = args.o
cutoff_limit = args.cutoff_limit
showfig = args.showfig

starname=inputpath.split('/')[-1].split('.')[0][4:-1]

print('Checking {}'.format(starname))
try:
  run(inputpath=inputpath,outputpath=outputpath,cutoff_limit=cutoff_limit,makelightcurve=True, showfig=showfig)#,campaign=14)

except Exception as inst:
  print('Module failed: {}'.format(inst))

print('-----------------------')
print('          DONE')
print('-----------------------\n')
