pro fig_tsmean,ps=ps
;+
;   fig_tsmean ,/ps
;
;            plots temporal mean of spectrum
;
;-
@common_sumer

if(n_elements(ps) ne 0) then set_plot,'ps'
file_base='item'+strtrim(string(item_nr),2)+'_'+ddate
if(!d.name eq 'PS') then begin
  file='fig_tsmean_'+ddate+'_'+strtrim(string(item_nr),2)+'.ps'
  device,file=file,xsize=18,ysize=25,xoff=2,yoff=2
endif

cc=2.99792e5
sumer_dmean,d,d_tmean,d_tsmean,d_twmean
npanel=n_elements(d_tsmean(0,*))
nwave=n_elements(d_tsmean(*,0))
!p.charsize=1.5
for i=0,npanel-1 do begin
  px=findgen(nwave)
  if lambda(0) eq 0. then begin
    dvpx=(px-0.5*(min(px)+max(px)))*disp(i)/wave(i)*cc
  endif else begin
    order=1
    if disp(i) lt 0.035 then order=2.
    dvpx=(lambda(ref_px(i)-nwave/2+px)/order-wave(i))/wave(i)*cc
  endelse
  x=dvpx
  panel,i,0,nx=npanel,ny=2,xdist=8,ydist=8,xleft=6,ytop=7,ybottom=3,ylabel=1,xlabel=1
  plot,x,d_tsmean(*,i),xstyle=1,$
   title=sumer_id(item_nr,i)+string(wave(i),format='(f8.2)'),$
   xtitle='Vel [km/s]',ytitle='Counts'
  px=sumer_px(item_nr,i)
  sumer_cont,item_nr,i,pxc,linec
  oplot,[1,1]*x(min(px)),[0,0.3]*(!y.crange(1)-!y.crange(0))+!y.crange(0),line=2
  oplot,[1,1]*x(max(px)),[0,0.3]*(!y.crange(1)-!y.crange(0))+!y.crange(0),line=2
  if(linec eq i) then begin
    y1c=0.3*(!y.crange(1)-!y.crange(0))+!y.crange(0)
    for j=0,n_elements(pxc)-1 do begin
      x0c=0.5*(x((pxc(j)-1)>0)+x(pxc(j)))
      x1c=0.5*(x((pxc(j)+1)<(nwave-1))+x(pxc(j)))
      polyfill,[x0c,x1c,x1c,x0c,x0c],[0,0,1,1,0]*y1c,$
       /line_fill,orient=45
    endfor
  endif
  if(max(intc(*,i,*,*)) gt 0.) then begin
    oplot,[!x.crange(0),!x.crange(1)],[1,1]*mean(intc(*,i,*,*)),line=1
  endif

  if(lambda(0) eq 0) then begin
    x=(findgen(nwave)-nwave/2)*disp(i)+wave(i)
  endif else begin
    x=lambda(findgen(nwave)-nwave/2+ref_px(i))
  endelse
  panel,i,1,nx=npanel,ny=2,xdist=8,ydist=8,xleft=6,ytop=7,ybottom=3,ylabel=1,xlabel=1
  plot,x,d_tsmean(*,i),xstyle=1,$
   title=sumer_id(item_nr,i)+string(wave(i),format='(f8.2)'),$
   yrange=[0,max(d_tsmean)],$
   xtitle='!4k!3 [Å]',ytitle='Counts'
  oplot,[1,1]*x(min(px)),[0,0.3]*(!y.crange(1)-!y.crange(0))+!y.crange(0),line=2
  oplot,[1,1]*x(max(px)),[0,0.3]*(!y.crange(1)-!y.crange(0))+!y.crange(0),line=2
  if(linec eq i) then begin
    y1c=0.3*(!y.crange(1)-!y.crange(0))+!y.crange(0)
    for j=0,n_elements(pxc)-1 do begin
      x0c=0.5*(x((pxc(j)-1)>0)+x(pxc(j)))
      x1c=0.5*(x((pxc(j)+1)<(nwave-1))+x(pxc(j)))
      polyfill,[x0c,x1c,x1c,x0c,x0c],[0,0,1,1,0]*y1c,$
       /line_fill,orient=45
    endfor
  endif
  if(max(intc(*,i,*,*)) gt 0.) then begin
    oplot,[!x.crange(0),!x.crange(1)],[1,1]*mean(intc(*,i,*,*)),line=1
  endif
endfor
panel,/reset
xyouts,0.5,0.98,file_base+'  temporal and spatial average',/norm,size=1.5,$
 align=0.5

reset
if(!d.name eq 'PS') then begin
  print,'figure file is: ',file
  device,/close
  set_plot,'x'
endif

end
