File: table_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 (84 lines) | stat: -rw-r--r-- 2,656 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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

"""Table with time fields."""

import os
import time

import numpy as np
import tables


# Describe a particle record
class Particle(tables.IsDescription):
    """This class defines a table record.
    """

    lati        = tables.IntCol(pos=0)
#    longi       = IntCol(pos=1)
    Time        = tables.Time64Col(pos=2)
    pressure    = tables.FloatCol(pos=3)
    ID          = tables.StringCol(itemsize=10, pos=4)
    Int16       = tables.UIntCol(itemsize=4, pos=5)
    Int64       = tables.IntCol(itemsize=8, pos=6)
    Bool        = tables.BoolCol(pos=7)

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

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

# Get the HDF5 root group
root = h5file.root
group = h5file.create_group(root, "Particles")
filters = tables.Filters(complevel=1, complib='lzo', shuffle=1)
nrows = 6000
table = h5file.create_table("/Particles", "TParticle", Particle,
                          "Sample set of particles ", filters,
                          expectedrows = nrows)

# Number of rows in buffer
nrowsbuf = 1000
# Fill the table with 10**N particles
for i in np.arange(0, nrows, nrowsbuf, dtype=np.int64):
    if i+nrowsbuf > nrows:
        nrapp = nrows-i
    else:
        nrapp = nrowsbuf
    # First, assign the values to the Particle record
    Int64 = np.arange(i, i+nrapp)
    Time = Int64 + time.time()
    lati = Int64
    pressure = lati - 10.4
    ID = (i + Int64).astype('S10')
    Int16 = lati - 50
    Bool = lati % 3 > 1
    # This injects the Record values
    table.append([lati, Time, pressure, ID, Int16, Int64, Bool])

# Flush the table buffers and close the file
h5file.close()