#!/bin/sh  
# \
exec oagwish "$0" "$@"



set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0  /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.28 $ \$Author: emery $"

APSApplication . -name LinacSynchDataAnalysis -version $CVSRevisionAuthor 

#======================================================================= 
# Process of the anything status output
#======================================================================= 
proc SetStatusText {text} {
   global configStatus         # variable to output status of anything
   set configStatus "$text"
   update
}

   set w .userFrame

   SetStatusText "I'm ready. Let us work, please..."

APSScrolledStatus .status -parent .userFrame -textVariable configStatus \
                          -width 90 -height 10
update

#======================================================================= 
# Process of the creating main window
#======================================================================= 
proc MakeMainWindow {widget args} {



    global typeLayout   # option layout for sddsplot
    global workingDir   # working directory
    global workingFile  # working file
    global workingFile0 # working file  (total name with path)
    global bpmName      # BPM's name  for the shift correlation analysis 
    global shiftyFrom   # lower limit for the shift correlation analysis 
    global shiftyTill   # upper limit for the shift correlation analysis
    global resetSetup   # flag for setSynch
    global colorDots
    global colorDotsInterval 
    global correlationThreshold
#    set workingDir /home/helios5/BORLAND/LEUTL/daily
#--------------------------------------------------------------
# For debugging:
    set workingDir [pwd]


    set parent ""
    set workingFile0 ""
    set workingFile ""
    set typeLayout 3,3
    set bpmName ""
    set shiftyFrom -6
    set shiftyTill 6
    set resetSetup Reset
    set colorDots 0
    set colorDotsInterval 60
    set correlationThreshold 0.5
    APSStrictParseArguments {parent}

#--------------------------------------------------------------------
#  main window:
#
    APSFrameGrid .main -parent $parent  -yList {up middle down} \
                       -packOption "-side top" \
                       -width 175 -height 20 \
                       -contextHelp "This application allows to alanlyse \
                                     some correlations for BPM orbits \
                                     measurements"

    set w $parent.main

    APSLabeledOutput .outputWorkingFile -parent $w.up  \
                                       -label "Selected file:" \
                                       -textVariable workingFile0 \
                 -gridPack "-column 0 -row 0 -columnspan 2 -sticky e" \
                                       -width 70 \
                                       -contextHelp "Selects which file to \
                                                     work with"
#--------------------------------------------------------------------
#  Set up BPM  action:
#
    APSFrame .setSynch -parent $w.down -label "" \
                     -gridPack "-column 0 -row 0" \
                     -contextHelp "Set up the linac RF for beam-synchronous 
                                   acquisition; \
                                   reset them to normal mode; collect data"       

    set w3 $w.down.setSynch.frame

    APSButton .monitorFile -parent $w3 -text \
                          "Pick synch. monitor file" \
                          -gridPack "-column 0 -row 0" \
                          -command  "selectMonitorFile $w3" \
                          -contextHelp "Selects which monitor file to use for synchronous channels."
    APSLabeledEntry .monitorFileLE -parent $w3 -label "->" \
      -gridPack "-column 1 -row 0" \
      -textVariable monitorFile -width 80 -contextHelp "Specifies monitor file to use for synchronous channels."\
      
    APSButton .wfMonitorFile -parent $w3 -text \
                          "Pick waveform monitor file" \
                          -gridPack "-column 0 -row 1" \
                          -command  "selectWaveformMonitorFile $w3" \
                          -contextHelp "Selects waveform file to use for synchronously acquired channels."
                  
    APSLabeledEntry .wfMonitorFileLE -parent $w3 -label "->" \
      -gridPack "-column 1 -row 1" \
      -textVariable waveformMonitorFile -width 80 -contextHelp \
      "Specifies waveform monitor file to use for synchronously acquired channels."

    APSButton .slowMonitorFile -parent $w3 -text \
                          "Pick slow monitor file" \
                          -gridPack "-column 0 -row 2" \
                          -command  "selectSlowMonitorFile $w3" \
                          -contextHelp "Selects monitor file to use for nonsynchronously acquired channels."
                  
    APSLabeledEntry .slowMonitorFileLE -parent $w3 -label "->" \
      -gridPack "-column 1 -row 2" \
      -textVariable slowMonitorFile -width 80 -contextHelp \
      "Specifies monitor file to use for nonsynchronously acquired channels."

#--------------------------------------------------------------------
#  Job7=setSynch:
#
    APSButton .synch -parent $w3 -text "     setSynch      " \
                   -gridPack "-column 0 -row 4" \
                   -command  "setSynch 1" \
                   -contextHelp "Set EPICS to read rf synchronous with beam pulses."
    APSButton .synch1 -parent $w3 -text "   resetSynch      " \
                   -gridPack "-column 0 -row 5" \
                   -command  "setSynch 0" \
                   -contextHelp "Reset EPICS to read rf at every rf pulse, rather than with beam pulse."
#    APSDisableButton $w3.synch.button                 
                      
#--------------------------------------------------------------------
#  Job8=collectData:
#
    APSButton .collect -parent $w3 -text "    collect data   " \
                   -gridPack "-column 0 -row 6" \
                   -command  "collectData" \
                   -contextHelp ""
    APSDisableButton $w3.collect.button                 

#--------------------------------------------------------------------
#  File, layout and action selections:
#
    APSFrame .job -parent $w.middle -label "" \
                  -width 175 -height 155 \
                  -contextHelp "Selects type of analysis"       

    set w1 $w.middle.job.frame

    APSButton .selectFile -parent $w1 -text \
                          "  Pick File...  " \
                          -gridPack "-column 0 -row 0" \
                          -command  "selectWorkingFile $w" \
                          -contextHelp "Selects which file to analize with"

    APSButton .clean -parent $w1 -text " Clean up files " \
      -gridPack "-column 1 -row 3" \
      -command "APSDeleteTempFiles" \
      -contextHelp "Removes working files.  Will be done at exit but you can do it deliberately to free up disk space."

      APSLabeledEntry .corrLimit -parent $w1 -label "Correlation\nthreshold: " -width 4 \
      -gridPack "-column 0 -row 4" -textVariable correlationThreshold -contextHelp \
      "Enter a threshold for correlations that get plotted.  Smaller values tend to result in exponentially more plots!"

    APSRadioButtonFrame .typeLayout -parent $w1 -label "Layout:" \
                                    -gridPack "-column 0 -row 5" \
                                    -variable typeLayout \
                                    -buttonList {1 2,2 3,3} \
                                    -valueList  {1 2,2 3,3} \
                                    -orientation horizontal -contextHelp \
                                    "Selects which layout to plote with." 
    APSRadioButtonFrame .colordots -parent $w1 -label "Color dots" \
                                    -gridPack "-column 1 -row 5" \
                                    -variable colorDots \
                                    -buttonList {Yes No} \
                                    -valueList  {1 0} \
                                    -orientation horizontal -contextHelp \
                                    "If Yes, then dots change color according to elapsed time."
    APSLabeledEntry .colordotsinterval -parent $w1 -label "Interval (s)" \
      -gridPack "-column 2 -row 5" -textVariable colorDotsInterval \
      -contextHelp "Interval (in seconds) for changes in dot color."
#--------------------------------------------------------------------
#  Job selection:
#
#--------------------------------------------------------------------
#  Job1=AnalyzeStats:
#
    APSButton .stats -parent $w1 -text "  analyzeStats  " \
                     -command  "AnalyzeStats $w" \
                     -gridPack "-column 0 -row 1" \
                     -contextHelp "Calculation of the standard deviations"
    APSDisableButton $w1.stats.button                 
 
#--------------------------------------------------------------------
#  Job2=AnalyzePatterns:
#
    APSButton .patterns -parent $w1 -text " analyzePatterns" \
                        -gridPack "-column 0 -row 2" \
                        -command  "AnalyzePatterns $w" \
                        -contextHelp "Calculation of the pseudoinverse matrix \
                                      for XPOS/YPOS measured values; plotting \ 
                                      of the SVD vectors for these \
                                      transformations"
    APSDisableButton $w1.patterns.button                 
 
#--------------------------------------------------------------------
#  Job3=PlotCorrelations:
#
    APSButton .plotCorrelations -parent $w1 -text "plotCorrelations" \
                                -gridPack "-column 1 -row 0" \
                                -command  "PlotCorrelations $w" \
                                -contextHelp "Calculation of the correlation \
                                              coefficients and their \
                                              significances between XPOS/YPOS \
                                              and another measured values; \
                                              Plotting the dependences of \
                                              these values  on XPOS/YPOS \
                                              magnitudes for each BPM"
    APSDisableButton $w1.plotCorrelations.button                 

    APSButton .plotCorrelations1 -parent $w1 -text "plotCorrelations1" \
                                -gridPack "-column 1 -row 1" \
                                -command  "PlotCorrelations1 $w" \
                                -contextHelp "Calculation of the correlation \
                                              coefficients and their \
                                              significances between a chosen readback and all others."
    APSDisableButton $w1.plotCorrelations1.button                 
 
#--------------------------------------------------------------------
#  Job4=PsdPlots:
#
    APSButton .psdPlots -parent $w1 -text "    psdPlots    " \
                        -gridPack "-column 0 -row 3" \
                        -command  "PsdPlots $w" \
                        -contextHelp "Calculation and plotting of the power \
                                      spectral densities"
    APSDisableButton $w1.psdPlots.button                 

#--------------------------------------------------------------------
#  Job5=XyCorrelation:
#
    APSButton .xyCorrelation -parent $w1 -text "  xyCorrelation " \
                             -gridPack "-column 1 -row 2" \
                             -command  "XyCorrelation $w" \
                             -contextHelp "Calculation and plotting of the \
                                        correlation functions between XPOS \
                                        and YPOS measurements"
    APSDisableButton $w1.xyCorrelation.button                 


#--------------------------------------------------------------------
#   window for Job6=Shifty:
#
    APSFrame .shifty -parent $w1 -label "" \
                     -gridPack "-column 2 -row 0 -rowspan 4" \
                     -contextHelp "Calculation and plotting of the \
                                   correlation functions"       

    set w2 $w1.shifty.frame

#--------------------------------------------------------------------
#   Job6=Shifty:
#
    APSButton .shiftyDone -parent $w2 -text "Correlation function" \
                          -gridPack "-column 0 -row 1" \
                          -command  shiftyDone \
                          -contextHelp "Calculation and plotting of the \
                                        autocorrelation functions"
    APSDisableButton $w2.shiftyDone.button                 

    APSLabeledEntry .from -parent $w2 -label " shift from: " \
                          -gridPack "-column 0 -row 2" \
                          -textVariable shiftyFrom -width 5 -type integer \
                          -contextHelp "Lower limit to calculate the \
                                        autocorrelation functions"

    APSLabeledEntry .till -parent $w2 -label " shift till: " \
                          -gridPack "-column 0 -row 3" \
                          -textVariable shiftyTill -width 5 -type integer \
                          -contextHelp "Upper limit to calculate the \
                                        autocorrelation functions"

    APSLabeledEntry .bpmName -parent $w2 -label "BPM:" \
                             -textVariable bpmName -width 15 \
                             -gridPack "-column 0 -row 4" \

    APSButton .bpm -parent $w1.shifty.frame -text "     Select BPM     " \
                   -gridPack "-column 0 -row 4" \
                   -command  "nameBPM $w" \
                   -contextHelp "Selects which BPM to calculate the \
                                 autocorrelation function  with"
    APSDisableButton $w2.bpm.button                 
}

#======================================================================= 
# Process of working file selection
#======================================================================= 
proc selectWorkingFile {w} {

    global workingFile workingDir workingFile0
    global rootname

    set w1 $w.middle.job.frame
    set w2 $w1.shifty.frame
    
    set file [APSFileSelectDialog [APSUniqueName .] -path $workingDir]

    set dlina [string length $file]
    if {$dlina==0} {
       SetStatusText "Choose working file, please"
       return               
    }
    set workingFile0 $file
    set workingDir [file dirname $file]
    set extention [string range $file [expr $dlina - 4] $dlina]
    if {![string compare [exec sddscheck $file] ok]==0} {
       SetStatusText "$file is not an SDDS file"
       return               
    }
    regsub -all $workingDir/ $file "" workingFile
    regsub -all .sdds $workingFile "" rootname

    cd $workingDir/ 
    APSEnableButton $w1.stats.button                 
    APSEnableButton $w1.patterns.button                 
    APSEnableButton $w1.plotCorrelations.button                 
    APSEnableButton $w1.plotCorrelations1.button                 
    APSEnableButton $w1.psdPlots.button                 
    APSEnableButton $w1.xyCorrelation.button                 
    APSEnableButton $w2.bpm.button                 
}

#======================================================================= 
# Process of monitor file selection
#======================================================================= 
    set monitorFile ""
    set monitorDir [pwd]

proc selectMonitorFile {w} {

    global monitorFile monitorDir 
    global rootname

    set w1 $w.synch.button
    set w2 $w.collect.button
    
    set file [APSFileSelectDialog [APSUniqueName .] -path $monitorDir]

    set dlina [string length $file]
    if {$dlina==0} {
       SetStatusText "Choose monitor file, please"
       return               
    }
    set monitorFile $file
    set monitorDir [file dirname $file]
    set extention [string range $file [expr $dlina - 3] $dlina]
    if {[catch {exec sddscheck $file} result] || \
          [string compare $result ok]!=0} {
        SetStatusText "Choose an SDDS file, please"
        set monitorFile ""
        return
    }
    # APSEnableButton $w1
    APSEnableButton $w2
}

    set slowMonitorFile ""
    set slowMonitorDir [pwd]

proc selectSlowMonitorFile {w} {

    global slowMonitorFile slowMonitorDir
    global rootname

    set w1 $w.synch.button
    set w2 $w.collect.button
    
    set file [APSFileSelectDialog [APSUniqueName .] -path $slowMonitorDir]

    set dlina [string length $file]
    if {$dlina==0} {
       SetStatusText "Choose monitor file, please"
       return               
    }
    set slowMonitorFile $file
    set slowMonitorDir [file dirname $file]
    if {[catch {exec sddscheck $file} result] || \
          [string compare $result ok]!=0} {
        SetStatusText "Choose an SDDS file, please"
        set slowMonitorFile ""
        return
    }

    # APSEnableButton $w1
    APSEnableButton $w2
}

    set waveformMonitorFile ""
    set waveformMonitorDir [pwd]

proc selectWaveformMonitorFile {w} {

    global waveformMonitorFile waveformMonitorDir
    global rootname

    set w1 $w.synch.button
    set w2 $w.collect.button
    
    set file [APSFileSelectDialog [APSUniqueName .] -path $waveformMonitorDir]

    set dlina [string length $file]
    if {$dlina==0} {
       SetStatusText "Choose monitor file, please"
       return               
    }
    set waveformMonitorFile $file
    set waveformMonitorDir [file dirname $file]
    if {[catch {exec sddscheck $file} result] || \
          [string compare $result ok]!=0} {
        SetStatusText "Choose an SDDS file, please"
        set waveformMonitorFile ""
        return
    }

    # APSEnableButton $w1
    APSEnableButton $w2
}

#======================================================================= 
# Process CheckOfFileValidity
#======================================================================= 
proc CheckOfFileValidity {w} {

    global workingFile workingDir

    if [string length $workingFile]==0 {
       SetStatusText "The name of the working file is empty"
       selectWorkingFile $w
       SetStatusText "Now working file is $workingFile"
       return
    }
    if [catch {exec sddsprocess $workingFile -pipe=out} results] {
       SetStatusText "Problems due to $results"
       selectWorkingFile $w
       SetStatusText "Now working file is $workingFile"
    }
}
    
#======================================================================= 
# Process AnalyzeStats (job1)
#======================================================================= 
proc AnalyzeStats {w} {

    global workingFile workingDir

    CheckOfFileValidity $w

#-----------------------------------------------------------------------
# For XPOS, YPOS and VAL measured values the  standard deviations 
# are calculated (for each BPM); obtained values are normalysed on 
# magnitude of the first BPM
#   
    if [catch {exec sddsprocess $workingFile -pipe=out \
                    -process=*.?POS,stand,%sStDev \
                    -process=*.VAL,stand,%sStDev \
                   | sddscollapse -pipe \
                   | sddscollect -pipe \
                    -collect=suffix=.XPOSStDev,column=xStDev \
                    -collect=suffix=.YPOSStDev,column=yStDev \
                    -collect=suffix=.VALStDev,column=IStDev \
                   | sddsprocess -pipe -process=*StDev,first,%sFirst \
                   | sddsprocess -pipe \
                     "-define=column,%sNorm,%s %sFirst /,select=*StDev" \
                   | sddsprintout -pipe=in $workingFile.stats \
                    "-title=Data from $workingDir/$workingFile" \
                    -column=Rootname,label=BPM -column=*StDev,format=%-10.3g \
                    -column=*StDevNorm,format=%10.3f} results] {
       SetStatusText "Problems due to $results"
       APSAddToTempFileList $workingFile.stats
       APSDeleteTempFiles                    
       return
    }
    SetStatusText "Data are written to file $workingFile.stats"
    APSAddToTempFileList $workingFile.stats

    APSFileDisplayWindow [APSUniqueName .] -fileName $workingFile.stats \
                         -deleteOnClose 1 -width 90

}

#======================================================================= 
# Process AnalyzePatterns (job2)
#======================================================================= 
proc AnalyzePatterns {w} {

    global workingFile workingDir

    set limit 500

    CheckOfFileValidity $w

#-----------------------------------------------------------------------
# For XPOS and YPOS measured values the pseudoinverse matrices are 
# calculated; the eigenvectors of the SVD decomposition are written to 
# the file $workingFile.vm; results are plotted
#

    foreach type {XPOS YPOS} {
        APSAddToTempFileList $workingFile.$type.vm $workingFile.$type.csv $workingFile.$type
        APSAddToTempFileList $workingFile.$type.traj
       if [catch {exec sddsconvert $workingFile -pipe=out \
                     -retain=column,*$type \
                     | sddsprocess -pipe \
                     -process=*$type,ave,%sMean \
                     "-redefine=column,%s,%s %sMean -,select=*$type" \
                     | tee $workingFile.$type \
                     | sddspseudoinverse -pipe \
                       -vmatrix=$workingFile.$type.vm -digits=4 \
                     | sdds2stream -pipe -array=SingularValues \
                     | tr " " "\n" \
                     | csv2sdds -pipe -column=name=SingularValue,type=double \
                     | sddsprocess -pipe=in $workingFile.$type.csv -clip=0,1 \
                      -define=column,VectorNumber,i_row,type=long } results] {
          SetStatusText "Problems due to $results"
          APSDeleteTempFiles                    
          return
       }
       if [catch {exec sddscollect $workingFile.$type $workingFile.$type.traj \
                    -collect=suffix=.$type} result] {
           SetStatusText "Problems due to $results"
           APSDeleteTempFiles
           return
       }
       SetStatusText "Data are written to file $workingFile.vm"
       SetStatusText "Data are written to file $workingFile.csv"
       SetStatusText "Data are written to file $workingFile.$type.traj"

        exec sddsplot -column=VectorNumber,SingularValue -graph=symbol,connect \
          $workingFile.$type.csv \
          "-topline=SVs for $type readbacks" \
          "-title=$workingDir/$workingFile" \
          -mode=y=log "-ylabel=edit=i/Log/" &
        exec sddsplot -col=Rootname,.$type $workingFile.$type.traj -split=page \
          "-topline=Overlayed jitter trajectories for $type" \
          "-title=$workingDir/$workingFile" &
        catch {exec sddsprocess $workingFile.$type.csv \
                 -filter=column,SingularValue,1e-6,1e300 -pipe=out \
                 -print=column,VectorColumnName,SV%04ld,VectorNumber \
                 | sdds2stream -pipe -column=VectorColumnName} columnList
        exec sddsplot -column=OldColumnNames,([join $columnList ,]) \
          $workingFile.$type.vm -separate -same \
          "-topline=SVD jitter vectors for $type readbacks" \
          "-title=$workingDir/$workingFile" &
    }

}

proc PlotCorrelations1 {w} {
    global typeLayout correlationThreshold
    global workingFile workingDir colorDots colorDotsInterval

    CheckOfFileValidity $w

    if [catch {APSGetSDDSNames -fileName $workingFile -class column} columnList] {
        return -code error "$columnList"
    }
    if [catch {APSChooseItemFromList -name "Choose item to correlate against." \
                 -itemList $columnList -returnIndices 0 -multiItem 0} chosenItem] {
        return -code error "$chosenItem"
    }
    if ![string length $chosenItem] {
        return
    }
    APSAddToTempFileList $workingFile.cor1
    if [catch {exec sddscorrelate $workingFile -pipe=out \
                 -withOnly=$chosenItem -column=* -exclude=Step,CAErrors -rankOrder \
                 | sddsprocess -pipe \
                 "-define=column,AbsCC,CorrelationCoefficient abs" \
                 | sddssort -pipe -column=CorrelatePair \
                 | sddsprocess -pipe \
                 -define=column,Index,i_row,type=long \
                 | sddssort -pipe=in $workingFile.cor1 \
                 -column=AbsCC,decr} result] {
        SetStatusText "Processing problems due to $result"
        APSDeleteTempFiles                    
        return
    }
    
    set optionList ""
    sdds load $workingFile.cor1 data
    foreach name1 [lindex $data(Column.Correlate1Name) 0] \
      name2 [lindex $data(Column.Correlate2Name) 0] \
      CC [lindex $data(Column.CorrelationCoefficient) 0] \
      CS [lindex $data(Column.CorrelationSignificance) 0] {
          if [expr abs($CC)>$correlationThreshold] {
              lappend optionList -column=$name1,$name2 \
                "-topline=CCoef: [format %.3f $CC]   CSig: [format %g $CS]" -end
          }
      }
    if {$colorDots && $colorDotsInterval>0} {
        set extraOpts [list -graph=dot,vary -split=column=Time,width=$colorDots]
    } else {
        set extraOpts -graph=dot
    }
    if {[string compare $typeLayout 1]==0} {
        eval exec sddsplot {"-title=$workingDir/$workingFile"} $workingFile $extraOpts $optionList &
    } else {
        eval exec sddsplot {"-title=$workingDir/$workingFile"} $workingFile $extraOpts \
          -layout=$typeLayout $optionList &
    }          
}

#======================================================================= 
# Process plotCorrelations (job3)
#======================================================================= 
proc PlotCorrelations {w} {

    global typeLayout correlationThreshold
    global workingFile workingDir colorDots colorDotsInterval

    set otherPlane Y

    CheckOfFileValidity $w

#-----------------------------------------------------------------------
# Calculation of the pseudoinverse matrix for XPOS/YPOS measured values; 
# plotting of the SVD vectors for these  transformations
# 
    foreach plane {X Y} {
       if [string compare $plane Y]==0 {
          set otherPlane X
       }
       if [catch {exec sddsquery -column $workingFile \
                     | grep ${otherPlane}POS} exclColList] {
          SetStatusText "Problems - $exclColList"
          return
       }
       set exclOptList ""
       foreach item $exclColList {
          lappend exclOptList -exclude=$item 
       }
       if [catch {eval exec sddscorrelate $workingFile -pipe=out \
                                          -column=L*,Time $exclOptList \
                                          -rankOrder \
                          | sddsprocess -pipe \
                          {"-define=column,AbsCC,CorrelationCoefficient abs"} \
                          | sddssort -pipe -column=CorrelatePair \
                          | sddsprocess -pipe \
                                        -define=column,Index,i_row,type=long \
                          | sddssort -pipe=in $workingFile.$plane.cor\
                                     -column=AbsCC,decr} result] {
          SetStatusText "Processing problems due to $results"
          APSAddToTempFileList $workingFile.$plane.cor 
          APSDeleteTempFiles                    
          return
       }
       SetStatusText "Data are written to file $workingFile.$plane.cor"
       APSAddToTempFileList $workingFile.$plane.cor 

       if [catch {exec sddsprintout "-title=Data from $workingDir/$workingFile" \
                                    $workingFile.$plane.cor \
                                    $workingFile.$plane.cor.help \
                                    -column=*Name \
                                    -column=CorrelationCoefficient \
                                    -column=CorrelationSignificance \
                                    -column=Index} results] {
          APSAddToTempFileList $workingFile.$plane.cor.help 
          return
       }
       APSFileDisplayWindow [APSUniqueName .] \
                            -fileName $workingFile.$plane.cor.help \
                            -deleteOnClose 1 -width 100
       APSAddToTempFileList $workingFile.$plane.cor.help 
       SetStatusText "Data are written to file $workingFile.$plane.cor.help"

       if [catch {exec sddshist $workingFile.$plane.cor \
                                $workingFile.$plane.cor.his \
                                -data=AbsCC -sizeOfBins=0.05 -lower=0 \
                                -upper=1} results] {
          SetStatusText "Problems due to $results"
          APSAddToTempFileList $workingFile.$plane.cor.his 
          APSDeleteTempFiles                    
          return
       }
       SetStatusText "Data are written to file $workingFile.$plane.cor.his"
       APSAddToTempFileList $workingFile.$plane.cor.his 

       set optionList ""
       sdds load $workingFile.$plane.cor data
       foreach name1 [lindex $data(Column.Correlate1Name) 0] \
               name2 [lindex $data(Column.Correlate2Name) 0] \
               CC [lindex $data(Column.CorrelationCoefficient) 0] \
               CS [lindex $data(Column.CorrelationSignificance) 0] {
          if [expr abs($CC)>$correlationThreshold] {
             lappend optionList -column=$name1,$name2 \
             "-topline=CCoef: [format %.3f $CC]   CSig: [format %g $CS]" -end
          }
       }
        if {$colorDots && $colorDotsInterval>0} {
            set extraOpts [list -graph=dot,vary -split=column=Time,width=$colorDots]
        } else {
            set extraOpts -graph=dot
        }
       if {[string compare $typeLayout 1]==0} {
          eval exec sddsplot {"-title=$workingDir/$workingFile"} $workingFile $extraOpts $optionList &
       } else {
          eval exec sddsplot {"-title=$workingDir/$workingFile"} $workingFile $extraOpts \
                    -layout=$typeLayout $optionList &
       }          
    }
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $workingFile.X.cor.his $workingFile.Y.cor.his \
              -column=AbsCC,frequency -mode=y=log,y=special \
              -graph=sym,vary,scale=2 &

}

#======================================================================= 
# Process XyCorrelation (job4)
#======================================================================= 
proc XyCorrelation {w} {

#--------------------------------------------------------- 
# Exec FindFiles process:
#  
    global workingFile workingDir colorDots colorDotsInterval

    CheckOfFileValidity $w

    if [catch {exec sddsconvert $workingFile -pipe=out \
                    -retain=column,L*XPOS,L*YPOS -delete=column,LTP* \
                  | sddscorrelate -pipe -rankOrder \
                  | sddsprocess -pipe \
                    -edit=column,Name1,Correlate1Name,%/.XPOS//%/.YPOS// \
                    -edit=column,Name2,Correlate2Name,%/.XPOS//%/.YPOS// \
                    -match=column,Name1=@Name2 \
                  | sddsprintout "-title=Data from $workingDir/$workingFile" -pipe=in $workingFile.XY.help -column=Name1 \
                   -column=CorrelationCoefficient,format=%10.3f \
                   -column=CorrelationSignificance,format=%10.3f} results] {
       SetStatusText "Problems - $results"
       APSAddToTempFileList $workingFile.XY.help 
       APSDeleteTempFiles                    
       return
    }
    SetStatusText "Data are written to file $workingFile.XY.help"
    APSAddToTempFileList $workingFile.XY.help 

    APSFileDisplayWindow [APSUniqueName .] -fileName $workingFile.XY.help \
                         -deleteOnClose 1 -width 70

    if [catch {exec sddsquery -column $workingFile | grep BPM.XPOS \
                  | editstring -stream -edit=%/:BPM.XPOS//} BPMList] {
       SetStatusText "Problems - $BPMList"
       return
    }
    foreach bpm $BPMList {
        lappend pairList -column=${bpm}:BPM.XPOS,${bpm}:BPM.YPOS 
    }
    if {$colorDots && $colorDotsInterval>0} {
        set extraOpts [list -graph=dot,vary -split=column=Time,width=$colorDots]
    } else {
        set extraOpts -graph=dot
    }
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $extraOpts \
         -layout=3,3 -separate=request $workingFile $pairList &
}

#======================================================================= 
# Process PsdPlots (job5)
#======================================================================= 
proc PsdPlots {w} {

    global typeLayout
    global workingFile workingDir

    set plottedDataFwd ""
    
    CheckOfFileValidity $w
    
    if [catch {eval exec sddsbreak $workingFile -pipe=out \
                 -gapin=Time,factor=2 \
                 | sddsprocess -pipe -process=Time,count,Count \
                 | sddscollapse -pipe \
                 | sddsprocess -pipe -process=Count,max,CountMax \
                 | sdds2stream -pipe -parameter=CountMax} countMax] {
        return -code error "$countMax"
    }
    SetStatusText "Using continuous segment of $countMax points"
    if [catch {eval exec sddsbreak $workingFile -pipe=out \
                 -gapin=Time,factor=2 \
                 | sddsprocess -pipe {"-test=parameter,n_rows $countMax =="} \
                 | sddsfft -truncate -pipe -nowarning \
                 -columns=Time,*BPM*,*Phase,*FwdPwr,LVid* \
                 -psdOutput -window -suppressaverage \
                 | sddssmooth -pipe=in $workingFile.psd.smo \
                 -columns=*BPM*,*Phase,*FwdPwr} result] {
        SetStatusText "Problems due to $result"                  
       APSAddToTempFileList $workingFile.psd.smo
       APSDeleteTempFiles                    
       return
    } 
    APSAddToTempFileList $workingFile.psd.smo
    SetStatusText "Data are written to file $workingFile.psd.smo"

    if {![string compare $typeLayout 1]==0} {
       lappend plottedData -layout=$typeLayout
    }
    lappend plottedData -group=names
    lappend plottedData -separate=names
    lappend plottedData -graphic=line,vary
    lappend plottedData -mode=y=logarithmic,y=spec,x=logarithmic,x=spec
    lappend plottedData -ylabel=edit='S?/(/10D'
    lappend plottedData -same=y

    set plottedDataFwd $plottedData
    lappend plottedDataFwd -column=f,PSD*Fwd* 
    lappend plottedDataFwd $workingFile.psd.smo
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $plottedDataFwd &

    set plottedDataPhase $plottedData
    lappend plottedDataPhase -column=f,PSD*Phase*
    lappend plottedDataPhase $workingFile.psd.smo
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $plottedDataPhase &

    set plottedDataVal $plottedData
    lappend plottedDataVal -column=f,PSD*BPM.VAL* 
    lappend plottedDataVal $workingFile.psd.smo
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $plottedDataVal &

    set plottedDataXPOS $plottedData
    lappend plottedDataXPOS -column=f,PSD*BPM.XPOS* 
    lappend plottedDataXPOS $workingFile.psd.smo
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $plottedDataXPOS &

    set plottedDataYPOS $plottedData
    lappend plottedDataYPOS -column=f,PSD*BPM.YPOS* 
    lappend plottedDataYPOS $workingFile.psd.smo
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $plottedDataYPOS &

    set plottedDataVid $plottedData
    lappend plottedDataVid -column=f,PSD*LVid* 
    lappend plottedDataVid $workingFile.psd.smo
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $plottedDataVid &

}

#======================================================================= 
# Process namePBM (start of job6)
#======================================================================= 
proc nameBPM {w} {

    global workingFile workingDir
    global bpmName
    global plane

    set bpmList ""
    set w2 $w.middle.job.frame.shifty.frame
    
    CheckOfFileValidity $w

    if [catch {APSGetSDDSNames -class column -fileName $workingFile} signalList] {
       SetStatusText "Checking columnList of file $workingFile \
                           is wrong: $signalList"
       return                    
    }
    
    set reference [APSChooseItemFromList -name "BPM name"  \
                      -itemList $signalList -returnIndices 0 \
                      -multiItem 0] 
    if ![string length $reference] return

    set bpmName $reference
    
    APSEnableButton $w2.shiftyDone.button 
    shiftyDone                
}
#======================================================================= 
# Process shiftyDone (job6)
#======================================================================= 
proc shiftyDone {} {

    global typeLayout
    global shiftyFrom
    global shiftyTill
    global workingFile workingDir
    global bpmName
    global plane

    set reference $bpmName
    set plottedData1 ""
    set plottedData2 ""
    set fileListToMerge ""
    APSAddToTempFileList $workingFile.shift 
    if [catch {exec sddsshiftcor $workingFile $workingFile.shift \
        -with=$reference -scan=start=$shiftyFrom,end=$shiftyTill,delta=1 \
        -columns=*XPOS,*YPOS,*FwdPwr,*Phase*,LVid*} result] {
        return -code error "$result"
    }
    eval exec sddsplot {"-title=$workingDir/$workingFile"} $workingFile.shift \
        {"-topline=Shifted correlations with $reference"} \
        -column=ShiftAmount,*ShiftedCor -graph=impulse -sep -same &
}

#======================================================================= 
# Process setSynch (job7)
#======================================================================= 
proc setSynch {mode} {

    if !$mode {
        if [catch {exec cavput \
                     -list=L1,L2,L3,L4,L5 -list=:RF:trigAutoSetTimerAI=33} result] {
            return -code error "$result"
        }
    } else {
        if [catch {exec cavput \
                     -list=L1,L2,L3,L4,L5 -list=:RF:trigAutoSetTimerAI=30000} result] {
            return -code error "$result"
        }
    }
}

#======================================================================= 
# Process collectData (job8)
#======================================================================= 
    set samples 800
    set time 10
    set synch .02
    
proc  collectData {} {

    global samples time synch collectFile

    set collect [APSUniqueName .]
    
    APSDialogBox $collect -name "Parameters choice" -okCommand doCollect
    
    APSLabeledEntry .sample -parent $collect.userFrame -label "Samples: " \
                          -textVariable samples -width 5 -type integer \
                          -gridPack "-column 0 -row 0 -sticky w" \
                          -contextHelp ""

    APSLabeledEntry .time -parent $collect.userFrame -label " Time(min): " \
                          -textVariable time -width 5 -type integer \
                          -gridPack "-column 1 -row 0 -sticky w" \
                          -contextHelp ""

    APSLabeledEntry .synch -parent $collect.userFrame -label "   Synch: " \
                          -textVariable synch -width 5 -type real \
                          -gridPack "-column 2 -row 0 -sticky w" \
                          -contextHelp ""

    APSLabeledEntry .outputCollectedFile -parent $collect.userFrame  \
                                       -label "Output file:" \
                                       -textVariable collectFile \
                 -gridPack "-column 0 -row 1 -columnspan 3 -sticky w" \
                                       -width 37 \
                                       -contextHelp "Selects which file to \
                                                     work with"
    APSButton .collectFile -parent $collect.userFrame -text \
                          "Pick output file" \
                          -gridPack "-column 1 -row 2 -sticky w" \
                          -command  selectCollectFile \
                          -contextHelp "Selects which collect data file \
                                        to use with"
}    

#======================================================================= 
# Process of collect data file selection
#======================================================================= 
    set collectFile0 ""
    set collectFile ""
    set collectDir [pwd]

proc selectCollectFile {} {

    global collectDir collectFile collectFile0
    global monitorDir monitorFile monitorFile0
    global rootname 

    set file [APSFileSelectDialog [APSUniqueName .] -path $collectDir]

    set dlina [string length $file]
    if {$dlina==0} {
       SetStatusText "Choose file to collect data, please"
       return               
    }
    set collectFile0 $file
    set workingDir [file dirname $file]
    set extention [string range $file [expr $dlina - 4] $dlina]
    if {![string compare $extention sdds]==0} {
       SetStatusText "Choose file for collect data with \"sdds\" extention"
       return               
    }
    regsub -all $collectDir/ $file "" collectFile
    regsub -all .sdds $collectFile "" rootname

    cd $collectDir/ 
}

#======================================================================= 
# Process collectData (actually job8)
#======================================================================= 
proc  doCollect {} {

    global samples time synch
    global collectDir collectFile
    global monitorDir monitorFile slowMonitorFile waveformMonitorFile

    if {$samples < 0} {
       SetStatusText "Choose Samples > 0, please"
       return
    } elseif {$samples == 0} { 
       SetStatusText "Choose Samples > 0, please"
       return
    }   

    if {$time < 0} {
       SetStatusText "Choose Time > 0, please"
       return
    } elseif {$time == 0} { 
       SetStatusText "Choose Time > 0, please"
       return
    }   
    set times $time,m  

    if {$synch < 0} {
       SetStatusText "Choose Synch > 0, please"
       return
    } elseif {$synch == 0} { 
       SetStatusText "Choose Synch > 0, please"
       return
    }   

    set dlina [string length $collectFile]
    if {$dlina==0} {
       SetStatusText "Choose file to collect data, please"
       return               
    }
    set synchlogTask "$monitorFile $collectFile \
                      -samples=$samples -time=$times -synch=$synch -verbose -saveTimeStamps=$collectFile.ts "
    if [string length $slowMonitorFile] {
        append synchlogTask " -slowData=$slowMonitorFile "
    }
    if [string length $waveformMonitorFile] {
        append synchlogTask " -waveformData=$waveformMonitorFile "
    }
    set callbackTask "\"File $collectDir/$collectFile is written\""
    APSExecLog .writeCollectData -name "Collect data" \
                                 -unixCommand "sddssynchlog $synchlogTask" \
                                 -width 90 \
                                 -callback "SetStatusText $callbackTask"
}

#======================================================================= 
# Creating menu in working window
#======================================================================= 

MakeMainWindow .plotting -parent .userFrame

