-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSRR_analysis.py
More file actions
376 lines (230 loc) · 10.7 KB
/
Copy pathPSRR_analysis.py
File metadata and controls
376 lines (230 loc) · 10.7 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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from scipy.optimize import curve_fit
# Planet regime
planet_size = 'Earth'
# Impact parameter
b = 0.25
# Time period
years = range(2012, 2021) # doesnt include 2021
dataframes = []
# Loop through each year and load the corresponding CSV
for year in years:
# Construct the file path
file_path = f"{planet_size}/b={b}/{year}.csv"
# Check if the file exists (optional, in case some years are missing)
if os.path.exists(file_path):
# Read the CSV and append to the list
df = pd.read_csv(file_path)
dataframes.append(df)
else:
print(f"File not found: {file_path}")
combined_df = pd.concat(dataframes, ignore_index=True)
#%% Plot all transit depths against wavelength
rows_as_arrays = []
# Loop through each row in the DataFrame
for _, row in combined_df.iterrows():
# Convert the row to a NumPy array and append it to the list
rows_as_arrays.append(row.values)
wavelengths = np.array([10833.15, 10832.13, 10831.00, 10830.30, 10829.60, 10828.47, 10827.45])
Rj_Rs = 69911 / 696340
Rn_Rs = 24622 / 696340
Re_Rs = 6371 / 696340
radius_ratio = Re_Rs
true_transit_depth = radius_ratio ** 2 *100 # as a percentage
fig, ax = plt.subplots(dpi=300)
for row in rows_as_arrays:
PSRR_values = np.array(row[2:9])
PSRR_error = np.array(row[9:])
transit_depth = PSRR_values ** 2 * 100
transit_depth_error_per = PSRR_error / PSRR_values * 2
transit_depth_error = transit_depth_error_per * transit_depth
ax.errorbar(wavelengths, transit_depth, yerr = transit_depth_error, fmt = 'o', ms = 4)
ax.set_xlabel('Wavelength (nm)')
ax.set_ylabel('Transit Depth (%)')
ax.set_title('Transit Depth (%) vs Wavelength')
ax.axhline(y = true_transit_depth, color = "red")
#%% Histograms
# PSRR extraction setup
PSRR_values_all = np.array([np.array(row[2:9]) for row in rows_as_arrays]) # Extract PSRR columns (columns 2-8)
transit_depth_all = PSRR_values_all ** 2 * 100
# Loop through each wavelength (0 to 6) and create a histogram for each corresponding PSRR values
for idx, wavelength in enumerate(wavelengths): # Looping through each wavelength index
# Extract the PSRR values corresponding to the current wavelength
extracted_values = [array[idx] for array in transit_depth_all] # Extract values at current index
# Plot the histogram for this wavelength's PSRR values
plt.figure(dpi=300)
plt.hist(extracted_values, bins=10, edgecolor='black', alpha=0.7) # Adjust bins as needed
plt.xlim(1.007, 1.016)
plt.axvline(x=true_transit_depth, color="red")
plt.xlabel(f'PSRR Value at Wavelength {wavelength} Å')
plt.ylabel('Frequency')
plt.title(f'{wavelength} Å 2012')
plt.grid(True)
plt.show()
#%% Fit Light Curves
def gaussian(x, amplitude, stddev, c):
mean = 10830.30
return amplitude * np.exp(-((x - mean) ** 2) / (2 * stddev ** 2)) + c
def linear(x, h):
return np.full_like(x, h)
wavelengths = np.array([10833.15, 10832.13, 10831.00, 10830.30, 10829.60, 10828.47, 10827.45])
p0 = [true_transit_depth * 0.002, 1., true_transit_depth]
p1 = [true_transit_depth]
lower_bound = [true_transit_depth * 0.9 - true_transit_depth, 0, true_transit_depth*0.995]
upper_bound = [true_transit_depth * 1.1 - true_transit_depth, 1, true_transit_depth*1.005]
results_dict = {}
for index, row in combined_df.iterrows():
Date = row[0]
date_fraction = row[1]
PSRR_values = np.array(row[2:9], dtype=np.float64)
PSRR_error = np.array(row[9:], dtype=np.float64)
transit_depth = PSRR_values ** 2 * 100
transit_depth_error_per = PSRR_error / PSRR_values * 2
transit_depth_error = transit_depth_error_per * transit_depth
fit_gaussian, cov_gaussian = curve_fit(gaussian, wavelengths, transit_depth, sigma = transit_depth_error,
bounds = (lower_bound, upper_bound))
fit_linear, cov_linear = curve_fit(linear, wavelengths, transit_depth, sigma = transit_depth_error, p0 = p1)
# gaussian_residuals = PSRR_values - gaussian(wavelengths, *fit_gaussian)
# linear_residuals = PSRR_values - linear(wavelengths, *fit_linear)
# linear_rms = np.sqrt(np.mean(linear_residuals ** 2))
background_values = np.concatenate((transit_depth[:2], transit_depth[-2:]))
background_stddev = np.std(background_values)
x_array = np.linspace(10827, 10834, 1000)
amplitude = abs(fit_gaussian[0])
middle_filter_res = PSRR_values[3] - gaussian(wavelengths[3], *fit_gaussian)
stddev_number = 3
# Choose the better model
if amplitude < background_stddev * stddev_number: # linear fit
fit = fit_linear
amplitude = 0
base = fit[0]
cov = cov_linear
error_amplitude = 0
error_base = np.sqrt(cov[0][0])
model = "linear"
fit_curve = linear(x_array, *fit)
else: # gaussiabn fit
fit = fit_gaussian
amplitude = fit[0]
base = fit[2]
cov = cov_gaussian
error_amplitude = np.sqrt(cov[0][0])
error_base = np.sqrt(cov[2][2])
model = "gaussian"
fit_curve = gaussian(x_array, *fit)
# plt.figure(dpi = 300)
# plt.errorbar(wavelengths, transit_depth, yerr = transit_depth_error,fmt = 'o', ms = 4)
# plt.plot(x_array, fit_curve)
# plt.title(f'{Date}')
# plt.xlabel('Wavelength (nm)')
# plt.ylabel('Transit Depth (%)')
# plt.axhline(y = true_transit_depth, color = "red")
results_dict[Date] = {"Date": Date,
"date fraction": date_fraction,
"model": model,
"amplitude": amplitude,
"amplitude error": error_amplitude,
"base": base,
"base error": error_base
}
if model == "gaussian":
print(f"Date: {Date} | Model: {model} | Amplitude: {fit[0]}")
else:
print(f"Date: {Date} | Model: {model} | Amplitude: 0")
#%%
def APENToS(x):
S = (x + 0.57) / 3.55
error = S * np.sqrt((0.01/0.57) ** 2 + (0.06/3.55) ** 2)
return S, error
df_A_PEN = pd.read_csv('A_PEN_time_series.csv')
# Convert results_dict to DataFrame
df_results = pd.DataFrame.from_dict(results_dict, orient="index")
# Reset index to make the date a column (optional)
df_results.reset_index(inplace=True)
df_results.drop(columns=["index"], inplace=True)
df_gaussian = df_results[df_results['model'] == 'gaussian']
merged_df = pd.merge(df_gaussian, df_A_PEN, on="Date", how="inner")
# Calculate the magnetic index and contamination
magnetic_index, magnetic_index_error = APENToS(merged_df['CaK'])
merged_df['S-index'] = magnetic_index
merged_df['S-index error'] = magnetic_index_error
merged_df = merged_df.sort_values(by='S-index', ascending=True).reset_index()
contamination = merged_df['amplitude'] / merged_df['base'] * 100
contamination_error = 2 * contamination * \
np.sqrt( (merged_df['amplitude error'] / merged_df['amplitude']) ** 2 +
(merged_df['base error'] / merged_df['base']) ** 2)
s_index = merged_df['S-index']
s_index_error = merged_df['S-index error']
# Create the scatter plot with a color bar based on the date fraction
# Create the colormap
cmap = plt.cm.viridis # Colormap
norm = plt.Normalize(np.min(merged_df['date fraction']), np.max(merged_df['date fraction'])) # Normalize the colormap
plt.figure(dpi=300)
scatter = plt.scatter(s_index, contamination,
c=merged_df['date fraction'],
cmap=cmap,
s=4)
errors_x = s_index_error
errors_y = abs(contamination_error)
# Plot error bars with colormap
for x, y, xerr, yerr, color_val in zip(s_index, contamination, errors_x, errors_y, merged_df['date fraction']):
plt.errorbar(x, y,
yerr=yerr,
fmt='none',
ecolor=cmap(norm(color_val)), # Map color based on the colormap
elinewidth=0.8, alpha=0.6)
# Add a color bar to the plot
cbar = plt.colorbar(scatter)
cbar.set_label("Year") # Label for the color bar
# Add labels to the plot
plt.xlabel("S-index")
plt.ylabel("Contamination (%)")
plt.title(f"Contamination vs S-index b={b}")
# Show the plot
plt.show()
#%% Moving average
df_gaussian_positive = df_gaussian[df_gaussian['amplitude'] > 0]
merged_df = pd.merge(df_gaussian_positive, df_A_PEN, on="Date", how="inner")
magnetic_index, magnetic_index_error = APENToS(merged_df['CaK'])
merged_df['S-index'] = magnetic_index
merged_df['S-index error'] = magnetic_index_error
merged_df = merged_df.sort_values(by='S-index', ascending=True).reset_index()
contamination = merged_df['amplitude'] / merged_df['base'] * 100
contamination_error = 2 * contamination * \
np.sqrt( (merged_df['amplitude error'] / merged_df['amplitude']) ** 2 +
(merged_df['base error'] / merged_df['base']) ** 2)
s_index = merged_df['S-index']
s_index_error = merged_df['S-index error']
# Define the chunk size (10 points)
chunk_size = 10
# Group the DataFrame by every 'chunk_size' number of rows and calculate the mean for each group
averaged_df = merged_df.groupby(merged_df.index // chunk_size).mean()
smoothed_s_index = averaged_df['S-index']
smoothed_contamination = (averaged_df["amplitude"] / averaged_df["base"]) * 100
plt.figure(dpi = 300)
scatter = plt.scatter(s_index, contamination,
c=merged_df['date fraction'],
cmap=cmap,
s=4)
errors_x = s_index_error
errors_y = abs(contamination_error)
# Plot error bars with colormap
for x, y, xerr, yerr, color_val in zip(s_index, contamination, errors_x, errors_y, merged_df['date fraction']):
plt.errorbar(x, y,
yerr=yerr,
fmt='none',
ecolor=cmap(norm(color_val)), # Map color based on the colormap
elinewidth=0.8, alpha=0.6)
plt.plot(smoothed_s_index, smoothed_contamination, color = "red")
# Add a color bar to the plot
cbar = plt.colorbar(scatter)
cbar.set_label("Year") # Label for the color bar
plt.ylim(0,10)
# Add labels to the plot
plt.xlabel("S-index")
plt.ylabel("Contamination (%)")
plt.title(f"Contamination vs S-index b={b}")
#%%