#!pip install -I "phoebe>=2.3,<2.4"

"""
Explanation: Comparing Contacts Binaries in PHOEBE 2 vs PHOEBE Legacy
NOTE: PHOEBE 1.0 legacy is an alternate backend and is not installed with PHOEBE 2.  In order to run this backend, you'll need to have PHOEBE 1.0 installed and manually install the python wrappers in the phoebe-py directory.
Setup
Let's first make sure we have the latest version of PHOEBE 2.3 installed (uncomment this line if running in an online notebook session such as colab).
End of explanation
"""


import phoebe
from phoebe import u
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary(contact_binary=True)
b['q'] = 0.7

"""
Explanation: As always, let's do imports and initialize a logger and a new bundle.
End of explanation
"""


b.add_dataset('lc', times=np.linspace(0,1,101), dataset='lc01')
b.add_dataset('rv', times=np.linspace(0,1,101), dataset='rv01')

"""
Explanation: Adding Datasets and Compute Options
End of explanation
"""


b.add_compute('legacy')

"""
Explanation: Now we add compute options for the 'legacy' backend.
End of explanation
"""


b.set_value_all('atm', 'extern_planckint')

"""
Explanation: Let's use the external atmospheres available for both phoebe1 and phoebe2
End of explanation
"""


b.set_value_all('gridsize', 30)

"""
Explanation: Set value of gridsize for the trapezoidal (WD) mesh.
End of explanation
"""


b.set_value_all('ld_mode', 'manual')
b.set_value_all('ld_func', 'logarithmic')
b.set_value_all('ld_coeffs', [0.0, 0.0])

b.set_value_all('rv_grav', False)
b.set_value_all('ltte', False)

"""
Explanation: Let's also disable other special effect such as heating, gravity, and light-time effects.
End of explanation
"""


b.run_compute(kind='phoebe', model='phoebe2model', irrad_method='none')

b.run_compute(kind='legacy', model='phoebe1model', irrad_method='none')

"""
Explanation: Finally, let's compute our models
End of explanation
"""


afig, mplfig = b.filter(dataset='lc01').plot(c={'phoebe2model': 'g', 'phoebe1model': 'r'}, linestyle='solid', 
                                             legend=True, show=True)

"""
Explanation: Plotting
Light Curve
End of explanation
"""


artist, = plt.plot(b.get_value('fluxes@lc01@phoebe2model') - b.get_value('fluxes@lc01@phoebe1model'), 'g-')
artist = plt.axhline(0.0, linestyle='dashed', color='k')

"""
Explanation: Now let's plot the residuals between these two models
End of explanation
"""


afig, mplfig = b['rv01'].plot(c={'phoebe2model': 'g', 'phoebe1model': 'r'}, linestyle='solid', 
                              legend=True, show=True)

artist, = plt.plot(b.get_value('rvs@primary@phoebe2model', ) - b.get_value('rvs@primary@phoebe1model'), color='g', ls=':')
artist, = plt.plot(b.get_value('rvs@secondary@phoebe2model') - b.get_value('rvs@secondary@phoebe1model'), color='g', ls='-.')
artist = plt.axhline(0.0, linestyle='dashed', color='k')
ylim = plt.ylim(-0.3, 0.3)

"""
Explanation: RVs
End of explanation
"""


