savesys,/aplot
if n_elements(mag) eq 0 then mag = 1
if n_elements(img_pos) eq 0 then img_pos = [0,0]
if n_elements(gridsep) eq 0 then gridsep = 15
if n_elements(mgridsep) eq 0 then mgridsep = 5
if n_elements(noerase) eq 0 then noerase = 1
if n_elements(refit) eq 0 then refit = 0
if n_elements(read_out) eq 0 then read_out = 0
if n_elements(fudge) eq 0 then stdfudge = [0,0] else stdfudge = [-11,0]
if n_elements(xfudge) eq 0 then xfudge = 0
if n_elements(yfudge) eq 0 then yfudge = 0
if n_elements(nogrid) eq 0 then nogrid = 0
if n_elements(draw_limb) eq 0 then draw_limb = 0
if n_elements(carr_lon) eq 0 then carr_lon = 361
;carr_lon0 = get_carr(index)
carr_lon0 = tim2clon(index)
carr_lon0 = (1.-(carr_lon0 - fix(carr_lon0)))*360.
;del_carr = carr_lon0 - carr_lon
if n_elements(thick) eq 0 then thick = 1

img_res = gt_res(index)
sec_per_pix = 2.453
rb0p = makvec(get_rb0p(index))
r_pix = rb0p(0)/sec_per_pix     ;radius in pixels
tilt = rb0p(1)*!radeg
img_siz = gt_shape(index)
fov = img_siz*(2^img_res)/r_pix
;;img_corner = float(index.sxt.corner_cmd)
;;img_corner(0) = 1024 - img_corner(0) - img_siz(0)*(2^img_res)
img_corner = gt_corner(index)   ;Corner in FR pixels    - MDM added 5-Aug-93    
;;img_corner = img_corner*(2^img_res)   ;MDM removed 7-Oct-93

if n_elements(center) eq 0 then begin
;;;  if keyword_set(get_center) eq 0 and n_elements(pdata) eq 0 then $          ;MDM removed 7-Oct-93
;;;         get_pnt,index,pdata,offset=offset else offset = 3600
;;;  if (offset(0) lt 3600) and (keyword_set(get_center) eq 0) then begin
;;;     if (n_elements(roll) eq 0) then roll = pdata.sc_pntg(0)/36000.
;   sxt_cen = gt_hxa(pdata,/sxtpix)
;   sunc = hxa_suncenter(pdata,index=index,code=hxacode)
;   sunc = get_suncenter(pdata,index=index,code=code)

    if (keyword_set(get_center) eq 0) then begin
    sunc = sxt_cen(index, roll=roll)
    sxt_cen = sunc(0:1,*)
    endif else begin
    ans0 = ''
    if keyword_set(get_center) eq 0 then begin
        print,' Pointing data unavailable for this time.'
        read, ' Do you want to fit the limb to find the center (def is yes)?', ans0
    endif
    if (strupcase(strmid(ans0,0,1)) ne 'N') or (keyword_set(get_center) eq 1) then begin
        if n_elements(data) eq 0 then begin
        print,' Cannot fit limb - DATA variable not supplied to SXT_GRID.'
        return
        endif
        a = get_xyrad(data, radius_fix=r_pix/(2^img_res)*mag,refit=refit)
        sxt_cen = [a(0),a(1)]*(2^img_res)/mag
    endif else begin
        print,' Stopping - no pointing data.'
        return
    endelse
    endelse

    center = (img_corner + img_siz*(2^img_res)/2 - sxt_cen)/r_pix       ; (FR pix) / r_pix
    center(0) = center(0) + (stdfudge(0) + xfudge*(2^img_res)/mag)/r_pix
    center(1) = center(1) + (stdfudge(1) + yfudge*(2^img_res)/mag)/r_pix
endif

if ((keyword_set(redisp) eq 1) and (n_elements(data) ne 0)) then $
  tvscl,data else $
  if ((keyword_set(redisp) eq 1) and (n_elements(data) eq 0)) then $
  message,/info,' Cannot re-display image because DATA parameter not passed.'
if keyword_set(latlon) eq 0 then $
  draw_grid_2,center,fov,img_siz=img_siz*mag,img_pos=img_pos, $
    roll=roll,tilt=tilt,gridsep=gridsep,mgridsep=mgridsep, $
    noerase=noerase,nogrid=nogrid,draw_limb=draw_limb,carr_lon=carr_lon, $
    index=index,color=color,thick=thick else $
  draw_grid_2,center,fov,img_siz=img_siz*mag,img_pos=img_pos, $
    roll=roll,tilt=tilt,gridsep=gridsep,mgridsep=mgridsep, $
    noerase=noerase,nogrid=nogrid,draw_limb=draw_limb,carr_lon=carr_lon, $
    index=index,latlon=latlon,color=color,thick=thick

if keyword_set(read_out) ne 0 then begin
  device,get_window = win_coords
; slf - use wdef to avoid error when wset,newwindow-1
  oldwindow=!d.window 
  wdef,new_window,xpos=win_coords(0),ypos=win_coords(1),256,32
  last_lat = '' & last_lon = ''
  !err = 0
  while !err ne 4 do begin
    wset,oldwindow
    cursor,y0,z0,2,/data    ; Note wait_mode = 2
    temp = y0*y0+z0*z0
    if (keyword_set(angle)) then begin  ;MDM added 1-Dec-93
      ang = [y0, z0]*r_pix*sec_per_pix  ;convert from units of solar radius to arcseconds (E/W, N/S)
      lat = 'N/S: ' + string(ang(1), format='(f6.0)')
      lon = 'E/W: ' + string(ang(0), format='(f6.0)')
      last_latlon = ang
    endif else begin
      if temp gt 1 then begin
    lat = "You're off the Sun.                   "
    lon = ''
      endif else begin
        x0 = sqrt(1 - temp)
        rtp0 = c2s([x0,y0,z0],b0=-tilt)
        lat = 'LAT: ' + string(rtp0(1),format='$(f5.1)')
        lon = 'LON: ' + string(rtp0(2),format='$(f5.1)')
    if (keyword_set(read_carr)) then begin
;PRINT,"Carr lon for CM and delt from CM:", carr_lon0, RTP0(2)
      rtp0(2)=carr_lon0+rtp0(2)
      if rtp0(2) lt 0 then rtp0(2)=rtp0(2)+360.
      if rtp0(2) gt 360 then rtp0(2) = rtp0(2)-360. ;GAL added to correct for 360 roll over
      lat = ' LAT: ' + string(rtp0(1),format='$(f5.1)')
          lon = 'CLON: ' + string(rtp0(2),format='$(f6.1)')
;if rtp0(2) gt 365 then STOP
    endif
        last_latlon=[rtp0([2,1])]   ;E/W,N/S
      end
    endelse
    wset,new_window
    xyouts,10,10,last_lat,/device,color=0,charsize=1.5,charthick=1.5
    xyouts,138,10,last_lon,/devic,color=0,charsize=1.5,charthick=1.5
    xyouts,10,10,lat,/device,charsize=1.5,charthick=1.5
    xyouts,138,10,lon,/device,charsize=1.5,charthick=1.5
    last_lat = lat & last_lon = lon
  endwhile
  wdelete,new_window
  wset,oldwindow
  print, 'The optional value in LAST_LATLON is [E/W,N/S] = ' + string(last_latlon, format='(2f6.1)')
endif
restsys,/aplot

end