Public
Snippet $269 authored by Kirill Smelkov

Example of 3D ZBigArray append (item is structure)

3dappend.py
#!/usr/bin/env python
# 3dappend.py - this program demonstrates using ZBigArray append with 3D arrays

from __future__ import print_function

from wendelin.bigarray.array_zodb import ZBigArray
from wendelin.lib.zodb import dbopen, dbclose
import transaction

import numpy as np
from math import cos


def main():
    # init db
    root = dbopen('test.fs')

    # every array element is this:
    elemv = [('tstart', '<i4'), ('v', '<f8')]
    
    # create Nx2x3 array; initially N=0
    A = ZBigArray((0, 2,3), dtype=elemv)
    root['A'] = A
    transaction.commit()

    # append 2D slices to A in a loop
    for i in range(4):
        # values inside 2D slice will be N + small
        # this way it is easy to track where a slice is in major dimensions from element value
        n = 100*i   

        # f(j,k) -> tstart(j), v(j,k)
        f = lambda j, k: (n+10*j+k, n + cos(j)*cos(k))

        s = np.array([[f(0,0), f(0,1), f(0,2)], [f(1,0), f(1,1), f(1,2)]], dtype=elemv)
        print('\n' + '-'*20)
        print('appending %s:' % (s.shape,))
        print(`s`)
        print()
    
        A.append(s[np.newaxis, ...])
        print('A %s:' % (A.shape,))
        print(`A[:]`)


if __name__ == '__main__':
    main()