-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg.py
515 lines (344 loc) · 15.3 KB
/
img.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import sys
sys.path.append('/Users/natj/projects/arcmancer/lib/')
import pyarcmancer as pyac
import numpy as np
import scipy.interpolate as interp
##################################################
# Auxiliary functions
#modulo 2pi
def mod2pi(x):
while (x > 2.0*np.pi):
x -= 2.0*np.pi
while (x < 0.0):
x += 2.0*np.pi
return x
# Transform cartesian xy to our special polar angle
def calc_chi(x,y):
return mod2pi(np.pi/2.0 - np.arctan2(y,x) )
# (x,y) to (r, chi)
def xy2pol(x, y):
return np.hypot(x, y), calc_chi(x, y)
class Imgplane:
incl = 90.0
distance = 150.0
rmax = 12.0
time0 = 0.0
verbose = 0
def __init__(self, conf, metric, surfaces):
self.conf = conf
self.metric = metric
self.surfaces = surfaces
def radius_stretch(self, r, c):
tau = 8.0
norm = 1.0 - np.exp(-tau)
#return self.rmax * (1.0 - np.exp(-tau*r**2))/norm
return self.edge(c) * (1.0 - np.exp(-tau*r**2))/norm
def radius_inv(self, rad, chi):
tau = 8.0
norm = 1.0 - np.exp(-tau)
#rinv = norm * rad / self.rmax
rinv = norm * rad / self.edge(chi)
return ( -np.log(1.0-rinv)/tau )**(1.0/2.0)
# in the limit m -> 0, BL coordinates go to oblate spheroidal minkowski. These
# go to minkowski for r -> inf or a ->0
def boyer_lindquist_position(self, x_cart):
#print "Transforming cartesian position {} to Boyer-Lindquist".format(x_cart)
x, y, z = x_cart
r = np.linalg.norm(x_cart)
theta = np.arccos(z/r)
phi = np.arctan2(y, x)
return np.array([0, r, theta, phi])
# construct local spherical axes in cartesian coordinates
def local_spherical_axes(self, pos_cart):
#print "Computing cartesian components of local spherical axes at {}".format(pos_cart)
u_r = pos_cart / np.linalg.norm(pos_cart)
u_phi = np.cross(np.array([0,0,1]), u_r)
u_phi /= np.linalg.norm(u_phi)
u_theta = -np.cross(u_r, u_phi)
u_theta /= np.linalg.norm(u_theta)
#print "result {} {} {}".format(u_r, u_theta, u_phi)
return [u_r, u_theta, u_phi]
#Build geodesic path from xy coordinates in image plane
def xy2geo(self, x, y):
# get coordinates for position of image plane point
normal = np.array([np.sin(self.incl),
0.0,
np.cos(self.incl)]
)
x_cart = np.array([-y*np.cos(self.incl),
x,
y*np.sin(self.incl)])
#shift to distance
x_cart += self.distance * normal
x_sph = self.boyer_lindquist_position(x_cart)
# get velocity by projecting to local B-L axes
# get axes in _cartesian_ coordinates
u_r, u_theta, u_phi = self.local_spherical_axes(x_cart)
vel_cart = normal
vel_sph = np.array([0,
np.dot(u_r , vel_cart) ,
np.dot(u_theta , vel_cart) / x_sph[1],
np.dot(u_phi , vel_cart) / (x_sph[1] * np.sin(x_sph[2]))])
# define vertical and horizontal
vert = pyac.normalize(self.metric, x_sph, np.array([0, 0, -1.0, 0]))
vert_vel = pyac.project_along(self.metric, x_sph, vel_sph, vert)
vert -= vert_vel
vert = pyac.normalize(self.metric, x_sph, vert)
horz = pyac.spatial_cross_product(
self.metric, pyac.static_observer(self.metric, x_sph), vert, vel_sph)
horz = pyac.normalize(self.metric, x_sph, horz)
horz = pyac.normalize(self.metric, x_sph, np.array([0, 0, 0, 1.0]))
geo = pyac.Geodesic(self.metric, x_sph, vel_sph, vert, horz, pyac.VectorType.null)
return geo
def polar2geo(self, rad, chi):
x = rad * np.sin(chi)
y = rad * np.cos(chi)
return self.xy2geo(x, y)
def compute_element(self, geo):
geo.compute(-(self.distance + 50.0), self.metric, self.conf, self.surfaces)
#Rotate in chi angle and find star boundaries
def find_boundaries(self,
Nedge=10,
reltol = 1.0e-3,
max_iterations = 20
):
if self.verbose > 0:
print "Finding edge boundaries for the star..."
print " # angles {}".format(Nedge)
print " max iterations {}".format(max_iterations)
print " relative tol {}".format(reltol)
chis = np.linspace(0.0, 1.0, Nedge)*2.0*np.pi + 0.001
rlims = np.zeros(Nedge)
rmin = 0.0
rmax = 12.0
for i, chii in enumerate(chis):
geos = []
rmini = rmin
rmaxi = rmax
rmid = 0.0
N = 0
relerr = 1.0
rmid_old = 100.0
while (N < max_iterations) and (relerr > reltol):
rmid = (rmini + rmaxi)/2.0
geos.append(self.polar2geo( rmid, chii) )
self.compute_element(geos[N])
hit = geos[N].front_termination().hit_surface
if hit:
rmini = rmid
else:
rmaxi = rmid
relerr = np.abs(rmid - rmid_old)/rmid
rmid_old = rmid
N += 1
if self.verbose > 1:
print "Iterating edge at {} after {} tries for angle={} ({})".format(rmid, N, chii, relerr)
rlims[i] = rmid
self.rmax = np.max(rlims)*1.0001
if self.verbose > 0:
print "Maximum edge {}".format(self.rmax)
#Build edge location interpolator
self.edge = interp.InterpolatedUnivariateSpline(chis, rlims)
##################################################
# Creates internal polar grid for the interpolation
def generate_internal_grid(self,
Nrad = 30,
Nchi = 20,
use_flat_chi = False
):
if self.verbose > 0:
print "Generating internal polar grid..."
self.Nrad = Nrad
self.Nchi = Nchi
#Build non-equidistant angle grid
# we specifically add points near the chi = 0
# and ease the interpolation with two egde points
dchi_edge = 0.001
chimin = 0.0 - dchi_edge
chimax = 2.0*np.pi + dchi_edge
#chi_diffs = 0.8 + np.sin( np.linspace(0.0, 2.0*np.pi, Nchi-3) )**2
#chi_diffs = np.insert(chi_diffs, 0, 0.0)
#self.chi_grid = chimin + (chimax - chimin) * np.cumsum(chi_diffs)/np.sum(chi_diffs)
#self.chi_grid = np.insert(self.chi_grid, 0, self.chi_grid[0] - dchi_edge)
#self.chi_grid = np.append(self.chi_grid, self.chi_grid[-1] + dchi_edge)
#If true, we fall back to linear angle spacing
#if use_flat_chi:
#self.chi_grid = np.linspace(0.0, 2.0*np.pi, Nchi)
self.chi_grid = np.linspace(chimin, chimax, Nchi)
# Build non-equidistant radius grid
# The grid is Gauss-Laguerre weighted so that
# more points are near the boundary where the
# surface curves more
#
# Actual computations are black magic so only
# modify these if you know what you are doing
#rad_diffs = 1.0 / np.exp( np.linspace(1.2, 2.0, Nrad-1)**2)
#self.rad_grid = self.rmax * np.cumsum(rad_diffs)/np.sum(rad_diffs)
#self.rad_grid = np.insert(self.rad_grid, 0, 0.001)
self.rad_grid = np.linspace(0.0, 1.01, Nrad)
self.grid = np.empty((Nrad,Nchi), dtype=np.object)
for i, chi in enumerate(self.chi_grid):
if self.verbose > 0:
if i % 10 == 0:
print "{} % done".format(float(i)/len(self.chi_grid) * 100)
for j, rad_s in enumerate(self.rad_grid):
if self.verbose > 2:
print " tracing geodesic at chi={} and r={}".format(chi, rad)
rad = self.radius_stretch(rad_s, chi)
#Trace geodesic from image plane to star
geo = self.polar2geo(rad, chi)
self.compute_element(geo)
self.grid[j,i] = geo
#def generate_polar_grid(self,
# Nrad = 20,
# Nchi = 20
# ):
# if self.verbose > 0:
# print "Generating polar grid..."
# chi_grid = np.linspace(0.0, 2.0*np.pi, Nchi)
# rad_grid = np.linspace(0.0, self.rmax, Nrad)
# self.pgrid = np.empty( (Nrad, Nchi), dtype=np.object)
# for i, chi in enumerate(chi_grid):
# if i % 10 == 0 and self.verbose > 0:
# print "{} % done".format(float(i)/len(self.chi_grid) * 100)
# for j, rad in enumerate(rad_grid):
# #Trace geodesic from image plane to star
# geo = self.polar2geo(rad, chi)
# self.compute_element(geo)
# self.pgrid[j,i] = geo
#Reduce geodesic into physical quantities
def dissect_geos(self):
if self.verbose > 0:
print "Dissecting geodesic paths to observed quantities..."
Nrad = self.Nrad
Nchi = self.Nchi
self.Reds = np.zeros((Nrad, Nchi))
self.Cosas = np.zeros((Nrad, Nchi))
self.Times = np.zeros((Nrad, Nchi))
self.Thetas = np.zeros((Nrad, Nchi))
self.Phis_sin = np.zeros((Nrad, Nchi))
self.Phis_cos = np.zeros((Nrad, Nchi))
for i, chi in enumerate(self.chi_grid):
if self.verbose > 1:
print "{} % done".format(float(i)/len(chi_grid) * 100)
for j, rad in enumerate(self.rad_grid):
geo = self.grid[j,i]
hit_pt = geo.get_points()[0]
obs_pt = geo.get_points()[-1]
#skip if we did not hit
hit = geo.front_termination().hit_surface
if not(hit):
self.Times[j,i] = self.Times[j-1,i]
self.Thetas[j,i] = self.Thetas[j-1,i]
self.Phis_sin[j,i] = self.Phis_sin[j-1,i]
self.Phis_cos[j,i] = self.Phis_cos[j-1,i]
self.Reds[j,i] = self.Reds[j-1,i]
continue
#coordinates
#surface_point.point.x
t = self.Times[j,i] = hit_pt.point.x[0]
th = self.Thetas[j,i] = hit_pt.point.x[2]
ps = self.Phis_sin[j,i] = np.sin(hit_pt.point.x[3])
pc = self.Phis_cos[j,i] = np.cos(hit_pt.point.x[3])
#Redshift
g = self.Reds[j,i] = \
self.metric.dot(obs_pt.point, pyac.static_observer(self.metric, obs_pt.x())) / \
self.metric.dot(hit_pt.point, self.surfaces[0].observer(self.metric, hit_pt.x()))
#hit angle
cosa = self.Cosas[j,i] = \
geo.front_termination().observer_hit_angle
#if rad > 0.85:
# self.Times[j,i] = self.Times[j-1,i]
# self.Thetas[j,i] = self.Thetas[j-1,i]
# self.Phis_sin[j,i] = self.Phis_sin[j-1,i]
# self.Phis_cos[j,i] = self.Phis_cos[j-1,i]
# self.Reds[j,i] = self.Reds[j-1,i]
#Now compute spline interpolators
if self.verbose > 0:
print "Building spline coefficients..."
#kx = ky = 1
kx = 2
ky = 2
s = 0.0
#Shift time XXX
time_, phi_, theta_, cosa_, reds_ = self.get_exact_pixel(0.0, 0.0)
self.time0 = time_
self.Times -= time_
self.intp_Times = interp.RectBivariateSpline(self.rad_grid, self.chi_grid, self.Times, kx=kx, ky=ky, s=s)
self.intp_Thetas = interp.RectBivariateSpline(self.rad_grid, self.chi_grid, self.Thetas, kx=kx, ky=ky, s=s)
self.intp_Phis_sin = interp.RectBivariateSpline(self.rad_grid, self.chi_grid, self.Phis_sin, kx=kx, ky=ky, s=s)
self.intp_Phis_cos = interp.RectBivariateSpline(self.rad_grid, self.chi_grid, self.Phis_cos, kx=kx, ky=ky, s=s)
self.intp_Cosas = interp.RectBivariateSpline(self.rad_grid, self.chi_grid, self.Cosas, kx=kx, ky=ky, s=s)
self.intp_Reds = interp.RectBivariateSpline(self.rad_grid, self.chi_grid, self.Reds, kx=kx, ky=ky, s=s)
#TODO define return class
def get_pixel(self, x, y):
rad, chi = xy2pol(x, y)
time = 0.0
phi = 0.0
theta = 0.0
cosa = 0.0
reds = 0.0
redge = self.edge(chi)
if rad <= redge:
#if rad < 0.98*redge:
#if rad <= self.edge(chi):
rs = self.radius_inv(rad, chi)
#if rs > 0.875:
# return self.get_exact_pixel(x, y)
time = self.intp_Times.ev(rs, chi)
phis = self.intp_Phis_sin.ev(rs, chi)
phic = self.intp_Phis_cos.ev(rs, chi)
phi = np.arctan2(phis, phic)
theta = self.intp_Thetas.ev(rs, chi)
cosa = self.intp_Cosas.ev(rs, chi)
reds = self.intp_Reds.ev(rs, chi)
#elif 0.98*redge <= rad < 1.01*redge:
# return self.get_exact_pixel(x, y)
#pix = pixel([time, theta, phi], reds, cosa)
return time, phi, theta, cosa, reds
#Return polar pixel (i.e. poxel)
def get_poxel(self, rad, chi):
time = 0.0
phi = 0.0
theta = 0.0
cosa = 0.0
reds = 0.0
redge = self.edge(chi)
if rad <= redge:
rs = self.radius_inv(rad, chi)
time = self.intp_Times.ev(rs, chi)
phis = self.intp_Phis_sin.ev(rs, chi)
phic = self.intp_Phis_cos.ev(rs, chi)
phi = np.arctan2(phis, phic)
theta = self.intp_Thetas.ev(rs, chi)
cosa = self.intp_Cosas.ev(rs, chi)
reds = self.intp_Reds.ev(rs, chi)
return time, phi, theta, cosa, reds
def get_exact_pixel(self, x, y):
geo = self.xy2geo(x, y)
self.compute_element(geo)
hit = geo.front_termination().hit_surface
if not(hit):
return 0.0, 0.0, 0.0, 0.0, 0.0
hit_pt = geo.get_points()[0]
obs_pt = geo.get_points()[-1]
#coordinates
time = hit_pt.point.x[0] - self.time0
theta = hit_pt.point.x[2]
phi = hit_pt.point.x[3]
#Redshift
reds = self.metric.dot(obs_pt.point, pyac.static_observer(self.metric, obs_pt.x())) / \
self.metric.dot(hit_pt.point, self.surfaces[0].observer(self.metric, hit_pt.x()))
#hit angle
cosa = geo.front_termination().observer_hit_angle
return time, phi, theta, cosa, reds
# Pixel class that connects the image plane coordinates to star coordinates
class pixel:
def __init__(self,
x,
redshift,
hit_angle):
#self.x = np.array([0.0, 0.0, 0.0])
self.x = x
self.redshift = redshift
self.hit_angle = hit_angle