-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDOS.py
35 lines (29 loc) · 1.03 KB
/
DOS.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 struct import pack,unpack,calcsize
version=1
intSize = calcsize('<i')
dubSize = calcsize('<d')
strSize = calcsize('<s')
def write(E,DOS,filename='DOS',comment=''):
"""Takes numpy DOS in with shape (N_e) and writes to binary file."""
f=open(filename,'w')
f.write(pack('<64s','DOS'))
f.write(pack('<i',version))
f.write(pack('<1024s',comment))
f.write(pack('<i',DOS.shape[0]))
f.write(pack('<d',E[1]-E[0]))
DOS = numpy.asarray( DOS, dtype="<d" )
f.write(DOS.tostring())
return
def read(filename='DOS'):
"""Takes filename, returns a tuple with information and DOS 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_Bins, = unpack('<i',f.read(intSize))
dE, = unpack('<d',f.read(dubSize))
DOS = numpy.fromstring(f.read(), dtype="<d")[:N_Bins]
E = numpy.arange(0,N_Bins*dE,dE)
return (filetype.strip('\x00'),version,comment.strip('\x00')),E,DOS