;+
; NAME:
;     PROFILE_DISPLAY
; PURPOSE:
;     This widget is used by ovsa_explorer to display the results of fitting movie.
;     It may be also used to display a movie saved by ovsa_explorer in a "*.med" file
; CATEGORY:
;     OVSA APC CALIBRATION ANALYSIS
; CALLING SEQUENCE:
;   profile_display, group = Group, movie=movie,time=time,profile=profile
; INPUTS:
;        movie - an array of fit profiles produced by tp_fit
;        time  - the time array corresponding to the movie
; OUTPUTS: profile - a structure containing the absolute spectral peak profile
;          each index correspond to a data subrange
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;
;
; ROUTINES CALLED:
;
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
;              ALL the frames in the movie should have the same number of fit ranges.
;              To assure this, one should call tp_fit with lf and uf keywords predefined
;
; MODIFICATION HISTORY:
;     Written 20-Aug-2002 by Gelu M. Nita (gn2@njit.edu)
;     14-April-2003, GN
;     Added prange keyword to show fit ranges if provided
;     1-Nov-2003, GN
;     Added file to allow displayng the profile of a already existing med file
;     Added notrealize to allow returning the movie and profile without creating the widget

   pro profile_display_event,event=event

   end

   pro profile_display, file, group = Group, movie=movie,time=time,profile=profile,prange=prange,notrealize=notrealize
      device,decompose=0
      loadct,39
      defsysv,'!DEFAULTS',EXISTS=exists
      if not exists then defparms
      pmulti=!p.multi
      dwindow=!d.window
      CATCH, Error_status
   IF Error_status NE 0 THEN BEGIN
     wset,dwindow
     !p.multi=pmulti
     return
   ENDIF

   IF N_ELEMENTS(Group) EQ 0 THEN GROUP=0

   if n_elements(movie) eq 0 then begin
   if n_elements(file) eq 0 then file=dialog_pickfile(filter='*.med',path=!defaults.datadir)
    restore,file
    time=movie_time
   end

   profile={LowFreq:0,HighFreq:0,HighLowCorr:0,StartTime:0.0,$
   PeakTime:0.0,EndTime:0.0,PeakFreq:0.0,PeakFlux:0.0,Comments:'',$
   A:0.0,B:0.0,alpha:0.0,beta:0.0,ncomponents:0}

     frames=movie.frame
     frames=frames(uniq(frames(sort(frames))))
     frame_time=time(frames)

     !p.multi=[0,2,2]
     h=histogram(movie.frame)
     nranges=max(h)
     nmovie=n_elements(movie)
     npoints=nmovie/nranges
     conv=(peakflux=(peakfreq=(alpha=(gamma=fltarr(nranges,npoints)))))
     A=(B=(beta=alpha))
     for i=0,nmovie-1 do begin
      peakflux[i mod nranges,i/nranges]=movie[i].peakflux
      peakfreq[i mod nranges,i/nranges]=movie[i].peakfreq
      a[i mod nranges,i/nranges]=movie[i].param[0]
      alpha[i mod nranges,i/nranges]=movie[i].param[1]
      b[i mod nranges,i/nranges]=movie[i].param[2]
      beta[i mod nranges,i/nranges]=movie[i].param[3]
      gamma[i mod nranges,i/nranges]=alpha[i]-beta[i]
      conv[i mod nranges,i/nranges]=movie[i].conv
     end

     abs_peaktime=(abs_peakfreq=(abs_peakflux=fltarr(nranges)))
     for k=0,nranges-1 do begin
     abs_peakflux[k] =max(peakflux[k,*],imax,/nan)
     abs_peakfreq[k]=peakfreq[k,imax]
     abs_peaktime[k]=time[imax]
     if abs_peakfreq[k] lt 3 then profile.LowFreq=1
     if abs_peakfreq[k] ge 3 then profile.HighFreq=1
     end

     h=histogram(abs_peaktime,binsize=4*(time[1]-time[0]))
     if max(h) gt 1 then profile.HighLowCorr=1
     profile.peakflux=max(abs_peakflux,kmax,/nan)
     profile.peakfreq=abs_peakfreq[kmax]
     profile.peaktime=abs_peaktime[kmax]
     profile.a=a[kmax,imax]
     profile.b=b[kmax,imax]
     profile.alpha=alpha[kmax,imax]
     profile.beta=beta[kmax,imax]
     profile.starttime=time[0]
     profile.endtime=time[n_elements(time)-1]
     profile.ncomponents=nranges

if keyword_set(notrealize) then begin
 wset,dwindow
 !p.multi=pmulti
 return
end

   wBase = WIDGET_BASE(GROUP_LEADER=Group, $
      COLUMN=1, $
      MAP=1, $
      MBAR=mbar, $
      TITLE='Display Profile', $
      UVALUE='MainBase')

      wdraw = WIDGET_DRAW(wBase, $
      XSIZE=640, YSIZE=500,FRAME=1)

     ExtraBase = WIDGET_BASE(wBase, $
      ROW=1, $
      MAP=1, $
      TITLE='ExtraBase', $
      UVALUE='ExtraBase')

   ProfileBase=WIDGET_BASE(wBase,/Row)

   PeakTimeLabel=Widget_Label(ProfileBase, VALUE='Abs Peak Time:',/Align_Left)
   wPeakTime=Widget_Text(ProfileBase,UNAME='PeakTime',Value=msec2str(profile.peaktime),UVALUE='PeakTime')


   wPeakFreqLabel=Widget_Label(ProfileBase, VALUE='Abs Peak Freq: ',/Align_Left)
   wPeakFreq=Widget_Text(ProfileBase,UNAME='PeakFreq',Value=strtrim(profile.peakfreq),UVALUE='PeakFreq')



   wPeakFluxLabel=Widget_Label(ProfileBase, VALUE='Abs Peak Flux:  ',/Align_Left)
   wPeakFlux=Widget_Text(ProfileBase,UNAME='PeakFlux',Value=strtrim(profile.peakflux),UVALUE='PeakFlux')

   widget_control,wbase,/realize

     yrange=[0,profile.peakflux]
     utplot,time/1000.,peakflux[0,*],datstr,Title='Peak Flux',back=255,color=0,yrange=yrange,ysty=1,psym=10,/nodata
     for k=0, nranges-1 do begin
      outplot,frame_time/1000.,peakflux[k,*],color=80*k,psym=10,thick=1+k;,linestyle=2*k
      if keyword_set(prange) then begin
       if ptr_valid(prange) then begin
        range=*prange
        if ptr_valid(range[k].peak) then begin
         peak=*range[k].peak
         for p=0,n_elements(peak)-1 do begin
             if peak[p].last ne 0L then begin
              outplot,[peak[p].first,peak[p].first]/1000.,yrange,linestyle=2,color=80*k
              outplot,[peak[p].last,peak[p].last]/1000.,yrange,linestyle=1,color=80*k
             end
         end
        end
       end
      end
     end

     yrange=[1,18]
     utplot,time/1000.,peakfreq[0,*],datstr,Title='Peak Frequency',back=255,color=0,yrange=yrange,ystyle=1,/ylog,psym=10,/nodata
     for k=0, nranges-1 do begin
      outplot,frame_time/1000.,peakfreq[k,*],color=80*k,psym=10,thick=1+k;,linestyle=2*k
      if keyword_set(prange) then begin
       if ptr_valid(prange) then begin
        range=*prange
        outplot,[time[0],time[n_elements(time)-1]]/1000.,[range[k].lf,range[k].lf],linestyle=1,color=80*k
        outplot,[time[0],time[n_elements(time)-1]]/1000.,[range[k].uf,range[k].uf],linestyle=1,color=80*k
        if ptr_valid(range[k].peak) then begin
         peak=*range[k].peak
         for p=0,n_elements(peak)-1 do begin
             if peak[p].last ne 0L then begin
              outplot,[peak[p].first,peak[p].first]/1000.,yrange,linestyle=2,color=80*k
              outplot,[peak[p].last,peak[p].last]/1000.,yrange,linestyle=1,color=80*k
             end
         end
        end
       end
      end
     end

     yrange=[0,7]
     utplot,time/1000.,alpha[0,*],datstr,Title='Low freq. power index',back=255,color=0,psym=10,yrange=yrange,ysty=1,/nodata
     for k=0, nranges-1 do begin
      outplot,frame_time/1000.,alpha[k,*],color=80*k,psym=10,thick=1+k;,linestyle=2*k
      if keyword_set(prange) then begin
       if ptr_valid(prange) then begin
        range=*prange
        if ptr_valid(range[k].peak) then begin
         for p=0,n_elements(peak)-1 do begin
             if peak[p].last ne 0L then begin
              outplot,[peak[p].first,peak[p].first]/1000.,yrange,linestyle=2,color=80*k
              outplot,[peak[p].last,peak[p].last]/1000.,yrange,linestyle=1,color=80*k
             end
         end
        end
       end
      end
     end

     yrange=[-7,0]
     utplot,time/1000.,gamma[0,*],datstr,Title='High freq. power index',back=255,color=0,psym=10,yrange=yrange,ysty=1,/nodata
     for k=0, nranges-1 do begin
      outplot,frame_time/1000.,gamma[k,*],color=80*k,psym=10,thick=1+k;,linestyle=2*k
      if keyword_set(prange) then begin
       if ptr_valid(prange) then begin
        range=*prange
        if ptr_valid(range[k].peak) then begin
         peak=*range[k].peak
         for p=0,n_elements(peak)-1 do begin
             if peak[p].last ne 0L then begin
              outplot,[peak[p].first,peak[p].first]/1000.,yrange,linestyle=2,color=80*k
              outplot,[peak[p].last,peak[p].last]/1000.,yrange,linestyle=1,color=80*k
             end
         end
        end
       end
      end
     end

     wset,dwindow
     !p.multi=pmulti
     xmanager,'profile_display',wBase, /NO_BLOCK
  end