;+
; NAME:
;     MAKE_EPHEM
; PURPOSE:
;     Creates the EPHEMERIS segment (segment code 9) that the APC is to
;     write into the data file at the start of each scan.  The EPHEMERIS
;     segment contains information on solar, lunar, calibrator, and active
;     region coordinates and other parameters.
; CATEGORY:
;     OVRO SPAN CALIBRATION
; CALLING SEQUENCE:
;     make_ephem[,/do_exit]
; INPUTS:
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     do_exit   Switch to force exit from IDL after routine finishes.
; ROUTINES CALLED:
;     get_dut, get_soleph, get_luneph, get_caleph, get_regn,
;     dayofyr, tl_encode, l2w, tlsnow
; OUTPUTS:
; COMMENTS:
;     Several files must exist and be properly formatted in order for this
;     routine to work.  They are:
;         !DEFAULTS.EPHEMDIR+'DUT.USN'
;         !DEFAULTS.EPHEMDIR+'SUN1994-2020.DAT'
;         !DEFAULTS.EPHEMDIR+'LUNAREPH.DAT'
;         !DEFAULTS.EPHEMDIR+'COORDS.FTH'
;         !DEFAULTS.EPHEMDIR+'REGIONS.NOA'
;         !DEFAULTS.EPHEMDIR+'J2000.CAT'
;     See the individual GET_*() routines for more information.
; SIDE EFFECTS:
;     Creates a file !DEFAULTS.EPHEMDIR+'EPHEM.REC' containing the properly
;     formatted data record.  If the file already exists, it will be
;     overwritten.
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 30-Jan-1999 by Dale E. Gary
;     05-Feb-1999  DG
;       Converted for use with FK5 (J2000) precess routines from SLALIB
;     25-Nov-1999  DG
;       Updated to agree with new Epoch defns
;     11-Jan-2000  DG
;       Eliminated hardwired directory locations
;     23-Jul-2000  DG
;       Changed to use the function TLSNOW (no functional change).
;     19-Aug-2000  DG
;       Added /do_exit switch instead of EXIT command, to fix
;       problem of STARTUP file exiting before running.
;     23-Jul-2003  DG
;       Changed WRITE_LOG to WRITE_OVSA_LOG to avoid conflict
;       with routine of the same name in BATSE tree.
;     18-Jan-2005  DG
;       No functional change, just changed the documentation above
;       to reflect the name change of the sun1994-2020.dat file.
;-
pro make_ephem,do_exit=do_exit

   write_ovsa_log,'Generating ephemeris'
   ; Read the system time as a Julian Date
   jd = systime(/julian)

   cd,!defaults.ephemdir,current=cwd

   ; spawn precess routine
   spawn,!defaults.cmddir+'prec2000'

   ; Get various ephemeris data structures
   dut   = get_dut(jd)
   solar = get_soleph(jd)
   lunar = get_luneph(jd)
   cal   = get_caleph()
   regn  = get_regn()

   ; Fill in an appropriate TIME/LABEL structure
   tl_struct = tlsnow(!SEGM.EPHEM)

   ; Convert tl_struct to binary data
   tldata = tl_encode(tl_struct)

   ; Declare storage for entire record
   data = intarr(1024)

   ; Fill in record
   data(0:11) = tldata                  ; Time/Label field
   data(12) = dut                       ; UT1-UTC
   for i = 0,2 do begin
      data(13+i*2) = l2w(solar.ra(i))   ; Solar RA
      data(19+i*2) = l2w(solar.dec(i))  ; Solar DEC
      data(25+i)   =     solar.rad(i)   ; Solar Radius
      data(28+i*2) = l2w(solar.pa(i))   ; Solar P Angle
      data(34+i*2) = l2w(solar.b0(i))   ; Solar B Angle
      data(40+i)   =     solar.hp(i)    ; Solar Horizontal Parallax
      data(43+i)   =     solar.reft(i)  ; Solar reference time [s]
      data(46+i)   =     solar.doy(i)   ; Solar reference day
      data(49+i*2) = l2w(lunar.ra(i))   ; Lunar RA
      data(55+i*2) = l2w(lunar.dec(i))  ; Lunar DEC
      data(61+i)   =     lunar.rad(i)   ; Lunar Radius
      data(64+i)   =     lunar.hp(i)    ; Lunar Horizontal Parallax
      data(67+i)   =     lunar.reft(i)  ; Lunar reference time [s]
      data(70+i)   =     lunar.doy(i)   ; Lunar reference day
   endfor
   for i = 0, 2 do begin
      data(73+i) = cal.epoch.(i)        ; Calibrator EPOCH (year,doy,sec)
   endfor
   data(76) = cal.ncal                  ; Number of calibrators
   ; Loop over number of calibrators
   for i = 0, cal.ncal-1 do begin
      data(77+i*8) = fix(byte(cal.caleph(i).name),0,4)  ; Calibrator NAME
      data(81+i*8) = l2w(cal.caleph(i).ra)  ; Calibrator RA
      data(83+i*8) = l2w(cal.caleph(i).dec) ; Calibrator DEC
   endfor
   for i = 0, 2 do begin
      data(277+i) = regn.epoch.(i)      ; Active Region EPOCH (year,doy,sec)
   endfor
   data(280) = regn.nregn               ; Number of active regions
   ; Loop over number of active regions
   for i = 0, regn.nregn-1 do begin
      data(281+i*13) = regn.info(i).noa                     ; Region NOAA number
      ; Active Region Latitute and Longitude, packed into a single word
      data(282+i*13) = regn.info(i).lat                     ; Region latitude (+N)
      data(283+i*13) = regn.info(i).lng                     ; Region longitude (+E)
      data(284+i*13) = regn.info(i).carrlng                 ; Region Carr. Longitude
      data(285+i*13) = regn.info(i).area                    ; Region Spot Area
      data(286+i*13) = fix(byte(regn.info(i).type),0,2)     ; Region Type
      data(288+i*13) = fix(byte(regn.info(i).magtype),0,6)  ; Region Magnetic Type
   endfor

   ; Open the output file to write the record.  This will overwrite any
   ; existing file, or create a new one if none exists.
   openw,lun,/get_lun,!defaults.ephemdir+'ephem.rec',bufsiz=2048
   ; Create an associated variable for the file
   b = assoc(lun,intarr(1024))
   ; Write the data
   b(0) = data
   ; Close the file and free the unit number
   free_lun,lun

   cd,cwd
   write_ovsa_log,'   done with ephemeris'

   if (keyword_set(do_exit)) then exit else return
end