
proc ProcessVLDData {args} {
    set group ""
    set output ""
    set configFile ""
    set ROISizex 150
    set ROISizey 150
    set ROIx0 50
    set ROIx1 718
    set ROIy0 0
    set ROIy1 479
    set blankOutx0 -1
    set blankOutx1 -1
    set blankOuty0 -1
    set blankOuty1 -1
    set sizeLinesx 3
    set sizeLinesy 3
    set statusCallback APSNoOp
    set removeHDF 0
    set backgroundHalfWidth 1
    set haloRemoval 0
    set symmetricBackgroundRemoval 0
    set lonerRemoval 0
    set spotImages ""
    APSStrictParseArguments {group output statusCallback configFile \
                               ROISizex ROISizey ROIx0 ROIx1 ROIy0 ROIy1 \
                               blankOutx0 blankOutx1 blankOuty0 blankOuty1 \
                               sizeLinesx sizeLinesy removeHDF backgroundHalfWidth haloRemoval \
                               symmetricBackgroundRemoval lonerRemoval \
                               spotImages}
    if 0 {
        foreach item {group output statusCallback configFile \
                        ROISizex ROISizey ROIx0 ROIx1 ROIy0 ROIy1 \
                        blankOutx0 blankOutx1 blankOuty0 blankOuty1 \
                        sizeLinesx sizeLinesy removeHDF backgroundHalfWidth haloRemoval \
                        symmetricBackgroundRemoval lonerRemoval \
                        spotImages} {
            puts stdout "$item = [set $item]"
        }
        flush stdout
    }

    if [string length $configFile] {
        if ![file exists $configFile] {
            return -code error "Not found: $configFile"
        }
        if [catch {exec sdds2stream -column=Rootname $configFile} rootnameList] {
            return -code error "Problem with configuration file: $rootnameList"
        }
    } else {
        return -code error "No configuration file for group $group"
    }
    eval $statusCallback {"Processing [llength $rootnameList] files"}
    eval $statusCallback {"Checking for HDF and SDDS files."}
    set fileList ""
    foreach rootname $rootnameList {
        set hdfFile $rootname.hdf
        set sddsFile $rootname.sdds.gz
        if [file exists $hdfFile] {
            set mainDirectory [file dirname $rootname]
            set hdfName [file tail $hdfFile]
            if ![file exists $sddsFile] {
                eval $statusCallback {"Converting $hdfFile to SDDS."}
                if [catch {exec hdf2sdds $mainDirectory/ $hdfName $sddsFile -binary -withIndex} result] {
                    return -code error "Conversion error for $file: $result"
                }
                if $removeHDF {
                    file delete -force $hdfFile
                }
            }
        } else {
            if ![file exists $sddsFile] {
                return -code error "Not match: $rootname"
            }
        }
        lappend fileList $sddsFile
    }
    if ![llength $fileList] {
        return -code error "No SDDS files created/found for given file list."
    }
    eval $statusCallback {"Combining and processing SDDS files."}
    set ROIList ""
    if $ROIx0!=$ROIx1 {
        lappend ROIList x0v=$ROIx0
        lappend ROIList x1v=$ROIx1
    }
    if $ROIy0!=$ROIy1 {
        lappend ROIList y0v=$ROIy0
        lappend ROIList y1v=$ROIy1
    }
    if [llength $ROIList] {
        set ROIArg -roi=[join $ROIList ,]
    } else {
        set ROIArg ""
    }
    set doBlankOut 4
    foreach type {x0 x1 y0 y1} {
        if [set blankOut$type]==-1 {
            incr doBlankOut -1
        }
    }
    if {$doBlankOut==4} {
        set blankOutArg -blankOut=x0v=$blankOutx0,x1v=$blankOutx1,y0v=$blankOuty0,y1v=$blankOuty1
    } elseif {$doBlankOut} {
        return -code error "blank out values are unacceptable."
    } else {
        set blankOutArg ""
    }
    set backGroundOpt -background=halfwidth=$backgroundHalfWidth
    if $haloRemoval {
        append backGroundOpt ,antihalo
    }
    if $symmetricBackgroundRemoval {
        append backGroundOpt ,symmetric
    }
    if $lonerRemoval {
        append backGroundOpt ,antiloner
    }
    set spotImageArg ""
    if [string length $spotImages] {
        set spotImageArg -spotImage=$spotImages
    }
    if [catch {eval exec sddscombine $fileList -pipe=out \
                 | sddsspotanalysis -pipe \
                 -despike -image=*:data* $blankOutArg \
                 $ROIArg -spotROISize=xv=$ROISizex,yv=$ROISizey \
                 -sizeLines=xv=$sizeLinesx,yv=$sizeLinesy \
                 $backGroundOpt $spotImageArg \
                 | sddsprocess -pipe=in $output \
                 {"-edit=column,FileIndexString,Filename,20Z/Z_%/.sdds.gz//"} \
                 {"-scan=column,FileIndex,FileIndexString,%ld,type=long"} } result] {
        return -code error "$result"
    }
    eval $statusCallback {"Done."}
}

