Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
swarbhanu / VLCArrayHyperslab.py
Created November 23, 2011 20:57
An example of creating two differently shaped hyperslabs in a CArray and a VLArray using the same data contained in a numpy array
#################################################################################################
# #
# This program takes a numpy array and feeds it into a hyperslab in a CArray. The numpy array #
# is then changed to a different shape and fed into a hyperslab in a VLArray. The end result #
# is that one has two differently shaped hyperslabs of the same data. #
# #
#################################################################################################
import sys
import tables as tb
@swarbhanu
swarbhanu / arrayHDF.py
Created November 29, 2011 14:32
This program is for writing an ordinary array into an hdf file. The file is closed and then opened again and the read array is converted into a numpy array using the numpy.asarray() method. Slices and for loops are tested on the array.
import sys
import numpy
from numpy import *
from tables import *
filename = 'arrayEx.h5'
fileh=openFile(filename,mode="w")
root=fileh.root
@swarbhanu
swarbhanu / CArrayHDF.py
Created November 29, 2011 14:47
This program creates a CArray using PyTables. A numpy array is initialized and used to fill a hyperslab of a CArray. Slices of the array are then printed.
import sys
import numpy as np
import tables as tb
filename='carrayEx1.h5'
fileh=tb.openFile(filename, 'w')
carray = fileh.createCArray(fileh.root, 'carray', tb.UInt32Atom(), shape=(200,300))
@swarbhanu
swarbhanu / EArrayHDF.py
Created November 29, 2011 14:48
This program creates a EArray using PyTables. One dimension is extendable. One dimensional numpy arrays of the same length are appended as rows.
import tables as tb
import numpy as np
#initialize
filename='earrayEx.h5'
atom=tb.StringAtom(itemsize=8)
shape=(0,)
#open hdf5 file and create an earray
fileh=tb.openFile(filename,'w')
@swarbhanu
swarbhanu / h5PyBasic.py
Created November 29, 2011 14:54
Creating a dataset and writing an array into an hdf5 file using h5Py
import numpy as np
import h5py as hp
filename='h5py1.h5'
#open a file
f=hp.File(filename,driver='sec2',mode='w')
#create a dataset
mydset=f.create_dataset("mydset",(10,10),'=f8',maxshape=(None,None))
@swarbhanu
swarbhanu / addColumn_PyTables.py
Created November 29, 2011 14:56
Example showing how to add a column in PyTables
from tables import *
# Describe a water class
class Water(IsDescription):
waterbody_name = StringCol(16, pos=1) # 16-character String
lati = Int32Col(pos=2) # integer
longi = Int32Col(pos=3) # integer
airpressure = Float32Col(pos=4) # float (single-precision)
temperature = Float64Col(pos=5) # double (double-precision)
@swarbhanu
swarbhanu / timeNumpyArrayConversion.py
Created November 29, 2011 15:00
This program measures the time needed to read an array from an hdf file and save it in a numpy array using a slow asarray() method and a fast method with the dtype provided. Run the program as $ python timeNumpyArrayConversion.py <tempfilename.h5> 100 100
#################################################################################
# #
# This program measures the time needed to read an array from an hdf file and #
# save it in a numpy array using a slow asarray() method and a fast method with #
# the dtype explicitly provided before construction. The fast method is to be #
# preferred for converting arrays read from hdf files using PyTables to Numpy #
# arrays. #
# #
# This file is meant to be run as in the following example: #
# #
@swarbhanu
swarbhanu / vlarrayExample.py
Created November 29, 2011 15:43
This program creates a VLArray using PyTables. One dimensional Numpy arrays of varying length are appended as rows.
##################################################################################
# #
# This program creates a VLArray using PyTables. One dimensional numpy arrays of #
# varying length are appended as rows # # #
# #
###################################################################################
import tables as tb
import numpy as np
@swarbhanu
swarbhanu / basicAddMeshVertices_evalFunc.py
Created December 13, 2011 20:40
Adding mesh vertices and evaluating functions
from dolfin import *
from numpy import *
#initialize an empty mesh
mesh1 = Mesh()
#initialize an empty mesh editor
editor1 = MeshEditor()
#################################################################################
@swarbhanu
swarbhanu / plottingMeshes_CellFunction.py
Created December 13, 2011 20:43
Localized mesh refinement using a CellFunction
from dolfin import *
import numpy
mesh = UnitSquare(8,8)
mesh = refine(mesh)
# editor = MeshEditor()
# editor.open(mesh,2,2)
# editor.add_cell(0,0,1,2)