#! /usr/bin/env python3
# -*- coding: iso-8859-15 -*-
"""
Created on Sun May 24 22:56:25 2015

@author: kervella
"""

# ATTENTION: necessary to install the reportlab and pdfrw libraries
# and the svg2rlg library for python 3 version

#==============================================================================
# MAIN GRAVITY QUALITY REPORT PROGRAM    
#==============================================================================

import os
import sys
import traceback
from astropy.io import fits
import gravi_visual
from glob import glob
from optparse import OptionParser

# Add options
usage = """
        usage:  %prog
"""
parser = OptionParser(usage)
parser.add_option("-o","--overwrite", action="store_true", dest="overwrite_flag", default=False,
                  help="Overwrite existing PDFs")
parser.add_option("--minwSC", dest="minwSC", default=1.99,
                  help="Minimum wavelength (microns) for SC plots (OIFITS) [1.99]")
parser.add_option("--maxwSC", dest="maxwSC", default=2.45,
                  help="Maximum wavelength (microns) for SC plots (OIFITS) [2.45]")

(argoptions, args) = parser.parse_args()

filelist=[]
## If the user specifies a file name or wild cards ("*_0001.fits")
if len(sys.argv) > 1 :
    longnames = [f for files in sys.argv[1:] for f in glob(files)]
    filelist = [os.path.splitext(f)[0] for f in longnames]
## Processing of the full current directory
else :
    for file in os.listdir("."):
        if file.endswith(".fits"):
            filelist.append(os.path.splitext(file)[0])

filelist.sort() # process the files in alphabetical order
    
for filename in filelist:
    fullname = os.getcwd()+'/'+filename
    gravi_file=fits.open(fullname+'.fits', mode='readonly')
    header = gravi_file[0].header
    
    if 'INSTRUME' in header: # only GRAVITY standard files are taken into account
        if 'GRAV' in header['INSTRUME']:
            datatype = 'RAW' # default data type RAW

            try:        
                print('=====')

                if 'HIERARCH ESO PRO TYPE' in header:
                    datatype = header['HIERARCH ESO PRO TYPE']
                    #print(('datatype=%s'%datatype))
                
                if 'HIERARCH ESO PRO CATG' in header:
                    datacatg = header['HIERARCH ESO PRO CATG']
                    #print(('datacatg=%s'%datacatg))
                
                if 'HIERARCH ESO DPR CATG' in header:
                    dprcatg = header['HIERARCH ESO DPR CATG']
                
                #for hdu in gravi_file:
                #    if hdu.name == 'PROFILE_DATA':
                #        datacatg = 'PROFILE'

                                        
                if datatype == 'RAW':
                        print(('> Creating report for '+dprcatg+'_'+datatype+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-RAW.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        raw = gravi_visual.Rawdata(fullname+'.fits')
                        gravi_visual.produce_raw_report(raw,filename)
                
                if datatype == 'REDUCED':
                    
                    # DISP model files
                    if datacatg == 'DISP_MODEL':
                        print(('> Creating report for '+datacatg+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-DISP.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        disp = gravi_visual.Disp(fullname+'.fits')
                        gravi_visual.produce_disp_report(disp,filename)
                        
                    # DISP vis files
                    if datacatg == 'DISP_VIS':
                        print(('> Creating report for '+datacatg+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-DISPVIS.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        disp = gravi_visual.Dispvis(fullname+'.fits')
                        gravi_visual.produce_dispvis_report(disp,filename)
                        continue
                        
                    # DARK files only are considered to produce dark reports
                    if datacatg == 'DARK':
                        print(('> Creating report for '+datacatg+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-DARK.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        dark = gravi_visual.Dark(fullname+'.fits')
                        gravi_visual.produce_dark_report(dark,filename)
                
                    # SKY files have the same data content as darks
                    if "SKY" in datacatg:
                        print(('> Creating report for '+datacatg+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-SKY.pdf") or os.path.isfile(fullname+"-DUAL_SKY.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        dark = gravi_visual.Dark(fullname+'.fits')
                        gravi_visual.produce_dark_report(dark,filename)

                    # BADPIX files only are considered to produce badpix reports
                    if datacatg == 'BAD':
                        print(('> Creating report for '+datacatg+' file:  '+filename+'.fits'))
                        if os.path.isfile(fullname+"-BADPIX.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        badpix = gravi_visual.Badpix(fullname+'.fits')
                        gravi_visual.produce_badpix_report(badpix,filename)
                
                    # PROFILE files identified by the presence of the PROFILE_DATA table
                    # Works on my Mac but crashes wgvpipeline >> commented
#                    if datacatg == 'FLAT':
#                        print (' Creating report for '+datacatg+' file: '+filename+'.fits')
#                        if os.path.isfile(fullname+"-FLAT.pdf") and argoptions.overwrite_flag==False:
#                            print ('  output PDF already exists, skip...')
#                            continue
#                        prof = gravi_visual.Profile(fullname+'.fits')
#                        gravi_visual.produce_profile_report(prof,filename)
                
                    # WAVE files only are considered to produce wave reports
                    if datacatg == 'WAVE':
                        print(('> Creating report for '+datacatg+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-WAVE.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        wave = gravi_visual.Wave(fullname+'.fits')
                        gravi_visual.produce_wave_report(wave,filename)
                
                    # P2VM files only are considered to produce p2vm reports
                    if datacatg == 'P2VM':
                        print(('> Creating report for '+datacatg+' file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-P2VM.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        p2vm = gravi_visual.P2vm(fullname+'.fits')
                        gravi_visual.produce_p2vm_report(p2vm,filename)
                
                    # # Preprocessed data files (sequence of spectra)
                    # if datacatg == 'PREPROC':
                    #     print(('> Creating report for PREPROC file: '+filename+'.fits'))
                    #     if os.path.isfile(fullname+"-PREPROC.pdf") and argoptions.overwrite_flag==False:
                    #         print ('  output PDF already exists, skip...')
                    #         continue
                    #     preproc = gravi_visual.Preproc(fullname+'.fits')
                    #     gravi_visual.produce_preproc_report(preproc,filename)
                
                    # P2VM reduced data files (interferometric quantities function of time)
                    if "P2VMRED" in datacatg:
                        print(('> Creating report for P2VMREDUCED file: '+filename+'.fits'))
                        if os.path.isfile(fullname+"-P2VMRED.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        p2vmreduced = gravi_visual.P2vmreduced(fullname+'.fits')
                        gravi_visual.produce_p2vmreduced_report(p2vmreduced,filename)

                    #  TODO: ASTRORED reduced data files report
                    if "ASTROREDUCED" in datacatg:
                        print ('> Creating report for ASTROREDUCED file: '+filename+'.fits')
                        if os.path.isfile(fullname+"-ASTRORED.pdf") and argoptions.overwrite_flag==False:
                            print ('  output PDF already exists, skip...')
                            continue
                        astroreduced = gravi_visual.Astroreduced(fullname+'.fits')
                        gravi_visual.produce_astroreduced_report(astroreduced,filename)
                        
                    # Final OIFITS data files
                    if 'HIERARCH ESO PRO CATG' in header:
                        datacatg = header['HIERARCH ESO PRO CATG']
                        protype = header['HIERARCH ESO PRO TYPE']
                        if ("VIS" in datacatg or "TF" in datacatg):
                            print(('> Creating report for OIFITS file: '+filename+'.fits'))
                            if os.path.isfile(fullname+"-OIFITS.pdf") and argoptions.overwrite_flag==False:
                                print ('  output PDF already exists, skip...')
                                continue
                            oifitsdata = gravi_visual.Oifits(fullname+'.fits')
                            gravi_visual.produce_oifits_report(oifitsdata,filename,\
                                            argoptions.minwSC,argoptions.maxwSC)
                    
            except Exception:
                print((traceback.format_exc()))
                print(('\n >>> Failed to create report for file: '+filename+'.fits... continuing to next file'))
                continue
                
