pro res_freq, startt, stopt, reshist=reshist, file=file

;

common res_freq_blk, window

if n_elements(startt) eq 0 then startt='1-jan-94'
if n_elements(stopt) eq 0 then stopt=!stime

day0=gt_day(startt)
dayn=gt_day(stopt)
nsamp=dayn-day0+1
timegrid=anytim2ints(startt,off=lindgen(nsamp)*24.*60.*60)

reshist=replicate({time:0l, day:0, reshist:fltarr(3)},nsamp)
reshist.day=timegrid.day & reshist.time=timegrid.time

lfile=concat_dir('$DIR_SITE_LOGS','res_freq')

if not keyword_set(file) then begin ; default is use existing
   restgen,file=lfile,reshist
endif else begin
; reading observing log 
message,/info,"Reading observing log for " + strtrim(nsamp-1,2) + " days..."
for i=0,nsamp-2 do begin
   message,/info,"Reading observing log for: " + gt_day(timegrid(i),/string)
   rd_obs,timegrid(i),timegrid(i+1),a,b,spr,/nobcs, /nosxtf
   if data_chk(spr,/struct) then hist=histogram(gt_res(spr),bin=1,min=0,max=2)
   reshist(i).reshist=float(hist)/total(hist)   
endfor
reshist=reshist(0:nsamp-2)
savegen,file=lfile,reshist, $
    text=['Exposure Resolution Frequency (Normalized Historgrams)', $
          'From: ' + startt + " Through: " + stopt]
endelse

ss=sel_timrange(reshist,startt,stopt,st_before=st,en_after=en)
case 1 of 
   ss(0) eq -1: begin
      tbeep
      message,/info,"No requested times in file...
      return
   endcase
   else: begin
      if st then message,/info,"Requested start time precedes first file time"
      if en then message,/info,"Requested stop time is later than last file time"
      reshist=reshist(ss)
   endcase
endcase

mkwind=n_elements(window) eq 0 
if not mkwind then begin
   device,window=ws
   mkwind=ws(window) eq 0
endif
if mkwind then wdef,window,512,512,/ur

message,/info,"Warning - corrupting low end of your color table..."
linecolors
utplot,reshist,reshist.reshist(0),color=5,psym=10,yrange=[-.1,1.3], $
 title='Exposure Resolution Distribution',ytitle='Normalized Frequency'
outplot,reshist,reshist.reshist(0),color=5,psym=2
outplot,reshist,reshist.reshist(1),color=2,psym=10
outplot,reshist,reshist.reshist(1),color=2,psym=2
outplot,reshist,reshist.reshist(2),color=10,psym=10
outplot,reshist,reshist.reshist(2),color=10,psym=2

xyouts,.2,.90,'------- Full Resolution',color=5,/normal,size=1.5
xyouts,.2,.87,'------- Half Resolution',color=2,/normal,size=1.5
xyouts,.2,.84,'------- Quar Resolution',color=10,/normal,size=1.5

return
end