-
Notifications
You must be signed in to change notification settings - Fork 0
/
_build_tofu.py
227 lines (174 loc) · 5.77 KB
/
_build_tofu.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'''
Function to build diagnostic as a ToFu Collection object
cjperks
Aug 5, 2024
'''
# Modules
import tofu as tf
import numpy as np
import cmp_tofu_xicsrt.setup._def_diag as dd
import cmp_tofu_xicsrt.utils as utils
__all__ = [
'_init_diag'
]
#################################################
#
# Main
#
#################################################
# Load diagnostic from ToFu
def _init_diag(
coll = None,
dplasma = None,
dcry = None,
lamb0 = None,
subcam = None,
doptics = None,
):
# If user is reloading a Collection object
if isinstance(coll, str):
coll = tf.data.load(coll)
# Else, build a new diagnostic
else:
coll = _build_diag(
dcry = dcry,
lamb0 = lamb0,
subcam = subcam,
doptics = doptics,
)
# Saves diagnostic
coll.save(path='/home/cjperks/cmp_tofu_xicsrt/diags')
# Output
return coll
#################################################
#
# Utilities
#
#################################################
# Script to build diagnostic in ToFu
def _build_diag(
dap = None,
dcry = None,
dmat = None,
dcam = None,
lamb0 = None,
subcam = None,
doptics = None,
):
# Labeling
if doptics is not None:
ap_option = doptics['key_diag']
ap_label = doptics['key_ap']
cry_option = doptics['key_diag']
mat_option = doptics['key_diag']
cry_label = doptics['key_cry']
cam_option = doptics['key_cam']
cam_label = doptics['key_cam']
diag_label = doptics['key_diag']
else:
ap_option = 'default'
ap_label = 'ap'
cry_option = dcry
mat_option = 'default'
cry_label = 'cry'
cam_options = 'default'
cam_label = 'cam'
diag_label = 'valid'
# Init
coll = tf.data.Collection()
# Gets default aperture geometry
if dap is None:
dap = dd.get_dap(option=ap_option)
# Adds aperture
coll.add_aperture(
key=ap_label,
**dap
)
# Gets dafault crystal geometry
if isinstance(cry_option, str):
dcry = dd.get_cry_dgeom(option=cry_option)
if dmat is None:
dmat = dd.get_cry_dmat(option=mat_option)
dmat['target']['lamb'] = lamb0*1e-10
# Adds crystal
coll.add_crystal(
key = cry_label,
dgeom = dcry,
dmat = dmat,
)
# Gets default camera geometry
if dcam is None:
dcam = dd.get_dcam(option=cam_option)
# Adds camera
coll.add_camera_2d(
key = cam_label,
dgeom = dcam
)
# Takes a subset of the camera area if requested
if subcam is not None:
dsub = dd.get_dsubcam(option=subcam)
# If user wants to look at a section of the detector in a finer pixel mesh
if dsub['method'] == 'refine':
if dsub['dx'] is not None:
dcam['cent'] += dcam['e0']*dsub['dx']
nx0 = len(dcam['cents_x0'])
pix_width = 2*dsub['xhalfsize']/(nx0-1)
dcam['outline_x0'] = 0.5* pix_width * np.r_[-1, 1, 1, -1]
dcam['cents_x0'] = dsub['xhalfsize'] * np.linspace(-1, 1, nx0)
if dsub['dy'] is not None:
dcam['cent'] += dcam['e1']*dsub['dy']
nx1 = len(dcam['cents_x1'])
pix_height = 2*dsub['yhalfsize']/(nx1-1)
dcam['outline_x1'] = 0.5* pix_height * np.r_[-1, -1, 1, 1]
dcam['cents_x1'] = dsub['yhalfsize'] * np.linspace(-1, 1, nx1)
# If user wants to zoom into a section of the detector on the same pixel mesh
elif dsub['method'] == 'zoom':
if dsub['dx'] is not None:
indx_up = np.where(
(
coll.ddata[cam_label+'_c0']['data']
+ coll.dobj['camera'][cam_label]['dgeom']['extenthalf'][0]
) <= dsub['dx'] + dsub['xhalfsize']
)[0][-1] # Last pixel physically before cutoff
indx_low = np.where(
(
coll.ddata[cam_label+'_c0']['data']
- coll.dobj['camera'][cam_label]['dgeom']['extenthalf'][0]
) <= dsub['dx'] - dsub['xhalfsize']
)[0][-1] # Last pixel physically before cutoff
dcam['cents_x0'] = (
coll.ddata[cam_label+'_c0']['data'][indx_low:indx_up]
)
if dsub['dy'] is not None:
indy_up = np.where(
(
coll.ddata[cam_label+'_c1']['data']
+ coll.dobj['camera'][cam_label]['dgeom']['extenthalf'][1]
) <= dsub['dy'] + dsub['yhalfsize']
)[0][-1] # Last pixel physically before cutoff
indy_low = np.where(
(
coll.ddata[cam_label+'_c1']['data']
- coll.dobj['camera'][cam_label]['dgeom']['extenthalf'][1]
) <= dsub['dy'] - dsub['yhalfsize']
)[0][-1] # Last pixel physically before cutoff
dcam['cents_x1'] = (
coll.ddata[cam_label+'_c1']['data'][indy_low:indy_up]
)
# Adds subcamera
cam_label += '_' + diag_label
coll.add_camera_2d(
key = cam_label,
dgeom = dcam
)
# Builds diagnostic
coll.add_diagnostic(
key = diag_label,
doptics = {cam_label: [cry_label, ap_label]},
compute = True, # compute LOS
compute_vos_from_los = True,
convex = True,
config = tf.load_config('SPARC-V0')
)
# Output
return coll