pack

From DavinciWiki
Jump to: navigation, search


64-bit SafeYes

Description

Packs binary data from a davinci struct

Arguments and Return Values

Arguments: A davinci struct and a filename

Return Value: The template string

Usage

Syntax: pack(struct=STRUCT, filename=STRING [, count=INT32, col_names=TEXT, force=BOOL, template=STRING, skip=INT32])

The pack function reads binary data from the davinci struct, interprets it according to the specified record template, and writes it to the file named by filename. The optional count parameter specifies the number of records to be packed (a negative value means all records). The optional col_names parameter can be used to specify the structure fields to pack and the order in which to do so. Thus it can be either a string (for a single column) or a TEXT object. Providing extra names or names not in the structure is an error. Providing fewer is fine.

The option force argument says whether to overwrite an existing file.

By default, if no template is provided davinci will pack the data as the types of the actual fields. There's really no reason not to do that and specifying template strings is painful and tedious.

Whether you provide a template string or not, the template string needed to unpack() the data is the return value. If you really want to specify the template string yourself, it has the same structure as unpack() except it does not support 3,5,6 or 7 byte int types, only 1,2,4,8, ie the native integer sizes.

The optional skip parameter specifies the bytes into the file to skip before encoding to the file.

pack() will pack structures with members with differing y lengths and will just pad the shorter columns with 0's so the file will be the size in bytes of the first row times the number of rows (y length) of the longest column.

Examples

Note in the example below I have to use col_names for unpack() because the hdf writing (or reading?) reorders the members and pack() just packs them in the index order. Letting unpack do it's default and assign names sequentially would mean atm and atm_in would compare unequal.

dv> atm = load("atm.hdf");
struct, 11 elements
    c1: 1x34288x1 array of uint32, bsq format [137,152 bytes]
    c10: 1x34288x1 array of float, bsq format [137,152 bytes]
    c11: Text Buffer with 34288 lines of text
        1: 3.06
        2: 3.06
        3: 3.06
        4: 3.06
        5: 3.06
        6: 3.06
        7: 3.06
        8: 3.06
        9: 3.06
        10: 3.06
    c2: 1x34288x1 array of uint16, bsq format [68,576 bytes]
    c3: 38x34288x1 array of uint16, bsq format [2,605,888 bytes]
    c4: 1x34288x1 array of uint16, bsq format [68,576 bytes]
    c5: 1x34288x1 array of uint16, bsq format [68,576 bytes]
    c6: 1x34288x1 array of float, bsq format [137,152 bytes]
    c7: 9x34288x1 array of int16, bsq format [617,184 bytes]
    c8: 1x34288x1 array of float, bsq format [137,152 bytes]
    c9: 1x34288x1 array of float, bsq format [137,152 bytes]
dv> template = pack(atm, "atm_packed.dat")
Packed 34288 records to to atm_packed.dat.
"u4r4a4u2u2*38u2u2r4i2*9r4r4"
dv> atm_in = unpack(template, "atm_packed.dat", col_names=get_struct_keys(atm))
struct, 11 elements
    c1: 1x34288x1 array of uint32, bsq format [137,152 bytes]
    c10: 1x34288x1 array of float, bsq format [137,152 bytes]
    c11: Text Buffer with 34288 lines of text
        1: 3.06
        2: 3.06
        3: 3.06
        4: 3.06
        5: 3.06
        6: 3.06
        7: 3.06
        8: 3.06
        9: 3.06
        10: 3.06
    c2: 1x34288x1 array of uint16, bsq format [68,576 bytes]
    c3: 38x34288x1 array of uint16, bsq format [2,605,888 bytes]
    c4: 1x34288x1 array of uint16, bsq format [68,576 bytes]
    c5: 1x34288x1 array of uint16, bsq format [68,576 bytes]
    c6: 1x34288x1 array of float, bsq format [137,152 bytes]
    c7: 9x34288x1 array of int16, bsq format [617,184 bytes]
    c8: 1x34288x1 array of float, bsq format [137,152 bytes]
    c9: 1x34288x1 array of float, bsq format [137,152 bytes]
dv> equals(atm, atm_in)
1

DavinciWiki Mini-Nav Bar

Contents


Contact Developers

  • davinci-dev [AT] mars.asu.edu

All other topics

  • See navigation on the left

Related Functions

Recent Core Changes

Modified On: 11-17-2016

Personal tools