# -*- coding: utf-8 -*-
# NOTE: ortega-exposure.html inlines this source in its <details> source viewer
# so search engines and noscript visitors see it. Keep the two in sync.
"""Exposure Time Calculator

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/15I6EacWttQ_XdIF9ssj-YXpEuKztZjb8
"""



# Commented out IPython magic to ensure Python compatibility.
# %matplotlib notebook
import numpy as np
from astropy.table import Table, Column
import matplotlib.pyplot as plt

Plate_scale = 0.00015625  # plate scale in rad / mm
Pixel_diamter = .0267 # mm/pixel
Plate_scale_relpix = Plate_scale * Pixel_diamter # plate scale in radians / pixel
readnoise = 2.9 # readnoise in units of e- / pix second

filter_names = np.array(['U','B','V','R','I']) #Johnson-Cousin filter names
filter_wav = np.array([3656, 4353, 5477, 6349, 8797]) # central wavenlength of the corresponding filter in Angstroms

seeing = 1.25 #@param {type:"number"}
seeing_disk = seeing
seeing_pix= ((((seeing_disk / 60) / 60) / 57.3) / (Plate_scale * Pixel_diamter))

lunar_age_data = {
    '0': np.array([22.0, 22.7, 21.8, 20.9, 19.9]),
    '3': np.array([21.5, 22.4, 21.7, 20.8, 19.9]),
    '7': np.array([19.9, 21.6, 21.4, 20.6, 19.7]),
    '10': np.array([18.5, 20.7, 20.7, 20.3, 19.5]),
    '14': np.array([17.0, 19.5, 20.0, 19.9, 19.2])
}
Lunar_Age = '7' #@param ["0", "3", "7", "10", "14"]
Chosen_age = lunar_age_data[Lunar_Age]

#array of counts for an object with mag = 15 in each U, B, V, R, I filters
N15_count = np.array([400, 2600, 2400, 2300, 1600])
# (10**(M1-M2)/(-2.5))*Fm15 = F1
N15_back = 10**((Chosen_age-15)/(-2.5))*N15_count

#@title Select the filter
filter_choice = 'I' #@param ["U", "B", "V", "R", "I"]

#Determine the array index for the selected filter
arr_index = np.where(filter_names == filter_choice)

sky_count = N15_back[arr_index] # Sky counts for the respective filter
ref_count = N15_count[arr_index] # Counts for a 15 magnitude star in the respective filter

#@title Enter value for the signal-to-noise ratio
snr = 125 #@param {type:"integer"}

#@title Enter value for the magnitude, mag
mag = 19.7 #@param {type:"number"}

# Create an array for the extinction coefficient
k_lam = np.array([.6, .4, .2, .1, .08])

# Correct the magnitude  for airmass = 1
mag_obs = mag + k_lam[arr_index]
# Convert the magnitude into counts using the respective reference filter magnitude
star_count = ref_count * 10**((mag_obs - 15.0) / (-2.5))

#Quadratic equation terms defined above.
a = star_count**2
b = (-1.0)*(snr**2)*(star_count + (seeing_pix*sky_count))
c = (-1.0)*(snr**2)*seeing_pix*readnoise

#Exposure time using the solution to th quadratic equation
exp_time = ((-1.0)*b + np.sqrt((b**2) - (4.0*a*c)))/(2*a)

print("The exposure time is",np.round(exp_time)[0], "seconds") #rounds the exposure to the nearest second
print(N15_back)
print(Chosen_age)
