#FindMaximumSeparationBetweenSources.py                  Lance Simms, LLNL 2010
#
#PURPOSE:
#  To determine the largest separation between two nearest sources in the A4 
#  catalog.
#
#EXAMPLES
#  run FindMaximumSeparationBetweenSources.py  
#
########

execfile('/Users/lances/Desktop/Software/Python/CZTCubeSat/CZTCubeSatSetup.py')

import ephem
from datetime import *

#Here is the variable we're after
MaxMinAngSep = 0

#Read in the A4 Catalog first of all 
Names, RAs, DECs, CountRates, CountRateErrs, Fluxes, DataTabs, Observatories = \
  readcol('/Users/lances/Desktop/Software/Python/CZTCubeSat/A4_Catalog_NoHeader.txt', \
          fsep="|",twod=False)

#Convert the RAs and DECs to decimal
Cat_Size = size(RAs)
RAs_Deg  = zeros(Cat_Size, dtype=float64)
DECs_Deg = zeros(Cat_Size, dtype=float64)
RAs_Rad  = zeros(Cat_Size, dtype=float64)
DECs_Rad = zeros(Cat_Size, dtype=float64)

for i in arange(len(RAs)):
  RAs_Deg[i]  = hms2str_deg_space(RAs[i])
  DECs_Deg[i] = dms2str_deg_space(DECs[i])
  RAs_Rad[i]  = (pi/180)*RAs_Deg[i]
  DECs_Rad[i] = (pi/180)*DECs_Deg[i]

#Go through each separation and figure out the minimum
for j in arange(Cat_Size):
 
  #Get the jth RA, DEC and figure out the minimum separation
  ThisRA_Rad  = RAs_Rad[j]
  ThisDEC_Rad = DECs_Rad[j] 
  
  #Set some high minimum angle separation
  MinAngSep = 360
  MinAngInd = 0

  #Now find all of the objects within FOV_Deg degrees of pointing
  for i in arange(Cat_Size):

    ThisAngSep  = Astro_Angular_Separation(ThisRA_Rad, ThisDEC_Rad, RAs_Rad[i], DECs_Rad[i]) 
    ThisAngSep  = (180/pi)*ThisAngSep

    #If object is in FOV, calculate flux (no cos dependence yet)
    if ((ThisAngSep <= MinAngSep) and (i!=j)):
      MinAngInd = i
      MinAngSep = ThisAngSep
       
  print "Min Ang Separation of " + str(j) + "th source = " + str(MinAngSep) 
  print "    With neighbor " + str(MinAngInd)
  if (MinAngSep > MaxMinAngSep):
    MaxMinAngSep = MinAngSep

#We've found it 
print "\n\n"
print "Maximum distance between two adjacent sources = " + str(MaxMinAngSep)

 
  
