
;+
; NAME:
;      POINT_HEX2
; PURPOSE:
;      Generate an OG for a specified pointing location. The output
;        is in hexadecimal, appropriate for OG editing via OPOGEDT.
; CATEGORY:
;      Yohkoh
; CALLING SEQUENCE:
;      point_hex2, angle, og_number, bc, offset = offset 
;      point_hex2, [-.0797+E1/3600.,.0295+E2/3600.], /name
;        (here the Euler angles E1 and E2 are those returned
;        by TERM_REVIEW,/SET - need to convert from degrees)
; INPUTS:
;      angle [eX,eY], in degrees (note: this is a low-level
;        program! These target angles are the Euler angles, not the
;        X,Y displacements.) 
;      og_number, the (decimal) OG number that the new commands are for
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;      offset is the nominal pointing, default = [0.0003889,0.002111]
;      normal is the Euler angles of the S/C NORMAL POINT command, default
;        at [-.0797,.0295]
;      name forces the printed output of a suggested name for the OG
;        command, under the convention (incremental displacements in
;        arc min relative to the NORMAL POINT location)
; OUTPUTS:
;      bc: an array of 8 hexadecimal numbers for entry into an OG 
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
;      does not create a Z-angle command (hard-wired to 00 00)
;      Errors as large as 2 arcsec in X (8 LSB units)
;	 and 10 arcsec in Y (50 LSB units) have been found
;	 in comparison with 6 previous cases.
;      The code has built-in limit switches to help prevent the generation
;        of large offpoints
; WARNING:
;      The Euler angles are defined such that X increasing => S/C south
;                                             Y increasing => S/C east
; MODIFICATION HISTORY:
;      HSH, written 13-Feb-1995
;      LWA, corrected and checked 15-Feb-95
;      HSH, added /name and /normal keywords, limit switches 5-Jun-95
;      LWA, Changed naming format to include decimals. 13-Nov-95
;      HSH, added print statements to match OG editor format; 
;           added og_number input variable. 13-Nov-95
;      LWA, OG_NUMBER feature disabled. 13-Nov-95 (!)
;      SKS, added if & format statements to adjust name_x/y output. 13-Nov-95
;      HSH, added another example to the calling sequence. 6-aug-96
;      HSH, improved the documentation a bit. 22-Nov-98
;      HSH, added text output to hardwired directory, 19-Nov-99
;      HSH, added nofile to defeat text, 7-Jun-00 
;-
pro point_hex2, angle, og_number, xy_hex, offset = offset, name = name, $
  normal = normal, qdebug = qdebug, text = text, nofile = nofile 
OG_NUMBER=' '
number = strtrim(string(og_number),1)
xy = angle 
safe = [-.2, .34, -.34, .34]
if (xy(0) lt safe(0) or xy(0) gt safe(1) or xy(1) lt safe(2) or $
  xy(1) gt safe(3)) then begin
  print,' '
  print,"WARNING: YOU'VE REQUESTED AN EXCESSIVE OFFPOINT. BACK OFF!"
  print,"WARNING: YOU'VE REQUESTED AN EXCESSIVE OFFPOINT. BACK OFF!"
  print,"WARNING: YOU'VE REQUESTED AN EXCESSIVE OFFPOINT. BACK OFF!"
  print,"WARNING: YOU'VE REQUESTED AN EXCESSIVE OFFPOINT. BACK OFF!"
  print,' '
  goto, bail
endif
lsb = 5.5555e-5 
if n_elements(offset) eq 0 then offset = [0.0003889,0.002111] ; ACS offset
if n_elements(normal) eq 0 then normal = [-.0797,.0295] ; from S/C NORMAL POINT
xy_red = -(xy-offset)/lsb
if xy_red(0) lt 0 then xy_red(0) = xy_red(0) + 65536
if xy_red(1) lt 0 then xy_red(1) = xy_red(1) + 65536
xy_hex = to_hex(65536 - xy_red,4)
bc = strarr(8)
bc(0) = '81
bc(1) = '00
bc(2) = strmid(xy_hex(0),0,2)
bc(3) = strmid(xy_hex(0),2,2)
bc(4) = strmid(xy_hex(1),0,2)
bc(5) = strmid(xy_hex(1),2,2)
bc(6) = '00
bc(7) = '00
;print,'Block Commands for Yohkoh Pointing under TFSS Control
;print,'
;print,'TARGET ANGLE-X
;print,'                  ',xy(0),' (DEG)
;print,'TARGET ANGLE-Y
;print,'                  ',xy(1),' (DEG)
print,'TARGET ANGLE-X     ',xy(0),' (DEG)
print,'TARGET ANGLE-Y     ',xy(1),' (DEG)
;print,'
print,'BLOCK COMMAND ===> ',bc

if keyword_set(name) then begin
; disp_x = fix((angle(0)) * 60.)
;  disp_x = fix((angle(0) - offset(0) - normal(0)) * 60.)
  disp_x = (angle(0) - offset(0) - normal(0)) * 60.

if (abs(disp_x) lt 1.0) then dx_nums = $
                          strmid(string(abs(disp_x),format='(f0.5)'),1,4) $
     else dx_nums = strmid(string(abs(disp_x),format='(f0.5)'),0,4)

  if disp_x lt 0. then name_x = 'N'+dx_nums
  if disp_x eq 0. then name_x = 'N0.0'
  if disp_x gt 0. then name_x = 'S'+dx_nums

; disp_y = fix((angle(1)) * 60.)
;  disp_y = fix((angle(1) - offset(1) - normal(1)) * 60.)
  disp_y = (angle(1) - offset(1) - normal(1)) * 60.
print,disp_x,disp_y


if (abs(disp_y) lt 1.0) then dy_nums = $
                        strmid(string(abs(disp_y),format='(f0.5)'),1,4) $
   else dy_nums = strmid(string(abs(disp_y),format='(f0.5)'),0,4)

  if disp_y lt 0. then name_y = 'W'+dy_nums
  if disp_y eq 0. then name_y = 'W0.0'
  if disp_y gt 0. then name_y = 'E'+dy_nums
  print,'COMMAND NAME:  S/C  ', name_y, ' ', name_x, $
    '            (offset angles in arcmin)'
endif
print,' '
print,'----- OG EDITOR OUTPUT ---------------'
print,' '
print,' NUMBER :  ',number,'    NAME : S/C ', name_y,' ',name_x
print,' '
print,'  1  04-DC-8A  ACS: ACP DATA SET+B7'
print,'  2  04-BC-81  ACS: BC
print,'  3  04-BC-',strmid(xy_hex(0),0,2),'  ACS: BC'
print,'  4  04-BC-',strmid(xy_hex(0),2,2),'  ACS: BC'
print,'  5  04-BC-',strmid(xy_hex(1),0,2),'  ACS: BC'
print,'  6  04-BC-',strmid(xy_hex(1),2,2),'  ACS: BC'
print,'  7  04-BC-00  ACS: BC
print,'  8  04-BC-00  ACS: BC
print,'  9  04-DC-11  ACS: BC EXEC     +++'
print,' '
if n_elements(text) ne 0 and n_elements(nofile) eq 0 then begin
  get_lun, lun
  openw, lun, '/home/sxt_co/terminator/pointing_request/scratch.txt'
  printf,lun,'TARGET ANGLE-X     ',xy(0),' (DEG)
  printf,lun,'TARGET ANGLE-Y     ',xy(1),' (DEG)
  printf,lun,'BLOCK COMMAND ===> ',bc
  printf,lun,disp_x,disp_y
  printf,lun,'COMMAND NAME:  S/C  ', name_y, ' ', name_x, $
    '            (offset angles in arcmin)'
  printf,lun,' '
  printf,lun,'----- OG EDITOR OUTPUT ---------------'
  printf,lun,' '
  printf,lun,' NUMBER :  ',number,'    NAME : S/C ', name_y,' ',name_x
  printf,lun,' '
  printf,lun,'  1  04-DC-8A  ACS: ACP DATA SET+B7'
  printf,lun,'  2  04-BC-81  ACS: BC
  printf,lun,'  3  04-BC-',strmid(xy_hex(0),0,2),'  ACS: BC'
  printf,lun,'  4  04-BC-',strmid(xy_hex(0),2,2),'  ACS: BC'
  printf,lun,'  5  04-BC-',strmid(xy_hex(1),0,2),'  ACS: BC'
  printf,lun,'  6  04-BC-',strmid(xy_hex(1),2,2),'  ACS: BC'
  printf,lun,'  7  04-BC-00  ACS: BC
  printf,lun,'  8  04-BC-00  ACS: BC
  printf,lun,'  9  04-DC-11  ACS: BC EXEC     +++'
  printf,lun,' '
  close, lun
  free_lun, lun
endif
bail:
if n_elements(qdebug) then stop
end
