;+
; Project     : SOHO - CDS     
;                   
; Name        : PREPARE_FT
;               
; Purpose     : Writes a script file to load the current solar feature
;               tracking parameters.  This should be called from CPT and the
;               associated script run from PSLOAD
;               
; Explanation : The solar feature tracking parameters used on board need
;               to be updated on a regular basis to cater for the changing 
;               solar B angle and solar radius.  This routine writes a script
;               which will load the appropriate parameters.
;               
; Use         : IDL> prepare_ft
;    
; Inputs      : None
;               
; Opt. Inputs : None
;               
; Outputs     : Write file to $CDS_TCL_FTRACK
;               
; Opt. Outputs: None
;               
; Keywords    : LATITUDE - used to calculate differential rotation. (default
;                          is zero)
;
;               INTERVAL - set interval after which rotation is calculated on
;                          board  (default 600s)
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: Needs CDS_TCL_FTRACK to be defined and to have write access
;               
; Side effects: None
;               
; Category    : Commanding
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 30_Apr-96
;               
; Modified    : Add differential rotation and INTERVAL.  CDP, 14-May-96
;               Fix typo bug.   CDP, 15-May-96
;               Get pointing parameters from database.  CDP, 22-Jul-96
;               Set default interval to 600 secs.  CDP, 21-Apr-97
;
; Version     : Version 5, 21-Apr-97
;-            

pro  prepare_ft, latitude=latitude, interval=interval

;
;  is env. variable set?
;
if getenv('CDS_TCL_FTRACK') eq '' then begin
   print,'CDS_TCL_FTRACK is not defined.'
   return
endif


;
;  test open output file
;
ofile = concat_dir('$CDS_TCL_FTRACK','cb2feat.today.dt')
openw,lun,ofile,/get_lun,error=err
if err ne 0 then begin
   print,'Error opening CB2FEAT output file.'
   return
endif
free_lun, lun

;
;  get pointing parameters
;
s = cp_get_mnemonic('CB2POINT', /QUIET )
ss = cp_get_indeces(s,[4,8,10])
ops_k = ss(0).active
ops_ltheta = ss(1).active
ops_lphi   = ss(2).active


;
;  latitude to use
;
if not keyword_set(latitude) then latitude = 0.0
if latitude lt -90 or latitude gt 90 then begin
   bell
   print,'Latitude must be in range -90 < lat < 90.'
   return
endif


;
;  Calculate synodic rotation period and hence angular velocity. Values are
;  interpolated from Tabel 3.1 (Phillips) 
;
rot_per = 27.560594D0 - $
           0.019241858d0*latitude + $
           0.0021363581d0*latitude*latitude

;
;  arcsec/hour
;
ang_vel = (360.0d0/rot_per/24.0d0)*3600.d0

;
;  correction interval
;
if not keyword_set(interval) then interval = 600.0

dec2hex,interval*1000000.0d0/64.0d0,cint,/quiet
cint = repchar(strpad(cint,8),' ','0')
int_msb = '0x'+strmid(cint,0,4)
int_lsb = '0x'+strmid(cint,4,4)

;
;  prepare variable values
;
get_utc,utc
dt = anytim2cal(utc)
spar = pb0r(utc,/soho,/arc)
sb0 = longhex(round(spar(1)*3600.))

;
;  prepare text
;
text = strarr(25)
text(0)  = '# cb2feat.today.dt'
text(1)  = '# Current feature tracking parameters.'
text(2)  = '# Written by PREPARE_FT on '+dt
text(3)  = '#'
text(4)  = '# OPS K length'
text(5)  = trim(ops_k)
text(6)  = '0x0'
text(7)  = '# OPS theta angle'
text(8)  = trim(ops_ltheta)
text(9)  = '0x0'
text(10) = '# OPS phi angle'
text(11) = trim(ops_lphi)
text(12) = '0x0'
text(13) = '# Update time (64 us)'
text(14) = int_lsb
text(15) = int_msb
text(16) = '# solar radius (arcsecs)'
text(17) = trim(round(spar(2)))
text(18) = '0x0'
text(19) = '# solar B angle (arcsecs)'
text(20) = '0x'+strmid(sb0,6,4)
text(21) = '0x'+strmid(sb0,2,4) 
text(22) = '# solar angular velocity (arcsecs/hour)
text(23) = trim(fix(ang_vel))
text(24) = '0x0

;
;  write file
;
print_str,text, file=ofile, /keep,/ quiet

return
end

