Last active
July 18, 2016 09:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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