-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sqe.py
35 lines (29 loc) · 1 KB
/
Sqe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
import numpy
from numpy import array
from struct import pack,unpack,calcsize
version=1
intSize = calcsize('<i')
dubSize = calcsize('<d')
strSize = calcsize('<s')
def write(Sqe,filename='Sqe',comment=''):
"""Takes numpy Sqe in with shape (N_e,N_q) and writes to binary file."""
f=open(filename,'w')
f.write(pack('<64s','Sqe'))
f.write(pack('<i',version))
f.write(pack('<1024s',comment))
f.write(pack('<i',Sqe.shape[0]))
f.write(pack('<i',Sqe.shape[1]))
Sqe = numpy.asarray(Sqe.reshape(-1), dtype="<d")
f.write(Sqe.tostring())
return
def read(filename='Sqe'):
"""Takes filename, returns a tuple with information and Sqe as a numpy."""
f=open(filename,'r')
filetype, = unpack('<64s',f.read(64*strSize))
version, = unpack('<i',f.read(intSize))
comment, = unpack('<1024s',f.read(1024*strSize))
N_e,N_q = unpack('<2i',f.read(2*intSize))
Sqe = numpy.fromstring(f.read(),dtype="<d")
Sqe.shape = (N_e,N_q)
return (filetype.strip('\x00'),version,comment.strip('\x00')),Sqe