"""
10/04/2022
Utilitiy script that uses beam phase center list generated from phaseCenterList.py to determine beams within a certain user-provided radius
(in deg)
User inputs:
-f --file_name - <required> path to list of beam phase centers generated by phaseCenterList.py
-ra --ra - <required> RA in hms format
-dec --dec - <required> declination in dms format
-r --radius - <required> radius (in deg) over which to determine inclusion of beams
__author__="Nickolas Pingel"
__version__="1.0"
__email__="Nickolas.Pingel@anu.edu.au"
__status__="Production"
"""
## imports
import argparse
from astropy.coordinates import SkyCoord

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file_name', help='<required> path to list of beam phase centers generated by phaseCenterList.py', required = True, type = str)
parser.add_argument('-r', '--ra', help ='<required> RA in hms format', required = True, type = str)
parser.add_argument('-d', '--dec', help = '<required> declination in dms format', required = True, type = str)
parser.add_argument('-b', '--radius', help = '<required> radius (in deg) over which to determine inclusion of beams', required = True, type = float)
args, unknown = parser.parse_known_args()
print(args)
## extract user provided arguments
file_path = args.file_name
radius = args.radius
center_ra = args.ra
center_dec = args.dec
center_coord = SkyCoord(center_ra, center_dec, frame = 'fk5')

## open file in read-only mode
f = open(file_path, 'r')

## order of beams is always 0-35 and interleaves A, B, C
## set counter for interleave (if cnt <= 35 we have inter A; if 36 <= cnt <= 71 we have inter B)
## also use counter for beam (by subtracting 36 or 72 if larger than those cnts )
cnt = 0
for line in f:
	
	## determine which beam and interleave
	if cnt < 36:
		beamNum = cnt
		inter = 'A'
	elif  36 <= cnt <= 71:
		beamNum = cnt - 36
		inter = 'B'
	else:
		beamNum = cnt - 72
		inter = 'C'
	## split line by space delimiter =
	raStr = line.split(' ')[1].strip()
	decStr = line.split(' ')[2].strip()
	c = SkyCoord(raStr, decStr, frame = 'fk5')
	sep = c.separation(center_coord)
	if sep.deg < radius:
		print(beamNum, inter)
	## increment counter
	cnt += 1