forked from ajsteele/mmcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsc.py
182 lines (172 loc) · 6.21 KB
/
csc.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# TO DO:
# * Make the read function read all metadata, not just the title!
import numpy
import datetime
version = '1.0'
def begin(meta,properties,filename):
global version
f=open(filename, 'w')
#write come comments at the top
f.write('# Generated by MmCalc v'+version+'\n')
f.write('# http://andrewsteele.co.uk/mmcalc/\n')
f.write('#\n')
#write the metadata
for key in meta:
f.write('# '+str(key)+'='+str(meta[key])+'\n')
f.write('# datetime='+datetime.datetime.now().strftime("%Y-%m-%d %H:%M")+'\n')
#column titles
columns = ''
for property in properties:
columns += (property+'\t')
columns = columns[:-1]+'\n'
f.write(columns)
return f
def append(attr,file):
cscline=''
for j in range(len(attr)):
cscline+=str(attr[j])+'\t'
#remove extra tab, add line break
cscline=cscline[:-1]+'\n'
file.write(cscline)
return True
def write(meta,properties,attr,filename):
global version
f=begin(meta,properties,filename)
#then numbers...
for i in range(len(attr)):
append(attr[i],f)
f.close()
return True
def close(file):
file.close()
def vector_update(dict,vname,vkey,val,dim=3):
if not dict.has_key(vname):
dict[vname] = numpy.zeros(dim,numpy.float)
dict[vname][vkey] = numpy.float(val)
return dict
def begin_read(filename):
f=open(filename, 'r')
meta = {}
properties = False
while True:
line = f.readline()
if(line[0] != '#'):
#discard the \n and/or \r characters
line = line.replace('\r','').replace('\n','')
#explode it by tabs to separate position and properties
line=line.split('\t')
#first line should be a tab-separated list of property names
if properties is False:
properties = line
return meta,properties,f
elif line[0:len('# title=')] == '# title=':
meta['title'] = line[len('# title='):].replace('\r','').replace('\n','') #ie everything after = but not line breaks
def readline(f,properties):
line = f.readline()
#readline returns a blank string when EOF reached
if line != '':
#if the first character isn't a hash, which implies comment
if(line[0] != '#'):
#discard the \n and/or \r characters
line = line.replace('\r','').replace('\n','')
#explode it by tabs to separate position and properties
line=line.split('\t')
if len(line) == len(properties):
line_values = {}
for i in range(len(properties)):
if properties[i] == 'r_x':
line_values = vector_update(line_values,'r',0,line[i])
elif properties[i] == 'r_y':
line_values = vector_update(line_values,'r',1,line[i])
elif properties[i] == 'r_z':
line_values = vector_update(line_values,'r',2,line[i])
elif properties[i] == 'u_x':
line_values = vector_update(line_values,'u',0,line[i])
elif properties[i] == 'u_y':
line_values = vector_update(line_values,'u',1,line[i])
elif properties[i] == 'u_z':
line_values = vector_update(line_values,'u',2,line[i])
elif properties[i] == 'mu_x':
line_values = vector_update(line_values,'m',0,line[i])
elif properties[i] == 'mu_y':
line_values = vector_update(line_values,'m',1,line[i])
elif properties[i] == 'mu_z':
line_values = vector_update(line_values,'m',2,line[i])
elif properties[i] == 'B_x':
line_values = vector_update(line_values,'B',0,line[i])
elif properties[i] == 'B_y':
line_values = vector_update(line_values,'B',1,line[i])
elif properties[i] == 'B_z':
line_values = vector_update(line_values,'B',2,line[i])
elif properties[i] == 'q':
line_values['q'] = numpy.int(line[i])
elif properties[i] == 'omega':
line_values['omega'] = numpy.float(line[i])
#anything else just outputs a string; this includes element name
else:
line_values[properties[i]] = line[i]
return line_values,None
else:
return None, 'Number of elements in line not equal to number of properties.'
else:
return None, 'This line is a comment.'
else:
return None, 'EOF'
def read(filename):
f=open(filename, 'r')
values = []
error = []
properties = False
i = 1
for line in f:
#if the first character isn't a hash, which implies comment
if(line[0] != '#'):
#discard the \n and/or \r characters
line = line.replace('\r','').replace('\n','')
#explode it by tabs to separate position and properties
line=line.split('\t')
#first line should be a tab-separated list of property names
if properties is False:
properties = line
else:
if len(line) == len(properties):
line_values = {}
for i in range(len(properties)):
if properties[i] == 'r_x':
line_values = vector_update(line_values,'r',0,line[i])
elif properties[i] == 'r_y':
line_values = vector_update(line_values,'r',1,line[i])
elif properties[i] == 'r_z':
line_values = vector_update(line_values,'r',2,line[i])
elif properties[i] == 'u_x':
line_values = vector_update(line_values,'u',0,line[i])
elif properties[i] == 'u_y':
line_values = vector_update(line_values,'u',1,line[i])
elif properties[i] == 'u_z':
line_values = vector_update(line_values,'u',2,line[i])
elif properties[i] == 'mu_x':
line_values = vector_update(line_values,'m',0,line[i])
elif properties[i] == 'mu_y':
line_values = vector_update(line_values,'m',1,line[i])
elif properties[i] == 'mu_z':
line_values = vector_update(line_values,'m',2,line[i])
elif properties[i] == 'B_x':
line_values = vector_update(line_values,'B',0,line[i])
elif properties[i] == 'B_y':
line_values = vector_update(line_values,'B',1,line[i])
elif properties[i] == 'B_z':
line_values = vector_update(line_values,'B',2,line[i])
elif properties[i] == 'q':
line_values['q'] = numpy.float(line[i])
elif properties[i] == 'omega':
line_values['omega'] = numpy.float(line[i])
#anything else just outputs a string; this includes element name
else:
line_values[properties[i]] = line[i]
values.append(line_values)
else:
error.append('Wrong number of values on line '+str(i)+' (expecting '+str(len(properties))+', received '+str(len(line))+')')
elif line[0:len('# title=')] == '# title=':
title = line[len('# title='):].replace('\r','').replace('\n','') #ie everything after = but not line breaks
i += 1
return title,values,error