File: array_timeseries.py

package info (click to toggle)
vitables 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,044 kB
  • sloc: python: 9,695; makefile: 214; sh: 90
file content (66 lines) | stat: -rw-r--r-- 2,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

#       Copyright (C) 2008-2024 Vicent Mas. All rights reserved
#
#       This program is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#       Author:  Vicent Mas - vmas@vitables.org

"""CArray with time fields.
"""

import os
import time

import tables

output_dir = '../timeseries'
try:
    os.mkdir(output_dir)
except OSError:
    pass

# Open a new empty HDF5 file
hdf5_name = "carray_ts.h5"
filepath_hdf5 = os.path.join(output_dir, hdf5_name)
h5file = tables.open_file(filepath_hdf5, mode="w",
title='Example CArray with time fields')

# Create a CArray and fill it
root = h5file.root
shape = (300, 2)
atom = tables.Time32Atom()
filters = tables.Filters(complevel=5, complib='zlib')
hdfarray = h5file.create_carray(root, 'test_carray_1', atom, shape,
    "Signed short array")
now = time.time()
seconds_by_day = 1*24*60*60
for index in range(0, 600, 2):
    seconds = now - seconds_by_day * index
    hdfarray[index/2, 0] = seconds
    hdfarray[299-index/2, 1] = seconds

# Create other CArray and fill it
shape = (300,)
atom = tables.Time32Atom(shape=(2,))
filters = tables.Filters(complevel=5, complib='zlib')
hdfarray = h5file.create_carray(root, 'test_carray_2', atom, shape,
    "Signed short array")
now = time.time()
seconds_by_day = 1*24*60*60
for index in range(0, 600, 2):
    seconds = now - seconds_by_day * index
    hdfarray[index/2] = [seconds, seconds]

h5file.close()