;
; NAME:
;       TMSTAT 
;
; PURPOSE:
;        This subroutine plots percentages of C1, C2, C3, and C4 telemetrybytes for 
;        QL or LZ data for a given date-range as a percentage of total telemetery
;        bytes on a daily, or monthly basis.
;
; CATEGORY:
;       UTIL
;
; CALLING SEQUENCE:
;        TMSTAT 
;
; INPUTS:
;        None
;
; KEYWORD PARAMETERS:
;       None
;
; OUTPUTS:
;        None
;
; SIDE EFFECTS:
;       Plot to a printer or save to a file.
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; EXAMPLE:
;       tmstat
;
; MODIFICATION HISTORY:
;       Written by:     Ed Esfandiari, NRL, 11/30/1999
;                       AEE  12/02/99  Added histogram or symbol plot option.
;                       AEE  07/29/00  Added checks to ignore images with naxis1
;                                      and 2 equal 0 and cf less than 1. These
;                                      sort of bad images caused the 07/11/00 tm 
;                                      bytes for C2 to force the monthly tm of C2 
;                                      to be 68.4% of the total tm!
;
;


pro box,x0,y0,x1,y1,color
  polyfill,[x0,x0,x1,x1],[y0,y1,y1,y0],col=color
  return
end




pro box_plot,ps_plt=ps_plt,multi=multi
  common tm_share,selected_img,fname,save,save_req,img,prof_win,$
              res_types,old_query,old_s,rtype,query,polar,ptype,$
              x_arr,y_arr,plot_cnt,plot_info,last_press,beg_date,end_date, $
              cnts,img_cnt,image_dates,pdates,flt_str,pol_str,img_type,ored,ogreen,oblue,$
              stypes,stype,snames,sname,tot_bytes,c1_bytes,c2_bytes,c3_bytes,c4_bytes


  z= where(tot_bytes eq 0.0,zcnt)
  if(zcnt gt 0) then begin
    c1_bytes(z)= 0.0
    c2_bytes(z)= 0.0
    c3_bytes(z)= 0.0
    c4_bytes(z)= 0.0
    tot_bytes(z)= 0.00001 
  end
  c1p= c1_bytes/tot_bytes*100.0
  c2p= c2_bytes/tot_bytes*100.0
  c3p= c3_bytes/tot_bytes*100.0
  c4p= c4_bytes/tot_bytes*100.0
 
  totbytes= total(tot_bytes)
  c1bytes=  total(c1_bytes)
  c2bytes=  total(c2_bytes)
  c3bytes=  total(c3_bytes)
  c4bytes=  total(c4_bytes)
  
  if(img_type eq 0) then $ 
    src= ' (src=QL)' $
  else $
    src= ' (src=LZ)'
  dates= pdates.mjd - pdates(0).mjd + 1
  img_dates= strmid(image_dates,2,8) ; remove 19 from 199x/xx/xx 
  img_dates= strmid(img_dates,3,5)+'/'+strmid(img_dates,0,2)
  cnt= cnts
!y.style=1
  if(cnt eq 0) then begin ; only one day of data
    cnt= 1   
    !x.range= [dates(0)-0.5,dates(0)+0.5] 
;    !y.range= [0,100]
    !y.range= [-1,101]
    !x.ticks= cnt
    !x.tickname= ' ' ;only 1 date->ticks =0 (IDL default for many)
    !x.tickv= dates  ;ticks) so use xyouts instead of tickname
    !xtitle= img_dates(0)
;    xyouts,295,23,img_dates,/dev,color=1
   if(keyword_set(multi)) then begin
    !p.multi=[0,2,2]
    xoff= 0.2
    !mtitle='C1 TM Percentage'+src 
    plot,[dates(0)-0.5,dates(0),dates(0)+0.5],[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c1p(i),1 
    xyouts,0.6,80,'Avg: '+string(c1bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    !mtitle='C2 TM Percentage'+src 
    plot,[dates(0)-0.5,dates(0),dates(0)+0.5],[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c2p(i),1
    xyouts,0.6,80,'Avg: '+string(c2bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    !mtitle='C3 TM Percentage'+src 
    plot,[dates(0)-0.5,dates(0),dates(0)+0.5],[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c3p(i),1
    xyouts,0.6,80,'Avg: '+string(c3bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    !mtitle='C4 TM Percentage'+src 
    plot,[dates(0)-0.5,dates(0),dates(0)+0.5],[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c4p(i),1
    xyouts,0.6,80,'Avg: '+string(c4bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    xyouts,0,0,"          Total TM: "+string(totbytes/1000000.,'(f11.2)')+"MB",/dev,color=1
    xyouts, ",  C1: "+string(c1bytes/1000000.,'(f10.1)')+"MB",/dev,color=1
    xyouts, ",  C2: "+string(c2bytes/1000000.,'(f9.1)')+"MB",/dev,color=1
    xyouts, ",  C3: "+string(c3bytes/1000000.,'(f9.1)')+"MB",/dev,color=1
    xyouts, ",  C4: "+string(c4bytes/1000000.,'(f9.1)')+"MB",/dev,color=1
   endif else begin
    !p.multi=[0,1,1]
    plot,[dates(0)-0.5,dates(0),dates(0)+0.5],[0,100.0,0],/nodata,color=1
    oplot,[dates(0)-0.5,dates(0),dates(0)+0.5],[-3,c1p,-3],psym=2,color=2
    oplot,[dates(0)-0.5,dates(0),dates(0)+0.5],[-3,c2p,-3],psym=4,color=3
    oplot,[dates(0)-0.5,dates(0),dates(0)+0.5],[-3,c3p,-3],psym=5,color=5
    oplot,[dates(0)-0.5,dates(0),dates(0)+0.5],[-3,c4p,-3],psym=6,color=7
   endelse
  endif else begin 
    !x.range= [dates(0)-0.5,dates(cnt-1)+0.5]
;    !y.range=[0,100]
    !y.range= [-1,101]
    if(dates(cnt-1)-dates(0) gt 8 and res_types(rtype) eq 'Daily') then begin 
      ; for more than 9 days, use only 3 x-axis dates (first, mid, last)
      d= [dates(0),(dates(cnt-1)+1)/2,dates(cnt-1)]
      !x.tickv= d 
      !x.ticks= 2 
      d= fix(d)-1
      date1= {mjd:0L,time:0l}
      date3= replicate(date1,3) 
      date3.mjd= d+pdates(0).mjd
      yymmdd= utc2yymmdd(date3)
      if(res_types(rtype) eq 'Daily') then begin
        !x.tickname= strmid(yymmdd,2,2)+'/'+strmid(yymmdd,4,2)+'/'+ $
                     strmid(yymmdd,0,2)  ; change to mm/dd/yy format
      endif else begin ; 'Monthly' resolution
        !x.tickname= strmid(yymmdd,4,2)+'/'+strmid(yymmdd,0,2) ; change to mm/yy
      endelse
    endif else begin
      !x.ticks= cnt-1
      !x.tickv= dates 
      !x.tickname= img_dates 
    endelse
   if(keyword_set(multi)) then begin
    !p.multi=[0,2,2]
    xoff= 0.45
    !mtitle='C1 TM Percentage'+src 
    plot,dates,[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c1p(i),1 
    xyouts,1,80,'Avg: '+string(c1bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    !mtitle='C2 TM Percentage'+src 
    plot,dates,[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c2p(i),1
    xyouts,1,80,'Avg: '+string(c2bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    !mtitle='C3 TM Percentage'+src 
    plot,dates,[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c3p(i),1
    xyouts,1,80,'Avg: '+string(c3bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    !mtitle='C4 TM Percentage'+src 
    plot,dates,[0,100.0,0],/nodata,color=1
    for i=0,cnt-1 do box,dates(i)-xoff,0, dates(i)+xoff,c4p(i),1
    xyouts,1,80,'Avg: '+string(c4bytes/totbytes*100.0,'(f5.1)')+'% of total TM',color=2
    xyouts,0,0,"          Total TM: "+string(totbytes/1000000.,'(f11.2)')+"MB",/dev,color=1
    xyouts, ",  C1: "+string(c1bytes/1000000.,'(f10.1)')+"MB",/dev,color=1
    xyouts, ",  C2: "+string(c2bytes/1000000.,'(f9.1)')+"MB",/dev,color=1
    xyouts, ",  C3: "+string(c3bytes/1000000.,'(f9.1)')+"MB",/dev,color=1
    xyouts, ",  C4: "+string(c4bytes/1000000.,'(f9.1)')+"MB",/dev,color=1
   endif else begin
    !p.multi=[0,1,1]
    plot,dates,c1_bytes,/nodata,color=1
    oplot,dates,c1p,psym=2,color=2
    oplot,dates,c2p*100.0,psym=4,color=3
    oplot,dates,c3p,psym=5,color=5
    oplot,dates,c4p,psym=6,color=7
   endelse
  endelse

  if(keyword_set(ps_plt)) then begin
   if(not keyword_set(multi)) then begin
    xyouts,0,0,"Total TM: "+string(totbytes/1000000.,'(f11.2)')+"MB",/dev,color=1
    xyouts, ", C1: "+string(c1bytes/1000000.,'(f10.1)')+"MB ("+string(c1bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=1
    xyouts, ", C2: "+string(c2bytes/1000000.,'(f9.1)')+"MB ("+string(c2bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=1
    xyouts, ", C3: "+string(c3bytes/1000000.,'(f9.1)')+"MB ("+string(c3bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=1
    xyouts, ", C4: "+string(c4bytes/1000000.,'(f9.1)')+"MB ("+string(c4bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=1
   end
  endif else begin
   if(not keyword_set(multi)) then begin
    xyouts,100,260,"Total TM:  "+string(totbytes/1000000.,'(f11.2)')+" MB",/dev,color=1
    xyouts,100,240,"  C1 TM:   "+string(c1bytes/1000000.,'(f10.2)')+" MB  ("+string(c1bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=2
    xyouts,100,220,"  C2 TM:   "+string(c2bytes/1000000.,'(f10.2)')+" MB  ("+string(c2bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=3
    xyouts,100,200,"  C3 TM:   "+string(c3bytes/1000000.,'(f10.2)')+" MB  ("+string(c3bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=5
    xyouts,100,180,"  C4 TM:   "+string(c4bytes/1000000.,'(f10.2)')+" MB  ("+string(c4bytes/totbytes*100.0,'(f5.1)')+"%)",/dev,color=7
   end
  endelse

  minval= !y.crange(0)

  return
end




PRO IMG_SELECT_EVENT, event
common tm_share,selected_img,fname,save,save_req,img,prof_win,$
              res_types,old_query,old_s,rtype,query,polar,ptype,$
              x_arr,y_arr,plot_cnt,plot_info,last_press,beg_date,end_date, $
              cnts,img_cnt,image_dates,pdates,flt_str,pol_str,img_type,ored,ogreen,oblue,$
              stypes,stype,snames,sname,tot_bytes,c1_bytes,c2_bytes,c3_bytes,c4_bytes
 
WIDGET_CONTROL, event.id, GET_UVALUE=uval
WIDGET_CONTROL, event.top, GET_UVALUE=struct   ; get structure from UVALUE


help_info=[$
   ' This program plots the C1, C2, C3, and C4 percentages of the total',$
   'telemetry on a daily or monthly basis for the provided date-range.',$
   'It also displays the total telemetry bytes and its distribution',$
   'amongst the four cameras for the entire period.',$
   '',$
   ' Since this tool can only access up to 100000 images at a time, avoid',$
   'dates-ranges of more than six months. To display the data on a daily',$
   'basis, select the "Daily" resolution (default). To display the data on',$
   'a monthly (calendar) basis, use the "Monthly" resolution option.',$
   '',$
   ' In addition, since the database only contains quick-look images for the',$
   'recent months, use the "QL" image type (default) only when querying for',$
   'the last couple of months. Otherwise, use the "LZ" image type option.',$
   '',$ 
   ' The "Histogram" plot option (default) divides the screen in four',$
   'sections and displays C1, C2, C3, and C4 percenatges in a separate plot',$
   'using the histogram mode. The "Symbol" option plots all the percentages',$
   'in the same plot using different symbols and colors for each camera.',$
   '',$ 
   ' The currently displayed plot can be printed, or saved as a postscript or',$
   'gif file. Simply select a printer or a postscript or gif filename before',$
   'clicking on the "Print Plot" or "Save Plot" buttons.']

select_info=[$
   ' No selection.']

;print,'uval= ',uval
 case (uval) of
  'Done': begin ; exit the program but delete the plot file first, if any.
                  if(last_press eq 'prn') then wait,3
                  openw,unit,'ff_img_files.ps',/get_lun,/delete
                  close,unit
                  free_lun,unit
                  tvlct,255*ored,255*ogreen,255*oblue
             WIDGET_CONTROL, /DESTROY, struct.base
          end
  'Help': begin
             last_press= 'help'
             save_req= 0 ;if this event selected, ignore the savescreen req.
             xdisplayfile,TITLE='Help',$
             GROUP=event.top, HEIGHT=15, WIDTH=75, $
             TEXT= help_info
          end 

  'proc': begin ; proceed with retrieving data and plotting
            last_press= 'proc'
            WIDGET_CONTROL,/hour
            old_query= query
            if(strpos(end_date(0),'23:59:59') lt 0) then $
              end_date= strtrim(end_date,2)+' 23:59:59'

            query= 'select date_obs,camera,blocks_total,comp_cf from img_leb_hdr,img_files where'
            query= query+' img_leb_hdr.fileorig=img_files.fileorig'
            query= query+' and img_leb_hdr.date_mod = img_files.date_mod'
            query= query+' and date_obs between "'+beg_date+'" and "'+end_date+'"'
            query= query+' and naxis1 > 0 and naxis2 > 0 and comp_cf >= 1.0'  ; AEE - added 07/29/00
            if(img_type eq 0) then query= query+' and source = 1'
            if(img_type eq 1) then query= query+' and source = 2'

            query= query(0)
            if(old_query ne query) then begin 
              ;print,'query= ',query
              qry= query
              s= qdb('lasco',qry) 
              old_s= s
              !p.multi=[0,1,1]  ; single plot
            endif else begin 
              s= old_s
            endelse

            blks= s.blocks_total
            cfs=  s.comp_cf
            z=where(cfs eq 0, zcnt)
            if(zcnt gt 0) then cfs(z)= 1.0  ; *** if 0.0 means no compression was done (check this).
            cam=  s.camera 

            ;Figure out how many images per day:
            s= strmid(s.date_obs,0,10)
            !x.tickname= ' ' 
            !x.tickv= 0  
            !x.style=1

            if(img_type eq 0) then it= 'QL'
            if(img_type eq 1) then it= 'LZ'
            mt= res_types(rtype)+' telemetry percentages for each camera (using '+it+' files)'
            !mtitle= mt 
            !ytitle= '% of total telemertry'
            !xtitle= 'Observation Dates'
            !ymin=0
            !ymax= 100
            if(s(0) ne '') then begin ; qdb returned data.
              cnts= 0
              if(res_types(rtype) eq 'Monthly') then s= strmid(s,0,7)  ; only keep yyyy/mm of date_obs
              if(n_elements(s) gt 1) then pos= where(s ne shift(s,1),cnts)
              if(cnts gt 0) then begin ; multiple days images
               img_cnt= shift(pos,-1)-pos
               img_cnt(cnts-1)= n_elements(s)+img_cnt(cnts-1) 
               image_dates= s(pos)
               tot_bytes= fltarr(cnts)
               c1_bytes= fltarr(cnts)
               c2_bytes= fltarr(cnts)
               c3_bytes= fltarr(cnts)
               c4_bytes= fltarr(cnts)
               for i=0,cnts-1 do begin
                 bytes= (blks(pos(i):pos(i)+img_cnt(i)-1) * 1024.0  * 2.0) / cfs(pos(i):pos(i)+img_cnt(i)-1)
                 tot_bytes(i)= total(bytes)
                 c1_bytes(i)= 0.0
                 c1= where(cam(pos(i):pos(i)+img_cnt(i)-1) eq 0,c1cnt)
                 if(c1cnt gt 0) then c1_bytes(i)= total(bytes(c1))
                 c2_bytes(i)= 0.0
                 c2= where(cam(pos(i):pos(i)+img_cnt(i)-1) eq 1,c2cnt)
                 if(c2cnt gt 0) then c2_bytes(i)= total(bytes(c2))
                 c3_bytes(i)= 0.0
                 c3= where(cam(pos(i):pos(i)+img_cnt(i)-1) eq 2,c3cnt)
                 if(c3cnt gt 0) then c3_bytes(i)= total(bytes(c3))
                 c4_bytes(i)= 0.0
                 c4= where(cam(pos(i):pos(i)+img_cnt(i)-1) eq 3,c4cnt)
                 if(c4cnt gt 0) then c4_bytes(i)= total(bytes(c4))
               end
              endif else begin ; only one day of images
               img_cnt= [n_elements(s)]
               bytes= (blks * 1024.0  * 2.0) / cfs
               tot_bytes= total(bytes)
               c1_bytes= 0.0
               c1= where(cam eq 0,c1cnt)
               if(c1cnt gt 0) then c1_bytes= total(bytes(c1))
               c2_bytes= 0.0
               c2= where(cam eq 1,c2cnt)
               if(c2cnt gt 0) then c2_bytes= total(bytes(c2))
               c3_bytes= 0.0
               c3= where(cam eq 2,c3cnt)
               if(c3cnt gt 0) then c3_bytes= total(bytes(c3))
               c4_bytes= 0.0
               c4= where(cam eq 3,c4cnt)
               if(c4cnt gt 0) then c4_bytes= total(bytes(c4))

               image_dates= s(0)
              endelse
              wset,prof_win
              if(res_types(rtype) eq 'Monthly') then begin
                pdates= yymmdd2utc(strmid(image_dates,2,2)+strmid(image_dates,5,2)+'01') 
              endif else begin
                pdates= yymmdd2utc(strmid(image_dates,2,2)+ $
                      strmid(image_dates,5,2)+strmid(image_dates,8,2)) 
              endelse
              if(ptype eq 0) then begin ; Box type plot
                box_plot,/multi 
              endif else begin ; outline type plot
                box_plot
              endelse
            endif else begin ; s= '' -> No data was returned
              cnts= -1
              !x.range= [0,1]
              !y.range=[0,1]
              !x.ticks= 0
              !x.tickname= ' '
              !x.tickv= 0.5
              plot,[0,0],[0,0],/nodata,color=1
              xyouts,.3,.5,'Sorry - No images in the selected range.',color=1 
            endelse
          end
  'pid':  begin
           ptype= event.index
           if(cnts gt -1) then begin
             if(ptype eq 0) then $
               box_plot,/multi $
             else $
               box_plot
           end
         end
 'r_type' : begin 
           last_press= 'r_type'
           rtype= event.index
  end 
  'imgtyp' : begin
           last_press= 'imgtyp'
           img_type= event.index
  end
  'bdte':  begin
             last_press= 'bdte'
             WIDGET_CONTROL, event.id ,get_value=beg_date
             beg_date= beg_date(0)
           end
  'edte':   begin
             last_press= 'edte'
             WIDGET_CONTROL, event.id ,get_value=end_date
             end_date= end_date(0)
           end
  'pwin':  begin
;             print,'Plot: event type, x, y= ',event.type,event.x,event.y
;             if(event.type eq 2) then print,'x, y motion= ',event.type,event.x,event.y
           end
  'sid':   begin
             last_press= 'sid'
             sname= snames(event.index) 
             stype= stypes(event.index)
             WIDGET_CONTROL, save ,set_value=sname
;              print,'save name= ',sname
           end
  'svn':   begin
             last_press= 'svn'
             WIDGET_CONTROL, save ,get_value=sname
              sname= sname(0)
;              print,'save name= ',sname
           end
  'sav':   begin
             last_press= 'sav'
;             msg='Save plot as '+stype+' in '+sname+'?'
             if(stype eq 'PS') then begin 
               msg=['Note: This file will be saved in the current working',$
                    'directory unless a path is provided with the filename.',$
                    '',$
                    'Save plot as a PostScript file in '+sname+'?']
             endif else begin 
               msg=['Note: This file will be saved in the current working',$
                    'directory unless a path is provided with the filename.',$
                    '',$
                    'Save plot as a GIF file in '+sname+'?']
             endelse
             res= WIDGET_MESSAGE(msg,/question)
             if(res ne "Yes") then return
             if(stype eq 'PS') then begin
               WIDGET_CONTROL, /HOUR
               print,'Creating PS: '+sname
               set_plot,'ps'
               device,file= sname
               device,/landscape
               if(cnts ge 0) then begin ; data was selected
                 if(ptype eq 0) then begin ; Box type plot
                   box_plot,/ps_plt,/multi 
                 endif else begin ; outline type plot
                   box_plot,/ps_plt
                 endelse
               endif else begin ; no data was selected.
                 plot,[0,0],[0,0],/nodata
                 xyouts,.3,.5,'Sorry - No images in the selected range.' 
               endelse
               device,/close_file
               set_plot,'x'
             endif else begin ; 'GIF'
               WIDGET_CONTROL, /HOUR
               print,'Creating GIF: '+sname
               img = TVRD()
               TVLCT, r, g, b, /GET
               WRITE_GIF, sname, img, r, g, b
             endelse
           end
  'pnm':   begin
             last_press='pnm'
             WIDGET_CONTROL, event.id ,GET_VALUE=fname
             fname= fname(0)
           end
  'prn': begin
              save_req= event.select
              if(event.select eq 1) then begin
                res=widget_message('Print to "'+fname+'"?',/question, $
                                   title="Printer")
                if(res eq "Yes") then begin
                  last_press= 'prn'
                  print,'printing to '+fname
                  set_plot,'ps'
                  device,file='ff_img_files.ps'
                  device,/landscape
                  if(cnts ge 0) then begin ; data was selected
                    if(ptype eq 0) then begin ; Box type plot
                      box_plot,/ps_plt,/multi 
                    endif else begin ; outline type plot
                      box_plot,/ps_plt
                    endelse
                  endif else begin ; no data was selected.
                    plot,[0,0],[0,0],/nodata,color=1
                    xyouts,.3,.5,'Sorry - No images in the selected range.' 
                  endelse
                  device,/close_file
                  set_plot,'x'
                  spawn,'lp -o nobanner -d '+fname+' ff_img_files.ps'
                end
              end
           end
 endcase

end


pro tmstat 
  common tm_share,selected_img,fname,save,save_req,img,prof_win,$
              res_types,old_query,old_s,rtype,query,polar,ptype,$
              x_arr,y_arr,plot_cnt,plot_info,last_press,beg_date,end_date, $
              cnts,img_cnt,image_dates,pdates,flt_str,pol_str,img_type,ored,ogreen,oblue,$
              stypes,stype,snames,sname,tot_bytes,c1_bytes,c2_bytes,c3_bytes,c4_bytes


  query= ''
  erase_plot= 'true'
  plot_cnt= 0
  ptype= 0
  last_press= ''
  plot_info= {plt:'',beg:0l,fin:0l,xout:0.0D,yout:0.0D,msg:'',keys:''}
  plot_info= replicate(plot_info,51)
  save_req= 0
  cnts= -1
  flt_str= 'All'
  pol_str= 'All'

;  Get the current color table and save it:
   tvlct,/get,ored,ogreen,oblue
;  Create a color table of Black, White, Red, Green, Blue, Yellow, Cyan, and
;  Magenta and load it into the display device:
;         B,W,R,G,B,Y,C,M
       r=[0,1,1,0,0,1,0,1]
       g=[0,1,0,1,0,1,1,0]
       b=[0,1,0,0,1,0,1,1]
       tvlct,255*r,255*g,255*b

  base=widget_base(title='tmstat - Telemetry Statistics',xsize=636,ysize=600,$
                    xoffset=2,/frame) 

  b1= widget_base(base,/ROW)
  b2= widget_base(b1,/COLUMN,/frame)     ; ,/frame frames entire window
  b3= widget_base(b2,/ROW)               ; ,/frame frames quit & help
  colb1= widget_base(b2,/ROW,/frame)     ; ,/frame frames plot window
  colb2= widget_base(b2,/ROW) 
  b4= widget_base(colb2,/COLUMN,/frame)  ; ,/frame frames selection icons
  colb0= widget_base(b2,/ROW,/FRAME)
  b5= widget_base(b2,/COLUMN,/frame)  ; ,/frame frames print section
  colb3= widget_base(b4,/ROW)
  colb4= widget_base(b4,/ROW)
  colb5= widget_base(b4,/ROW)
  colb6= widget_base(b4,/ROW)
  cimg= widget_base(b4,/ROW)
  colb7= widget_base(b4,/ROW)
  colb8= widget_base(b5,/ROW)
  rowb1= widget_base(colb1,/ROW)


;  txt= widget_label(b3,value='                           ')
;  quit = WIDGET_BUTTON(b3, VALUE='Quit', UVALUE='Done',/frame)
;  txt= widget_label(b3,value='                            ')
;  help= WIDGET_BUTTON(b3, VALUE='Help',UVALUE='Help',/frame)

;  lineprofile=  widget_draw(rowb1,xsize=600,ysize=300,retain=2,/button_events, $
;                    /viewport_events,UVALUE='pwin',/frame)
  
;  txt= widget_label(colb4,value='                                        ') 
;  proceed = WIDGET_BUTTON(colb4, VALUE='    Submit    ', UVALUE='proc',/frame)


  txt= widget_label(b3,value='                  ')
  quit = WIDGET_BUTTON(b3, VALUE='Quit', UVALUE='Done',/frame)
  ptypes= ['Histogram','Symbol']
  txt= widget_label(b3,value='           ')
  pltid= cw_bselector(b3, UVALUE='pid',ptypes,label_left="Plot Type:",/frame)
  txt= widget_label(b3,value='           ')
  help= WIDGET_BUTTON(b3, VALUE='Help',UVALUE='Help',/frame)

  lineprofile=  widget_draw(rowb1,xsize=600,ysize=300,retain=2,/button_events, $
                    /viewport_events,UVALUE='pwin',/frame)

  txt= widget_label(colb4,value='                                        ')
  proceed = WIDGET_BUTTON(colb4, VALUE='    Submit    ', UVALUE='proc',/frame)


;  beg_date= '10/01/99'
;  end_date= '10/01/99'

;  spawn,"date -u '+%m/%d/%y'",end_date
  spawn,"date -u '+%y%m%d'",end_date
  end_date= end_date(0)
  dte=yymmdd2utc(end_date)
  dte.mjd= dte.mjd-30L
  end_date= utc2yymmdd(dte)
  beg_date= end_date 

  beg_dte= cw_field(colb5,UVALUE='bdte', VALUE= beg_date, xsize= 12,$
                    title='                       Plot Range:',/row,/ALL_EVENTS)
  end_dte= cw_field(colb5,UVALUE='edte', VALUE= end_date, xsize= 12, $
                    title='to',/row,/ALL_EVENTS)

  res_types=['Daily','Monthly']
  rtype= 0 ; initialize it to 'Daily' 
  txt= widget_label(colb6,value='             ')
  res_id= cw_bselector(colb6, UVALUE='r_type',res_types, label_left="Resolution: ",/frame)

  imtype=['QL','LZ']
  img_type= 0
  txt= widget_label(colb6,value='                ')
  imtp_id= cw_bselector(colb6, UVALUE='imgtyp',imtype,label_left="Image Type: ",/frame)
  txt= widget_label(colb6,value='                   ')

  txt= widget_label(colb8,value='     ')
  cb77= widget_base(colb8,/ROW)
  cb8= widget_base(colb8,/ROW,/FRAME)
  b_id= widget_button(cb8, VALUE="Print Plot", UVALUE="prn")
  fname="hp4-247"
  val= cw_field(cb8,UVALUE='pnm', VALUE= fname,title="Printer:",xsize=10, $
                       /ALL_EVENTS) ; can add /frame 
  
  txt= widget_label(colb8,value='    ')
  cb9= widget_base(colb8,/ROW,/FRAME)
  s_id= widget_button(cb9, VALUE="Save Plot", UVALUE="sav")
  txt= widget_label(cb9,value='as')
  snames=['tm_plot.ps','tm_plot.gif']
  sname= snames(0)
  stypes=['PS','GIF']
  stype= stypes(0)
  svid= cw_bselector(cb9, UVALUE='sid',stypes,/frame)
  save= cw_field(cb9,UVALUE='svn', VALUE= sname,title="in",xsize=12,/ALL_EVENTS)
 
  widget_control,/realize,base
  widget_control,lineprofile,get_value= prof_win 

  struct = { $
    base:base $
  }
  
;  save= 0 ; default - do not save the list of selected image names
;  ssfname= ['CURRENT_SCR.GIF']

  widget_control,base, SET_UVALUE=struct

  XMANAGER, 'IMG_SELECT', base, EVENT_HANDLER='IMG_SELECT_EVENT'

  return

end
