-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathioutils.py
133 lines (112 loc) · 3.62 KB
/
ioutils.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import re
import sys
import numpy as np
import struct
def readparamfile(fields,fname,sepchar='='):
ifp = open(fname,'r')
mydict = {}
for f in fields:
mydict[f] = None
for line in ifp:
elts = line.split(sepchar)
## remove blanks
for jndx in range(elts.count('')):
elts.remove('')
assert len(elts) == 2
myfield = elts[0].strip()
if myfield in mydict:
mydict[myfield] = elts[1].strip()
return mydict
ifp.close()
def compareascii(fname1,fname2,plist,comparetype=0,printopt=0):
"""
comparetype specifies how to compare to files of numbers.
plist contains the parameters of that comparison.
if plist needs only one value, send a number works.
comparetype = 0 checks if absolute values are less than plist[0]
printopt = 1 will print first 100 discrepant lines.
returns -1 if file structure does not match
otherwise returns number of mismatched lines.
"""
if type(plist) is not list:
plist = [plist]
if(comparetype != 0):
print 'comparetype not defined, returning -1'
return -1
ifp1 = open(fname1,'r')
ifp2 = open(fname2,'r')
cnt = 0
badmatch = 0
for line1 in ifp1:
line2 = ifp2.readline()
if line2 == '': ## end of file!
print 'files have different lengths!'
return -1
cnt += 1
a1 = np.array(line1.split(),dtype='float')
a2 = np.array(line2.split(),dtype='float')
if len(a1) != len(a2):
print 'diff number of columns',len(a1),len(a2),cnt
return -1
if(comparetype == 0):
if(np.fabs(a1 - a2) > plist[0]).any():
badmatch += 1
if(printopt == 1 and badmatch <= 100):
print line, line2
ifp1.close()
ifp2.close()
return badmatch
def comparebinary(fname1,fname2,plist,formatstring,bytesperstruct,comparetype=0,printopt=0):
"""
** ASSUMES number of lines is given first as an int. **
** FOLLOWED by a list of structs unpacked with formatstring **
formatstring for subsample files is 'ffffffi', which is 28 bytes.
comparetype specifies how to compare to files of numbers.
plist contains the parameters of that comparison.
if plist needs only one value, send a number works.
comparetype = 0 checks if absolute values are less than plist[0]
printopt = 1 will print first 100 discrepant lines.
returns -1 if file structure does not match
otherwise returns number of mismatched lines.
"""
if type(plist) is not list:
plist = [plist]
if(comparetype != 0):
print 'comparetype not defined, returning -1'
return -1
ifp1 = open(fname1,'rb')
ifp2 = open(fname2,'rb')
## nevermind, not in my std header.
# x1 = ifp1.read(4)
# N1 = (struct.unpack("i",x1))[0]
# x2 = ifp2.read(4)
# N2 = (struct.unpack("i",x2))[0]
# if(N1 != N2):
# print 'files have different lengths!',N1,N2
# return -1
badmatch = 0
# for i in range(N1): ## read subsample particles one at a time.
while(0==0):
x1 = ifp1.read(bytesperstruct)
x2 = ifp2.read(bytesperstruct)
if not x1:
break
if not x2:
break
a1 = np.array(struct.unpack(formatstring,x1))
a2 = np.array(struct.unpack(formatstring,x2))
if(comparetype == 0):
if(np.fabs(a1 - a2) > plist[0]).any():
badmatch += 1
if(printopt == 1 and badmatch <= 100):
print a1
print a2
print ' '
ifp1.close()
ifp2.close()
return batmatch
if __name__ == '__main__':
myfields = ['particle_minimum','density_threshold','DMfilebase','density_file','Omega_m','density_min','search_radius','input_file_type','nfiles','dendir','outfilebase','Npcuberoot','Lbox']
mydict = readparamfile(fields=myfields,fname="L0lowz.SOini")
print mydict