Skip to content

Commit

Permalink
tweak import_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
meihuisu committed Feb 2, 2024
1 parent d68a8af commit 86985a7
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pycvm/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ def import_binary(self, fname, num_x, num_y):
if( k != -1) :
rawfile = rawfile[:k] + "_data.bin"
try :
fh = open(rawfile, 'r')
fh = open(rawfile, 'rb')
except:
print("ERROR: binary data does not exist.")
exit(1)
Expand All @@ -973,16 +973,22 @@ def import_binary(self, fname, num_x, num_y):
if len(floats) == 2 * (num_x * num_y) :
fh.seek(0,0)
floats = np.fromfile(fh, dtype=np.float)
fh.close()

## maybe it is a np array
if len(floats) != (num_x * num_y) :
ffh = open(rawfile, 'rb')
ffloats = np.load(ffh)
ffh.close()
floats=ffloats.reshape([num_x * num_y]);

print("TOTAL number of binary data read:"+str(len(floats))+"\n")

# sanity check,
if len(floats) != (num_x * num_y) :
print("import_binary(), wrong size !!!"+ str(len(floats))+ " expecting "+ str(num_x * num_y))
print("import_binary(), wrong size !!!"+ str(len(floats)) + " expecting "+ str(num_x * num_y))
exit(1)

fh.close()

if len(floats) == 1:
return floats[0]

Expand All @@ -991,7 +997,7 @@ def import_binary(self, fname, num_x, num_y):
# export np float array to an exernal file
#
def export_np_float_array(self, floats, fname):
# print("calling export_np_float_array")
# print("calling export_np_float_array -",len(floats))
rawfile = fname
if rawfile is None :
rawfile="data.bin"
Expand All @@ -1005,11 +1011,13 @@ def export_np_float_array(self, floats, fname):
exit(1)

np.save(fh, floats)
fh.close
fh.close()



# export raw floats nxy ndarray to an external file
def export_binary(self, floats, fname):
# print("calling export_binary")
print("calling export_binary -",len(floats))
rawfile = fname
if rawfile is None :
rawfile="data.bin"
Expand Down Expand Up @@ -1044,7 +1052,7 @@ def import_metadata(self, fname):

# export ascii meta data to an external file
def export_metadata(self,meta,fname):
# print("calling export_metadata")
print("calling export_metadata")
metafile=fname
if metafile is None:
metafile = "meta.json"
Expand Down Expand Up @@ -1075,7 +1083,7 @@ def import_matprops(self, fname):

# export material properties in JSON form to an external file
def export_matprops(self,blob,fname):
# print("calling export_matprops")
print("calling export_matprops")
matpropsfile=fname
if matpropsfile is None :
matpropsfile="matprops.json"
Expand Down

0 comments on commit 86985a7

Please sign in to comment.