-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate_camera_file.py
193 lines (145 loc) · 6.04 KB
/
generate_camera_file.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
183
184
185
186
187
188
189
190
191
192
193
import sys
import os
import getpass
from collections import OrderedDict
import datetime
def camera(fname,
camera_options={}, geometry_options={},
sampling_options={}, results_options={},
remove=[], comment=None, verbose=False,
overwrite=False):
"""
refer to wikispace.librat.com for full definitions
INPUT
-----
fname: path to save camera file
results: default None, str
If this is None then a random name is generated, otherwise
str is used.
remove: default [], list
list of commands as strings in the librat format that
are to be not included in the camera file
comment: default None, str
a comment to inlcude at the beigging of the camera file
e.g. associtated light file etc.
"""
if os.path.isfile(fname) and not overwrite:
R = raw_input('this camera exists, overwrite? (y/n)').lower()
if R != 'y':
sys.exit()
camera = OrderedDict()
camera['camera.name'] = 'simple camera'
for K, V in camera_options.items():
camera[K] = V
geometry = OrderedDict()
geometry['geometry.perspective'] = True
geometry['geometry.azimuth'] = 0.0
geometry['geometry.zenith'] = 0.0
geometry['geometry.twist'] = 0.0
geometry['geometry.lookAt'] = [0, 0, 0]
geometry['geometry.idealArea'] = 2
geometry['geometry.boomLength'] = 10.
for K, V in geometry_options.items():
geometry[K] = V
sampling = OrderedDict()
sampling['samplingCharacteristics.nPixels'] = 262144
sampling['samplingCharacteristics.rpp'] = 4
for K, V in sampling_options.items():
sampling[K] = V
### setting results
if 'oname' in results_options.keys():
oname = results_options['oname']
else:
oname = '-'.join(camera['camera.name'].split()) + '_' + datetime.datetime.now().strftime("%y-%m-%d_%H:%M:%S")
results = OrderedDict()
results['result.integral.mode'] = 'scattering order'
results['result.integral.format'] = 'ascii'
results['result.image'] = oname + '.hips'
results['result.integral'] = oname + '.results'
### concatenate settings
camera_settings = OrderedDict(camera.items() +
geometry.items() +
sampling.items() +
results.items())
for drop in remove:
del camera_settings[drop]
write_camera(fname, camera_settings, comment, verbose)
return oname
def light(fname, camera_options={}, geometry_options={},
comment=None, verbose=False,
overwrite=False, remove=[]):
if os.path.isfile(fname) and not overwrite:
R = raw_input('this camera exists, overwrite? (y/n)').lower()
if R != 'y':
sys.exit()
camera = OrderedDict()
camera['camera.name'] = 'simple camera'
for K, V in camera_options.items():
camera[K] = V
geometry = OrderedDict()
geometry['geometry.azimuth'] = 0.0
geometry['geometry.zenith'] = 0.0
for K, V in geometry_options.items():
geometry[K] = V
### concatenate settings
camera_settings = OrderedDict(camera.items() +
geometry.items())
for drop in remove:
del camera_settings[drop]
write_camera(fname, camera_settings, comment, verbose)
def update_existing_camera(old_camera, new_camera, oname=None,
new_options={}, comment=None, verbose=False,
remove=[]):
with open(old_camera) as cam:
camera_settings = OrderedDict()
for line in cam.readlines():
if '#' in line or '{' in line or '}' in line or len(line) < 10:
continue
else:
var, val = line.strip().strip(';').split('=')
try: val = float(val)
except: pass
if isinstance(val, str):
val = val.replace('"', '')
val = val.replace('"', '')
val = ' '.join(val.split())
if isinstance(val, str) and len(val.split(',')) > 1:
val = [float(v) for v in val.split(',')]
camera_settings[var.strip()] = val
for K, V in new_options.items():
camera_settings[K] = V
if oname:
camera_settings['result.image'] = oname + '.hips'
camera_settings['result.integral'] = oname + '.dat'
for drop in remove:
del camera_settings[drop]
write_camera(new_camera, camera_settings, comment, verbose)
def write_camera(fname, camera_settings, comment, verbose):
with open(fname, 'w') as cam:
cam.write('camera {\n')
cam.write('\n')
cam.write('# camera location: {}\n'.format(fname))
cam.write('# created by: {}\n'.format(getpass.getuser()))
if comment is not None:
for line in range((len(comment) // 75) + 1):
cam.write('# ' + comment[line * 75: (line + 1) * 75] + '\n')
cam.write('\n')
for K, V in camera_settings.items():
if isinstance(V, list):
V = ','.join([str(v) for v in V])
elif isinstance(V, str):
V = '"{}"'.format(V)
elif isinstance(V, bool):
V = '{}'.format({True:'TRUE', False:'FALSE'}[V])
cam.write('{} = {};\n'.format(K, V))
if verbose: print '{} = {};\n'.format(K, V).strip()
cam.write('}')
if __name__ == '__main__':
fname = sys.argv[1]
if len(sys.argv) > 2:
for i, arg in enumerate(sys.argv[2::2]):
if arg == '--comment':
comment = sys.argv[i + 3]
else:
print '{} not understood'.format(arg)
camera(fname, comment=comment)