Skip to content

Instantly share code, notes, and snippets.

@j-jith
Last active July 18, 2016 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-jith/b5ce75f6152fabac4f3a9447679067f8 to your computer and use it in GitHub Desktop.
Save j-jith/b5ce75f6152fabac4f3a9447679067f8 to your computer and use it in GitHub Desktop.
Read deal.ii binary files written with Vector<>::block_write() into Python numpy arrays
import sys
import os
import numpy as np
def read_deal_vec(filename):
if not os.path.isfile(filename):
print('File not found!')
else:
with open(filename, 'rb') as f:
n = int(f.readline().strip()) # length of the vector
# skip file till start of vector
while f.read(1) != b'[':
pass
# read vector from file
v = np.fromfile(f, dtype=float, count=n)
# check end of file
eof = f.read(1)
if eof != b']':
print('Something has gone wrong! EOF does not match "]".')
else:
return v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment