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
|
#!/usr/bin/env python3
# Copyright (C) 2005-2007 Carabos Coop. V. All rights reserved
# 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
"""This program creates a potentially big table for tests purposes.
The program is based on the tutorial programs included in the PyTables
User's Guide.
Modified to use buffers so that filling times are bettered by one order of
magnitude. F. Altet 2006-02-27
"""
import time
import tables
from numpy import arange
t0 = time.time()
# Describe a particle record
class Particle(tables.IsDescription):
"""Description of a table record."""
lati = tables.IntCol(pos=0)
longi = tables.IntCol(pos=1)
temperature = tables.FloatCol(pos=2)
pressure = tables.FloatCol(pos=3)
# Open a file in "w"rite mode
fileh = tables.open_file("large_table.h5", mode = "w")
# Get the HDF5 root group
root = fileh.root
# Create a group
group = fileh.create_group(root, "Particles")
# Now, create and fill the tables in Particles group
filters = tables.Filters(complevel=1, complib='lzo', shuffle=1)
nrows = 10**7
table = fileh.create_table("/Particles", "TParticle", Particle,
"Sample set of particles ", filters, expectedrows = nrows)
# Number of rows in buffer
nrowsbuf = 10000
# Fill the table with 10**N particles
for i in range(0, nrows, nrowsbuf):
if i + nrowsbuf > nrows:
nrapp = nrows - i
else:
nrapp = nrowsbuf
# First, assign the values to the Particle record
lati = arange(i, i + nrapp)
longi = 10 - lati
temperature = lati + 0.5
pressure = 10.5 - lati
# This injects the Record values
table.append([lati, longi, temperature, pressure])
# Flush the table buffers and close the file
fileh.close()
tsec = round((time.time() - t0), 3)
print(f'Last of the run: {tsec} s')
|