[bd84570] | 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 |
---|
[ef727a6] | 30 | # THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND. NEITHER THE |
---|
[bd84570] | 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 | |
---|
[be75350] | 40 | from __future__ import print_function |
---|
[bd84570] | 41 | import numpy as np |
---|
| 42 | import ctypes as ct |
---|
[be75350] | 43 | import os,sys |
---|
[bd84570] | 44 | |
---|
| 45 | #Define where the library is and load it |
---|
[c13d973] | 46 | _path = os.path.dirname(__file__) |
---|
[45bb293] | 47 | libpygio = ct.CDLL(_path + '/../mpi/libpygio.so') |
---|
[bd84570] | 48 | #we need to define the return type ("restype") and |
---|
| 49 | #the argument types |
---|
| 50 | libpygio.get_elem_num.restype=ct.c_int64 |
---|
[7889dc4] | 51 | libpygio.get_elem_num.argtypes=[ct.c_int,ct.c_char_p] |
---|
[bd84570] | 52 | |
---|
| 53 | libpygio.get_variable_type.restype=ct.c_int |
---|
| 54 | libpygio.get_variable_type.argtypes=[ct.c_char_p,ct.c_char_p] |
---|
| 55 | |
---|
[9796d0a] | 56 | libpygio.get_variable_field_count.restype=ct.c_int |
---|
| 57 | libpygio.get_variable_field_count.argtypes=[ct.c_char_p,ct.c_char_p] |
---|
| 58 | |
---|
[dd56945] | 59 | libpygio.read_gio_uint16.restype=None |
---|
| 60 | libpygio.read_gio_uint16.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_uint16),ct.c_int] |
---|
| 61 | |
---|
[bd84570] | 62 | libpygio.read_gio_int32.restype=None |
---|
[9796d0a] | 63 | libpygio.read_gio_int32.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int),ct.c_int] |
---|
[bd84570] | 64 | |
---|
| 65 | libpygio.read_gio_int64.restype=None |
---|
[9796d0a] | 66 | libpygio.read_gio_int64.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_int64),ct.c_int] |
---|
[bd84570] | 67 | |
---|
| 68 | libpygio.read_gio_float.restype=None |
---|
[9796d0a] | 69 | libpygio.read_gio_float.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_float),ct.c_int] |
---|
[bd84570] | 70 | |
---|
| 71 | libpygio.read_gio_double.restype=None |
---|
[9796d0a] | 72 | libpygio.read_gio_double.argtypes=[ct.c_char_p,ct.c_char_p,ct.POINTER(ct.c_double),ct.c_int] |
---|
[bd84570] | 73 | |
---|
| 74 | libpygio.inspect_gio.restype=None |
---|
| 75 | libpygio.inspect_gio.argtypes=[ct.c_char_p] |
---|
| 76 | |
---|
[7889dc4] | 77 | def gio_read_(comm,file_name,var_name): |
---|
[be75350] | 78 | if sys.version_info[0] == 3: |
---|
| 79 | file_name = bytes(file_name,'ascii') |
---|
| 80 | var_name = bytes(var_name,'ascii') |
---|
[7889dc4] | 81 | var_size = libpygio.get_elem_num(comm,file_name) |
---|
| 82 | var_type = libpygio.get_variable_type(comm,file_name,var_name) |
---|
| 83 | field_count = libpygio.get_variable_field_count(comm,file_name,var_name) |
---|
[bd84570] | 84 | if(var_type==10): |
---|
[be75350] | 85 | print("Variable not found") |
---|
[bd84570] | 86 | return |
---|
| 87 | elif(var_type==9): |
---|
[dd56945] | 88 | print("variable type not known (not uint16/int32/int64/float/double)") |
---|
[bd84570] | 89 | elif(var_type==0): |
---|
| 90 | #float |
---|
[c8c291b] | 91 | result = np.ndarray((var_size),dtype=np.float32) |
---|
[7889dc4] | 92 | libpygio.read_gio_float(comm,file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_float)),field_count) |
---|
[bd84570] | 93 | return result |
---|
| 94 | elif(var_type==1): |
---|
| 95 | #double |
---|
[c8c291b] | 96 | result = np.ndarray((var_size),dtype=np.float64) |
---|
[7889dc4] | 97 | libpygio.read_gio_double(comm,file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_double)),field_count) |
---|
[bd84570] | 98 | return result |
---|
| 99 | elif(var_type==2): |
---|
| 100 | #int32 |
---|
[c8c291b] | 101 | result = np.ndarray((var_size),dtype=np.int32) |
---|
[7889dc4] | 102 | libpygio.read_gio_int32(comm,file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int32)),field_count) |
---|
[bd84570] | 103 | return result |
---|
| 104 | elif(var_type==3): |
---|
| 105 | #int64 |
---|
[c8c291b] | 106 | result = np.ndarray((var_size),dtype=np.int64) |
---|
[7889dc4] | 107 | libpygio.read_gio_int64(comm,file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_int64)),field_count) |
---|
[bd84570] | 108 | return result |
---|
[dd56945] | 109 | elif(var_type==4): |
---|
| 110 | #uint16 |
---|
| 111 | result = np.ndarray((var_size,field_count),dtype=np.uint16) |
---|
[7889dc4] | 112 | libpygio.read_gio_uint16(comm,file_name,var_name,result.ctypes.data_as(ct.POINTER(ct.c_uint16)),field_count) |
---|
[dd56945] | 113 | return result |
---|
[9796d0a] | 114 | |
---|
[7889dc4] | 115 | def gio_read(comm,file_name,var_names): |
---|
[c8c291b] | 116 | ret = [] |
---|
| 117 | if not isinstance(var_names,list): |
---|
| 118 | var_names = [ var_names ] |
---|
| 119 | for var_name in var_names: |
---|
[7889dc4] | 120 | ret.append( gio_read_(comm,file_name,var_name) ) |
---|
[c8c291b] | 121 | return np.array( ret ) |
---|
| 122 | |
---|
[7889dc4] | 123 | def gio_has_variable(comm,file_name,var_name): |
---|
[be75350] | 124 | if sys.version_info[0] == 3: |
---|
| 125 | file_name=bytes(file_name,'ascii') |
---|
| 126 | var_name=bytes(var_name,'ascii') |
---|
[7889dc4] | 127 | var_size = libpygio.get_elem_num(comm,file_name) |
---|
| 128 | var_type = libpygio.get_variable_type(comm,file_name,var_name) |
---|
[9796d0a] | 129 | return var_type!=10 |
---|
| 130 | |
---|
[7889dc4] | 131 | def gio_inspect(comm,file_name): |
---|
[be75350] | 132 | if sys.version_info[0] == 3: |
---|
| 133 | file_name=bytes(file_name,'ascii') |
---|
[bd84570] | 134 | libpygio.inspect_gio(file_name) |
---|
| 135 | |
---|
[33686f4] | 136 | read = gio_read |
---|
| 137 | has_variable = gio_has_variable |
---|
| 138 | inspect = gio_inspect |
---|
| 139 | |
---|