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

"""
Explanation: 'lp' (Line Profile) Datasets and Options
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

logger = phoebe.logger()

b = phoebe.default_binary()

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


b.add_dataset('lp', times=[0,1,2], wavelengths=phoebe.linspace(549, 551, 101))
print(b.get_dataset(kind='lp', check_visible=False))

"""
Explanation: Dataset Parameters
Line profiles have an extra dimension than LC and RV datasets which have times as their independent variable.  For that reason, the parameters in the LP dataset are tagged with individual times instead of having a separate times array.  This allows the flux_densities and sigmas to be per-time.  Because of this, times is not a parameter, but instead must be passed when you call b.add_dataset if you want to attach actual line-profile data (flux_densities) to your dataset.  At that point, in order to change the times you would need to remove and re-add the dataset.  If you only want to compute synthetic line profiles, use compute_times or compute_phases instead.
Let's add a line profile dataset to the Bundle (see also the lp API docs).  Some parameters are only visible based on the values of other parameters, so we'll pass check_visible=False (see the filter API docs for more details).  These visibility rules will be explained below.
End of explanation
"""


print(b.get_dataset(kind='lp').times)

"""
Explanation: For information on the included passband-dependent parameters (not mentioned below), see the section on the lc dataset.
times
End of explanation
"""


print(b.get_parameter(qualifier='compute_times'))

print(b.get_parameter(qualifier='compute_phases', context='dataset'))

print(b.get_parameter(qualifier='phases_t0'))

"""
Explanation: compute_times / compute_phases
See the Compute Times & Phases tutorial.
End of explanation
"""


print(b.filter(qualifier='wavelengths'))

print(b.get_parameter(qualifier='wavelengths', component='binary'))

"""
Explanation: wavelengths
End of explanation
"""


print(b.filter(qualifier='flux_densities'))

print(b.get_parameter(qualifier='flux_densities', 
                      component='binary',
                      time=0.0))

"""
Explanation: components
Line profiles will be computed for each component in which the wavelengths are provided.  If we wanted to expose the line profile for the binary as a whole, we'd set the wavelenghts for wavelengths@binary.  If instead we wanted to expose per-star line profiles, we could set the wavelengths for both wavelengths@primary and wavelengths@secondary.
If you're passing wavelengths to the b.add_dataset call, it will default to filling the wavelengths at the system-level.  To override this, pass components=['primary', 'secondary'], as well.  For example: b.add_dataset('lp', wavelengths=np.linspace(549,551,101), components=['primary', 'secondary']).
flux_densities
Note that observation flux_densities parameters are exposed per-time, according to the value of times passed to add_dataset.
flux_densities parameters will be exposed in the model based on compute_times/compute_phases if not empty, otherwise according to times.  For more information, see the Compute Times & Phases tutorial.
End of explanation
"""


print(b.filter(qualifier='sigmas'))

print(b.get_parameter(qualifier='sigmas', 
                      component='binary',
                      time=0))

"""
Explanation: sigmas
Note that, like flux_densities, sigmas parameters are exposed per-time, according to the value of times passed to add_dataset.
End of explanation
"""


print(b.get_parameter(qualifier='profile_func'))

"""
Explanation: profile_func
End of explanation
"""


print(b.get_parameter(qualifier='profile_rest'))

"""
Explanation: profile_rest
End of explanation
"""


print(b.get_parameter(qualifier='profile_sv'))

"""
Explanation: profile_sv
End of explanation
"""


b.run_compute(irrad_method='none')

print(b.filter(context='model').twigs)

"""
Explanation: Synthetics
End of explanation
"""


print(b.filter(qualifier='flux_densities', context='model'))

print(b.get_parameter(qualifier='flux_densities', context='model', time=0))

"""
Explanation: The model for a line profile dataset will expose flux-densities at each time and for each component where the corresponding wavelengths Parameter was not empty.  Here since we used the default and exposed line-profiles for the entire system, we have a single entry per-time.
End of explanation
"""


afig, mplfig = b.filter(dataset='lp01', context='model', time=0).plot(show=True)

"""
Explanation: Plotting
By default, LP datasets plot as 'flux_densities' vs 'wavelengths' for a single time.
End of explanation
"""


b.add_dataset('mesh', times=[0], dataset='mesh01')

print(b.get_parameter(qualifier='columns').choices)

"""
Explanation: Mesh Fields
Let's add a single mesh and see which columns from the line profile dataset are available to expose as a column in the mesh.
End of explanation
"""


