'''
Takes a SuperCOSMOS iam.rdsrt file and outputs a CSV file suitable for input into  _crf3.py which in
turn enables a Gaia DR3 CRF re-reduction of a SuperCOS plate scan catalogue.
'''
import numpy as np
import math
rad2deg = 180.0 / math.pi
pcenxy = 17750000
mm2deg = 67.14 / 3600.0

# from the legacy SuperCOS Sky Survey file store
iam_file = '/Users/nch/BDs/HIP94931/PAE0340/iam.rdsrt'
mag_file = '/Users/nch/BDs/HIP94931/PAE0340/mags.dat'

# corresponding output file
output_file = '/Users/nch/BDs/HIP94931/PAE0340/iam_rdsrt.csv'
# header line for the above
headers = 'recno,ra,dec,sMag,xdeg,ydeg,rdeg\n'
fo = open(output_file, 'w')
fo.write(headers)

# Open the file in binary mode
fiam = open(iam_file, mode = 'rb')
fmag = open(mag_file, mode = 'rb')
recno = 0
array = np.fromfile(fiam, dtype=np.int32, offset = 0, count = 32)
marray = np.fromfile(fmag, dtype=np.short, offset = 0, count = 1)
while len(array) > 0:
    
    # IAM parameter filters for deblended good point source quality
    if array[29] < 2048 and array[28] > -1:#array[30] > -3000 and array[30] < +3000 and array[28] > -1 and array[29] < 2048:
    
        # extract the required data
        ra = array[0] * 1.0e-8 * rad2deg
        dec = array[1] * 1.0e-8 * rad2deg
        x = (array[10] - pcenxy) * 1.0e-5 * mm2deg 
        y = (array[11] - pcenxy) * 1.0e-5 * mm2deg
        r = math.sqrt(x*x + y*y)
        smag = marray[0] * 1.0e-3 
        
        # TODO magnitude cut to reduce file size
        if smag < 18.0:
        
            # format the output
            line = '%d,%.9e,%.9e,%.3f,%e,%e,%e\n'%(recno,ra,dec,smag,x,y,r)            
            # output the data line
            fo.write(line)
    
    # next one
    recno = recno + 1
    # Read the data into a NumPy array
    array = np.fromfile(fiam, dtype=np.int32, count = 32)    
    marray = np.fromfile(fmag, dtype=np.short, count = 1)
    
# tidy up
print ('Finished; read %d records'%(recno))
fo.close()
