from __future__ import print_function
import os, urllib, numpy as np
%matplotlib inline

import sys
sys.path.append('../python')
import desc.sltimer

%load_ext autoreload
%autoreload 2

"""
Explanation: SLTimer Example Analysis of TDC2 Data
This notebook shows you how to find the estimation of a lens time delay from TDC2 light curve data using the PyCS code. For a detailed tutorial through the PyCS code, please visit this address.
First, we'll import SLTimer, as well as a few other important commands.
End of explanation
"""


timer = desc.sltimer.SLTimer()

url = "http://www.slac.stanford.edu/~pjm/LSST/DESC/SLTimeDelayChallenge/release/tdc2/gateway/tdc2-gateway-1.txt"
timer.download(url, and_read=True, format='tdc2')

timer.display_light_curves(jdrange=(59500,63100))

"""
Explanation: Data Munging
Now, let's start a timer object, and download some data to use. The output should show 1006 imported points if we are using the correct tdc2-gateway-1.txt file.
End of explanation
"""


# Time delays all set to zero:
# timer.initialize_time_delays(method=None)

# Draw time delays from the prior, using knowledge of H0 and the lens model:
# timer.initialize_time_delays(method='H0_prior', pars=[70.0, 7.0])

# "Guess" the time delays - for testing, let's try something close to the true value:
timer.initialize_time_delays(method='guess', pars={'AB':55.0})

"""
Explanation: Initialization
The TDC2 light curve data files have headers that contain some lens model information: the Fermat potential differences between the image pairs. These are related to the time delays, by a cosmological distance factor $Q$ and the Hubble constant $H_0$. A broad Gaussian prior on $H_0$ will translate to an approximately Gaussian prior on the time delays. Let's draw from this prior to help initialize the time delays in our model.
End of explanation
"""


timer.estimate_time_delays(method='pycs', microlensing='spline', agn='spline', error=None, quietly=True)
timer.report_time_delays()

timer.display_light_curves(jdrange=(59500,63100))

"""
Explanation: Spline Modeling
We're now ready to analyze this data. We'll start with it as-is, and then later try "whitening" it.
The following lines will run an entire free-knot spline technique on your data with a complete error analysis using the TDC2 method. Below, you can specify how the time delays will be analyzed. The default is listed below according to the PyCS tutorial. See the bottom of the page for alternate methods.
End of explanation
"""


# timer.estimate_uncertainties(n=3,npkl=5)

"""
Explanation: While the time delays have been estimated, we can see that the different images' light curves are not shifted and microlensing-corrected terribly well.
End of explanation
"""


wtimer = desc.sltimer.SLTimer()
wtimer.download(url, and_read=True, format='tdc2')

wtimer.whiten()

"""
Explanation: Whitening the Light Curves
In the above analysis we ignored the fact that the magnitudes were measured in 6 different filters, and just used them all as if they were from the same filter. By offsetting the light curves to a common mean, we should get a set of points that look more like they were taken in one filter. This process is known as "whitening."
End of explanation
"""


wtimer.display_light_curves(jdrange=(59500,63100))

wtimer.initialize_time_delays(method='guess', pars={'AB':55.0})
wtimer.estimate_time_delays(method='pycs', microlensing='spline', agn='spline', error=None, quietly=True)

wtimer.display_light_curves(jdrange=(59500,63100))

"""
Explanation: The change brought about by whitening is pretty subtle: the means of each image's light curve stay the same (by design), but the scatter in each image's light curve is somewhat reduced.
End of explanation
"""


wtimer.report_time_delays()

"""
Explanation: How does the plot compare? Note that the y axis scale is different: the shifted light curves seem to match up better now.
Now let's look at the estimated time delays - by how much do they differ, between the unwhitened and whitened data?
Whitened:
End of explanation
"""


timer.report_time_delays()

"""
Explanation: Unwhitened:
End of explanation
"""


truthurl = "http://www.slac.stanford.edu/~pjm/LSST/DESC/SLTimeDelayChallenge/release/tdc2/gateway/gatewaytruth.txt"
truthfile = truthurl.split('/')[-1]
if not os.path.isfile(truthfile):
    urllib.urlretrieve(truthurl, truthfile)
d = np.loadtxt(truthfile).transpose()
truth = d[0]
print("True Time Delays:", truth[0])

"""
Explanation: Truth:
End of explanation
"""


