import glob, sys
from astropy.io import fits
import statistics
import pandas as pd
import numpy as np
import csv
colnames=['Match', 'Ref', 'Dup', 'RA', 'Dec', 'Incl']

flist=glob.glob('../products/tm[1,5,6,7]*en0*src.fits')
total1=0
total2=0

for f in flist:

    hdul = fits.open(f)
    try:
        table=hdul[1].data['RADEC_ERR']
    except:
        continue
    hdul.close()
    
    dref=f.replace("src.fits","ref.fits")
    hdul = fits.open(dref)
    try:
        rtable=hdul[1].data['RA']
    except:
        continue
    hdul.close()

    err=[]
    err10=[]
    dfile=f.replace("shu2019.src.fits","xfm.log.dat.awk")
    with open(dfile) as csvfile:
        spamreader = csv.reader(csvfile,  delimiter=' ')    
        total=0
        for row in spamreader:
            if(row[5] == 'N'):
                continue
            for ii,values in np.ndenumerate(rtable):
                if(int(row[2]) == int(ii[0])):
                    total=total+1
                    #print(">>",row[5],rtable[ii],table[ii],ii[0])
                    if(table[ii]>0.0):
                        err.append(table[ii])
                    if(table[ii]>10.0):
                        err10.append(table[ii])
                        
    total1=total1+total
    total2=total2+len(err)
    #print("{} MIN {:.2f} MAX {:.2f} ({}/{})".format(f[12:15],min(err),max(err),len(err10),len(err)))
    print("{} MIN {:.2f} MAX {:.2f} ({}/{})".format(f,min(err),max(err),len(err10),len(err)))
    #sys.exit()


    #print(total1,total2)
