pro wplothisto,cs,nbselprm,param,h,period,nbtot,tim,val,index,selectend
;***
;###############################################################################
;#Procedure_name   : wplothisto                                              #
;#                                                                             #
;#Author           : Jacqueline Platzer                                        #
;#Language         : IDL                                                       #
;#Purpose          : Display Histogram of HK data                              #
;#Creation date    : May 1992                                                  #
;#Updating date    : SEP 1992                                                  #
;#Version            2                                                         #
;#External Modules : convtime                                                  #
;#                                                                             #
;#Call sequence    :                                                           #
;#     WPLOTHISTO ,cs,nbselprm,param,h,period,nbtot,tim,val,index,selectend  #
;#                                                                             #
;#Arguments        :                                                           #
;#             CS  : (input) float scalar                                      #
;#                   character size                                            #
;#        NBSELPRM : (input) int. number of param in the selection             #
;#           PARAM : (input) array of structures                               #
;#                   description of selected param. (see RDPRMSEL proc.)       #
;#               h : (input) int. vect of nbselprm values                      #
;#                   h(i)=3 si param(i) has to be displayed                    #
;#          PERIOD : (input) byte(12) beginning time and ending time of        #
;#                   selected period  (see proc:RDPRMSEL)                      #
;#           NBTOT : (input) int array of nbselprm values                      #
;#                   total number of values for each param (nbtot<=1000)       #
;#             TIM : (input) long word int. array dimensions:(recnumb,1000)    #
;#                   time in seconds                                           #
;#             VAL : (input) 2 dim.floating array (nbselprm,1000)              #
;#                   values of parameters                                      #
;#           INDEX : (input) 1 dim array of nbselprm values allowing           #
;#                   relation between tim(index(i) and val(i) arrays           #
;#       SELECTEND : (output) int.  end code  selectend=1 ==>exit              #
;#                                                                             #
;#External files   :                                                           #
;#      output.ps  : (output) postcript file for printing graphics             #
;#                   The file is deleted after being printed                   #
;#                                                                             #
;#Description      : This procedure plots on the screen histogram              #
;#                   of the parameters for which t(i)=3.                       #
;#                   At the end of each plot the observer can interactively    #
;#                   continue or stop the display.                             #
;###############################################################################
;***
;===============================================================================
;== initialization
;===============================================================================
axtitle=strarr(4)
axtitle=['time in sec','time in min','time in hours','time in days']
monthlst=['JAN','FEV','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV',$
'DEC']
selectend=0
unittype=strarr(nbselprm)
unittype(*)=''
idat=period(0:5)
xmin=convtime(idat)
idat=period(6:11)
dat=strtrim(period(2),2)+'-'+monthlst(period(1)-1)+'-'+strtrim(period(0),2)
xmax=convtime(idat)
sel=intarr(nbselprm)
sel(*)=0

j=0

;===============================================================================
;MAIN Repeat on various UNIT (one graph per unit type) until test=0 or 
;                                                            selectend=1 (exit)
;===============================================================================
repeat begin
test=0
testplot=0
;===============================================================================
; search the next next histogram to be displayed
;===============================================================================
    if (h(J) eq 1 and nbtot(j) gt 0)then  begin
    test=1
;===============================================================================
; scale definition calculate  min and max for Y-AXIS
;===============================================================================
    ymin=min(val(j,0:nbtot(j)-1))
    ymax=max(val(j,0:nbtot(j)-1))

;===============================================================================
; histogram possible or impossible
;===============================================================================

    if (ymax-ymin ne 0 and nbtot(j) gt 20) then begin
       testplot=1 
       bins=(ymax-ymin)/20
;===============================================================================
; display histogram of 21 ranges
;===============================================================================
print,'numero2'
    erase
          v=fltarr(nbtot(j))
          v(0:nbtot(j)-1)=val(j,0:nbtot(j)-1)
          valhisto=histogram(v,binsize=bins)
          vmax=max(valhisto)
          nel=n_elements(valhisto)
          PLOT,findgen(nel)*bins+ymin+bins/2.,valhisto,psym=10,$
               xrange=[ymin-bins,ymax+bins],yrange=[0,vmax*1.2]
          xyouts,.1,.97,'date : '+dat,color=5,charsize=cs,/normal
          xyouts,.5,.97,'POP/UDP: ',color=5,charsize=cs,/normal
          xyouts,.2,.85,'!8Histogram  !3param : '+param(j).pn,color=5,$
          charsize=cs,/normal
;===============================================================================
; display a menu to control the display
;===============================================================================
   Repeat begin ;(until distest ne 1 )
   distest=0

     xyouts,.2,0.,'Continue',charsize=cs,color=3,/normal
     xyouts,.5,0.,'Print',charsize=cs,color=3,/normal
     xyouts,.8,0.,'Exit',charsize=cs,color=3,/normal
     cursor,x,y,/normal,/down
     if (y ge 0. and y le .05) then begin
        case 1 of

;===============================================================================
;       case 'CONTINUE'
;===============================================================================
       (x ge .18 and x lt .45): begin
       distest=1
       end 


;===============================================================================
;      case 'PRINT' (on a postcript printer)
;===============================================================================
       (x ge 0.48 and x lt .60):begin

       SET_PLOT,'PS'
       device,filename='output.ps',/landscape
          v=fltarr(nbtot(j))
          v(0:nbtot(j)-1)=val(j,0:nbtot(j)-1)
          valhisto=histogram(v,binsize=bins)
          nel=n_elements(valhisto)
          PLOT,findgen(nel)*bins+ymin+bins/2.,valhisto,psym=10,$
               xrange=[ymin-bins,ymax+bins],yrange=[0,vmax*1.2]
          xyouts,.1,.97,'date : '+dat,color=5,charsize=cs,/normal
          xyouts,.5,.97,'POP/UDP: ',color=5,charsize=cs,/normal
          xyouts,.2,.85,'!8Histogram  !3param : '+param(j).pn,color=5,$
          charsize=cs,/normal
          !p.font=-1 
       device,/close
       cmd='print /que=postprinter /del output.ps'
       spawn,cmd
       SET_PLOT,'X'
       end

;===============================================================================
;      case 'EXIT'
;===============================================================================

       (x ge .78 and x le .90):begin
       distest=1
       selectend = 1
       end
       endcase
     endif
   endrep until distest eq 1

   endif else begin

;===============================================================================
; display a menu to continue the display
;===============================================================================
   erase
   Repeat begin ;(until distest ne 1 )
   distest=0
     xyouts,.2,.8,param(j).pn+'==> HISTOGRAM IMPOSSIBLE' ,$
     charsize=cs,color=5,/normal
     xyouts,.2,.7,'(ymin=ymax or pixels number  too small)',$
     charsize=cs,color=5,/normal
     xyouts,.2,0.,'Continue',charsize=cs,color=3,/normal
     xyouts,.8,0.,'Exit',charsize=cs,color=3,/normal
     cursor,x,y,/normal,/down
     if (y ge 0. and y le .05) then begin
        case 1 of

;===============================================================================
;       case 'CONTINUE'
;===============================================================================
       (x ge .18 and x lt .45): begin
       distest=1
       end 

;===============================================================================
;      case 'EXIT'
;===============================================================================

       (x ge .78 and x le .90):begin
       distest=1
       selectend = 1
       end
       endcase
     endif
     endrep until distest eq 1
    endelse
    endif
    j=j+1
endrep until j eq nbselprm or selectend eq 1
return
end

