
proc APSSRCheckHVCurrentCheck {args} {
    # Check statistics of setpoints and readbacks on SR H and V corrector power supplies.
    # Make a file appropriate for use with scorr to eliminate "bad" correctors.

    set samples 60 
    set interval 1
    set fileName ""
    set flagSuffix "Stability"
    set statusCallback "puts stderr"
    APSStrictParseArguments {samples interval fileName statusCallback flagSuffix}

    set input /home/helios/oagData/sr/sddsmonitorFiles/HVCurrentAI.mon

    if [string length $fileName]==0 {
        return -code error "APSSRCheckHVCurrentCheck: no fileName supplied"
    }

    if [string length $statusCallback] {
        eval $statusCallback {"Acquiring $samples corrector readback samples at intervals of $interval seconds."}
    }
    set tmpFile /tmp/[APSTmpString]
    if [catch {exec sddsmonitor \
                 $input $tmpFile -steps=${samples} -interval=${interval},seconds} result] {
        if [string length $statusCallback] {
            eval $statusCallback {"APSSRCheckHVCurrentCheck: $result"}
        }
        catch {exec rm $tmpFile}
        return -code error "APSSRCheckHVCurrentCheck: $result"
    }
    if [string length $statusCallback] {
        eval $statusCallback {"Finished data acquisition.  Processing..."}
    }

    if [catch \
          {exec sddsprocess $tmpFile -pipe=out \
             -process=*CurrentAI,drange,%sDRange \
             -process=*CurrentAI,stand,%sStDev \
             | sddscollapse -pipe \
             | sddscollect -pipe \
             -collect=suffix=:CurrentAIDRange,column=DecileRange \
             -collect=suffix=:CurrentAIStDev,column=StDev \
             | sddssort -column=DecileRange,decr -pipe \
             | sddsprocess -pipe=in $fileName \
             "-define=column,Rank,n_rows i_row -,type=long" \
             "-define=param,DegFree,$samples 1 -,type=long" \
             -edit=column,PSName,Rootname,eb%/:// \
             -process=StDev,average,StDevMean,ftail=0.9,bottom=2e-6 \
             "-define=col,ReducedChiSqr,StDev 2e-9 + StDevMean / sqr DegFree *" \
             "-define=col,ProbSmaller,DegFree 2 / ReducedChiSqr 2 / gamP" 
             } result] {
#        catch {exec rm $tmpFile}
        eval $statusCallback {"APSSRCheckHVCurrentCheck: $result"}
        return -code error "APSSRCheckHVCurrentCheck: $result"
    }
    if [string length $statusCallback] {
        eval $statusCallback {"Finished processing."}
    }
    catch {exec rm $tmpFile}
}

