pro fem_grid, saa=saa, ksc=ksc, noday=noday, $
	yrange=yrange, fillstyle=fillstyle, $
	fillnight=fillnight, fillsaa=fillsaa, $
	ncolor=ncolor, scolor=scolor, kcolor=kcolor, defcolor=defcolor
;
;+
;NAME:
;	fem_grid
;PURPOSE:
;	To overplot a grid of data held in the fem_data structure 
;	(days/nights/SAA) on a previously drawn utplot graph
;
;CALLING SEQUENCE:
;       fem_grid [,/fillsaa, /fillnight, /ksc, /defcolor]
;
;CALLING EXAMPLES:
;	fem_grid			; just vert lines for day/night
;	fem_grid, /saa			; include saa grid points
;       fem_grid, /ksc, /fillsaa, /defcolor	; fill saa, add KSC and color
;   	fem_grid, yrange=[.8*max(ydata),.9*max(ydata)]
;		  
;OPTIONAL KEYWORD PARAMTERS:
;   	yrange - 2 el. vector of yaxis grid range (def=full width=!y.crange)
;	saa -	if set, saa (radition belt) grid points are included
;	ksc -   if set, ksc contact time grid points are included       
;       fillnight - if set, polyfill is used to fill night times
;	fillsaa   - if set, polyfill is used to fill saa times
;	noday - if set, turns of day/night grid points
;       defcolor - make it a color plot using some default colors
;
;HISTORY:
;	Written SLFreeland, 21-Sep-1992 
;	22-Nov-1993 (SLF) added ncolor and scolor
;	 1-dec-1993 (JRL) Fixed the default ncolor and scolor option
;			  Changed it so it doesn't bomb if requested interval
;			  is less than 1 hour.
;        1-nov-1994 (SLF) allow ytype=1 (log)
;       10-nov-1994 (SLF) add additional check to 1-nov mod 
;        2-mar-1995 (SLF) enabled KSC keyword function, add defcolor keyword
;
;RESTRICTIONS - can get awfully cluttered for long time intervals
;		looks best with intervals less than 24 hours
;-
;
ksc=keyword_set(ksc) or keyword_set(kcolor)

if keyword_set(defcolor) then begin
   linecolors
   scolor=4
   ncolor=9
   kcolor=7
endif

if n_elements(ncolor) eq 0 then ncolor=!color
if n_elements(scolor) eq 0 then scolor=!color
if n_elements(kcolor) eq 0 then kcolor=!color

if not keyword_set(yrange) then $
   if !y.type and total(fix(abs(!y.crange))) ne 0.0 then $  
        yrange=10.^!y.crange else yrange=!y.crange

fillnight=keyword_set(fillnight)
if not keyword_set(fillstyle) then fillstyle=1

; get FEM data for window around plot data
getut, utbase=startt, /string			; utplot common block read 
startf=anytim2ints(startt,offset=-2.*60.*60)	; back up two hours
stopf=anytim2ints(startt, offset=!x.crange(1))	; last x.crange for stop
stopf=anytim2ints(stopf, offset= 2.*60.*60.)	; add on two hours (JRL)
rd_fem,startf,stopf,fem_data			; get fem data

; put FEM times in terms of utplot data
ref_time = startt
fday =   int2secarr(fem_data,ref_time)	;fem record start times=Yohkoh Day
fnight = fday + fem_data.night		; other FEM fields in seconds offset

eday=fday
enight=fnight
vday=where(fday gt 0,vdcount)
vnight=where(fnight gt 0,vncount)
if vdcount gt 0 then eday=eday(vday)
if vncount gt 0 then enight=enight(vnight)

; 

sday=shift(fday,-1)			;used for night fill 
nsday=n_elements(sday)			;(fill Nt(n)->Day(n+1)
sday(nsday-1) = !x.crange(1)		;last element


saast  = where(fem_data.st_saa,stcount)
saaen  = where(fem_data.en_saa,encount)
fsaast=0 & fsaaen=0
if stcount gt 0 then fsaast = fday(saast) + fem_data(saast).st_saa
if encount gt 0 then fsaaen = fday(saaen) + fem_data(saaen).en_saa

;
; set up logic for plotting loop
fillnight=keyword_set(fillnight)
fillsaa=keyword_set(fillsaa)
saa=(keyword_set(saa) or fillsaa) and (total(fsaast + fsaaen) gt 0)
day= 1 - keyword_set(noday)
;
if day then begin
   for i=0,n_elements(eday)-1 do $
      plots,replicate(eday(i),2) < !x.crange(1)  ,yrange, linestyle=1, color=ncolor
   for i=0,n_elements(enight)-1 do $
      plots,replicate(enight(i),2) < !x.crange(1),yrange, linestyle=1, color=ncolor
endif

if saa then begin
   for i=0,stcount-1 do $
      plots,replicate(fsaast(i)>0,2) < !x.crange(1) ,yrange, linestyle=4, color=scolor
   for i=0,encount-1 do $
      plots,replicate(fsaaen(i)>0,2) < !x.crange(1) ,yrange, linestyle=4, color=scolor
endif

; slf 2-mar-95 Enable KSC option (use newer generic routines, get_utevent and
;    evt_grid - if fem_grid were rewritten, it would call these routines.

if ksc then begin
   if n_elements(kcolor) eq 0 then kcolor=!p.color
   get_utevent,/ksc, k0, k1				; get KSC events
   evt_grid,k1,color=kcolor,linestyle=0			; overplot end times
   evt_grid,k0,color=kcolor, /vertical, $		; overplot/label start
      label=replicate('KSC',n_elements(k0)), linestyle=0, labpos=.8
endif

;
; now do filling of nights or saas if requested
;
yfill=[replicate(yrange(0),2),replicate(yrange(1),2)]	;use for all

if fillnight then begin
   nights=fnight
   if fday(0) gt fnight(0) then begin
      sday=[fday(0),sday]
      nights=[!x.crange(0),fnight]
   endif
   nights=nights < !x.crange(1)			;dont fill past plot
   nights=nights >0
   sday  =sday < !x.crange(1)			; don't fill past the plot
   for i=0,n_elements(nights)-2 do begin
         x=[nights(i),sday(i),sday(i),nights(i)] 
         if(x(0) lt x(1)) then $
           polyfill,x,yfill,orientation=45,/line_fill,spacing=.5, color=ncolor
   endfor
endif   


if fillsaa then begin
   if encount eq stcount+1 then $		; saa at starttime
         fsaast=[!x.crange(0),fsaast]
   for i=0,n_elements(fsaast)-1 do begin            
      x=[fsaast(i),fsaaen(i),fsaaen(i),fsaast(i)] < !x.crange(1)
      x = x > 0
      polyfill,x,yfill,orientation=-45,/line_fill,spacing=.5, color=scolor
   endfor
endif

;

return
end
