-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdac2diffDac.py
144 lines (110 loc) · 4.26 KB
/
dac2diffDac.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
132
133
134
135
136
137
138
139
140
141
142
"""
Aim: Transform Feflow *.dac file into another *.dac file presenting data
values differences with time step 0 instead of absolute data values
"""
# Imports #####
import sys
# Input file choice #####
try:
input_file=sys.argv[1]
except:
print "Usage: dac2diffDac.py FeflowFileName.dac"
sys.exit(1)
"""
input_file='AA_AllDiri_beginningTest.dac'
input_file='BTSL_raf3d_failles_comp1_nest_couv_go_1_0.dac'
"""
output_file=input_file[:-4]+'_diffDac.dac'
input_data=open(input_file,'r')
output_data=open(output_file,'w')
# Debug tools #####
debug=0
if debug:
test1=open('test1','w')
test2=open('test2','w')
# Main #####
init_cond=[] # Initial conditions variable
lineInit=input_data.readline()
if lineInit=='\n' or lineInit=='\r':
line=' '
else:
line=lineInit.rstrip('\n\r')
while line != '':
if line[0]=='$': # New time step !
print >> output_data, line
lineS=line.replace(',',' ').replace(' ',' ').replace(' ',' ').replace(' ',' ').split(' ')
print lineS[1]
lineInit=input_data.readline()
if lineInit=='\n' or lineInit=='\r':
line=' '
else:
line=lineInit.rstrip('\n\r')
if float(lineS[1]) == 0: # Initial conditions time step
print "Initial data record"
while line[0] not in ['','$','D']: # 'D' for 'DIAGRAM'
init_cond.append(line.split(',')[:-1]) # recording IC variable
print >> output_data, line # copying in output file
lineInit=input_data.readline()
if lineInit=='\n' or lineInit=='\r':
line=' '
else:
line=lineInit.rstrip('\n\r')
if float(lineS[1]) > 0: # Time step t
print "Comparision initial data and time step #",lineS[1]
count=0
countFloat0=0 # check
countFloatT=0 # check
while line[0] not in ['','$','D'] and line [0:2] != ' ': # 'D' for 'DIAGRAM'
data_line_out=''
data_line_in=line.split(',')[:-1]
for u in range(len(data_line_in)):
try:
point=float(data_line_in[u])
init=float(init_cond[count][u])
if type(point)==float: # check
countFloatT+=1 # check
if type(init)==float: # check
countFloat0+=1 # check
data_line_out += '{:1.14e}, '.format(point-init)
if countFloat0 != countFloatT: # check
print "Different number of floats in time step",lineS[1],"(",countFloatT,"versus",countFloat0,"at time step 0)"
point,init='','' # check
if u == len(data_line_in)-1 and debug:
print >> test1, 'count',count,'u',u,'" "+data line out[:-1]',' '+data_line_out[:-1]
except:
data_line_out += '1.#QNAN000000000e+00, '
if u == len(data_line_in)-1 and debug:
print >> test1, 'count',count,'u',u,'" "+data line out[:-1]',' '+data_line_out[:-1]
if debug:
print >> test2, 'count',count,'u',u,'" "+data line out[:-1]',' '+data_line_out[:-1]
print >> output_data, ' '+data_line_out[:-1]
lineInit=input_data.readline()
if lineInit=='\n' or lineInit=='\r':
line=' '
else:
line=lineInit.rstrip('\n\r')
count+=1
if countFloat0 != countFloatT: # check
print "Different number of floats in time step",lineS[1],"(",countFloatT,"versus",countFloat0,"at time step 0)"
# print init_cond # Only for small *.dac files
if line[0:3]==' ':
print >> output_data,line
lineInit=input_data.readline()
if lineInit=='\n' or lineInit=='\r':
line=' '
else:
line=lineInit.rstrip('\n\r')
if line[0]!='$': # Normal copy
print >> output_data, line
lineInit=input_data.readline()
if lineInit=='\n' or lineInit=='\r':
line=' '
else:
line=lineInit.rstrip('\n\r')
# End #####
input_data.close()
output_data.close()
if debug:
test1.close()
test2.close()
print '\n = = = = Done = = = =\n'