source: python/genericio.py @ be75350

Revision be75350, 4.9 KB checked in by Thomas Uram <turam@…>, 6 years ago (diff)

Make genericio.py Python2/3-agnostic

  • 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
40from __future__ import print_function
41import numpy as np
42import ctypes as ct
43import os,sys
44
45#Define where the library is and load it
46_path = os.path.dirname(__file__)
47libpygio = ct.CDLL(_path + '/../frontend/libpygio.so')
48#we need to define the return type ("restype") and
49#the argument types
50libpygio.get_elem_num.restype=ct.c_int64
51libpygio.get_elem_num.argtypes=[ct.c_char_p]
52
53libpygio.get_variable_type.restype=ct.c_int
54libpygio.get_variable_type.argtypes=[ct.c_char_p,ct.c_char_p]
55
56libpygio.get_variable_field_count.restype=ct.c_int
57libpygio.get_variable_field_count.argtypes=[ct.c_char_p,ct.c_char_p]
58
59libpygio.read_gio_int32.restype=None
60libpygio.read_gio_int32.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int),ct.c_int]
61
62libpygio.read_gio_int64.restype=None
63libpygio.read_gio_int64.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int64),ct.c_int]
64
65libpygio.read_gio_float.restype=None
66libpygio.read_gio_float.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_float),ct.c_int]
67
68libpygio.read_gio_double.restype=None
69libpygio.read_gio_double.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_double),ct.c_int]
70
71libpygio.inspect_gio.restype=None
72libpygio.inspect_gio.argtypes=[ct.c_char_p]
73
74def gio_read(file_name,var_name):
75    if sys.version_info[0] == 3:
76        file_name = bytes(file_name,'ascii')
77        var_name = bytes(var_name,'ascii')
78    var_size = libpygio.get_elem_num(file_name)
79    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        return
84    elif(var_type==9):
85        print("variable type not known (not int32/int64/float/double)")
86    elif(var_type==0):
87        #float
88        result = np.ndarray((var_size,field_count),dtype=np.float32)
89        libpygio.read_gio_float(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_float)),field_count)
90        return result
91    elif(var_type==1):
92        #double
93        result = np.ndarray((var_size,field_count),dtype=np.float64)
94        libpygio.read_gio_double(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_double)),field_count)
95        return result
96    elif(var_type==2):
97        #int32
98        result = np.ndarray((var_size,field_count),dtype=np.int32)
99        libpygio.read_gio_int32(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int32)),field_count)
100        return result
101    elif(var_type==3):
102        #int64
103        result = np.ndarray((var_size,field_count),dtype=np.int64)
104        libpygio.read_gio_int64(file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int64)),field_count)
105        return result       
106
107def gio_has_variable(file_name,var_name):
108    if sys.version_info[0] == 3:
109        file_name=bytes(file_name,'ascii')
110        var_name=bytes(var_name,'ascii')
111    var_size = libpygio.get_elem_num(file_name)
112    var_type = libpygio.get_variable_type(file_name,var_name)
113    return var_type!=10
114
115def gio_inspect(file_name):
116    if sys.version_info[0] == 3:
117        file_name=bytes(file_name,'ascii')
118    libpygio.inspect_gio(file_name)
119
Note: See TracBrowser for help on using the repository browser.