pro plot_hxsph, index, data, range = range, channel = channel, err = err, $
        timerange = timerange,  sum = sum, disp = disp, notitle=notitle, $

        $    ; field of !p
        background = background, $
        charsize   = charsize,   charthick  = charthick, $
        clip       = clip,       color      = color, $
        font       = font,       linestyle  = linestyle, $
        noclip     = noclip,     noerase    = noerase, $
        symsize    = symsize,    position   = position, $
        psym       = psym,       subtitle   = subtitle, $
        t3d        = t3d,        thick      = thick, $
        ticklen    = ticklen,    title      = title, $

        $    ; field of !x
        xcharsize  = xcharsize,  xmargin    = xmargin, $
        xticklen   = xticklen,   xtitle     = xtitle, $
        xstyle     = xstyle,     $

        $    ; field of !y
        ycharsize  = ycharsize,  ymargin    = ymargin, $
        yminor     = yminor,     yrange     = yrange, $
        ystyle     = ystyle,     yticklen   = yticklen, $
        ytickname  = ytickname,  yticks     = yticks, $
        ytickv     = ytickv,     ytitle     = ytitle, $
        ytype      = ytype


if ( keyword_set(range) ) then begin
   range=float(range)
   if ( n_elements(range) eq 1 ) then range = [ range,range ]
endif

if ( n_elements(channel) ge 1 ) then begin
   ch = fix(channel)
   if ( n_elements(ch) eq 1 ) then ch = [ ch,ch ]
endif

if ( keyword_set(timerange) ) then timerange0=timerange

exflag = 0

st_ds79=gt_day(index(0))
st_msod=gt_time(index(0))

nnn = n_elements(data(where(index.wbs.nmf eq 3)))

wbsph = mk_hxsph( index, data, xtime = xtime )

if keyword_set( err ) then begin

  data0=data(where(index.wbs.nmf eq 3))
  index0=index(where(index.wbs.nmf eq 3))
  bitrate   = gt_dp_rate( index0 )

  dnu = n_elements(data0)
  itime = fltarr( dnu*4 )
  hiint = 1.0

  for i = 0,dnu-1 do begin
    case bitrate(i) of
      4: begin
          for j = 0,3 do begin
             itime(i*4+j)=1.0
          endfor
        end
      2: begin
          for j = 0,3 do begin
             itime(i*4+j)=8.0
          endfor
        end
    endcase
  endfor

endif

;.........................................................................
        psave = !p
        xsave = !x
        ysave = !y
        !p.nsum=fcheck(sum,1) ;clear nsum
        
graphic:

hxs_rel = fltarr( 6,32 )
energy = fltarr( 2,32 )

if ( st_ds79 ge 4909 ) then begin
   openr,1,'/ys/wbs/response/hxs_01.rel'
   header_scan,nr=1
   readf,1,hxs_rel
   close,1
endif else begin
   openr,1,'/ys/wbs/response/hxs_21.rel'
   header_scan,nr=1
   readf,1,hxs_rel
   close,1
endelse

energy(0,*) = hxs_rel(1,*)
energy(1,*) = hxs_rel(3,*)

case 1 of

  n_elements( channel ) ge 1 : begin
    egy = string( energy(0,ch(0)),format="(g5.4)") + $
        ' - ' + string( energy(1,ch(1)),format="(g5.4)" )
   end

  keyword_set( range ) : begin
    ch = intarr( 2 )
    for i = 0,31 do begin
       if ( range(0) ge energy(0,i) ) and ( range(0) lt energy(1,i) ) then $
             ch(0) = i
       if ( range(1) ge energy(0,i) ) and ( range(1) lt energy(1,i) ) then $
             ch(1) = i
    endfor
    if ( range(0) lt energy(0,0) ) then ch(0) = 0
    if ( range(1) lt energy(0,0) ) then ch(1) = 0
    if ( range(0) ge energy(1,31) ) then ch(0) = 31
    if ( range(1) ge energy(1,31) ) then ch(1) = 31
    egy = string( energy(0,ch(0)),format="(g5.4)" ) + $
        ' - ' + string( energy(1,ch(1)),format="(g5.4)" )
   end

  else : begin
    print,'% Input error - please input ''channel'' or ''range''.'
    return
   end

endcase

  if not keyword_set( notitle ) then begin
    if ( !p.nsum eq 1 ) then yttl = 'counts/sec' else begin
       yttl = 'counts/' + strcompress( string( fix( !p.nsum ) ),/remove_all ) $
                 +'sec'
    endelse
        !p.title='HXS PH TIMEPROFILE  '+egy+' keV'
        !y.title=yttl
  endif

        !p.background=fcheck(background,!p.background)
        !p.charsize=fcheck(charsize,!p.charsize)
        !p.charthick=fcheck(charthick,!p.charthick)
        !p.clip=fcheck(clip,!p.clip)
        !p.color=fcheck(color,!p.color)
        !p.font=fcheck(font,!p.font)
        !p.linestyle=fcheck(linestyle,!p.linestyle)
        !p.noclip=fcheck(noclip,!p.noclip)
        !p.noerase=fcheck(noerase,!p.noerase)
        !p.position=fcheck(position,!p.position)
        !p.psym=fcheck(psym,!p.psym)
        !p.subtitle=fcheck(subtitle,!p.subtitle)
        !p.t3d=fcheck(t3d,!p.t3d)
        !p.thick=fcheck(thick,!p.thick)
        !p.ticklen=fcheck(ticklen,!p.ticklen)
        !p.title=fcheck(title, !p.title)

        !x.charsize=fcheck(xcharsize, !x.charsize)
        !x.margin=fcheck(xmargin, !x.margin)
        !x.ticklen=fcheck(xticklen, !x.ticklen)
        !x.title=fcheck(xtitle,!x.title)
        !x.style=fcheck(xstyle,!x.style)

        !y.charsize=fcheck(ycharsize, !y.charsize)
        !y.margin=fcheck(ymargin, !y.margin)
        !y.minor=fcheck(yminor, !y.minor)
        !y.range=fcheck(yrange, !y.range)
        !y.style=fcheck(ystyle, !y.style)
        !y.ticklen=fcheck(yticklen,!y.ticklen)
        !y.tickname=fcheck(ytickname,!y.tickname)
        !y.ticks=fcheck(yticks, !y.ticks)
        !y.tickv=fcheck(ytickv, !y.tickv)
        !y.title=fcheck(ytitle,!y.title)
        !y.type=fcheck(ytype, 0) ;clear ytype


wbsph1 = fltarr( nnn*4 )

for i = 0, nnn*4-1 do begin
  wbsph1( i ) = total( wbsph( ch(0):ch(1),i ) )
endfor

if ( keyword_set( timerange0 ) ) and  ( exflag eq 0 ) then begin

  utplot,xtime,wbsph1*!p.nsum,index(0),symsize=fcheck(symsize,1.0),ytype=!y.type,xstyle=1,timerange=timerange0

endif else begin

  utplot,xtime,wbsph1*!p.nsum,index(0),symsize=fcheck(symsize,1.0),ytype=!y.type

endelse

if ( keyword_set( err ) ) then begin
  wbsph2 = fltarr( nnn*4/!p.nsum )
  xtime2 = fltarr( nnn*4/!p.nsum )
  itime2 = fltarr( nnn*4/!p.nsum )
  for i = 0,( nnn )*4/!p.nsum-1 do begin
     wbsph2( i ) = total( wbsph1( i*!p.nsum:i*!p.nsum+!p.nsum-1 ) )
     xtime2( i ) = total( xtime( i*!p.nsum:i*!p.nsum+!p.nsum-1 ) )/!p.nsum
     itime2( i ) = total( itime( i*!p.nsum:i*!p.nsum+!p.nsum-1 ) )/!p.nsum
  endfor
  errplot,xtime2,wbsph2-sqrt(wbsph2)/sqrt(itime2/hiint)*err, $
                 wbsph2+sqrt(wbsph2)/sqrt(itime2/hiint)*err,width=0
endif

if ( keyword_set( disp ) ) then begin

  menu:
   menu = [ ' Expand this picture     ', $
            ' Reset                   ', $
            ' End                     ']
   sel = wmenu_sel( menu,/one )

   case 1 of
     ( sel(0) eq -1 ):goto,next
     ( sel(0) eq 0 ) :goto,expand
     ( sel(0) eq 1 ) :goto,reset
     ( sel(0) eq 2 ) :goto,next
  endcase

  expand:
   exflag = 0
   timerange0 = strarr(2)
   tvrdc,st,y,3
   exst_msod = ( st_msod + long( st*1000 ) ) mod 86400000
   exst_ds79 = st_ds79 + fix( ( st_msod + long( st*1000 ) )/86400000 )
   if ( exst_msod lt 0 ) then begin
     exst_msod = exst_msod + 86400000
     exst_ds79 = exst_ds79 - 1
   endif
   plots,[ st,st ],[ !y.crange(0),!y.crange(1)],/data
   tvrdc,ed,y,3
   if ( ( ed -st ) lt 0.01 ) then ed = st + 0.01
   exed_msod = ( st_msod  + long( ed*1000 ) ) mod 86400000
   exed_ds79 = st_ds79 + fix( ( st_msod + long( ed*1000 ) )/86400000 ) 
   plots,[ ed,ed ],[ !y.crange(0),!y.crange(1)],/data
   int2ex,exst_msod,exst_ds79,ut
   timerange0(0) = gt_time(ut,/string,/msec)+' '+gt_day(exst_ds79,/string)
   int2ex,exed_msod,exed_ds79,ut
   timerange0(1) = gt_time(ut,/string,/msec)+' '+gt_day(exed_ds79,/string)
  goto,graphic

  reset:
   exflag=1
  goto,graphic

  next:
   if ( keyword_set(timerange0) ) then begin
     timerange=timerange0
     save,timerange,filename='timerange.dat'
   endif

endif

;save new system variables needed for overplotting
xcrange = !x.crange
xs      = !x.s
xwindow = !x.window
xregion = !x.region
xtype   = !x.type

ycrange = !y.crange
ys      = !y.s
ywindow = !y.window
yregion = !y.region
ytype   = !y.type

pmulti  = !p.multi
pclip   = !p.clip

;restore original system variables
!x=xsave
!p=psave
!y=ysave

;replace system variables needed for overplotting
!x.crange = xcrange
!x.s      = xs
!x.window = xwindow
!x.region = xregion
!x.type   = xtype

!y.crange = ycrange
!y.s      = ys
!y.window = ywindow
!y.region = yregion
!y.type   = ytype

!p.multi  = pmulti
!p.clip   = pclip

return
end