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

# $Log: not supported by cvs2svn $

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

APSApplication . -name SRbpmGainReview -version 1 \
    -overview {This application is for review of the bpm gains calculated by the response matrix fitting.}

APSLabeledEntry .addOptions -parent .userFrame \
    -label "Additional sddsplot option:" \
    -textVariable addOptions -width 60

set chooseFilesButton .userFrame.chooseFiles.button
APSButton .chooseFiles -parent .userFrame -text "Choose datasets..." -command \
    {
	APSDisableButton $chooseFilesButton
	set datasetList [ChooseDatasets]
	APSEnableButton $chooseFilesButton
	GeneratePlot $plotFile $addOptions
	update idletasks
    } -contextHelp "Chooses datasets and then plot them."

set plotButton .userFrame.plot.button
APSButton .plot -parent .userFrame -text "Replot" -command \
    {
	GeneratePlot $plotFile $addOptions
	update idletasks
    } -contextHelp "Replots chosen datasets."

proc GeneratePlot { plotFile addOptions } {

    set commandString ""
    foreach bpm {P1 P2 P3 P4 P5 P0} {
	set commandStringX ""
	set commandStringY ""
	foreach column [exec sddsquery -col $plotFile | grep C] {
	    append commandStringX "-col=BPMName,$column -match=col,BPMName=*$bpm -match=col,ElementParameter=XC* $addOptions -graph=symbol,connect=subtype,scale=1.5,vary=subtype -legend=specified=$column -ylabel=Xcalibration -title=$bpm -topTitle $plotFile "
	    append commandStringY "-col=BPMName,$column -match=col,BPMName=*$bpm -match=col,ElementParameter=YC* $addOptions -graph=symbol,connect=subtype,scale=1.5,vary=subtype -legend=specified=$column -ylabel=Ycalibration -title=$bpm -topTitle $plotFile "
	}
	append commandString "$commandStringX -layout=1,2 -end $commandStringY -layout=1,2 -end "
    }
    eval exec sddsplot $commandString &

}


proc mycompare { a b } {
    set adate [string range $a 5 18]
    set bdate [string range $b 5 18]
    set res [string compare $adate $bdate]
    if {$res != 0} {
        return $res
    } else {
        return [string compare $a $b]
    }
}

proc ChooseDatasets { } {
    global plotFile
    set dirList [exec ls /oagscratch/OAGSCRATCH/sajaev/config/]
    set commentList ""
    set datasetList ""
    foreach dir $dirList {
	set directory /oagscratch/OAGSCRATCH/sajaev/config/$dir
	if [file exists $directory/bpm.gain] {
	    set comment "${dir}: "
	    lappend commentList [append comment [exec cat $directory/comment]]
	    lappend datasetList $directory/bpm.gain
	}
    }
    set sortedCommentList [lsort -command mycompare $commentList]
    set sortedDatasetList ""
    foreach comment $sortedCommentList {
	set i [lsearch -exact $commentList $comment]
	if {$i >= 0} {
	    lappend sortedDatasetList [lindex $datasetList $i]
	} else {
	    puts stderr "Error in the lists!"
	}
    }
    
    set chosenList [APSChooseItemFromList \
			-name "Data Set Selection" \
			-itemList $sortedCommentList \
			-returnList $sortedDatasetList \
			-returnIndices 0 \
			-multiItem 1 \
			-contextHelp "Select a beta function measurement data set for data reprocessing."]

    if ![llength $chosenList] {
	APSAlertBox .noChosen -errorMessage "Error: No files chosen!"
	return
    } else {
	set tmpFile /tmp/[APSTmpString]
	exec sddsconvert [lindex $chosenList 0] $plotFile -retain=col,BPMName -retain=col,ElementParameter
	foreach file $chosenList {
	    set colName C[file tail [file dirname $file]]
	    exec sddsxref -nowarning $plotFile $file -pipe=out -take=Gain -match=BPMName \
		| sddsconvert -pipe=in $tmpFile -rename=col,Gain=$colName
	    file copy -force $tmpFile $plotFile
	}
	file delete $tmpFile
	return $chosenList
    }

}

set plotFile /tmp/bpmGainPlot.tmp
set addOptions " "