-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransit_sim.py
More file actions
586 lines (419 loc) · 19.6 KB
/
transit_sim.py
File metadata and controls
586 lines (419 loc) · 19.6 KB
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Transit Simulation
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy import units as u
from astropy.units import au
from astropy.constants import R_sun
from photutils.aperture import CircularAperture, aperture_photometry
# Step 1: Open the FITS file and load the data
fits_file = '18_07_13.fts' # replace with your FITS file name
with fits.open(fits_file) as hdul:
data = hdul[0].data
# Step 2: Extract the second plane (Continuum Intensity)
plane2 = data[1, :, :] # Continuum Intensity (2D array)
# Step 2.1: Find centre and radius of Sun
from skimage import filters, measure
# Apply a threshold to segment the Sun from the background
threshold_value = filters.threshold_otsu(plane2)
binary_image = plane2 > threshold_value
# Find contours in the thresholded image
contours = measure.find_contours(binary_image, level=0.5)
# Identify the largest contour (should correspond to the Sun's disk)
largest_contour = max(contours, key=len)
# Calculate the center and radius
y_center = np.mean(largest_contour[:, 0])
x_center = np.mean(largest_contour[:, 1])
radii = np.sqrt((largest_contour[:, 0] - y_center) ** 2 + (largest_contour[:, 1] - x_center) ** 2)
radius = np.mean(radii)
# Step 3: Define the apertures
# Whole Sun aperture
sun_center = (x_center, y_center) # example coordinates of the Sun's center
sun_radius = radius # example radius of the Sun
# Occulting object aperture
object_center = (1000, 1000) # example coordinates of the object's center
object_radius = 50 # exa mple radius of the occulting object
# Create the apertures
sun_aperture = CircularAperture(sun_center, r=sun_radius)
object_aperture = CircularAperture(object_center, r=object_radius)
# Step 4: Perform aperture photometry on the Continuum Intensity plane
sun_photometry = aperture_photometry(plane2, sun_aperture)
object_photometry = aperture_photometry(plane2, object_aperture)
# Extract the summed flux values
sun_flux = sun_photometry['aperture_sum'][0]
object_flux = object_photometry['aperture_sum'][0]
# Step 5: Subtract the flux of the occulting object from the total flux of the Sun
visible_flux = sun_flux - object_flux
# Display the results
print(f"Total flux of the Sun (Continuum Intensity): {sun_flux}")
print(f"Flux of the occulting object (Continuum Intensity): {object_flux}")
print(f"Visible flux after occultation (Continuum Intensity): {visible_flux}")
normalized_flux_percentage = (object_flux / sun_flux) * 100
print(f"Normalized Flux Occulted: {normalized_flux_percentage:.4f}%")
# Optional: Display the Continuum Intensity image with the apertures overlaid
plt.imshow(plane2, cmap='gray', origin='lower')
sun_aperture.plot(color='red', lw=1, alpha=0.5)
object_aperture.plot(color='blue', lw=1, alpha=0.5)
plt.title("Continuum Intensity with Apertures")
plt.colorbar()
plt.show()
# Calculate Pixel to distance ratio
MetrePerPixel = R_sun.value / radius
AUPerPixel = MetrePerPixel / au.to(u.m)
print(f"Estimate of Metres per Pixel: {MetrePerPixel:.4f}")
#%% Masking Planet and Sun
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy import units as u
from astropy.units import au
from astropy.constants import R_sun
from photutils.aperture import CircularAperture, aperture_photometry
# Step 1: Open the FITS file and extract the data
file_path = '18_07_13.fts'
with fits.open(file_path) as hdul:
data = hdul[0].data # Extract the 3D data array
# Extract the second 2D plane from your data (Continuum Intensity)
plane2 = data[1, :, :] # Shape (2048, 2048)
from skimage import filters, measure
# Apply a threshold to segment the Sun from the background
threshold_value = filters.threshold_otsu(plane2)
binary_image = plane2 > threshold_value
# Find contours in the thresholded image
contours = measure.find_contours(binary_image, level=0.5)
# Identify the largest contour (should correspond to the Sun's disk)
largest_contour = max(contours, key=len)
# Calculate the center and radius
y_center = np.mean(largest_contour[:, 0])
x_center = np.mean(largest_contour[:, 1])
radii = np.sqrt((largest_contour[:, 0] - y_center) ** 2 + (largest_contour[:, 1] - x_center) ** 2)
radius = np.mean(radii)
print(f"Center: ({x_center}, {y_center}), Radius: {radius}")
# Create a circular mask for the Sun's disk
ny, nx = plane2.shape # extracts the height (ny) and width (nx) of the 2D image (plane2)
y, x = np.ogrid[:ny, :nx] # Create two separate arrays that correspond to the row indices (y) and column indices (x) of the image
# Create a circular mask for Sun
distance_from_center = np.sqrt((x - x_center)**2 + (y - y_center)**2)
mask = distance_from_center <= radius
# Apply the mask to the image (set background pixels to zero)
sun_region = np.where(mask, plane2, 0)
# Calculate the integrated flux of the Sun (sum of the pixels within the disk)
integrated_flux_sun = sun_region.sum()
sun_flux = integrated_flux_sun
sun_region_masked = sun_region.copy()
# Create Planet Mask
object_center = (1000, 1000) # example coordinates of the object's center
object_radius = 50 # exa mple radius of the occulting object
distance_from_center2 = np.sqrt((x - object_center[1])**2 + (y - object_center[0])**2)
mask2 = distance_from_center2 <= object_radius
sun_region_masked[mask2] = 0
integrated_flux_masked = sun_region_masked.sum()
# Print the result
print(f"Integrated Flux of the Sun: {integrated_flux_sun} counts")
print(f"Integrated Flux of the Sun with Dot: {integrated_flux_masked} counts")
# Step 4: Plot the modified image with the circular black dot
plt.figure(figsize=(8, 8))
plt.imshow(sun_region_masked, cmap='gray', origin='lower')
plt.colorbar(label='Continuum Intensity [counts]')
plt.xlabel('X Pixels')
plt.ylabel('Y Pixels')
plt.show()
#%% Calculate Limb Darkening Coefficients
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from scipy.integrate import quad
def nonlinear(mu, c1, c2, c3, c4):
return 1 - (c1 * (1 - mu**(1/2)) +
c2 * (1 - mu) +
c3 * (1 - mu**(3/2)) +
c4 * (1 - mu**2))
def quadratic(mu, u1, u2):
"""Quadratic limb darkening model."""
return (1 - u1 * (1 - mu) - u2 * (1 - mu)**2)
intensity_array = sun_region
central_intensity = intensity_array[int(y_center), int(x_center)]
normalized_intensity_array = intensity_array / central_intensity
# Get the dimensions of the array
ny, nx = normalized_intensity_array.shape
# Create a grid of distances from the center
y, x = np.indices((ny, nx))
r = np.sqrt((x - x_center)**2 + (y - y_center)**2)
r_normalized = r / radius
# Mask to only include points within the Sun's disk
mask = r_normalized <= 1
# Average the intensity in radial bins
num_bins = 100
bins = np.linspace(0, 1, num_bins)
radial_profile = np.zeros(num_bins - 1)
radial_profile_error = np.zeros(num_bins - 1)
for i in range(num_bins - 1):
bin_mask = (r_normalized >= bins[i]) & (r_normalized < bins[i + 1]) & mask
bin_values = normalized_intensity_array[bin_mask]
radial_profile[i] = np.mean(bin_values)
# Calculate the standard error for the current bin
if len(bin_values) > 0:
radial_profile_error[i] = np.std(bin_values) / np.sqrt(len(bin_values))
else:
radial_profile_error[i] = 0
# Handle cases where bins might be empty (NaNs)
radial_profile = np.nan_to_num(radial_profile)
bin_centers = (bins[:-1] + bins[1:]) / 2
mu_values = np.sqrt(1 - bin_centers ** 2)
fit_quad, cov_quad = curve_fit(quadratic, mu_values, radial_profile, sigma=radial_profile_error)
fit_nonlinear, _ = curve_fit(nonlinear, mu_values, radial_profile, sigma=radial_profile_error)
print(f'Quadratic Fit: u1={fit_quad[0]:.3f}, u2={fit_quad[1]:.3f}')
print(f'Nonlinear Fit: u1={fit_nonlinear[0]:.3f}, u2={fit_nonlinear[1]:.3f}, u3={fit_nonlinear[2]:.3f}, u3={fit_nonlinear[3]:.3f}')
# Plot the results
plt.figure(dpi = 300)
plt.errorbar(mu_values, radial_profile, yerr=radial_profile_error,capsize = 2, fmt='o', ms = 2, label='Observed radial profile')
plt.plot(mu_values, nonlinear(mu_values, *fit_nonlinear), label=f'Nonlinear Fit')
plt.plot(mu_values, quadratic(mu_values, *fit_quad), label=f'Quadratic Fit')
plt.legend()
plt.xlabel('mu')
plt.ylabel('Normalized Intensity')
plt.legend()
plt.show()
#%% Orbital Sim
import numpy as np
import matplotlib.pyplot as plt
from astropy.units import au
from astropy import units as u
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def findMeanAnomaly(t, t0, P):
return 2 * np.pi / P * (t-t0)
def f(E, M, e):
return E - e * np.sin(E) - M
def f_prime(E, e):
return 1 - e * np.cos(E)
def findEccentricAnomaly(M_array, e, initial_guess, tol=1e-8, max_iter=100): # through Newton method
E_array = np.zeros_like(M_array)
for i, M in enumerate(M_array):
E = initial_guess[i]
for _ in range(max_iter):
f_val = f(E, M, e)
if np.abs(f_val) < tol:
E_array[i] = E
break
f_prime_val = f_prime(E, e)
E = E - f_val / f_prime_val
else:
raise ValueError(f"Failed to converge for M = {M}")
return E_array
def findTrueAnomalyDash(E, e):
numerator = np.cos(E) - e
denominator = 1 - e * np.cos(E)
arg = numerator / denominator
return np.arccos(arg)
def findTrueAnomaly(E_values, theta_prime_values):
theta_values = np.zeros_like(E_values)
for i, E in enumerate(E_values):
theta_prime = theta_prime_values[i]
while E >= 2 * np.pi: # ensures if/else conditions repreated every 2pi
E -= 2 * np.pi
if E <= np.pi:
theta_values[i] = theta_prime
elif np.pi < E < 2 * np.pi:
theta_values[i] = 2 * np.pi - theta_prime
return theta_values
def RadiusFromFocus(a, e, theta):
r = (a * (1 - e**2)) / (1 + e * np.cos(theta))
return r
def PolartoCartesian(r, theta):
x = r * np.cos(theta)
y = r * np.sin(theta)
return x, y
def RotatePointsAroundY(x, y, z, inclination):
# Convert angle from degrees to radians
anlge_between_orbitalplane_refplane = 90 - inclination
rad = np.radians(-anlge_between_orbitalplane_refplane)
# Define the rotation matrix for counter-clockwise rotation around the y-axis
x_rotated = x * np.cos(rad) + z * np.sin(rad)
y_rotated = y
z_rotated = - x * np.sin(rad) + z * np.cos(rad)
return x_rotated, y_rotated, z_rotated
# AU per Pixel: 4.946822969244625e-06
a = 1/ AUPerPixel # au.to(u.km) is 1 AU
e = 0.0
P = 365 # days
t = np.linspace(0,365,100000)
t0 = 0
i = 89.8
MeanAnomalyE = findMeanAnomaly(t,t0,P)
EccentricAnomaly = findEccentricAnomaly(MeanAnomalyE,e,MeanAnomalyE)
TrueAnomalyDashE = findTrueAnomalyDash(EccentricAnomaly, e)
TrueAnomalyE = findTrueAnomaly(EccentricAnomaly, TrueAnomalyDashE)
RadiusFromFocusE = RadiusFromFocus(a,e,TrueAnomalyE)
x_values, y_values = PolartoCartesian(RadiusFromFocusE, TrueAnomalyE)
x, y, z = RotatePointsAroundY(x_values, y_values, np.zeros_like(x_values), i)
fig = plt.figure(dpi=300)
ax = fig.add_subplot()
ax.axis('equal')
ax.plot(x_values,y_values,'.', ms = 1)
ax.plot(x,y,'.', color = 'red',ms = 1)
ax.plot(0,0,'.', color = 'orange')
ax.set_xlabel('x (Pixels)')
ax.set_ylabel('y (Pixels)')
plt.title("X-Y plane View")
fig = plt.figure(dpi=300)
ax = fig.add_subplot()
ax.axis('equal')
ax.plot(y_values,np.zeros_like(x_values),'.', ms = 1)
ax.plot(y,z,'.', color = 'red',ms = 1) # observer view
ax.plot(0,0,'.', color = 'orange')
ax.set_xlabel('z (Pixels)')
ax.set_ylabel('y (Pixels)')
plt.title("Observer View")
fig = plt.figure(dpi=300)
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_values, y_values, np.zeros_like(x_values), color='blue', label='Original Points', s = 3)
# Plot the rotated points in red
ax.axis('equal')
ax.scatter(x, y, z, color='red', label='Rotated Points', s = 1)
ax.set_xlabel('x (Pixels)')
ax.set_ylabel('y (Pixels)')
ax.set_zlabel('z (Pixels)')
plt.show()
#%% Lightcurve
import pandas as pd
# x,y,z, as the positions of centre of the occulting body (in pixels)
# we took positive x axis as direction towards observer so we discard that coordinate
# Transpose y and z array such that the initial point is at centre Sun
y_transpose = y + x_center
z_transpose = z + y_center
# Create a dataframe of orbital sim
df = pd.DataFrame({
'x (Pixels)': x,
'y (Pixels)': y_transpose,
'z (Pixels)': z_transpose,
't (Days)': t
})
n = len(df)
first_quarter_end_index = int(n // 4)
last_quarter_start_index = int((3 * n) // 4)
# Filter the DataFrame for points near transit
first_quarter = df.iloc[:first_quarter_end_index][(df['y (Pixels)'] >= x_center) & (df['y (Pixels)'] < 2200)]
last_quarter = df.iloc[last_quarter_start_index:][(df['y (Pixels)'] > -200) & (df['y (Pixels)'] <= x_center)]
last_quarter['t (Days)'] = last_quarter['t (Days)'] - 365
filtered_df= pd.concat([first_quarter, last_quarter]).sort_values(by='y (Pixels)', ascending=True).reset_index(drop=True)
# Plot graph
fig, ax = plt.subplots(dpi = 300) # Create a figure and an axis
# Display the image on the axis
im = ax.imshow(plane2, cmap='gray', origin='lower')
# Set the title
ax.set_title("Position of Transit on Stellar Disk")
# Plot Centres of occulting body
ax.plot(filtered_df['y (Pixels)'], filtered_df['z (Pixels)'], '.', ms = 1)
# Add a colorbar to the figure
fig.colorbar(im, ax=ax)
# Show the plot
plt.show()
object_fluxes = []
object_positions = list(zip(filtered_df['y (Pixels)'], filtered_df['z (Pixels)']))
object_radius = 50
integrated_fluxes_masked = []
object_fluxes_mix = []
for position in object_positions:
# Create a circular mask for the Sun's disk
ny, nx = plane2.shape # extracts the height (ny) and width (nx) of the 2D image (plane2)
y_grid, x_grid = np.ogrid[:ny, :nx] # Create two separate arrays that correspond to the row indices (y) and column indices (x) of the image
# create mask
sun_region_masked = sun_region.copy()
distance_from_center2 = np.sqrt((x_grid - position[1])**2 + (y_grid - position[0])**2)
mask2 = distance_from_center2 <= object_radius
sun_region_masked[mask2] = 0
integrated_flux_masked = sun_region_masked.sum()
integrated_fluxes_masked.append(integrated_flux_masked)
for position in object_positions:
# Create an aperture for the current object position
object_aperture = CircularAperture(position, r=object_radius)
# Perform aperture photometry for the current object
object_photometry = aperture_photometry(sun_region_masked, object_aperture)
# Extract the summed flux value for the current object and store it in the list
object_flux_mix = object_photometry['aperture_sum'][0]
object_fluxes_mix.append(object_flux_mix)
# Iterate over each object position to create apertures and perform photometry
for position in object_positions:
# Create an aperture for the current object position
object_aperture = CircularAperture(position, r=object_radius)
# Perform aperture photometry for the current object
object_photometry = aperture_photometry(plane2, object_aperture)
# Extract the summed flux value for the current object and store it in the list
object_flux = object_photometry['aperture_sum'][0]
object_fluxes.append(object_flux)
Transmission_array = (sun_flux - object_fluxes) / sun_flux
Transmission_array_2 = -(integrated_flux_sun -integrated_fluxes_masked) / integrated_flux_sun
Transmission_array_3 = (integrated_flux_sun - object_fluxes_mix) / integrated_flux_sun
filtered_df['Transmission (%)'] = Transmission_array - 1 # APERTURE METHOD
filtered_df['Transmission (%) 2'] = Transmission_array_2 # MASK METHOD
filtered_df['Transmission (%) 3'] = Transmission_array_3 - 1 # APERTURE + MASK METHOD
# Filter df to remove NaN
filtered_df['Transmission (%)'] = filtered_df['Transmission (%)'].fillna(0)
filtered_df['Transmission (%) 3'] = filtered_df['Transmission (%) 3'].fillna(0)
transmission_range = max(filtered_df['Transmission (%)']) - min(filtered_df['Transmission (%)'])
fig = plt.figure(dpi=300)
ax = fig.add_subplot()
ax.plot(filtered_df['t (Days)'], filtered_df['Transmission (%)'], label = "Apertrue")
ax.plot(filtered_df['t (Days)'], filtered_df['Transmission (%) 2'],label = "Mask")
ax.plot(filtered_df['t (Days)'], filtered_df['Transmission (%) 3'],label = "APERTURE + MASK")
ax.set_ylabel('Transmission Loss (%)')
ax.set_xlabel('Time (Days)')
ax.legend()
ax.set_xlim(filtered_df['t (Days)'].iloc[0], filtered_df['t (Days)'].iloc[-1])
ax.set_ylim(min(filtered_df['Transmission (%)']) - transmission_range * 0.1,
max(filtered_df['Transmission (%)']) + transmission_range * 0.1)
#%% Fitting Light Curve
import numpy as np
import batman
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
# Transmission and Time
Transmission = np.array(filtered_df['Transmission (%) 3']) + 1
time = np.array(filtered_df['t (Days)'])
# Define the transit model parameters
def fit_func(time, rp):
# Create a TransitParams object
params = batman.TransitParams()
# Set fixed parameters
params.t0 = t0 # Fixed time of central transit
params.per = 365 # Fixed orbital period
params.a = a / sun_radius # Fixed semi-major axis / star radius
params.inc = i # Fixed inclination (degrees)
params.ecc = e # Fixed eccentricity
params.w = 90.0 # Fixed longitude of periastron (degrees)
# Set variable parameters
params.rp = rp # Variable: planet radius / star radius
params.limb_dark = "nonlinear" # Limb darkening model
params.u = fit_nonlinear # Variable: limb darkening coefficients
# 0.9036 -0.2312 limb darkeining
# Calculate the light curve using the parameters
m = batman.TransitModel(params, time)
return m.light_curve(params)
# Provide initial guesses for the parameters
initial_rp = object_radius / sun_radius # Initial guess for the planet radius / star radius
# Initial parameter array for curve_fit and bounds
p0 = [initial_rp]
lower_bounds = [0]
upper_bounds = [1]
fit_params, covariance = curve_fit(fit_func, time, Transmission, p0=p0, bounds=(lower_bounds, upper_bounds), maxfev=10000)
fig = plt.figure(dpi=300)
ax = fig.add_subplot()
#ax.plot(filtered_df['t (Days)'], filtered_df['Transmission (%)'], 'x', ms = 3, label = "Aperture")
#ax.plot(filtered_df['t (Days)'], filtered_df['Transmission (%) 2'], 'x', ms = 3, label = "Mask")
ax.plot(filtered_df['t (Days)'], filtered_df['Transmission (%) 3'], 'x', ms = 3, label = "Aperture + Mask")
ax.set_ylabel('Transmission Loss (%)')
ax.set_xlabel('Time (Days)')
ax.set_xlim(filtered_df['t (Days)'].iloc[0], filtered_df['t (Days)'].iloc[-1])
ax.set_ylim(min(filtered_df['Transmission (%)']) - transmission_range * 0.1,
max(filtered_df['Transmission (%)']) + transmission_range * 0.1)
ax.plot(time, fit_func(time, *fit_params)-1, label = "Batman Fit")
ax.legend()
transit_depth = fit_params[0] ** 2 * 100
print(f"Original Rp /Rs: {object_radius / sun_radius:.4f}")
print(f"Fitted Rp /Rs: {fit_params[0]:.4f}")
print(f"Transit Depth: {transit_depth:.4f}%")
formatted_coeffs = ', '.join([f'{coeff:.4f}' for coeff in fit_params[1:]])
print(f"Limb Darkening Coefficients: {formatted_coeffs}")