Skip to content

Instantly share code, notes, and snippets.

@swarbhanu
swarbhanu / hdf_hello_client_script.py
Created December 16, 2011 18:30
Example script file to be run from within pyon container (interactive shell). Running this script results in a client terminal sending a message bearing an hdf file to a server set up in another pyon shell. The client then waits for a reply. When the repl
import h5py
import numpy
# use the core driver of h5Py to write an hdf file
file = h5py.File('myfile_check.hdf5', mode = 'w', driver='core')
#file = h5py.File('myfile_check2.hdf5', mode = 'w', driver='log')
grp = file.create_group('myGroup')
dataset = grp.create_dataset('dataset', (10,10), '=f8', maxshape=(None,None))
file['dataset'] = numpy.ones((10,10))
file.flush()
@swarbhanu
swarbhanu / ASubB_facets.py
Created December 13, 2011 20:45
Subtracting two FacetFunctions
from dolfin import *
import numpy
mesh = UnitCube(4, 4, 4)
# Creating two mesh functions, empty to start with....
a = FacetFunction("double", mesh)
b = FacetFunction("double", mesh)
result = FacetFunction("double", mesh)
@swarbhanu
swarbhanu / ASubB_cells.py
Created December 13, 2011 20:44
Subtracting two CellFunctions
from dolfin import *
import numpy
mesh = UnitSquare(8, 8)
# Creating two mesh functions, empty to start with....
a = CellFunction("double", mesh)
b = CellFunction("double", mesh)
result = CellFunction("double", mesh)
@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)
@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 / 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 / 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 / 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 / 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 / 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')