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

"""
Explanation: Advanced: mask_phases and mask_enabled
Setup
Let's first make sure we have the latest version of PHOEBE 2.4 installed (uncomment this line if running in an online notebook session such as colab).
End of explanation
"""


import phoebe
from phoebe import u # units
import numpy as np

b = phoebe.default_binary()

"""
Explanation: Let's get started with some basic imports.
End of explanation
"""


b = phoebe.default_binary()
b.add_dataset('lc', times=phoebe.linspace(0,5,1001))
b.run_compute()

times = b.get_value('times@model')
fluxes = b.get_value('fluxes@model')
sigmas = np.ones_like(times) * 0.01

b = phoebe.default_binary()
b.add_dataset('lc', compute_phases=phoebe.linspace(0,1,101),
              times=times, fluxes=fluxes, sigmas=sigmas,
              dataset='lc01')

"""
Explanation: And then we'll build a synthetic "dataset" and initialize a new bundle with those data
End of explanation
"""


b.get_parameter(qualifier='mask_enabled', dataset='lc01', context='dataset')

print(b.get_parameter(qualifier='mask_enabled', dataset='lc01', context='dataset').description)

"""
Explanation: mask_enabled and mask_phases parameters
Each dataset will have a mask_enabled parameter and, if mask_enabled=True, a mask_phases parameter.
End of explanation
"""


b.get_parameter(qualifier='mask_phases', dataset='lc01', context='dataset')

print(b.get_parameter(qualifier='mask_phases', dataset='lc01', context='dataset').description)

"""
Explanation: This is True by default, but mask_phases is set to an empty list, meaning that no masking will actually be applied.  This mask_enabled parameter just allows for setting a mask via mask_phases and then toggling that mask on and off without having to reset mask_phases.
End of explanation
"""


_ = b.plot(show=True)

_ = b.plot(x='phases', show=True)

"""
Explanation: We can now plot the observations to see the unmasked (fake) data.
End of explanation
"""


b.set_value('mask_phases', [(-0.1, 0.1), (0.4,-0.4)])

_ = b.plot(show=True)

_ = b.plot(x='phases', show=True)

"""
Explanation: If we wanted to only include the eclipses, we could set mask_phases as a list of tuples, where each tuple contains the phase range that we want to keep.
End of explanation
"""


b.run_compute()

_ = b.plot(x='phases', show=True, marker={'model': '+'})

b.set_value('compute_phases', [])
b.run_compute()

_ = b.plot(x='phases', show=True, marker={'model': '+'})

"""
Explanation: Note that b.run_compute, computes the forward model ignoring that mask, whether compute_phases is set or not.
End of explanation
"""


print(b.get_parameter('phases_t0').description)

print(b.get_parameter('phases_t0').choices)

"""
Explanation: However, when calling b.run_solver, the mask will be applied when determining what times to compute the forward-model (see Advanced: Solver Times) and within the cost-function (see calculate_residuals,  calculate_chi2, and calculate_lnlikelihood).
Phasing Options
Like the constraint between compute times and phases, phase-masking adheres to the per-dataset phases_t0 parameter to set which of the various t0s to use when mapping between time and phase.
And in the case of time-dependent systems, these will also adhere to phases_dpdt (in the case of dpdt and/or phases_period (in the case of apsidal motion (dperdt).
End of explanation
"""


