pro sxt_obs_coord, item1, item2, data=data, suncenter=suncenter, $
  sec_per_pix=sec_per_pix, side=side, bin=bin, res=res, grid=grid, $
  nomark1=nomark, nomark2=nomark2, color1=color1, color2=color2, $
  qstop=qstop, query=query

;+
;NAME:
;	sxt_obs_coord
;PURPOSE:
;	To print out the OR table coordinate for a region clicked on 
;	a HR FFI image
;CALLING SEQUENCE:
;	sxt_obs_coord, item1 [, item2, data, side=side, /nomark1, bin=bin]
;	sxt_obs_coord, index
;	sxt_obs_coord, index, index1, data=data,side=128  ; 2x2 OR
;	sxt_obs_coord, index, index1, side=[64,128]	  ; 1x2 OR (NS x EW)
;INPUT:
;	item1	- Time of image in any format (index, etc)
;                 If item1 is string a 512x512 image is assumed.
;	item2	- Time in any format at which coordinates of chosen point
;		  are desired (index, etc)
;KEYWORDS (INPUT):
;	data	- Image.  Only needed if image is to be re-displayed.
;	suncenter - Center of sun in full res (IDL) pixels.  
;		    Defaults to suncenter=sxt_cen(item1)
;	sec_per_pix - Pixel size in arcsec.
;	side	- Size of square in FR pixels to draw on image (def = 64). If 
;		  side =[N,M], will draw a rectangle that is N (NS) by M (EW)
;	bin	- Binning width if image is re-displayed (def is 1).
;	res	- Summation mode.  Only needed if item1 is not an index
;		  or roadmap record.
;	grid	- If set, overlay heliographic coordinate grid
;	nomark1	- If present, the location on the image which 
;		  is selected is NOT marked (default is to mark it).
;	nomark2	- If present, the location of the rotated position 
;		  is NOT marked (default is to mark it).
;	color1	- Color of ROI box (def = 192).
;	color2	- Color of rotated ROI box (def = 255).
;	qstop	- If set, stop execution without returning to calling program.
;	query   - If set, query user for coordinate information
;	helio   - Heliographic coordinates of chosen point
;	date_helio - Epoch time of above coordinates in any format
;KEYWORDS (OUTPUT):
;	coords	- Output OR coordinates (TODO).
;HISTORY:
;	20-Nov-91 - MDM - Written.
;	 2-Apr-92 - LWA - Algorithm derived
;	 3-aug-93 - GLS - Extensive re-write to make use of new pointing
;			  and coordinate transformation routines.
;       30-nov-93, JRL, side=[n,m] will now draw a rectangle.
;			Added the epoch times and adjusted the message formats.
;			Changed nomark= to nomark1 (nomark was ambiguous)
;       22-dec-93, JRL, Fixed an IDL V3.1 related bug
;	24-mar-94, JRL, Fixed case when input of suncenter correctly
;       14-apr-94, LWA, Mod to handle case where item1 is a string (time).
;	17-Jun-94, BNH, Added feature to choose coordinates manually
;	30-Nov094, LWA, Changed color to color1 to avoid IDL ambiguity.
;-

if n_elements(item1) eq 0 then begin
   message,'item1 argument was not defined',/info
   doc_library,'sxt_obs_coord'
   return
endif

sn = [' S',' N'] & ew = [' E',' W']

if n_elements(sec_per_pix) eq 0 then sec_per_pix = 2.453
if n_elements(suncenter) eq 0 then cen = sxt_cen(item1) else cen = suncenter
if n_elements(side) eq 0 then side = 64
if n_elements(bin) eq 0 then bin = 1
if n_elements(res) eq 0 then res = 1
if n_elements(color1) eq 0 then color1 = 192
if n_elements(color2) eq 0 then color2 = 255


sside = [side(0),side(0)]			; Allow for rectangles
if n_elements(side) gt 1 then sside(0) = side(1)	; side(0)=NS (1)=EW

siz_item1 = size(item1)
type1 = siz_item1( siz_item1(0)+1 )

if n_elements(data) ne 0 then begin
  siz = size(data)
  nx = siz(1)
  ny = siz(2)
endif

if (type1 eq 8) then begin
  res = gt_res(item1)
  nx = item1.sxt.shape_sav(0) & ny = item1.sxt.shape_sav(1)
endif 


                               
; IF ITEM1 IS A TIME THEN ASSUME A 512X512 IMAGE IS ON THE SCREEN
if (type1 eq 7) then begin
  nx=512
  ny=512
endif

if n_elements(data) ne 0 then $
  tvscl, rebin(data, nx*bin, ny*bin, /sample)
if keyword_set(grid) then sxt_grid,item1

button = 0

epoch_time = fmt_tim(item1)
if keyword_set(query) then begin
   input, '* Do you want Helio or SXT image coordinates (H,S)', $
          ans, 'H'
   ans = strmid(strupcase(ans),0,1)

   if ans eq 'H' then begin
     read,'* Enter NS helio coordinates (N +, S -):  ',ns_input
     read,'* Enter EW helio coordinates (W +, E -):  ',ew_input

     print, '* Epoch time for specified coordinates:'
     input,'  ', epoch_time, epoch_time

; Rotate epoch time -- convert to sxt_x and sxt_y
     pixels = conv_h2p([ew_input, ns_input], epoch_time)
     x0 = pixels(0) / 2.^res  &  y0 = pixels(1) / 2.^res
   endif else begin
     x0 = 0 & y0 = 0
     read,'* Enter SXT image coordinates (x,y):', x0,y0
   endelse
endif else begin                               ; keyword_set(query)
   print,''
   print,' Left mouse button to select coordinate, right button to quit.'
   wait, 0.3
   cursor,x0,y0,/device
   button = !err    
endelse				               ; not_keyword_set(query)

while button ne 4 do begin
  x = fix(255 - x0*(2.^(res-2))/bin)
  y = fix(y0*(2.^(res-2))/bin)
  sign_x = fix(x/abs(x))>0 & sign_y = fix(y/abs(y))>0 
  helio = conv_p2h([x0,y0]*(2.^res)/bin,suncen=cen)
  xh = helio(0) & yh = helio(1)
  sign_xh = fix(xh/abs(xh))>0 & sign_yh = fix(yh/abs(yh))>0
  print,format='(33(" -"))'
  print,'ORIGINAL COORDINATES      :   ',fmt_tim(epoch_time)
  print,' ROI center (image coords):   x  = ' + $
    string(x0,format='$(i4)') + '     y  = ' + string(y0,format='$(i4)')
  print,' OR Coordinates           :   NS = ' + $
    string(y,format='$(i4)') + '     EW = ' + string(x,format='$(i4)')
  print,' Heliographic Coordinates :  ' + $
    sn(sign_yh) + string(abs(yh),format='$(f6.1)') + '      ' + $
    ew(sign_xh) + string(abs(xh),format='$(f6.1)')
  print,''
  if (not keyword_set(nomark)) then begin
    xl = (x0 - sside(0)/2/(2.^res)*bin) > 0 < (nx-1)
    yl = (y0 - sside(1)/2/(2.^res)*bin) > 0 < (ny-1)
    xu = (x0 + sside(0)/2/(2.^res)*bin) > 0 < (nx-1)
    yu = (y0 + sside(1)/2/(2.^res)*bin) > 0 < (ny-1)
    draw_boxcorn, xl,yl,xu,yu,color=color1,/device
  endif

  if n_elements(item2) ne 0 then begin
    ang = track_h2a(item2,helio,epoch_time)
    pix = conv_a2p(ang,suncen=cen)
    xt = fix(255 - pix(0)/4)
    yt = fix(pix(1)/4)
    sign_xt = fix(xt/abs(xt))>0 & sign_yt = fix(yt/abs(yt))>0 
    helio = conv_p2h(pix,suncen=cen)
    xht = helio(0) & yht = helio(1)
    sign_xht = fix(xht/abs(xht))>0 & sign_yht = fix(yht/abs(yht))>0
    print,'ROTATED COORDINATES       :    ',fmt_tim(item2)
    print,' Rotated OR Coordinates   :   NS = ' + $
      string(yt,format='$(i4)') + '     EW = ' + string(xt,format='$(i4)')
    print,' Heliographic Coordinates :  ' + $
      sn(sign_yht) + string(abs(yht),format='$(f6.1)') + '      ' + $
      ew(sign_xht) + string(abs(xht),format='$(f6.1)')
    print,''
    if (not keyword_set(nomark2)) then begin
      xlt = ((pix(0) - sside(0)/2)/(2.^res)*bin) > 0 < (nx-1)
      ylt = ((pix(1) - sside(1)/2)/(2.^res)*bin) > 0 < (ny-1)
      xut = ((pix(0) + sside(0)/2)/(2.^res)*bin) > 0 < (nx-1)
      yut = ((pix(1) + sside(1)/2)/(2.^res)*bin) > 0 < (ny-1)
      draw_boxcorn, xlt,ylt,xut,yut,color=color2,/device
    endif
  endif

  wait, 0.3
  print,' Left mouse button to select coordinate, right button to quit.'
  cursor,x0,y0,/device
  button = !err
  if keyword_set(qstop) then stop
endwhile

end
