;
;	op_first_guess	(V4.0)  05-feb-96
; ****************************************************************************
;	( 27-apr-93)
pro op_setup,filein_og_table,filein_rules,filein_aoslos,	 $
		og_table,op_rules,path_out

nfiles = n_elements(filein_aoslos)
for i=0,nfiles-1 do begin
  rd_orbit_event,filein_aoslos(i),start_rec=1,end_rec=1,orbit_params=orbit_params
  print,i,filein_aoslos(i),'(Orb. el.: '+orbit_params+')',format='(i3,2(2x,a))'
endfor

if nfiles gt 1 then begin
   print,' ' & indx = nfiles-1       ; Default file index
   input,'* Enter index of the desired file',indx,indx,0,indx
   filein_aoslos = filein_aoslos(indx)
endif else begin
   filein_aoslos = filein_aoslos(0)	;turn it into scalar - MDM 13-Apr-93
end

print,'  OG lookup table: ',filein_og_table
print,'  OP rules       : ',filein_rules
print,'  AOSLOS file    : ',filein_aoslos
print,' ' 

og_table  = get_og_lookup(filein_og_table)	; Read the og lookup table
op_rules  = get_op_rules(filein_rules)		; Read the rules file

input,'* Enter output filename path', path_out, path_out
path_out = strtrim(path_out,2)

print_op_rules,-1,op_rules,filein_rules=filein_rules
return
end
; ****************************************************************************
;	(14-nov-91)
pro print_op_rules,lun,op_rules,filein_rules=filein_rules

printf,lun,' ' 
if n_elements(filein_rules) ne 0 then begin
   xx = findfile(filein_rules)
   printf,lun,'  OP rules read from ',xx(0)
   printf,lun,' '
endif

tags = tag_names(op_rules)
for i=0,n_elements(tags)-1 do printf,lun,op_rules.(i).a,	$
					 op_rules.(i).b,format='(f10.2,3x,a)'

return
end
; ****************************************************************************
;  15-oct-91, JRL, Written
;   6-dec-93, JRL, Change 000000 to 240000
function fmt_og,event

fmt = '(i3,2x,3(i2.2),"-",i3,"-",5x,a)'

nn = n_elements(event)
outarr = strarr(nn)

for i=0,nn-1 do begin
  outarr(i) = string(i,event(i).timarr(0:2),event(i).og,event(i).com,format=fmt)
  if total(event(i).timarr(0:2)) eq 0 then outarr(i) = $
	string(i,24,event(i).timarr(1:2),event(i).og,event(i).com,format=fmt)
endfor
return,outarr
end
; ****************************************************************************
;+
;	routine:	op_first_guess
;
;	purpose:	Generate a first guess for the OP editor tables.
;
;			Reads the output file of AOSLOS (as run on FACOM
;			mainframe).
;
;			Reads a lookup of table of OG commands.
;
;			Reads a files containing "rules" for computing
;			og times.
;
;       For diagnostic mode, set noprint=1 before running to prevent printing.
;
;  	Modification History:
;		15-oct-91, JRL, V0.0 Written
;		21-oct-91, JRL, V0.2 Added Hex output option
;		28-oct-91, JRL, V1.0 Added an information message
;		14-nov-91, JRL, V1.1 DSN commanding added.
;		22-nov-91, JRL, V1.2 Default output path to /3p/guest/opog
;		10-dec-91, JRL, V1.3 Summarize KSC and DSN contacts
;		11-dec-91, JRL, V1.4 Improved wording of one question
;		14-jan-92, JRL, V1.5 Add DSN los night/day op rules
;		23-jan-92, JRL, V1.6 Add dsn_switch op rule.
;		 6-jul-92, JRL, V1.7 Changed the re-start sequence.
;		 5-aug-92, JRL, V1.8 Changed print-out of station summary
;		11-aug-92, JRL, V1.9 Added call to pass_sheets; Changed
;				      default output directory for isass1,isass4
;		11-sep-92, JRL, V2.0 Fixed pass_sheets, Read RASM file; 
;		 6-feb-93, JRL, V2.1 Changed call to print_flare1
;		27-apr-93, JRL, V2.2 Change default directory for opog output
;					 Added call to cover_page in pass_sheets
;		 5-may-93, JRL, V2.3 Changed DSN Rep logic at night.  Changed
;					 output names.  Added ftp code.
;		 6-may-93, JRL, V2.4 Fixed the output name.
;		14-oct-93, JRL, V2.5 Align times to 32sec boundaries;
;				     Add SXT terminator images
;               30-nov-93, JRL, V2.6 Change the way the start time is handled.
;				     If AOS Ant is different from actual on pass sheet, put the
;				     actual antenna in ( )'s to try to prevent misunderstanding.
;				     Added call to op_same_time (from op_edit)
;				     Changed KSC pre-antenna algorithm to catch all cases
;					where antenna change occurs before 5 degrees.
;		 3-dec-93, JRL, V2.7 Fixed a bug in pass_sheet involved with
;				     position of Day OG (error made in V2.6 mod)
;		 6-dec-93, JRL, V2.8 Fixed a bug in AOS selection (V2.6).
;				     Force output times '000000' to be '240000'
;		16-jun-94, JRL, V2.9 Added code to switch to Quiet Med Rate during SAA
;				     Added new rule for op_terminator (must be 10s before D/N)
;		19-jun-94, JRL, V3.0 Don't ask about FTP to mainframe if .hex file is not created.
;		25-jun-94, JRL, V3.1 Display number of OG slots used before asking about .txt file
;		30-jun-94, JRL, V3.2 Changed op_same_time to display the correct conflic entry numbers
;		 2-jul-94, JRL, V3.3 Fixed a bug with pre-contact antenna change
;		30-jul-94, JRL, V3.4 Fixed a bug with QT Med Rate during SAA
;				     Allow user to delete any pass (for ASCA conflicts)
;		 7-oct-94, JRL, V3.5 Added op_special options
;		16-nov-94, JRL, V3.6 Make 'No' and 'Yes' responses to questions work as 'N' and 'Y'
;				     Fixed a bug op_saa_med
;		 9-dec-94, JRL, V3.7 Changed timing of CMPN OBS OFF to 6 * 32s after DAY OG
;		28-mar-95, JRL, V3.8 Add Wallops
;		22-oct-95, JRL, V3.9 Changed printing procedure
;		 5-feb-96, JRL, V4.0 Changed the AOS algorithm -- new op_first_guess.rule also
;- ------------------------------------------------------------------------

version = '****** op_first_guess (V4.0)   5-feb-96  ******'
print,version,format='(12x,a)' & print, ' '

filein_og_table  = '$DIR_GEN_ORBIT/og_lookup.ascii'
;;filein_og_table  = 'og_lookup.ascii'			; Test version
;;noprint = 1						; Test version (set for diagnostic mode)
if n_elements(noprint) eq 0 then noprint = 0 
if keyword_set(noprint) then print,' ** Info: NOPRINT is set'
if n_elements(verbose) eq 0 then verbose = 1		; Turn on information from op_same_time
filein_rules     = '$DIR_GEN_ORBIT/op_first_guess.rules'
;;filein_rules     = 'op_first_guess.rules'		; Test version
rasm_path	 = '$DIR_GEN_ORBIT_RAW'			; RASM file path
path 		 = '$DIR_GEN_ORBIT_RAW/*16s.ascii'	; Path to aoslos files
if n_elements(path_out) eq 0 then path_out = '$DIR_GEN_ORBIT/opog'
op_special_file  = '$DIR_GEN_ORBIT_RAW/op_special'	

if n_elements(end_time) ne 0 then begin
  print,'  First time = ',fmt_tim(start_time)
  print,'  Last  time = ',fmt_tim(end_time) & print,' '
  input,'* Enter new start and stop times?', ans, 'N'
  ans = strmid(strupcase(ans),0,1)
endif else ans = 'Y'

if ans eq 'Y' then begin
  filein_aoslos = findfile(path)			; Get input file name
  if filein_aoslos(0) eq '' then begin
	print,'No files found on path = ',path & stop
  endif
  op_setup,filein_og_table,filein_rules,filein_aoslos,		$
		og_table,op_rules,path_out
  op_times,filein_aoslos,start_time,end_time,start_rec,end_rec,ksc_first
  print,'Reading file ',filein_aoslos,' ...',format='($,3a)
  rd_orbit_event, filein_aoslos, orbit, orbit_params=orbit_params, $
		  start_rec = start_rec, end_rec = end_rec, /new, /plus
  dsn_num = 0			; Force new reading of DSN inputs
  ans3= 'N'			; Default for eliminating weekend KSC contacts
  ans5= 'Y'			; Default for changing to QT/Med in SAAs
endif
print,'There are ',strtrim(n_elements(orbit),2),' times'
print,'Orbit parameters: ',orbit_params
start_time = orbit(0).timarr

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Enter DSN requests:
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if dsn_num gt 0 then begin
   dsn_show, dsn_num, dsn_select		; Echo selections to tty
   ans = 'N' & input,'* Re-enter DSN requested stations?', ans, ans
   ans = strmid(strupcase(ans),0,1)
endif else ans = 'Y'
if ans eq 'Y' then begin
   print,' '
   print,'Enter DSN AOS times to within',op_rules.dsn_time.a,' min',format='(a,f5.1,a)'
   dsn_input,dsn_num,dsn_select,start_time=ksc_first,end_time=end_time;,rasm=rasm_path
endif

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Enter requests to eliminate certain KSC passes:
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print,'* Eliminate KSC contacts for a certain time period?'
input,'* (Normally, ONLY answer yes for Sat-Sun table load)', ans3, ans3
ans3 = strmid(strupcase(ans3),0,1)

if ans3 eq 'Y' then begin
  if n_elements(KSC_str1) eq 0 then $
	KSC_str1 = fmt_tim(addtime(anytim2ex(start_time),del=10.*60))
  if n_elements(KSC_str2) eq 0 then KSC_str2 = KSC_str1
  print,'* Enter start time to eliminate KSC passes'
  input,'*               ', KSC_str1, KSC_str1
  print,'* Enter stop  time to eliminate KSC passes'
  input,'*               ', KSC_str2, KSC_str2
  KSC_tim1 = timstr2ex(KSC_str1)
  KSC_tim2 = timstr2ex(KSC_str2)
  weekend = 1			; Flag for the cover_page routine
  ans4 = 'N'			; Default - no terminators for weekend
;  ans5 = 'N'			; Default - no SAA Rate changes for weekend
endif else begin
  KSC_tim1 = intarr(7) & KSC_tim2 = intarr(7)	; All zero's: no effect
  weekend = 0			; Flag for the cover_page routine
  ans4 = 'Y'			; Default - add terminators 
;  ans5 = 'Y'			; Default - Change rate in SAA
endelse
print,'* Add SXT terminator images?'
input,'* (Normally,      answer NO  for Sat-Sun table load)', ans4, ans4
ans4 = strmid(strupcase(ans4),0,1)
terminator = 0
if ans4  eq 'Y' then terminator=1 else if ans4  eq 'N' then terminator=0
input,'* Change SAA times to MED RATE?                     ', ans5, ans5
ans5 = strmid(strupcase(ans5),0,1)
qrate_change = 0
if ans5 eq 'Y' then qrate_change=1 else if ans5 eq 'N' then qrate_change=0
op_get_special, op_special_file, spec_times, spec_og	; Get times for special commands
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

op_edit, orbit, og_table, op_rules, event, event2, 			$
		dsn_num, dsn_select, dsn_str_select=dsn_str_select,	$
		KSC_tim1=KSC_tim1, KSC_tim2=KSC_tim2,			$
		KSC_str_remove=KSC_str_remove,pass_sum=pass_sum,	$
		comment=comment, contacts=contacts, 			$
		terminator=terminator, qrate_change=qrate_change,	$
		ksc_pass_num=ksc_pass_num,verbose=verbose,		$
                spec_times=spec_times, spec_og=spec_og

print,'  Number of OG slots used = ',strtrim(n_elements(event2),2)
if n_elements(event2) gt 128 then begin
   tbeep,5
   print,'  ***  WARNING:  More than 128 Slots used *** '
endif
filecode = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g']
if keyword_set(noprint) then print,' ** Info: NOPRINT is set'
ans1 = 'Y' & input,'* Write ascii output file?', ans1, ans1
ans1 = strmid(strupcase(ans1),0,1)
if ans1 ne 'N' then begin
	fileout = concat_dir(path_out,'op'+strmid(comment,1,5)+filecode(fix(strmid(comment,7,2)))+'.txt')
	wr_opasc,fileout,event2,orbit_params=orbit_params,	$
		   comment=comment,				$
		   op_rules=op_rules,filein_rules=filein_rules,	$
		   dsn_str_select = dsn_str_select,		$
		   KSC_tim1=KSC_tim1, KSC_tim2=KSC_tim2,	$
		   ksc_str_remove = ksc_str_remove,		$
		   pass_sum=pass_sum,terminator=op_term_sum(event2,og_table)
endif								; ans1 eq 'Y'

ans2 = 'Y' & input,'* Write hex   output file?', ans2, ans2
ans2 = strmid(strupcase(ans2),0,1)
if ans2 ne 'N' then begin
  fileout2= concat_dir(path_out,'op'+strmid(comment,1,5)+filecode(fix(strmid(comment,7,2)))+'.hex')
  wr_ophex,fileout2,event2,01,comment			; 01 = KSC pass number
endif							; ans2 eq 'Y'

; Produce the pass sheets if the ascii output was requested
if ans1 ne 'N' then begin
	fileout3 = concat_dir(path_out,'idl.ps')
	fileout4 = concat_dir(path_out,'cover.ps')
	op_pass_sheets,contacts,version=version,comment=comment,		$
			orbit_params=orbit_params,			$
			weekend=weekend,ksc_pass_num=ksc_pass_num,	$
			file=fileout3,cover_file=fileout4

; Try to print ascii and idl.ps files on the local printer:

  if not noprint then begin
    pprint,fileout,/force			; op list
    pprint,fileout4,/force			; Cover page of Pass sheets
    pprint,fileout3,/force			; Pass sheets
  endif else print,'  Output NOT sent to printer'
endif

; Try to ftp the .hex file to the mainframe

if ans2 ne 'N' then begin
  input,'* FTP the .hex to the mainframe?', ans, 'Y'
  ans = strmid(strupcase(ans),0,1)
  if ans ne 'N' then op_facom_ftp,fileout2
endif

; Send an informational message to tty: 

if (ans1 ne 'N') and (ans2 ne 'N') then print,' '
if ans1 ne 'N' then print,'Ascii file: ',fileout
if ans2 ne 'N' then print,'Hex   file: ',fileout2

end
