genericio.py 4.9 KB
Newer Older
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
#                    Copyright (C) 2015, UChicago Argonne, LLC
#                               All Rights Reserved
# 
#                               Generic IO (ANL-15-066)
#                     Hal Finkel, Argonne National Laboratory
# 
#                              OPEN SOURCE LICENSE
# 
# Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne,
# LLC, the U.S. Government retains certain rights in this software.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#   1. Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
# 
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
# 
#   3. Neither the names of UChicago Argonne, LLC or the Department of Energy
#      nor the names of its contributors may be used to endorse or promote
#      products derived from this software without specific prior written
#      permission.
# 
# *****************************************************************************
# 
#                                  DISCLAIMER
30
# THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND.  NEITHER THE
31
32
33
34
35
36
37
38
39
# UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
# UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
# EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
# ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
# PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
# PRIVATELY OWNED RIGHTS.
# 
# *****************************************************************************

40
from __future__ import print_function
41
42
import numpy as np
import ctypes as ct
43
import os,sys
44
45

#Define where the library is and load it
46
47
_path = os.path.dirname(__file__)
libpygio = ct.CDLL(_path + '/../frontend/libpygio.so')
48
49
50
51
52
53
54
55
#we need to define the return type ("restype") and
#the argument types
libpygio.get_elem_num.restype=ct.c_int64
libpygio.get_elem_num.argtypes=[ct.c_char_p]

libpygio.get_variable_type.restype=ct.c_int
libpygio.get_variable_type.argtypes=[ct.c_char_p,ct.c_char_p]

56
57
58
libpygio.get_variable_field_count.restype=ct.c_int
libpygio.get_variable_field_count.argtypes=[ct.c_char_p,ct.c_char_p]

59
libpygio.read_gio_int32.restype=None
60
libpygio.read_gio_int32.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int),ct.c_int]
61
62

libpygio.read_gio_int64.restype=None
63
libpygio.read_gio_int64.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int64),ct.c_int]
64
65

libpygio.read_gio_float.restype=None
66
libpygio.read_gio_float.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_float),ct.c_int]
67
68

libpygio.read_gio_double.restype=None
69
libpygio.read_gio_double.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_double),ct.c_int]
70
71
72
73
74

libpygio.inspect_gio.restype=None
libpygio.inspect_gio.argtypes=[ct.c_char_p]

def gio_read(file_name,var_name):
75
76
77
    if sys.version_info[0] == 3:
        file_name = bytes(file_name,'ascii')
        var_name = bytes(var_name,'ascii')
78
79
    var_size = libpygio.get_elem_num(file_name)
    var_type = libpygio.get_variable_type(file_name,var_name)
80
    field_count = libpygio.get_variable_field_count(file_name,var_name)
81
    if(var_type==10):
82
        print("Variable not found")
83
84
        return
    elif(var_type==9):
85
        print("variable type not known (not int32/int64/float/double)")
86
87
    elif(var_type==0):
        #float
88
89
        result = np.ndarray((var_size,field_count),dtype=np.float32)
        libpygio.read_gio_float(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_float)),field_count)
90
91
92
        return result
    elif(var_type==1):
        #double
93
94
        result = np.ndarray((var_size,field_count),dtype=np.float64)
        libpygio.read_gio_double(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_double)),field_count)
95
96
97
        return result
    elif(var_type==2):
        #int32
98
99
        result = np.ndarray((var_size,field_count),dtype=np.int32)
        libpygio.read_gio_int32(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int32)),field_count)
100
101
102
        return result
    elif(var_type==3):
        #int64
103
104
        result = np.ndarray((var_size,field_count),dtype=np.int64)
        libpygio.read_gio_int64(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int64)),field_count)
105
        return result        
106
107

def gio_has_variable(file_name,var_name):
108
109
110
    if sys.version_info[0] == 3:
        file_name=bytes(file_name,'ascii')
        var_name=bytes(var_name,'ascii')
111
112
113
114
    var_size = libpygio.get_elem_num(file_name)
    var_type = libpygio.get_variable_type(file_name,var_name)
    return var_type!=10

115
def gio_inspect(file_name):
116
117
    if sys.version_info[0] == 3:
        file_name=bytes(file_name,'ascii')
118
119
    libpygio.inspect_gio(file_name)