예제 #1
0
def print_data(cursor):
    type_class = coda.cursor_get_type_class(cursor)
    if type_class == coda.coda_array_class:
        num_elements = coda.cursor_get_num_elements(cursor)
        if num_elements > 0:
            print "[",
            coda.cursor_goto_first_array_element(cursor)

            for i in xrange(num_elements):
                print_data(cursor)
                if i < num_elements - 1:
                    print ", ",
                    coda.cursor_goto_next_array_element(cursor)

            print "]",
            coda.cursor_goto_parent(cursor)

    elif type_class == coda.coda_special_class:
        special_type = coda.cursor_get_special_type(cursor)
        if special_type == coda.coda_special_time:
            data = coda.cursor_read_double(cursor)
            time_string = coda.time_to_string(data)
            print "%s" % time_string,
        else:
            print "*** Unexpected special type (%s) ***", coda.type_get_special_type_name(special_type),
    else:
        read_type = coda.cursor_get_read_type(cursor)
        if read_type == coda.coda_native_type_int8 or \
           read_type == coda.coda_native_type_int16 or \
           read_type == coda.coda_native_type_int32:
            data = coda.cursor_read_int32(cursor)
            print "%ld" % data,
        elif read_type == coda.coda_native_type_uint8 or \
             read_type == coda.coda_native_type_uint16 or \
             read_type == coda.coda_native_type_uint32:
            data = coda.cursor_read_uint32(cursor)
            print "%lu" % data,
        elif read_type == coda.coda_native_type_int64:
            data = coda.cursor_read_int64(cursor)
            print "%ld" % data,
        elif read_type == coda.coda_native_type_uint64:
            data = coda.cursor_read_uint64(cursor)
            print "%lu" % data,
        elif read_type == coda.coda_native_type_float or \
             read_type == coda.coda_native_type_double:
            data = coda.cursor_read_double(cursor)
            print "%g" % data,
        elif read_type == coda.coda_native_type_char:
            data = coda.cursor_read_char(cursor)
            print "'%c'" % data,
        elif read_type == coda.coda_native_type_string:
            str = coda.cursor_read_string(cursor)
            print "\"%s\"" % str,
        else:
            print "*** Unexpected read type (%s) ***", coda.type_get_native_type_name(read_type),
예제 #2
0
파일: harpplot.py 프로젝트: stcorp/visan
 def attr_value(value, variable):
     if hasattr(variable, "unit"):
         if " since " in variable.unit:
             # this is a time value
             base, epoch = variable.unit.split(" since ")
             if base in ["s", "seconds", "days"]:
                 if base == "days":
                     value *= 86400
                 formats = "yyyy-MM-dd HH:mm:ss.SSSSSS|yyyy-MM-dd HH:mm:ss|yyyy-MM-dd"
                 value = value + coda.time_string_to_double(formats, epoch)
                 return coda.time_to_string(value)
         return "%s [%s]" % (str(value), variable.unit)
     return str(value)
예제 #3
0
def GetDataAsString(type, data, maxLength=-1):
    assert data is not None, "\"data\" argument should not be None."

    if type == TYPE_STRING or type == TYPE_CHAR:
        if maxLength >= 0 and len(data) > maxLength:
            return "<string of length %u>" % (len(data), )
        else:
            return "\"%s\"" % (data, )

    elif type == TYPE_TIME:
        try:
            return coda.time_to_string(data)
        except coda.CodacError:
            return "<invalid time value>"

    elif type == TYPE_BYTES:
        # convert length in bytes to length in characters ("0x%x " per byte).
        if maxLength >= 0 and (data.size * 5) > maxLength:
            return "<data block of %u byte(s)>" % (data.size, )
        elif data.size > 0:
            result = ""
            for byte in data[:-1]:
                result += "0x%x " % (byte, )
            result += "0x%x" % (data[-1], )
            return result
        else:
            return ""

    elif type == TYPE_RECORD:
        # return a key-value list of the fields between braces
        if len(data) == 0:
            return "<empty record>"
        result = "("
        for field in data._registeredFields:
            result += "%s=%s, " % (
                field,
                str(data.__dict__[field]),
            )
        result = result[:-2] + ")"
        if maxLength >= 0 and len(result) > maxLength:
            return "<record with %u field%s>" % (len(data),
                                                 "s" if len(data) != 1 else "")
        return result

    else:
        return str(data)