Skip to content

Instantly share code, notes, and snippets.

@mikofski
Last active May 19, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikofski/c52e1f0c05d1690440ea1a5820e0d292 to your computer and use it in GitHub Desktop.
Save mikofski/c52e1f0c05d1690440ea1a5820e0d292 to your computer and use it in GitHub Desktop.
IronPython Example of Writing ISO 8601 Strings to HDF5 from .NET
# Writing Fixed Length Strings to HDF5 file using HDF5DotNet
# http://hdf5.net/
# * download HDF5 library and install first
# * add shared objects to PATH
# set PATH=%PATH%;C:\Program Files\HDF Group\HDF5\1.8.9\bin
# * download and extract HDF5DotNet assemblies
# * start IronPython (ipy64.exe), must be 64-bit version!
# help links:
# http://stackoverflow.com/a/29565641/1020470
# https://support.hdfgroup.org/ftp/HDF5/examples/misc-examples/strarray.c
# http://hdf5.net/FAQ.aspx
# main snippet
import clr # import MS common language runtime (clr)
# add reference to HDF5DotNet.dll any way you can, EG: by file and path
clr.AddReferenceToFileAndPath('C:\\HDF5DotNet-Net4.0-x86_64\\HDF5DotNet.dll')
import HDF5DotNet
from HDF5DotNet import *
import System
from System import Array, Int64, Byte
LEN_DATETIME = 26 # fixed length of datetime strings
DATETIMES = ["2017-05-17T12:00:00.000000", "2017-05-17T13:00:00.000000"]
FILENAME = 'sample.h5'
def write_datetime_to_hdf5(iso8601_datetimes=DATETIMES, datetime_size=LEN_DATETIME, filename=FILENAME):
for dt in iso8601_datetimes:
if len(dt) > datetime_size:
msg = ('length %d of datetime "%s" must be smaller than string size %d'
% (len(dt), dt, datetime_size))
raise Exception(msg)
num_datetimes = len(iso8601_datetimes)
# start hdf5
# ----------
H5.Open()
# open hdf5 file
# --------------
h5file = H5F.create(filename, H5F.CreateMode.ACC_TRUNC)
# create dataspace
# ----------------
ndims = 1 # number of dimensions of data space
# dataspace dimensions must be Int64
dims = Array[Int64]((num_datetimes,)) # length of each dimension, EG: 2 ISO8601 date strings
dspace = H5S.create_simple(ndims, dims) # maxdims is dims
# setting maxdims:
# maxdims = Array[Int64]((2,)) # must be > dims, NULL is same as dim
# -1 is unlimited length, but then must use chunked data creation property list
# https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2011-May/004686.html
# dspace = H5S.create_simple(ndims, dims, maxdims)
# create datatype
# ---------------
dtype = H5T.copy(H5T.H5Type.C_S1)
H5T.setSize(dtype, datetime_size) # set the length of each string
# check size of fixed string:
# LOGGER.debug('string size: %d', H5T.getSize(dtype))
# 26
# create dataset and link to file
P_DEFAULT = H5PropertyListId(H5P.Template.DEFAULT)
dset = H5D.create(h5file, 'dates', dtype, dspace, P_DEFAULT, P_DEFAULT, P_DEFAULT)
# setting dataspace with unlimited dimensions
# if any dataspaces are unlimited, then must use chunked
# https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2011-May/004686.html
# see example in http://hdf5.net/
# plist = H5P.create(H5P.PropertyListClass.DATASET_CREATE)
# H5P.setChunk(plist, Array[Int64]((32, 64)))
# H5P.setDeflate(plist, 7)
# dset = H5D.create(h5file, 'dates', dtype, dspace, P_DEFAULT, plist, P_DEFAULT)
# create byte arrays from strings, must use ASCII encoding only
buf = Array.CreateInstance(Byte, num_datetimes, datetime_size)
for idx, dt in enumerate(iso8601_datetimes):
dt_buf = System.Text.Encoding.ASCII.GetBytes(dt)
for jdx in range(datetime_size):
buf[idx, jdx] = dt_buf[jdx]
# write buffer to HDF5 file, must be byte array
H5D.write(dset, dtype, H5Array[Byte](buf))
# close and clean up
H5F.close(h5file)
H5.Close()
if __name__ == "__main__":
write_datetime_to_hdf5()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment