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

"""
Explanation: 'rv' 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
from phoebe import u # units

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('rv')
print(b.get_dataset(kind='rv', check_visible=False))

"""
Explanation: Dataset Parameters
Let's add an RV dataset to the Bundle (see also the rv 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_parameter(qualifier='times', component='primary'))

"""
Explanation: For information on the included passband-dependent parameters (not mentioned below), see the section on the lc dataset (these are used only to compute fluxes when rv_method is 'flux-weighted')
times
End of explanation
"""


b.set_value('times', component='primary', value=[0])

print(b.get_parameter(qualifier='rvs', component='primary'))

"""
Explanation: rvs
The rvs parameter is only visible if the respective times parameter is not empty.
End of explanation
"""


print(b.get_parameter(qualifier='sigmas', component='primary'))

"""
Explanation: sigmas
The sigmas parameter is also only visible if the respective times parameter is not empty.
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.get_compute())

"""
Explanation: Compute Options
Let's look at the compute options (for the default PHOEBE 2 backend) that relate to the RV dataset.
Other compute options are covered elsewhere:
* parameters related to dynamics are explained in the section on the orb dataset
* parameters related to meshing, eclipse detection, and subdivision (used if rv_method=='flux-weighted') are explained in the section on the mesh dataset
* parameters related to computing fluxes (used if rv_method=='flux-weighted') are explained in the section on the lc dataset
End of explanation
"""


print(b.get_parameter(qualifier='rv_method', component='primary'))

"""
Explanation: rv_method
End of explanation
"""


print(b.get_parameter(qualifier='rv_grav', component='primary'))

"""
Explanation: If rv_method is set to 'dynamical' then the computed radial velocities are simply the z-velocities of the centers of mass of each component.  In this case, only the dynamical options are relevant.  For more details on these, see the section on the orb dataset.
If rv_method is set to 'flux-weighted' then radial velocities are determined by the z-velocity of each visible surface element of the mesh, weighted by their respective intensities.  Since the stars are placed in their orbits by the dynamic options, the section on the orb dataset is still applicable.  So are the meshing options described in mesh dataset and the options for computing fluxes in lc dataset.  See also the Rossiter-McLaughlin example.
rv_grav
End of explanation
"""


b.set_value_all('times', phoebe.linspace(0,1,101))

b.run_compute(irrad_method='none')

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

print(b.get_parameter(qualifier='times', component='primary', kind='rv', context='model'))

print(b.get_parameter(qualifier='rvs', component='primary', kind='rv', context='model'))

"""
Explanation: See the Gravitational Redshift example for more details on the influence this parameter has on radial velocities.
Synthetics
End of explanation
"""


afig, mplfig = b.plot(show=True)

"""
Explanation: Plotting
By default, RV datasets plot as 'rvs' vs 'times'.
End of explanation
"""


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

"""
Explanation: Since these are the only two columns available in the synthetic model, the only other options is to plot in phase instead of time.
End of explanation
"""


print(b.filter(qualifier='period').components)

afig, mplfig = b.plot(x='phases:binary', show=True)

"""
Explanation: In system hierarchies where there may be multiple periods, it is also possible to determine whose period to use for phasing.
End of explanation
"""


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

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

b.set_value('columns', value=['rvs@rv01'])

b.run_compute(irrad_method='none')

print(b.get_model().datasets)

"""
Explanation: Mesh Fields
By adding a mesh dataset and setting the columns parameter, radial velocities per-element quantities can be exposed and plotted.  Since the radial velocities are flux-weighted, the flux-related quantities are also included (except relative intensities/luminosities that would require pblum scaling).  For a description of these, see the section on the lc dataset.
Let's add a mesh at the first time of the rv dataset and re-call run_compute
End of explanation
"""


print(b.filter(dataset='rv01', kind='mesh', context='model').twigs)

"""
Explanation: These new columns are stored with the rv's dataset tag, but with the mesh model-kind.
End of explanation
"""


afig, mplfig = b.filter(kind='mesh').plot(fc='rvs', ec='None', show=True)

"""
Explanation: Any of these columns are then available to use as edge or facecolors when plotting the mesh (see the section on the MESH dataset).
End of explanation
"""


print(b.get_parameter(qualifier='rvs', 
                      component='primary', 
                      dataset='rv01', 
                      kind='mesh', 
                      context='model'))

"""
Explanation: rvs
End of explanation
"""


