pro lastsfd, index, data, nimg, nar=nar, info=info, notv=notv, obsr=obsr, $
	color=color, sfr=sfr, stepper=stepper, allobs=allobs, $
	oldfile=oldfile, rebin=rebin, usefile=usefile, mag=mag, $
	spr=spr, spd=spd, select=select, zbuffer=zbuffer, halfres=halfres, $
        shorttitle=shorttitle, nogrid=nogrid, notitle=notitle, text=text, $
        movie=movie, goes=goes, month=month, ssc=ssc, sss=sss
;+
;   Name: lastsfd
;
;   Purpose: read and display most recent sfd or sfr image(s) 
;
;   Output Paramters:
;      index - sxt index
;      data  - sxt data (image or cube)
;
;   Input Parameters
;      nimg (optional) - number to read (default is 1) - last nimg images read
;
;   Optional Keyword Parameters:
;      nar -   if set, overlay noaa active regions
;      notv -  if set, just return index/data (no image display)
;      obsr -  if set,  read corresponding spr and overlay observing region
;      color - if set, load color table 3, tone down grid brighteness 
;      sfr   - if set, use sfr, not sfd
;      spr   - if set, usd spr (PFI) files, not sfd
;      spd   - if set, usd spd (processed PFI) files, not sfd
;      rebin - if set, rebin to size (512 or 256 only)
;      halfres - if set, select most recent Half Res image (if available) 
;                according to the following priority...
;		 1:AlMg(half)  2:-Al.1(half) 3:Quarter(last image in file)
;                Note - /halfres is ignored if nimg is present
;
;   Calling Examples:
;      lastsfd, /movie [,/goes]     ; last ~ 30 imgs "prepped" , spawn xstepper
;      lastsfd, /nar		    ; display sxt image and overlay NOAA regions
;      lastsfd, /nar,/obsr	    ; same with recent OR    
;      lastsfd, /nar,/obsr,/color   ; same with standard colors
;      lastsfd, index,data,10,/notv ; return index/data for last 10 sfd images
;      lastsfd, index,data,10	    ; same with display of last
;      lastsfd, index,data,5,/sfr,/stepper ; use sfr files - select w/stepper
;      lastsfd, /allobs	    	    ; boxes for uniq sprs in corresponding file
;      lastsfd, /allobs,/oldfile    ; same in next to last file
;      lastsfd, /allobs, oldfile=5  ; same in 5th to last file
;      lastsfd, /spd,index,data,50  ; return last 50 SPD (pre-processsed pfi)
;      lastsfd, index,data,/half    ; Last Half-Res image if available
;      lastsfd, index,data,/sss     ; access 'sss' data base (8bit log)
;      lastsfd, index,data,/ssc     ; access 'ssc' data base (floating)
;
;   Calls newsfd.pro to get recent SFD image(s)
;
;   Side effects:
;      may open new window
;
;   History:      7-May-1993 SLF (Written)
;	         10-may-1993 add obsr, color
;		 13-may-1993 add stepper keyword and sfr
;		 30-aug-1993 add /allobs keyword
;			     (filter out night PFIs)
;		  9-Dec-1993 add /spd and /spr switches
;                14-Jul-1994 zbuffer switch to plot_nar
;		 17-Jul-1994 Filter out Non-Xray images in obs plot
;			     (dont display dark-cal / diffuser)
;                 1-Dec-1994 (SLF) update header (spd,spr,half keywords)
;                22-aug-1995 (SLF) made /half the default
;                30-aug-1995 (SLF) fixed bug caused by 22-aug (3rd paramter caused crash)
;                10-feb-1997 (SLF) add /MOVIE and /GOES keywords
;                8-Aug-2000 (SLF) add /SSC and /SSS
;-

if keyword_set(movie) then begin
   lastsfdmovie, index, data, goes=goes, month=month, notv=notv
   return                                             ; EARLY EXIT!!
endif 

halfres=(1-keyword_set(nimg))

if n_elements(nimg) eq 0 then nimg=1
if not keyword_set(rebin) then rebin=0

if rebin eq 256 then charsize=1.5

data=newsfd(/last,index,info,nimg=nimg,mag=mag,sfr=sfr,sfds=sfds, $
		oldfile=oldfile, rebin=rebin, usefile=usefile, $
		spr=spr, ssc=ssc, sss=sss, spd=spd, select=select, halfres=halfres)
if not keyword_set(notv) then begin
;  then we need a window
   if (!d.window eq -1 and !d.name eq 'X') or keyword_set(zbuffer)  then $
	wdef,free_win,512,/ur, zbuffer=zbuffer


   short=keyword_set(shorttitle) or rebin eq 256
   stit='YOHKOH/SXT' + ' ' + fmt_tim(index)   + ' ' + $
       gt_filtb(index(0),/string) + ' ' + gt_res(index,/string) 

   
   if rebin eq 256 then stit='Yohkoh/SXT ' + gt_time(index,/string) else $
      if short then charsize=2.
   if keyword_set(notitle) then stit=''
   if !d.name eq 'Z' then charsize=charsize*.6
   if keyword_set(stepper) then begin	; allow stepper selection
      stepper,data,info,lastss=lss
      nindex=index(lss)
   endif else begin
      if nimg eq 1 then begin
         tvscl, data
         if short then xyouts, 10,10, stit, charsize=charsize,/device else $
            stepper_title,info(0), charsize=charsize
         nindex=index(0)
      endif else begin
         tvscl,data(*,*,nimg-1)
         if short then xyouts, 10,10, stit, charsize=charsize else $
            stepper_title,info(nimg-1), charsize=charsize
         nindex=index(nimg-1)
      endelse 
   endelse
   color=keyword_set(color) or keyword_set(allobs) 
   obsr=keyword_set(obsr) or keyword_set(allobs)
   if keyword_set(obsr) then begin
      pfi_color=!p.color	 ; emphasize OR if displayed      
      ncolor=(abs(pfi_color-45))
   endif

;   if keyword_set(color) then loadct,3
 
   text=keyword_set(text)

;   if keyword_set(obsr) and 1-keyword_set(nar) then text=0 else text=1
;  since OR drawn via plot_nar, fake or only via color=0 and no text (kludge!)
   nar=keyword_set(nar) or keyword_set(obsr)	
   if keyword_set(nar) then begin
      if obsr then begin
         null=newsfd(/nodata,sfds=sfds,sfr=sfr,oldfile=oldfile, $
		mag=mag , rebin=rebin, usefile=usefile)
         lastsfx=sfds(n_elements(sfds)-1)
         sprf=lastsfx
         if keyword_set(sfr) then sprf=str_replace(sprf,'sfr','spr') else $
             sprf=str_replace(sprf,'sfd','spr')
         if file_exist(sprf) then begin
            rd_roadmap,sprf,rmap,ndsets
            if keyword_set(allobs) then set=indgen(ndsets) else set=(tim2dset(rmap,nindex))(0)
            rd_sda,sprf,set,pindex,/nodata
;	    filter out non-uniq pointings
	    pindex=pindex(sxt_uniq(pindex))
;	    filter out bad pointings due to occulted patrol image
            tim2orbit,pindex,tim2night=tim2night
            dayimages=where(tim2night gt 1.0,dcount)
	    nightimages=where(tim2night le 1.0,ncount)
            if dcount gt 0 and ncount gt 0 then begin
               message,/info,  $
		 'Ignoring ' + strtrim(ncount,2) + $
		    ' images taken within 1 minute of sunset'
               pindex=pindex(dayimages) 
            endif 
            xrimage=xrayss(pindex,xcount)
            if xcount gt 0 then pindex=pindex(xrimage) else pindex=''
         endif else begin
            message,/info,'Cant find corresponding SPR file for OR display..
         endelse
      endif
      
      plot_nar,nindex,/overlay,mag=mag(0), nogrid=nogrid, $
         pfi_index=pindex, pfi_lstyle=2, pfi_color=pfi_color, color=ncolor, $
         fill=0, text=text, charsize=charsize, zbuffer=zbuffer
;     plot_nar called with /text will open another window - so delete
;     first window if it was opened by this routine (not preexisting)
      if n_elements(free_win) gt 0 and !d.window eq 31 then wdelete,free_win
   endif

endif

return
end
