__init__.py 2.86 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
#TODO: use the rewrite to swap depending whether mpi is there are not..

import numpy

gsell's avatar
gsell committed
5 6
import H5hut as H5hut_rewrite
from H5hut import *
gsell's avatar
gsell committed
7 8 9 10


__h5hut_table__ = {}

gsell's avatar
gsell committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#
# Rewrite functions for reading and writing datasets and attributes for
# the types
# - Int32
# - Int64
# - Float32
# - Float64
#
# These functions have either three or four arguments:
# - functions to read or write a dataset have three arguments
# - functions to read an attribute have three arguments
# - functions to write an attribute have four attributes (except the functions
#   to write a string attribute, which are not handled here)
#
# The third arguments is an array in all cases.
#
gsell's avatar
gsell committed
27 28
funcx = """
def {0}(*args, **kwargs): 
gsell's avatar
gsell committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    import numpy
    if not len(args) == 3:
        print 'wrong number of arguments'
        return -2

    if type (args[2]) is numpy.ndarray:
        dtype = args[2].dtype
        size = args[2].size
    elif isinstance (args[2], (numpy.str)):
        dtype = numpy.str
        size = len (args[2])
    elif isinstance (args[2], (list, tuple)):
        dtype = numpy.dtype (type(args[2][0]))
        size = len (args[2])
    else:
        print 'third argument must be an array'
        return -2

    if 'Attrib' in '{0}' and 'Write' in '{0}' and not dtype == type(''):
        return __h5hut_table__['{0}'][dtype] (args[0], args[1], args[2], size, **kwargs)
gsell's avatar
gsell committed
49
    else:
gsell's avatar
gsell committed
50
        return __h5hut_table__['{0}'][dtype](*args, **kwargs)
gsell's avatar
gsell committed
51 52 53 54 55 56 57 58 59
"""

def __update_types__():
    import numpy
    import sys
    import inspect

    __h5hut_api__ = dir(H5hut_rewrite)

gsell's avatar
gsell committed
60
    __h5hut_types__ = [ "Int32", "Int64", "Float32", "Float64", "String" ]
gsell's avatar
gsell committed
61 62 63
    __numpy_types__ = [ numpy.dtype(numpy.int32), 
                        numpy.dtype(numpy.int64), 
                        numpy.dtype(numpy.float32), 
gsell's avatar
gsell committed
64 65
                        numpy.dtype(numpy.float64),
                        numpy.str]
gsell's avatar
gsell committed
66

gsell's avatar
gsell committed
67 68 69 70
    #
    # loop over all H5hut C-functions and above types.
    # Replace function if type is in name.
    #
gsell's avatar
gsell committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    for __i__ in __h5hut_api__:
        for __j__ in __h5hut_types__:
            if __j__ in __i__:
                key = __i__[:-len(__j__)]

                numpy_key = __numpy_types__[__h5hut_types__.index(__j__)]
                func = H5hut_rewrite.__dict__[__i__]

                if key not in __h5hut_table__:
                    __h5hut_table__[key] = {}

                __h5hut_table__[key][numpy_key] = func

                del H5hut_rewrite.__dict__[__i__]
                del sys.modules["H5hut"].__dict__[__i__]

    for __new_func__ in __h5hut_table__.keys():
        types = __h5hut_table__[__new_func__]
        #args = inspect.getargspec(types[0])
        #print __new_func__
        result = funcx.format(__new_func__) 
        exec(result)

        #H5hut_rewrite.__dict__[__new_func__] = globals()[__new_func__]
        sys.modules["H5hut"].__dict__[__new_func__] = locals()[__new_func__]

__update_types__()