source: python/gio.py @ ef727a6

Revision ef727a6, 4.1 KB checked in by Hal Finkel <hfinkel@…>, 8 years ago (diff)

Some python cleanups and an example

  • Property mode set to 100644
Line 
1#                    Copyright (C) 2015, UChicago Argonne, LLC
2#                               All Rights Reserved
3#
4#                               Generic IO (ANL-15-066)
5#                     Hal Finkel, Argonne National Laboratory
6#
7#                              OPEN SOURCE LICENSE
8#
9# Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne,
10# LLC, the U.S. Government retains certain rights in this software.
11#
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions are met:
14#
15#   1. Redistributions of source code must retain the above copyright notice,
16#      this list of conditions and the following disclaimer.
17#
18#   2. Redistributions in binary form must reproduce the above copyright
19#      notice, this list of conditions and the following disclaimer in the
20#      documentation and/or other materials provided with the distribution.
21#
22#   3. Neither the names of UChicago Argonne, LLC or the Department of Energy
23#      nor the names of its contributors may be used to endorse or promote
24#      products derived from this software without specific prior written
25#      permission.
26#
27# *****************************************************************************
28#
29#                                  DISCLAIMER
30# THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND.  NEITHER THE
31# UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
32# UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
33# EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
34# ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
35# PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
36# PRIVATELY OWNED RIGHTS.
37#
38# *****************************************************************************
39
40import numpy as np
41import ctypes as ct
42import os
43
44#Define where the library is and load it
45_path = os.path.dirname('__file__')
46libpygio = ct.CDLL(os.path.abspath('../frontend/libpygio.so'))
47#we need to define the return type ("restype") and
48#the argument types
49libpygio.get_elem_num.restype=ct.c_int64
50libpygio.get_elem_num.argtypes=[ct.c_char_p]
51
52libpygio.get_variable_type.restype=ct.c_int
53libpygio.get_variable_type.argtypes=[ct.c_char_p,ct.c_char_p]
54
55libpygio.read_gio_int32.restype=None
56libpygio.read_gio_int32.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int)]
57
58libpygio.read_gio_int64.restype=None
59libpygio.read_gio_int64.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int64)]
60
61libpygio.read_gio_float.restype=None
62libpygio.read_gio_float.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_float)]
63
64libpygio.read_gio_double.restype=None
65libpygio.read_gio_double.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_double)]
66
67libpygio.inspect_gio.restype=None
68libpygio.inspect_gio.argtypes=[ct.c_char_p]
69
70def gio_read(file_name,var_name):
71    var_size = libpygio.get_elem_num(file_name)
72    var_type = libpygio.get_variable_type(file_name,var_name)
73    if(var_type==10):
74        print "Variable not found"
75        return
76    elif(var_type==9):
77        print "variable type not known (not int32/int64/float/double)"
78    elif(var_type==0):
79        #float
80        result = np.ndarray(var_size,dtype=np.float32)
81        libpygio.read_gio_float(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_float)))
82        return result
83    elif(var_type==1):
84        #double
85        result = np.ndarray(var_size,dtype=np.float64)
86        libpygio.read_gio_double(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_double)))
87        return result
88    elif(var_type==2):
89        #int32
90        result = np.ndarray(var_size,dtype=np.int32)
91        libpygio.read_gio_int32(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int32)))
92        return result
93    elif(var_type==3):
94        #int64
95        result = np.ndarray(var_size,dtype=np.int64)
96        libpygio.read_gio_int64(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int64)))
97        return result       
98       
99def gio_inspect(file_name):
100    libpygio.inspect_gio(file_name)
101
Note: See TracBrowser for help on using the repository browser.