pro plot_nar, ref_item, st_item, en_item, img=img, $
  roll=roll, showback=showback, erase=erase, noerase=noerase, $
  redisp=redisp, nogrid=nogrid, charsize=charsize, charthick=charthick, $
  thick=thick, hc=hc, qstop=qstop, noclear=noclear, text=text, $
  new=new, plot_pos=plot_pos, gridsep=gridsep, mag=mag, color=color, $
  drcurve=drcurve, howard=howard, allen=allen, sidereal=sidereal, $
  synodic=synodic, howsyn=howsyn, fill=fill, rect=rect, psym=psym, $
  nolabel=nolabel, unique=unique, overlay=overlay, clear=clear, $
  pfi_index=pfi_index, pfi_color=pfi_color, pfi_lstyle=pfi_lstyle, zbuffer=zbuffer

;+
;NAME:
;	plot_nar
;PURPOSE:
;	To plot positions of NOAA Active Regions on a heliocentric
;	grid
;CALLING SEQUENCE:
;	plot_nar, roadmap(i)
;	plot_nar, index(i)
;	plot_nar, '30-oct-91'
;INPUT:
;       ref_item - The reference time to which all feature coordinates
;	  will be differentially rotated before plotting on the grid.
;	  Form can be:
;	    (1) structure with a .time and .day field,
;	    (2) standard 7-element external representation,
;	 or (3) a string of the format "hh:mm dd-mmm-yy"
;	 If no date is entered, the current date is used.
;	 The year is not required, but if entered should be
;	 of the form 1-Oct-91 style.  The date should be entered
;	 in string style with date first.
;OPTIONAL INPUT:
;	st_item - The start time to search NOAA active region list for
;	  regions.  If not passed, only the time entries nearest to
;	  REF_ITEM are plotted.
;	en_item - The end time to search NOAA active region list for
;	  regions.  If not passed, the NOAA data base is searched for
;	  entries between ST_ITEM and REF_ITEM.
;OPTIONAL KEYWORD INPUT:
;	hc	- If set, write the results to a temporary file, print
;		  it and delete it.
;	text	- If set, list active region parameters
;OUTPUT:
;HISTORY:
;	Written 26-Mar-93 by G. L. Slater
;               10-may-93 (slf) added pfi_color, pfi_lstyle keywords
;		 1-Jun-93 (MDM) Added correction for the placement of the SXT
;				PFI window.
;		21-jun-93 (gal) quick bug fix for case where frontpts are
;				undefined [e.g. frontpts=where(x_coord ge 0)].
;		14-Jul-93 (MDM) Added draw_grid common block
;		16-Oct-93 (MDM) Removed call to GET_SUNCENTER and replaced with
;				a call to SXT_CEN
;                3-Jul-94 (SLF) Fix bug in pfi resolution, only do 'uniq' pfis
;				since pfi code is slow
;                4-Jul-94 (SLF) use gt_corner_cmd
;		14-Jul-94 (SLF) Clean up, allow z-buffer, use savesys,restsys
;		18-Jul-94 (SLF) do uniq pfi outside this routine
;		29-Mar-96 (GLS) Added /nosavrest to DRAW_GRID call
;				Corrected wset problem
;
;QUESTIONS, PROBLEMS, THINGS TO DO:
;	- Properly position window at creation, using screen size
;-
common draw_grid_blk, x_sys, y_sys, p_sys       ;MDM added 14-Jul-93

devsave=!d.name
if keyword_set(zbuffer) then set_plot,'z'	
savesys,/aplot			; save !x,!y,!z,!p
win_save = !d.window            ; Save the current window index
xsiz_save = !d.x_size
ysiz_save = !d.y_size

sec_per_pix = 2.453
rsun = 6.96d10			; Radius of Sun in cm
cmperdeg = 1d/!radeg*rsun	; cm per degree on photosphere

ew_arr = ['E', 'W']
ns_arr = ['S', 'N']
drstr = ['allen','howard','sidereal','synodic','howsyn']
xuc = cos(findgen(33)*2d*!pi/32d)
yuc = sin(findgen(33)*2d*!pi/32d)
wnar = 31
wx0 = 512 & wy0 = 512
wx1 = 512 & wy1 = 768

if keyword_set(hc) then begin
  titlefac = 1.4
  frontfac = 1.2
  rearfac =  1.1
  hdrfac =   0.9
  tablefac = 0.8
  spotfac =  3.0
endif else begin
  titlefac = 1.1
  frontfac = 1.0
  rearfac =  0.9
  hdrfac =   0.8
  tablefac = 0.8
  spotfac =  2.0
endelse
if n_elements(fill) eq 0 then fill = 1

if not keyword_set(noclear) then restsys,/aplot,/init

if n_elements(redisp) eq 0 then redisp = 0
if keyword_set(hc) and n_elements(gridsep) eq 0 then gridsep = 10 $
  else if n_elements(gridsep) eq 0 then gridsep = 15
if n_elements(nogrid) eq 0 then nogrid = 0
if n_elements(roll) eq 0 then roll = 0
if n_elements(charsize) eq 0 then charsize = 1
if n_elements(charthick) eq 0 then charthick = 1.2
if n_elements(thick) eq 0 then thick = 1.2
if n_elements(psym) eq 0 then psym = 1
if keyword_set(hc) and n_elements(color) eq 0 then color = 0 $
  else if n_elements(color) eq 0 then color = 255
if n_elements(pfi_color) eq 0 then pfi_color=color
if n_elements(pfi_lstyle) eq 0 then pfi_lstyle=!p.linestyle
if n_elements(unique) eq 0 then unique = 1
if n_elements(file) eq 0 then file = 'plot_nar.tmp'
if n_elements(drcurve) eq 0 then drcurve = 3
if n_elements(howard) eq 0 then howard = 0
if n_elements(allen) eq 0 then allen = 0
if n_elements(sidereal) eq 0 then sidereal = 0
if n_elements(synodic) eq 0 then synodic = 0
if n_elements(howsyn) eq 0 then howsyn = 1


if n_elements(ref_item) eq 0 then begin
  ref_tim = !stime
  typ_ref_item = -1
endif else begin
  siz_ref_item = size(ref_item)
  typ_ref_item = siz_ref_item( siz_ref_item(0)+1 )
  ref_tim = fmt_tim(anytim2ex(ref_item(0)))
  tdiff = addtime(!stime,diff=ref_tim)
  if tdiff lt 0 then begin
    st_item = !stime & en_item = fmt_tim(addtime(!stime,delta_min=24d*60-1))
  endif
endelse
if n_elements(st_item) eq 0 then begin
  sttim = ref_tim & entim = ref_tim
endif else sttim = fmt_tim(anytim2ex(st_item(0)))
if n_elements(en_item) eq 0 then begin
  tdiff = addtime(sttim,diff=ref_tim)
  if tdiff ge 0 then begin
    entim = sttim & sttim = ref_tim
  endif else entim = ref_tim
endif else entim = fmt_tim(anytim2ex(en_item(0)))

if n_elements(erase) eq 0 then erase = 0
if ((typ_ref_item eq 8) and (n_elements(noerase) eq 0)) then noerase = 1

img_pos = fltarr(2) & img_siz = fltarr(2)

if (not keyword_set(text)) and (not keyword_set(hc)) and $
   (n_elements(ref_time) eq 0) then begin
  if (!d.window eq -1) and not keyword_set(zbuffer) then wdef,wnar,wx0,wy0,/uright
  img_siz(0) = min([!d.x_size,!d.y_size])
  img_siz(1) = img_siz(0)
  img_pos = [0,0]
endif

if (keyword_set(text)) and (not keyword_set(hc)) then begin
  img_siz(0) = wx1
  img_siz(1) = wx1
  img_pos = [0,wy1-wx1]
  if not keyword_set(zbuffer) then wdef,wnar,wx1,wy1,/uright
  if (keyword_set(overlay)) and (typ_ref_item eq 8) then begin
      case 1 of
         keyword_set(zbuffer): begin
            olddat=tvrd(0,0,xsiz_save,ysiz_save)
            wdef,wnar,wx1,wy1,/zbuffer
            tv,olddat,img_pos(0),img_pos(1)
         endcase
         win_save ne -1: $
	   device,copy=[0,0,xsiz_save,ysiz_save,img_pos(0),img_pos(1),win_save]
         else:
      endcase
   endif
endif

if keyword_set(hc) then begin
  set_plot,'ps'  
  device,/portrait,file=file
  xfrac = 1.0 & yfrac = 1.
  xmarg = 0.2 & ymarg = 0.0
;  img_siz(0) = xfrac*!d.x_size
  IMG_SIZ(0) = XFRAC*!D.Y_SIZE	; KLUGE - DON'T UNDERSTAND - GLS
  img_siz(1) = img_siz(0)
  img_pos(0) = (1-xfrac)*!d.x_size
  IMG_POS(0) = (!D.X_SIZE - !D.Y_SIZE)/2
  img_pos(1) = !d.y_size - img_siz(1) -(1-yfrac)*!d.y_size
endif

;xchar_per_cm = 4.4
XCHAR_PER_CM = 3.2	; KLUGE
if keyword_set(hc) or keyword_set(zbuffer) then begin
  xchar_per_cm0 = !d.x_px_cm/!d.x_ch_size
  charfac = xchar_per_cm0/xchar_per_cm
  xpix_per_char = charsize*charfac*!d.x_ch_size
  ypix_per_char = charsize*charfac*!d.y_ch_size
endif else begin
  xpix_per_char0 = font_size(1,ypix_per_char0)
  charfac = 9./xpix_per_char0
  xpix_per_char = charsize*charfac*xpix_per_char0
  ypix_per_char = charsize*charfac*ypix_per_char0
endelse

charsize = charsize*charfac
rd_nar,sttim,entim,nar_data,/near

siz_nar_data = size(nar_data)
typ_nar_data = siz_nar_data( siz_nar_data(0)+1 )
if typ_nar_data ne 8 then begin
  print,' Data not available for this date.'
  return
endif
ntotal = n_elements(nar_data)
noaa_num_all = nar_data.noaa

ss = indgen(ntotal)
if keyword_set(unique) then ss = uniqo(noaa_num_all)

timstrarr = fmt_tim(nar_data(ss))
npoints = n_elements(ss)
timabbrv = timstrarr
for i=0,npoints-1 do $
  timabbrv(i) = strmid(timstrarr(i),0,2) + strmid(timstrarr(i),3,3) + $
		strmid(timstrarr(i),7,2)
noaa_num = nar_data(ss).noaa
lon_reg = nar_data(ss).location(0)
lat_reg = nar_data(ss).location(1)
area = nar_data(ss).area
carr_lon = nar_data(ss).longitude
nspots = nar_data(ss).num_spots
z = string(nar_data(ss).st$macintosh)
long_ext = nar_data(ss).long_ext
mag_type = string(nar_data(ss).st$mag_type)
rb0p_ref = get_rb0p(ref_tim) & b0_ref = rb0p_ref(1)*!radeg

deltas = int2secarr(anytim2ex(nar_data(ss)), ref_tim)
if howsyn ne 0 then del_lon = dr_photo(lat_reg,deltas/86400d) else $
  del_lon = diff_rot(deltas/86400d,lat_reg,allen=allen,howard=howard, $
	    sidereal=sidereal,synodic=synodic)
lon_reg = (lon_reg + (-del_lon mod 360)) mod 360
big = where(lon_reg gt 180)
if big(0) ne -1 then lon_reg(big) = lon_reg(big) - 360
little = where(lon_reg lt -180)
if little(0) ne -1 then lon_reg(little) = lon_reg(little) + 360
latlon = transpose([[lat_reg],[lon_reg]])
sign_lat = lat_reg eq abs(lat_reg)
sign_lon = lon_reg eq abs(lon_reg)

rtp = transpose([[[fltarr(npoints)+1]],[transpose(latlon)]])
rtpc = s2c(rtp,roll=roll,b0=-b0_ref)
x_coord = reform(rtpc(0,*))
frontpts = where(x_coord ge 0) & backpts = where(x_coord lt 0)

if keyword_set(rect) then begin
  plot,lon_reg,lat_reg,psym=psym,xrange=[-180,180],yrange=[-90,90], $
    xstyle=1,ystyle=1
  for i = 0,n_elements(frontpts)-1 do begin
    xspot = xuc*sqrt(area(frontpts(i))*2e-6)*rsun/cmperdeg + $
	    lon_reg(frontpts(i))
    yspot = yuc*sqrt(area(frontpts(i))*2e-6)*rsun/cmperdeg + $
	    lat_reg(frontpts(i))
    if fill eq 1 then polyfill,xspot,yspot,/data,color=color else $
      oplot,xspot,yspot,thick=thick,color=color
    if not keyword_set(nolabel) then $
      xyouts,lon_reg(frontpts(i))+.05,lat_reg(frontpts(i))+.05,/data, $
	     strtrim(noaa_num(frontpts(i)),2),charsize=charsize*frontfac, $
	     charthick=charthick,color=color
  endfor
  if keyword_set(showback) ne 0 then begin
    for i = 0,n_elements(backpts)-1 do begin
      xspot = xuc*sqrt(area(backpts(i))*2e-6)*rsun/cmperdeg + $
	      lon_reg(backpts(i))
      yspot = yuc*sqrt(area(backpts(i))*2e-6)*rsun/cmperdeg + $
	      lat_reg(backpts(i))
      oplot,xspot,yspot,thick=thick,color=color
      if not keyword_set(nolabel) then $
	xyouts,lon_reg(backpts(i))+.05,lat_reg(backpts(i))+.05,/data, $
	  strtrim(noaa_num(backpts(i)),2),charsize=charsize*rearfac, $
	  charthick=charthick,color=color
    endfor
  endif
endif else begin
  if (typ_ref_item eq 8) and (erase ne 1) then $
    if n_elements(img) gt 0 then $
      sxt_grid,ref_item,img,noerase=noerase,thick=thick,img_pos=img_pos, $
      redisp=redisp,nogrid=nogrid,gridsep=gridsep,mag=mag,color=color, $
      pdata=pdata $
    else $
      sxt_grid,ref_item,noerase=noerase,thick=thick,img_pos=img_pos, $
      redisp=redisp,nogrid=nogrid,gridsep=gridsep,mag=mag,color=color, $
      pdata=pdata $
  else begin
    if erase eq 1 then erase
    draw_grid,roll=roll,tilt=b0_ref,gridsep=gridsep,mgridsep=mgridsep, $
      noerase=noerase,nogrid=nogrid,img_pos=img_pos,img_siz=img_siz, $
      color=color,thick=thick,/nosavrest
    xyouts,0,1.1,align=.5,/data,ref_tim,charsize=charsize*titlefac, $
	   charthick=charthick
  endelse

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

; KLUGE - SPOT RADII CURRENTLY INCREASED BY EMPIRICAL FACTOR OF 'SPOTFAC'
  if frontpts(0) ne -1 then begin		;quick --bug fix....
  for i = 0,n_elements(frontpts)-1 do begin
    xspot = $
      xuc*sqrt((area(frontpts(i))>15)*spotfac*2e-6)+rtpc(1,frontpts(i))
    yspot = $
      yuc*sqrt((area(frontpts(i))>15)*spotfac*2e-6)+rtpc(2,frontpts(i))
    if fill eq 1 then polyfill,xspot,yspot,/data,color=color else $
      oplot,xspot,yspot,thick=thick,color=color
    if not keyword_set(nolabel) then $
      xyouts,rtpc(1,frontpts(i))+.05,rtpc(2,frontpts(i))+.05,/data, $
	strtrim(noaa_num(frontpts(i)),2),charsize=charsize*frontfac, $
	charthick=charthick,color=color
  endfor
  endif
  if keyword_set(showback) ne 0 then begin
    for i = 0,n_elements(backpts)-1 do begin
      xspot = xuc*sqrt(area(backpts(i))*spotfac*2e-6)+rtpc(1,backpts(i))
      yspot = yuc*sqrt(area(backpts(i))*spotfac*2e-6)+rtpc(2,backpts(i))
      oplot,xspot,yspot,thick=thick,color=color
      if not keyword_set(nolabel) then $
	xyouts,rtpc(1,backpts(i))+.05,rtpc(2,backpts(i))+.05,/data, $
	  strtrim(noaa_num(backpts(i)),2),charsize=charsize*rearfac, $
	  charthick=charthick,color=color
    endfor
  endif

; slf - allow pfi_index to be a vector

  for i=0,n_elements(pfi_index)-1 do begin
;   should only do uniq set  
    pfiindex=pfi_index(i)

    pfi_rb0p = get_rb0p(pfiindex)
    pfi_r_pix = pfi_rb0p(0)/sec_per_pix
    pfi_res = gt_res(pfiindex)
    pfi_siz = gt_shape_cmd(pfiindex)
    fov = pfi_siz*(2^pfi_res)/pfi_r_pix
    pfi_corn = float(gt_corner_cmd(pfiindex))
    pfi_corn(1) = pfi_corn(1) - (gt_or_expnum(pfiindex)-1)*64		;MDM added 1-Jun-93
    pfi_corn(0) = 1024 - pfi_corn(0) - pfi_siz(0)*(2^pfi_res)
;    pfi_corn = pfi_corn*(2^pfi_res)		; slf removed 
    pfi_sunc = sxt_cen(pfiindex)		;MDM added 16-Oct-93

    pfi_cen = pfi_sunc(0:1,*)
    pfi_corn_norm = (pfi_corn - pfi_cen)/pfi_r_pix
    x_pfi_box = [0,0,fov(0),fov(0),0] + pfi_corn_norm(0)
    y_pfi_box = [0,fov(1),fov(1),0,0] + pfi_corn_norm(1)
    oplot,x_pfi_box,y_pfi_box,thick=thick,color=pfi_color,linestyle=pfi_lstyle
  endfor
endelse

if keyword_set(text) then begin
  ew_string = ['E','W']
  ns_string = ['S','N']
  ystart = !d.y_size - !d.x_size
; KLUGE - DON'T UNDERSTAND - GLS
  IF KEYWORD_SET(HC) THEN YSTART = 1000
  if max(strlen(strtrim(mag_type,2))) gt 5 then xtextmarg = 3 else $
    xtextmarg = 5
  ytextmarg = 3
  xspace = 2
  xtextpos = [xtextmarg]
  xtextpos = [xtextpos,xtextpos( 0)+7+1]
  xtextpos = [xtextpos,xtextpos( 1)+4+1]
  xtextpos = [xtextpos,xtextpos( 2)+1]
  xtextpos = [xtextpos,xtextpos( 3)+2+1]
  xtextpos = [xtextpos,xtextpos( 4)+1]
  xtextpos = [xtextpos,xtextpos( 5)+3+1]
  xtextpos = [xtextpos,xtextpos( 6)+3+1]
  xtextpos = [xtextpos,xtextpos( 7)+4+1]
  xtextpos = [xtextpos,xtextpos( 8)+3+1]
  xtextpos = [xtextpos,xtextpos( 9)+2+1]
  xtextpos = [xtextpos,xtextpos(10)+2+xspace]
  deltapos = [1,1,1,0,0,0,0,1,1,0,0,-1]
  titlepos = xtextpos + deltapos

  xtextpos = xtextpos*xpix_per_char
  titlepos = titlepos*xpix_per_char

  title = $
    ['Date','AR','Location','','','']
  title = [title,'Carr','Area','Z','LL','NN','MagType']
  for i = 0,n_elements(title)-1 do $
    xyouts,titlepos(i),ystart-(ytextmarg+1)*ypix_per_char,/dev, $
	   strtrim(title(i),2),charsize=charsize*hdrfac, $
	   charthick=charthick
  for j = 0,npoints-1 do begin
    textarr = [strmid(timabbrv(j),0,9),string(noaa_num(j),'$(i4)'), $
      ns_string(sign_lat(j)), $
      string(abs(lat_reg(j)),'$(i2)'), $
      ew_string(sign_lon(j)), $
      string(abs(lon_reg(j)),'$(i3)'), $
      string(carr_lon(j),'$(i3)'),string(area(j),'$(i4)'), $
      strtrim(z(j),2),string(long_ext(j),'$(i2)'), $
      string(nspots(j),'$(i2)'),strtrim(mag_type(j),2)]
    for i = 0,11 do $
      xyouts,xtextpos(i),ystart-(ytextmarg+j*1.5+3)*ypix_per_char,/dev, $
	     textarr(i),charsize=charsize*tablefac,charthick=charthick
  endfor
endif


if keyword_set(hc) then pprint,file,/delete
if (keyword_set(qstop)) then stop

; Reset various system variables to entry values:
restsys,/aplot
if (not keyword_set(hc) and (win_save ne -1)) then wset,win_save
if keyword_set(clear) then restsys,/aplot,/init
set_plot,!d.name
return
end

