#############################################################################
# CDMS: Interface to CDMS-simulator
#   (c) 1997/2000: Rolf Schrödter, DLR, Institut für Planetenerkundung
#############################################################################
# $Id: BrowseRolisData.tcl,v 1.3 2006-11-17 16:34:45+01 rolf Exp rolf $
#############################################################################
# BrowseRolis.tcl :    Rolis data callbacks
#############################################################################

namespace eval browse::rolis {      ;# create empty namespace if not exists
    variable DataType
    variable RawDataSubtype

    array set DataType {
        0   AsciiText
        1   RawImgSkip
        2   RawImgMacro
        3   RolisISB
        4   DarkRef
        8   CompressedImg1
        14  CodePageDump
        15  Test
    }
    array set RawDataSubtype {
        0   "<continued frame>"
        1   "<first frame>"
        2   "<last frame>"
        3   "<single frame>"
    }
}

#####################################################
# Swap low,hi bytes
#####################################################
proc browse::rolis::swap { data } {
    binary scan $data s* intList    ;# read 16-bit little endian
    binary format S* $intList       ;# format 16-bit big-endian
}

#####################################################
# AsciiText frames ComDPU+Rolis
#####################################################
proc browse::rolis::ascii { data } {
    set data [swap $data]                   ;# Swap lo<-->hi
    binary scan $data A* str                ;# remove trailing BLANKS and NULLS
    set str [string trim $str]              ;# remove leading and trailing whitespaces (CR,LF)
    return "--- $str ---"
}
proc browse::rolis::tryAscii { data } {
    binary scan $data s1s1a* type count bin ;# ROLIS frame type
    if { ($type == 0x436F) && ($count == 0x6D44) } {
        # It's a ComDPU message
        return "<ComDPU>\n[ascii $data]"
    } elseif { ($type == 0x5000) && ($count != 0x5001) } {
        # Is it a ROLIS TEXT frame, but not a ComDPU Test-frame
        return [ascii $bin]
    } else {
        return "<ComDPU ?>"
    }
}

#####################################################
# Formatted science data listing for text output
#####################################################
proc browse::rolis::list_data { data {fmt 0} } {
    variable DataType
    variable RawDataSubtype

    binary scan $data @0s1s1 x tcount
    browse::unsigned x tcount

    set ident   [expr {($x & 0xF000) >> 12}]
    set type    [expr {($x & 0x0F00) >> 8}]
    set subtype [expr {($x & 0x00FF)}]

    switch -- $ident {
        5       {   ;# continue with ROLIS }
        12      { return "<CIVA>" }
        default { return [tryAscii $data] }
    }

    if { [info exists DataType($type)] } {
        set result [format "type=%-15s tcount=%d " $DataType($type) $tcount]
    } else {
        set result [format "type=%-15s tcount=%d " "<unknown>" $tcount]
        return $result
    }

    switch -- $type {
        0 {
            set str [tryAscii $data]
            if {[string first \n $str] >= 0} {  ;# multiple line message
                append result \n$str
            } else {                            ;# single line listing
                append result \n$str
            }
        }
        1   -
        2   -
        14 {
            set subtype [expr {$subtype & 0x03}]
            append result [format "%s " $RawDataSubtype($subtype)]
            if { $type == 14 } {
                binary scan $data @4s1 adr
                append result [format "adr(hex)=%04X" $adr]
            } else {
                switch -- $subtype {
                    1   -
                    3 {
                        binary scan $data @4s1s1s1s1s1s1s1 img mask y x ny nx incr
                        browse::unsigned img mask y x ny nx incr
                        append result [format "img=%d mask=%04X y=%d x=%d ny=%d nx=%d incr=%d" $img $mask $y $x $ny $nx $incr]
                    }
                }
            }
        }
        3 {
            # For ISB's we need big-endian
            # because there are a few BYTE parameters
            #
            set data [swap $data]
            array set isb [::browse::isb::interpret $data]

            append result [format "\nID-Magic='%s'  BufA=%d  BufB=%d" $isb(Id) $isb(BufA) $isb(BufB)]
            for {set i 0} {$i < 8} {incr i} {
                array set isbX $isb($i)

                append result "\n" [string repeat "-" 80]
                append result [format "\nBufNo=%d  Flags=%s" $isbX(BufNo) $isbX(Flags)]

                # Display more ISB parameters only
                # if image is exposed
                #
                set exposed [browse::isb::isExposed $isbX(Flags)]
                if { $exposed } {

                    # All isb(*) values are formatted physical values
                    #
                    append result [format "\nRTime=%s  LOBT=%s  ExpTime=%s" $isbX(RTime) $isbX(LOBT) $isbX(ExpTime) $isbX(LED) $isbX(IFL-Pos) $isbX(ExpTime)]
                    append result [format "\nLED=%s  IFL-Pos=%s  Avrg=%d  DMA-Err=%d  Filter=%d  ErrCode=%d" $isbX(LED) $isbX(IFL-Pos) $isbX(Avrg) $isbX(DMA-Err) $isbX(Filter) $isbX(ErrCode)]
                    append result [format "\nCH-Temp1=%s  CH-Temp2=%s" $isbX(CH-Temp1) $isbX(CH-Temp2)]
                }
            }
        }
        4 {
            binary scan $data @4s1s1 row exp
            browse::unsigned row exp
            append result [format "row=%d exp=%d " $row $exp]
        }
        8 {
            set rate    [expr {$subtype >> 4}]
            set fcount  [expr {$subtype & 0x0F}]
            append result [format "rate=%d fcount=%d " $rate $fcount]
            if { $fcount == 0 } {
                binary scan $data @4s1 x
                browse::unsigned x
                set tile    [expr {($x & 0xFF00) >> 8}]
                set img     [expr {($x & 0x00E0) >> 5}]
                set msb     [expr {($x & 0x001F)}]
                append result [format "tile=%d img=%d msb=%d " $tile $img $msb]
            }
        }
        15 {
        }
    }

    return $result
}

#####################################################
# Formatted science data listing for windowed output
#####################################################

proc browse::rolis::ascii_data { frame } {
    browse::rolis::list_data $frame
}
