common draw_grid_blk, x_sys, y_sys, p_sys   ;MDM added 14-Jul-93

dum = findgen(32)*(!pi*2/32.)
usersym,2*cos(dum),2*sin(dum)

if n_elements(center) eq 0 then center = [0.,0.]
if n_elements(fov) eq 0 then fov = [2.5,2.5]
fov = float(fov)
if n_elements(img_siz) eq 0 then begin
  if !d.x_size ge !d.y_size then img_siz = [!d.y_size,!d.y_size] else $
  img_siz = [!d.x_size,!d.x_size]
endif
if n_elements(img_pos) eq 0 then img_pos = [0,0]
if n_elements(roll) eq 0 then roll = 0
if n_elements(tilt) eq 0 then tilt = 0
if n_elements(gridsep) eq 0 then gridsep = 15
if n_elements(mgridsep) eq 0 then mgridsep = 5
if n_elements(thick) eq 0 then thick = 1
if n_elements(res) eq 0 then res = 1
if n_elements(noerase) eq 0 then noerase = 0
if n_elements(color) eq 0 then color = 255

ymin = center(0) - fov(0)/2 & ymax = center(0) + fov(0)/2
zmin = center(1) - fov(1)/2 & zmax = center(1) + fov(1)/2

if noerase eq 0 then erase & noerase = 1

plot,[ymin,ymax],[zmin,zmax],/nodata,noerase=noerase, $
     color=color,thick=thick, $
     xstyle = 1+4, ystyle = 1+4, $
     xrange = [ymin,ymax], yrange = [zmin,zmax], $
     position = [img_pos(0),img_pos(1), $
             img_pos(0)+img_siz(0)-1, $
             img_pos(1)+img_siz(1)-1],/device

if keyword_set(draw_limb) then $
  oplot,cos(findgen(361)/!radeg),sin(findgen(361)/!radeg), $
           color=color,thick=thick
if not keyword_set(nogrid) then begin
  mk_grid_coords,gridsep,mgridsep,res,lat,lon,/deg
  n_plotpts = 360./res
  latc = reform(s2c(lat,roll=roll,b0=-tilt),3,n_plotpts,180/gridsep-1)
  lonc = reform(s2c(lon,roll=roll,b0=-tilt),3,n_plotpts,180/gridsep)

  oplot,cos(findgen(361)/!radeg),sin(findgen(361)/!radeg), $
           color=color,thick=thick
  for i = 0,180/gridsep-2 do begin
    xlat0 = reform(latc(0,0:n_plotpts/2,i))
    xlat1 = reform(latc(0,n_plotpts/2+1:*,i))
    ylat0 = reform(latc(1,0:n_plotpts/2,i))
    ylat1 = reform(latc(1,n_plotpts/2+1:*,i))
    zlat0 = reform(latc(2,0:n_plotpts/2,i))
    zlat1 = reform(latc(2,n_plotpts/2+1:*,i))
    goodlat0 = where((xlat0 ge 0) and (ylat0 ge ymin) and $
             (ylat0 le ymax) and (zlat0 ge zmin) and $
             (zlat0 le zmax),count_lat0)
    goodlat1 = where((xlat1 ge 0) and (ylat1 ge ymin) and $
             (ylat1 le ymax) and (zlat1 ge zmin) and $
             (zlat1 le zmax),count_lat1)
    if count_lat0 gt 0 then $
      oplot,ylat0(goodlat0),zlat0(goodlat0),color=color,thick=thick
    if count_lat1 gt 0 then $
      oplot,ylat1(goodlat1),zlat1(goodlat1),color=color,thick=thick
  endfor

  for i = 0,180/gridsep-1 do begin
    xlon0 = reform(lonc(0,0:n_plotpts/2,i))
    xlon1 = reform(lonc(0,n_plotpts/2+1:*,i))
    ylon0 = reform(lonc(1,0:n_plotpts/2,i))
    ylon1 = reform(lonc(1,n_plotpts/2+1:*,i))
    zlon0 = reform(lonc(2,0:n_plotpts/2,i))
    zlon1 = reform(lonc(2,n_plotpts/2+1:*,i))
    goodlon0 = where((xlon0 ge 0) and (ylon0 ge ymin) and $
             (ylon0 le ymax) and (zlon0 ge zmin) and $
             (zlon0 le zmax),count_lon0)
    goodlon1 = where((xlon1 ge 0) and (ylon1 ge ymin) and $
             (ylon1 le ymax) and (zlon1 ge zmin) and $
             (zlon1 le zmax),count_lon1)
    if count_lon0 gt 0 then $
      oplot,ylon0(goodlon0),zlon0(goodlon0),color=color,thick=thick
    if count_lon1 gt 0 then $
      oplot,ylon1(goodlon1),zlon1(goodlon1),color=color,thick=thick
  endfor
endif

if abs(carr_lon) le 360 then begin
;  carr_lon0 = get_carr(index)
;  carr_lon0 = (carr_lon0 - fix(carr_lon0))*360
  carr_lon0 = tim2clon(index)
  carr_lon0 = (1.-(carr_lon0 - fix(carr_lon0)))*360.
;  del_carr = carr_lon0 - carr_lon
  del_carr = carr_lon - carr_lon0
;  clon_phi = (fltarr(180./res) - del_carr(0))  ;line remove see next line  
  clon_phi = -(fltarr(180./res) - del_carr(0))  ;KTS & GAL sign convention correction
  clon_theta = (findgen(180./res)*res-90)
  clon_vec = transpose([[fltarr(180./res)+1],[clon_theta],[clon_phi]])
  clonc = s2c(clon_vec,roll=roll,b0=-tilt)
  xclon = reform(clonc(0,*))
  yclon = reform(clonc(1,*))
  zclon = reform(clonc(2,*))
  goodclon = where((xclon ge 0) and (yclon ge ymin) and $
     (yclon le ymax) and (zclon ge zmin) and $
     (zclon le zmax),count_clon)
  if count_clon gt 0 then $
    oplot,yclon(goodclon),zclon(goodclon),color=color,thick=3
endif  

;if n_elements(carr_vec) eq 4 then begin
;
;  if ((carr_vec(1) eq -90) and (carr_vec(3) eq 90)) then begin
;    lon_box = [fltarr(181)+carr_vec(0),fltarr(181)+carr_vec(2)]
;    lat_box = [findgen(181)-90,reverse(findgen(181)-90)]
;  endif
;  if ((carr_vec(1) eq -90) and (carr_vec(3) ne 90)) then begin
;    if carr_vec(2) gt carr_vec(0) then begin
;      lon_box = [fltarr(181)+carr_vec(0),findgen(fltarr(181)+carr_vec(2)]
;      lat_box = 
;
;
;
;  carr_lon0 = tim2clon(index)
;  carr_lon0 = (1.-(carr_lon0 - fix(carr_lon0)))*360.
;  del_carr = carr_lon - carr_lon0
;  clon_phi = (fltarr(180./res) + del_carr(0))
;  clon_theta = (findgen(180./res)*res-90)
;  clon_vec = transpose([[fltarr(180./res)+1],[clon_theta],[clon_phi]])
;  clonc = s2c(clon_vec,roll=roll,b0=-tilt)
;  xclon = reform(clonc(0,*))
;  yclon = reform(clonc(1,*))
;  zclon = reform(clonc(2,*))
;  goodclon = where((xclon ge 0) and (yclon ge ymin) and $
;     (yclon le ymax) and (zclon ge zmin) and $
;     (zclon le zmax),count_clon)
;  if count_clon gt 0 then $
;    oplot,yclon(goodclon),zclon(goodclon),color=color,thick=3
;;STOP
;endif  

if keyword_set(latlon) eq 1 then begin
  sz_latlon = size(latlon)
  npts_latlon = n_elements(latlon(0,*))
  rtp = transpose([[[fltarr(npts_latlon)+1]],[transpose(latlon)]])
  rtpc = s2c(rtp,roll=roll,b0=-tilt)
  for i=0,npts_latlon-1 do oplot,[rtpc(1,i),rtpc(1,i)], $
                 [rtpc(2,i),rtpc(2,i)],psym=8, $
                 color=color,thick=thick
endif

!X.STYLE = 1+4
!Y.STYLE = 1+4
!X.RANGE = [YMIN,YMAX]
!Y.RANGE = [ZMIN,ZMAX]
POS_NORM = CONVERT_COORD([IMG_POS(0),IMG_POS(0)+IMG_SIZ(0)-1], $
                         [IMG_POS(1),IMG_POS(1)+IMG_SIZ(1)-1], $
             /DEV, /TO_NORM)
!P.POSITION = POS_NORM(0:1,*)
;!P.POSITION = [IMG_POS(0),IMG_POS(1), $
;          IMG_POS(0)+IMG_SIZ(0)-1, $
;          IMG_POS(1)+IMG_SIZ(1)-1],/DEVICE

if keyword_set(qs) then stop

x_sys = !x      ;MDM added 14-Jul-93
y_sys = !y
p_sys = !p
return
end