;+
; NAME:
;     GET_CALEPH
; PURPOSE:
;     Return precessed coordinates for calibrators, as found in
;     the file !DEFAULTS.EPHEMDIR+COORDS.FTH.
; CATEGORY:
;     OVRO APC EPHEMERIS
; CALLING SEQUENCE:
;     cal = get_caleph([file=file])
; INPUTS:
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     file    Optional file name for COORDS.FTH input file.  If omitted,
;               !DEFAULTS.EPHEMDIR+'COORDS.FTH' is assumed.
; ROUTINES CALLED:
;     get_ovcal_struct
; OUTPUTS:
;     cal     A calibrator ephemeris structure of the form:
;               {EPOCH: STRUCTURE  ; Epoch and other info from file
;                   {YR:   INT     ; Year of the calibrator coords
;                    DOY:  INT     ; Day of year of coords
;                    SEC:  INT }   ; Time of coords
;                NCAL:     INT     ; Number of calibrator sources
;                CALEPH: STRUCTURE ; Array of ephemeris info
;                   {NAME: STRING  ; Name of calibrator (A8)
;                    RA:   LONG    ; Right ascen. of calibrator [0.1 mdeg]
;                    DEC:  LONG}   ; Declination of calibrator [0.1 mdeg]
;               }
; COMMENTS:
;   Requires the file !DEFAULTS.EPHEMDIR+'COORDS.FTH', which is the file
;   created by PRECESS and sent to the CPC on a daily basis.
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 12-Jan-1999 by Dale E. Gary
;     30-Jan-1999  DG
;       Slight change to use GET_CAL_STRUCT() routine
;     07-Feb-1999  DG
;       Added FILE keyword argument
;     25-Nov-1999  DG
;       Updated to agree with Epoch defn in GPARM structure
;     11-Jan-2000  DG
;       Eliminated hardwired directory locations
;     23-Jul-2003  DG
;       Changes GET_CAL_STRUCT to GET_OVCAL_STRUCT to avoid conflict
;       with routine of the same name in LASCO tree.
;-

function get_caleph,file=file

   ON_IOERROR,bail

   ; Get a copy of a CAL structure
   cal = get_OVcal_struct()

   if (not keyword_set(file)) then begin
      ; Open the COORDS.FTH file
      openr,/get_lun,lun,!defaults.ephemdir+'coords.fth'
   endif else begin
      openr,/get_lun,lun,file
   endelse
   line = ''

   ; Read the date line
   readf,lun,line

   ; Decode the date and determine DOY
   year = strmid(line,37,4)*1
   mo   = strmid(line,33,3)
   da   = strmid(line,30,2)
   doy = dayofyr(year,mo,da)

   MAXCAL = 25
   name = strarr(MAXCAL)
   ra = lonarr(MAXCAL)
   dec = lonarr(MAXCAL)
   sec = 0L  ; Always zero
   names=''
   i = -1
   ; Loop over all of the calibrator sources
   while (not eof(lun)) do begin
      readf,lun,line
      if (strlen(line) gt 10) then begin
         reads,line,coord,names,format='(f9.4,7x,a)'
         idash = strpos(names,'-')
         if (idash eq -1) then goto,bail
         if (strmid(names,idash+1,2) eq "RA") then begin
            i = i + 1
            if (i eq MAXCAL) then begin
               i = i - 1
               goto,done
            endif
            name(i) = strmid(names,0,idash)+'        '
            name(i) = strmid(name(i),0,8)
            ra(i) = long(coord*10000.)
         endif else begin
            dec(i) = long(coord*10000.)
         endelse
      endif
   endwhile
done:
   ; Close the file and free the logical unit number
   free_lun,lun

   ; Set up the structures
   for j = 0, i do begin
      cal.caleph(j).name = name(j)
      cal.caleph(j).ra = ra(j)
      cal.caleph(j).dec = dec(j)
   endfor

   cal.epoch.yr = year
   cal.epoch.doy = doy
   cal.epoch.sec = sec
   cal.ncal = i + 1

   return,cal
bail:
   if(n_elements(lun) ne 0) then free_lun,lun
   return,-1
end
