pro displaytempor,cs,nbselprm,param,t,period,nbtot,tim,val,index,selectend
;***
;###############################################################################
;#Procedure_name   : Displaytempor                                             #
;#                                                                             #
;#Author           : Jacqueline Platzer                                        #
;#Language         : IDL                                                       #
;#Purpose          : Display HK data in temporal evolution                     #
;#Creation date    : May 1992                                                  #
;#Update date      : Septembre 1992                                            #
;#Version           : 2                                                        #
;#External Modules : convtime                                                  #
;#                                                                             #
;#Call sequence    :                                                           #
;#     DISPLAYTEMPOR,cs,nbselprm,param,t,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.)       #
;#               t : (input) int. vect of nbselprm values                      #
;#                   t(i)=1 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 the values versus time #
;#                   of the parameters for which t(i)=1.All the parameters     #
;#                   expressed in terms of the same unit are ploted on the same#
;#                   graphic.Each curb has a typical colour and line type.     #
;#                   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
nbcourb=intarr(nbselprm)
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)
time=strtrim(period(3),2)+':'+strtrim(period(4),2)+':'+strtrim(period(5),2)
xmax=convtime(idat)
sel=intarr(nbselprm)
sel(*)=0

j=0
ntyp=0
;===============================================================================
;MAIN Repeat on various UNIT (one graph per unit type) until test=0 or 
;                                                            selectend=1 (exit)
;===============================================================================

repeat begin
test=0

;===============================================================================
; search the next new unit type
;===============================================================================
  repeat begin
    if (t(J) ne 0 and sel(j) ne 1 )then  begin
    unittype(ntyp)=param(J).pu
    test=1
    sel(J)=1
    endif  else j=j+1
  endrep until test eq 1 or j eq nbselprm

;===============================================================================
; scale definition calculate  min and max for Y-AXIS
;===============================================================================
  if test eq 1 then begin
          nbcourb(ntyp)=n_elements(where (param.pu eq unittype(ntyp)))
;===============================================================================
; display param with same unit type
;===============================================================================

    erase
    for jp=J,nbselprm-1 do begin
         if (param(jp).pu eq unittype(ntyp) and t(jp) eq 1) then begin
             if (nbtot(jp) ne 0) then begin
;               ymin1=min(val(jp,0:nbtot(jp)-1))
;              ymax1=max(val(jp,0:nbtot(jp)-1))
               v=val(jp,0:nbtot(jp)-1)
               ymin1=min(v)
               ymax1=max(v)
               if jp eq j then begin
               ymin=ymin1 & ymax=ymax1
               endif else begin
               ymin=ymin < ymin1
               ymax=ymax > ymax1
                              endelse
             endif else print,'ERROR TOTAL NUMBER OF POINTS IS:',nbtot(jp)
          endif
    endfor
    for jp=J,nbselprm-1 do begin
if (t(jp) eq 1 and nbtot(jp) ne 0 and param(jp).pu eq unittype(ntyp)) then begin
              sel(jp)=1
              v=fltarr(nbtot(jp))
              ti=lonarr(nbtot(jp))
              v(0:nbtot(jp)-1)=val(jp,0:nbtot(jp)-1)
              ti(0:nbtot(jp)-1)=tim(index(jp),0:nbtot(jp)-1)-xmin
              if (jp eq j) then begin
          PLOT,ti,v,xrange=[0,(xmax-xmin)*1.1],$
          yrange=[ymin,ymax],xtitle=axtitle(0),ytitle=unittype(ntyp),$
          color=1
          xyouts,(xmax-xmin),v(nbtot(j)-1),param(j).pn,color=1,charsize=cs
          endif else begin
          OPLOT,ti,v,linestyle=(jp mod 5)+1,xrange=[0,(xmax-xmin)*1.1],$
          yrange=[ymin,ymax],color=(jp mod 5)+1
          xyouts,(xmax-xmin),v(nbtot(jp)-1),param(jp).pn,color=(jp mod 5)+1,$
          charsize=cs
          endelse
       endif
         xyouts,.1,.97,'date : '+dat+'  '+time,color=5,charsize=cs,/normal
         xyouts,.7,.97,'POP/UDP: ',color=5,charsize=cs,/normal   
  endfor

;===============================================================================
; 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
       for jp=J,nbselprm-1 do begin
 if t(jp) ne 0 and nbtot(jp) ne 0 and param(jp).pu eq unittype(ntyp) then begin
              v=fltarr(nbtot(jp))
              ti=lonarr(nbtot(jp))
              v(0:nbtot(jp)-1)=val(jp,0:nbtot(jp)-1)
              ti(0:nbtot(jp)-1)=tim(index(jp),0:nbtot(jp)-1)-xmin
          if (jp eq j) then begin
             PLOT,ti,v,xrange=[0,(xmax-xmin)*1.1],$
             yrange=[ymin,ymax],xtitle=axtitle(0),ytitle=unittype(ntyp)
             xyouts,(xmax-xmin),v(nbtot(j)-1),param(j).pn
          endif else begin
             OPLOT,ti,v,linestyle=jp mod 6 ,xrange=[0,(xmax-xmin)*1.1],$
             yrange=[ymin,ymax]
             xyouts,(xmax-xmin),v(nbtot(jp)-1),param(jp).pn
          endelse
       endif
          xyouts,.1,.97,'date : '+dat+'  '+time,color=5,charsize=cs,/normal
          xyouts,.5,.97,'POP/UDP: ',color=5,charsize=cs,/normal
    
       endfor
       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
    ntyp=ntyp+1
    j=j+1
endrep until test eq 0 or selectend eq 1 or j eq nbselprm
return
end

