<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Passband Luminosity
Setup
Let's first make sure we have the latest version of PHOEBE 2.2 installed. (You can comment out this line if you don't use pip for your installation or don't want to update to the latest release).
Step1:  As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
Step2:  And we'll add a single light curve dataset so that we can see how passband luminosities affect the resulting synthetic light curve model.
Step3:  Lastly, just to make things a bit easier and faster, we'll turn off irradiation (reflection), use blackbody atmospheres, and disable limb-darkening (so that we can play with weird temperatures without having to worry about falling of the grids).
Step4:  Relevant Parameters & Methods
NEW in PHOEBE 2.2
Step5:  For any of these modes, you can expose the intrinsic (excluding extrinsic effects such as spots and irradiation) and extrinsic computed luminosities of each star (in each dataset) by calling b.compute_pblums.
Note that as its an aspect-dependent effect, boosting is ignored in all of these output values.
Step6:  For more details, see the section below on "Accessing Model Luminosities" as well as the b.compute_pblums API docs
pblum_mode = 'component-coupled'
pblum_mode='component-coupled' is the default option and maintains the default behavior from previous releases.  Here the user provides passband luminosities for a single star in the system for the given dataset/passband, and all other stars are scaled accordingly.
By default, the value of pblum is set for the primary star in the system, but we can instead provide pblum for the secondary star by changing the value of pblum_component.
Step7:  Note that in general (for the case of a spherical star), a pblum of 4pi will result in an out-of-eclipse flux of ~1.
Now let's just reset to the default case where the primary star has a provided (default) pblum of 4pi.
Step8:  NOTE
Step9:  Let's see how changing the value of pblum affects the computed light curve.  By default, pblum is set to be 4 pi, giving a total flux for the primary star of ~1.
Since the secondary star in the default binary is identical to the primary star, we'd expect an out-of-eclipse flux of the binary to be ~2.
Step10:  If we now set pblum to be only 2 pi, we should expect the luminosities as well as entire light curve to be scaled in half.
Step11:  And if we halve the temperature of the secondary star - the resulting light curve changes to the new sum of fluxes, where the primary star dominates since the secondary star flux is reduced by a factor of 16, so we expect a total out-of-eclipse flux of ~0.5 + ~0.5/16 = ~0.53.
Step12:  Let us undo our changes before we look at decoupled luminosities.
Step13:  pblum_mode = 'decoupled'
The luminosities are decoupled when pblums are provided for the individual components.  To accomplish this, set pblum_mode to 'decoupled'.
Step14:  Now we see that both pblum parameters are available and can have different values.
Step15:  If we set these to 4pi, then we'd expect each star to contribute 1.0 in flux units, meaning the baseline of the light curve should be at approximately 2.0
Step16:  Now let's make a significant temperature-ratio by making a very cool secondary star.  Since the luminosities are decoupled - this temperature change won't affect the resulting light curve very much (compare this to the case above with coupled luminosities).  What is happening here is that even though the secondary star is cooler, its luminosity is being rescaled to the same value as the primary star, so the eclipse depth doesn't change (you would see a similar lack-of-effect if you changed the radii - although in that case the eclipse widths would still change due to the change in geometry).
Step17:  In most cases you will not want decoupled luminosities as they can easily break the self-consistency of your model.
Now we'll just undo our changes before we look at accessing model luminosities.
Step18:  pblum_mode = 'absolute'
By setting pblum_mode to 'absolute', luminosities and fluxes will be returned in absolute units and not rescaled.  Note that third light and distance will still affect the resulting flux levels.
Step19:  As we no longer provide pblum values to scale, those parameters are not visible when filtering.
Step20:  (note the exponent on the y-axis of the above figure)
pblum_mode = 'dataset-scaled'
Setting pblum_mode to 'dataset-sclaed' is only allowed if fluxes are attached to the dataset itself.  Let's use our existing model to generate "fake" data and then populate the dataset.
Step21:  Now if we set pblum_mode to 'dataset-scaled', the resulting model will be scaled to best fit the data.  Note that in this mode we cannot access computed luminosities via b.compute_pblums (which would raise an error if we attempted to do so), nor can we access scaled intensities from the mesh.
Step22:  Before moving on, let's remove our fake data (and reset pblum_mode or else PHOEBE will complain about the lack of data).
Step23:  pblum_mode = 'dataset-coupled'
Setting pblum_mode to 'dataset-coupled' allows for the same scaling factor to be applied to two different datasets.  In order to see this in action, we'll add another LC dataset in a different passband.
Step24:  Here we see the pblum_mode@lc01 is set to 'component-coupled' meaning it will follow the rules described earlier where pblum is provided for the primary component and the secondary is coupled to that.  pblum_mode@lc02 is set to 'dataset-coupled' with pblum_dataset@lc01 pointing to 'lc01'.
Step25:  Accessing Model Luminosities
Passband luminosities at t0@system per-star (including following all coupling logic) can be computed and exposed on the fly by calling compute_pblums.
Step26:  By default this exposes 'pblum' and 'pblum_ext' for all component-dataset pairs in the form of a dictionary.  Alternatively, you can pass a label or list of labels to component and/or dataset.
Step27:  For more options, see the b.compute_pblums API docs.
Note that this same logic is applied (at t0) to initialize all passband luminosities within the backend, so there is no need to call compute_pblums before run_compute.
In order to access passband luminosities at times other than t0, you can add a mesh dataset and request the pblum_ext column to be exposed.  For stars that have pblum defined (as opposed to coupled to another star or dataset), this value should be equivalent to the value of the parameter (at t0 if no features or irradiation are present, and in simple circular cases will probably be equivalent at all times).
Let's create a mesh dataset at a few times and then access the synthetic luminosities.
Step28:  Since the luminosities are passband-dependent, they are stored with the same dataset as the light curve (or RV), but with the mesh method, and are available at each of the times at which a mesh was stored.
Step29:  Now let's compare the value of the synthetic luminosities to those of the input pblum
Step30:  In this case, since our two stars are identical, the synthetic luminosity of the secondary star should be the same as the primary (and the same as pblum@primary).
Step31:  However, if we change the temperature of the secondary star again, since the pblums are coupled, we'd expect the synthetic luminosity of the primary to remain fixed but the secondary to decrease.
Step32:  And lastly, if we re-enable irradiation, we'll see that the extrinsic luminosities do not match the prescribed value of pblum (an intrinsic luminosity).
Step33:  Now, we'll just undo our changes before continuing
Step34:  Role of Pblum
Let's now look at the intensities in the mesh to see how they're being scaled under-the-hood.  First we'll recompute our model with the equal temperatures and irradiation disabled (to ignore the difference between pblum and pblum_ext).
Step35:  'abs_normal_intensities' are the intensities per triangle in absolute units, i.e. W/m^3.
Step36:  The values of 'normal_intensities', however, are significantly samller (in this case).  These are the intensities in relative units which will eventually be integrated to give us flux for a light curve.
Step37:  'normal_intensities' are scaled from 'abs_normal_intensities' so that the computed luminosity matches the prescribed luminosity (pblum).
Here we compute the luminosity by summing over each triangle's intensity in the normal direction, and multiply it by pi to account for blackbody intensity emitted in all directions in the solid angle, and by the area of that triangle.
<ASSISTANT_TASK:>
Python Code:
!pip install -I "phoebe>=2.2,<2.3"
Explanation: Passband Luminosity
Setup
Let's first make sure we have the latest version of PHOEBE 2.2 installed. (You can comment out this line if you don't use pip for your installation or don't want to update to the latest release).
End of explanation
%matplotlib inline
import phoebe
from phoebe import u # units
import numpy as np
logger = phoebe.logger()
b = phoebe.default_binary()
Explanation: As always, let's do imports and initialize a logger and a new bundle.  See Building a System for more details.
End of explanation
b.add_dataset('lc', times=phoebe.linspace(0,1,101), dataset='lc01')
Explanation: And we'll add a single light curve dataset so that we can see how passband luminosities affect the resulting synthetic light curve model.
End of explanation
b.set_value('irrad_method', 'none')
b.set_value_all('ld_mode', 'manual')
b.set_value_all('ld_func', 'linear')
b.set_value_all('ld_coeffs', [0.])
b.set_value_all('ld_mode_bol', 'manual')
b.set_value_all('ld_func_bol', 'linear')
b.set_value_all('ld_coeffs_bol', [0.])
b.set_value_all('atm', 'blackbody')
Explanation: Lastly, just to make things a bit easier and faster, we'll turn off irradiation (reflection), use blackbody atmospheres, and disable limb-darkening (so that we can play with weird temperatures without having to worry about falling of the grids).
End of explanation
print(b.get_parameter(qualifier='pblum_mode', dataset='lc01'))
Explanation: Relevant Parameters & Methods
NEW in PHOEBE 2.2: A pblum_mode parameter exists for each LC dataset in the bundle.  This parameter defines how passband luminosities are handled.  The subsections below describe the use and parameters exposed depening on the value of this parameter.
End of explanation
print(b.compute_pblums())
Explanation: For any of these modes, you can expose the intrinsic (excluding extrinsic effects such as spots and irradiation) and extrinsic computed luminosities of each star (in each dataset) by calling b.compute_pblums.
Note that as its an aspect-dependent effect, boosting is ignored in all of these output values.
End of explanation
print(b.filter(qualifier='pblum'))
print(b.get_parameter(qualifier='pblum_component'))
b.set_value('pblum_component', 'secondary')
print(b.filter(qualifier='pblum'))
Explanation: For more details, see the section below on "Accessing Model Luminosities" as well as the b.compute_pblums API docs
pblum_mode = 'component-coupled'
pblum_mode='component-coupled' is the default option and maintains the default behavior from previous releases.  Here the user provides passband luminosities for a single star in the system for the given dataset/passband, and all other stars are scaled accordingly.
By default, the value of pblum is set for the primary star in the system, but we can instead provide pblum for the secondary star by changing the value of pblum_component.
End of explanation
b.set_value('pblum_component', 'primary')
print(b.get_parameter(qualifier='pblum', component='primary'))
Explanation: Note that in general (for the case of a spherical star), a pblum of 4pi will result in an out-of-eclipse flux of ~1.
Now let's just reset to the default case where the primary star has a provided (default) pblum of 4pi.
End of explanation
print(b.compute_pblums())
Explanation: NOTE: other parameters also affect flux-levels, including limb darkening, third light, boosting, irradiation, and distance
If we call b.compute_pblums, we'll see that the computed intrinsic luminosity of the primary star (pblum@primary@lc01) matches the value of the parameter above.
End of explanation
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: Let's see how changing the value of pblum affects the computed light curve.  By default, pblum is set to be 4 pi, giving a total flux for the primary star of ~1.
Since the secondary star in the default binary is identical to the primary star, we'd expect an out-of-eclipse flux of the binary to be ~2.
End of explanation
b.set_value('pblum', component='primary', value=2*np.pi)
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: If we now set pblum to be only 2 pi, we should expect the luminosities as well as entire light curve to be scaled in half.
End of explanation
b.set_value('teff', component='secondary', value=0.5 * b.get_value('teff', component='primary'))
print(b.filter(qualifier='teff'))
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: And if we halve the temperature of the secondary star - the resulting light curve changes to the new sum of fluxes, where the primary star dominates since the secondary star flux is reduced by a factor of 16, so we expect a total out-of-eclipse flux of ~0.5 + ~0.5/16 = ~0.53.
End of explanation
b.set_value_all('teff', 6000)
b.set_value_all('pblum', 4*np.pi)
Explanation: Let us undo our changes before we look at decoupled luminosities.
End of explanation
b.set_value('pblum_mode', 'decoupled')
Explanation: pblum_mode = 'decoupled'
The luminosities are decoupled when pblums are provided for the individual components.  To accomplish this, set pblum_mode to 'decoupled'.
End of explanation
print(b.filter(qualifier='pblum'))
Explanation: Now we see that both pblum parameters are available and can have different values.
End of explanation
b.set_value_all('pblum', 4*np.pi)
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: If we set these to 4pi, then we'd expect each star to contribute 1.0 in flux units, meaning the baseline of the light curve should be at approximately 2.0
End of explanation
print(b.filter(qualifier='teff'))
b.set_value('teff', component='secondary', value=3000)
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: Now let's make a significant temperature-ratio by making a very cool secondary star.  Since the luminosities are decoupled - this temperature change won't affect the resulting light curve very much (compare this to the case above with coupled luminosities).  What is happening here is that even though the secondary star is cooler, its luminosity is being rescaled to the same value as the primary star, so the eclipse depth doesn't change (you would see a similar lack-of-effect if you changed the radii - although in that case the eclipse widths would still change due to the change in geometry).
End of explanation
b.set_value_all('teff', 6000)
b.set_value_all('pblum', 4*np.pi)
Explanation: In most cases you will not want decoupled luminosities as they can easily break the self-consistency of your model.
Now we'll just undo our changes before we look at accessing model luminosities.
End of explanation
b.set_value('pblum_mode', 'absolute')
Explanation: pblum_mode = 'absolute'
By setting pblum_mode to 'absolute', luminosities and fluxes will be returned in absolute units and not rescaled.  Note that third light and distance will still affect the resulting flux levels.
End of explanation
print(b.filter(qualifier='pblum'))
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: As we no longer provide pblum values to scale, those parameters are not visible when filtering.
End of explanation
fluxes = b.get_value('fluxes', context='model') * 0.8 + (np.random.random(101) * 0.1)
b.set_value('fluxes', context='dataset', value=fluxes)
afig, mplfig = b.plot(context='dataset', show=True)
Explanation: (note the exponent on the y-axis of the above figure)
pblum_mode = 'dataset-scaled'
Setting pblum_mode to 'dataset-sclaed' is only allowed if fluxes are attached to the dataset itself.  Let's use our existing model to generate "fake" data and then populate the dataset.
End of explanation
b.set_value('pblum_mode', 'dataset-scaled')
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True)
Explanation: Now if we set pblum_mode to 'dataset-scaled', the resulting model will be scaled to best fit the data.  Note that in this mode we cannot access computed luminosities via b.compute_pblums (which would raise an error if we attempted to do so), nor can we access scaled intensities from the mesh.
End of explanation
b.set_value('pblum_mode', 'component-coupled')
b.set_value('fluxes', context='dataset', value=[])
Explanation: Before moving on, let's remove our fake data (and reset pblum_mode or else PHOEBE will complain about the lack of data).
End of explanation
b.add_dataset('lc', times=phoebe.linspace(0,1,101), 
              ld_mode='manual', ld_func='linear', ld_coeffs=[0],
              passband='Johnson:B', dataset='lc02')
b.set_value('pblum_mode', dataset='lc02', value='dataset-coupled')
Explanation: pblum_mode = 'dataset-coupled'
Setting pblum_mode to 'dataset-coupled' allows for the same scaling factor to be applied to two different datasets.  In order to see this in action, we'll add another LC dataset in a different passband.
End of explanation
print(b.filter('pblum*'))
print(b.compute_pblums())
b.run_compute()
afig, mplfig = b.plot(show=True, legend=True)
Explanation: Here we see the pblum_mode@lc01 is set to 'component-coupled' meaning it will follow the rules described earlier where pblum is provided for the primary component and the secondary is coupled to that.  pblum_mode@lc02 is set to 'dataset-coupled' with pblum_dataset@lc01 pointing to 'lc01'.
End of explanation
print(b.compute_pblums())
Explanation: Accessing Model Luminosities
Passband luminosities at t0@system per-star (including following all coupling logic) can be computed and exposed on the fly by calling compute_pblums.
End of explanation
print(b.compute_pblums(dataset='lc01', component='primary'))
Explanation: By default this exposes 'pblum' and 'pblum_ext' for all component-dataset pairs in the form of a dictionary.  Alternatively, you can pass a label or list of labels to component and/or dataset.
End of explanation
b.add_dataset('mesh', times=np.linspace(0,1,5), dataset='mesh01', columns=['areas', 'pblum_ext@lc01', 'ldint@lc01', 'ptfarea@lc01', 'abs_normal_intensities@lc01', 'normal_intensities@lc01'])
b.run_compute()
Explanation: For more options, see the b.compute_pblums API docs.
Note that this same logic is applied (at t0) to initialize all passband luminosities within the backend, so there is no need to call compute_pblums before run_compute.
In order to access passband luminosities at times other than t0, you can add a mesh dataset and request the pblum_ext column to be exposed.  For stars that have pblum defined (as opposed to coupled to another star or dataset), this value should be equivalent to the value of the parameter (at t0 if no features or irradiation are present, and in simple circular cases will probably be equivalent at all times).
Let's create a mesh dataset at a few times and then access the synthetic luminosities.
End of explanation
print(b.filter(qualifier='pblum_ext', context='model').twigs)
Explanation: Since the luminosities are passband-dependent, they are stored with the same dataset as the light curve (or RV), but with the mesh method, and are available at each of the times at which a mesh was stored.
End of explanation
t0 = b.get_value('t0@system')
print(b.get_value(qualifier='pblum_ext', time=t0, component='primary', kind='mesh', context='model'))
print(b.get_value('pblum@primary@dataset'))
print(b.compute_pblums(component='primary', dataset='lc01'))
Explanation: Now let's compare the value of the synthetic luminosities to those of the input pblum
End of explanation
print(b.get_value(qualifier='pblum_ext', time=t0, component='primary', kind='mesh', context='model'))
print(b.get_value(qualifier='pblum_ext', time=t0, component='secondary', kind='mesh', context='model'))
Explanation: In this case, since our two stars are identical, the synthetic luminosity of the secondary star should be the same as the primary (and the same as pblum@primary).
End of explanation
b['teff@secondary@component'] = 3000
print(b.compute_pblums(dataset='lc01'))
b.run_compute()
print(b.get_value(qualifier='pblum_ext', time=t0, component='primary', kind='mesh', context='model'))
print(b.get_value(qualifier='pblum_ext', time=t0, component='secondary', kind='mesh', context='model'))
Explanation: However, if we change the temperature of the secondary star again, since the pblums are coupled, we'd expect the synthetic luminosity of the primary to remain fixed but the secondary to decrease.
End of explanation
print(b['ld_mode'])
print(b['atm'])
b.run_compute(irrad_method='horvat')
print(b.get_value(qualifier='pblum_ext', time=t0, component='primary', kind='mesh', context='model'))
print(b.get_value('pblum@primary@dataset'))
print(b.compute_pblums(dataset='lc01', irrad_method='horvat'))
Explanation: And lastly, if we re-enable irradiation, we'll see that the extrinsic luminosities do not match the prescribed value of pblum (an intrinsic luminosity).
End of explanation
b.set_value_all('teff@component', 6000)
Explanation: Now, we'll just undo our changes before continuing
End of explanation
b.run_compute()
areas = b.get_value(qualifier='areas', dataset='mesh01', time=t0, component='primary', unit='m^2')
ldint = b.get_value(qualifier='ldint', component='primary', time=t0)
ptfarea = b.get_value(qualifier='ptfarea', component='primary', time=t0)
abs_normal_intensities = b.get_value(qualifier='abs_normal_intensities', dataset='lc01', time=t0, component='primary')
normal_intensities = b.get_value(qualifier='normal_intensities', dataset='lc01', time=t0, component='primary')
Explanation: Role of Pblum
Let's now look at the intensities in the mesh to see how they're being scaled under-the-hood.  First we'll recompute our model with the equal temperatures and irradiation disabled (to ignore the difference between pblum and pblum_ext).
End of explanation
print(np.median(abs_normal_intensities))
Explanation: 'abs_normal_intensities' are the intensities per triangle in absolute units, i.e. W/m^3.
End of explanation
print(np.median(normal_intensities))
Explanation: The values of 'normal_intensities', however, are significantly samller (in this case).  These are the intensities in relative units which will eventually be integrated to give us flux for a light curve.
End of explanation
pblum = b.get_value(qualifier='pblum', component='primary', context='dataset')
print(np.sum(normal_intensities * ldint * np.pi * areas) * ptfarea, pblum)
Explanation: 'normal_intensities' are scaled from 'abs_normal_intensities' so that the computed luminosity matches the prescribed luminosity (pblum).
Here we compute the luminosity by summing over each triangle's intensity in the normal direction, and multiply it by pi to account for blackbody intensity emitted in all directions in the solid angle, and by the area of that triangle.
End of explanation
<END_TASK>