#############################################################################
# CDMS: Interface to CDMS-simulator
#   (c) 1997/2000: Rolf Schrödter, DLR, Institut für Planetenerkundung
#############################################################################
# $Id: BrowseExtract.tcl,v 1.1 2006-01-17 14:27:54+01 rolf Exp rolf $
#############################################################################
# BrowseExtract.tcl : Abstract frame extractor
#############################################################################

proc browse::extract { args } {
    variable BROWSE

    if { [llength $args] == 0 } {
        errorMsg "Extract: valid options are:
    -file filename  - filename to extract
    -from num       - extract from record number (def=0)
    -to num         - extract to record number (def=end)
    -out filename   - name of output file (def=stdout)
    -format fmt     - output format raw|cdms (default=cdms)
    -pattern pat    - glob-style pattern which frames to be extracted,
                      e.g. *ROLIS*DATA*type=Compressed*"
    }

    # default parameters
    set ARG(From)       0
    set ARG(To)         [expr {0x7FFFFFFF}]
    set ARG(File)       {}
    set ARG(Out)        {}
    set ARG(Format)     cdms
    set ARG(Pattern)    "*"

    # parse command parameters
    foreach {opt val} $args {
        set opt [string tolower $opt]
        switch -glob -- $opt {
            -from       { set ARG(From)     $val }
            -to         { set ARG(To)       $val }
            -file       { set ARG(File)     $val }
            -out        { set ARG(Out)      $val }
            -format     { set ARG(Format)   $val }
            -pattern    { set ARG(Pattern)  $val}
            default { errorMsg "Extract: invalid option $opt" }
        }
    }
    if { [string length $ARG(File)] } {
        set fin [browse::open_infile $ARG(File)]
    } else {
        errorMsg "Extract: no data file specified (-file filename)"
    }
    if { [string length $ARG(Out)] == 0 } {
        set fout stdout
        browse::config_stdfile $fout
    } else {
        set fout [browse::open_outfile $ARG(Out)]
    }
    browse::detect_frame_size $fin

    seek $fin [expr {$ARG(From)*$BROWSE(Frame.Size)}]
    set BROWSE(FrameNo) $ARG(From)

    while { 1 } {
        set frame [read $fin $BROWSE(Frame.Size)]
        if { [string length $frame] != $BROWSE(Frame.Size) } {
            break
        }
        if { [catch {browse::cdms_frame $frame X} err ] } {
            puts stderr [format "*** %s ***" $err]
        }
        set str [browse::format_cdms X]
        append str " " [browse::format_data X]
        if { [string match -nocase $ARG(Pattern) $str] } {
            if { $X(ChksumErr) } {
                puts stderr "Extract: Checksum error in frame #$BROWSE(FrameNo)"
            } else {
                switch -- [string tolower $ARG(Format)] {
                    raw     { puts -nonewline $fout $X(Data)  ;# 128 HK/Science data words }
                    cdms    { puts -nonewline $fout $frame    ;# complete frame incl. CDMS header }
                    default { errorMsg "Extract: Invalid -format specified: $ARG(Format)" }

                }
            }
        }
        incr BROWSE(FrameNo)
        if { $BROWSE(FrameNo) > $ARG(To) } {
            break
        }
    }
    close $fin

}
