stellarMagnitude.frink

/** Library to perform calculations with stellar magnitudes. Stellar
magnitudes use a logarithmic scale in which a decrease in 5 magnitudes
corresponds to a increase of 100 in brightness. This means that a
decrease of 1 magnitude corresponds to exactly 100^(1/5) (that is, the
fifth root of 100) or about 2.5118864.
Smaller numbers are brighter, and the brightest stars have negative
magnitudes.

/** This returns the ratio by which star 1 is brighter than star 2,
given their magnitudes. */
brightnessRatio[mag1, mag2] := magnitudeDifferenceToRatio[mag1 - mag2]

/** Returns the magnitude you have to *subtract* from star 1 to get the
brightness of star 2. The brightnesses form a true ratio of brightness. */
magnitudeDifference[brightness1, brightness2] := ratioToMagnitudeDifference[brightness1 / brightness2]

/** Given a brightness ratio, this returns the magnitude difference
between the two stars. For example, if you pass in the ratio 100, meaning
100 times brighter, this function returns -5, which is the number you have
to *add* to the magnitude of the original star to get the magnitude that's
100 times brighter. */
ratioToMagnitudeDifference[ratio] := ln[1/ratio] / ln[100^(1/5)]

/** Given a difference in magnitudes, this returns the ratio of their
brightnesses as star1 / star2. For example, if you have star1 with
magnitude of -4, and a dimmer star2 with magnitude of 1, pass in
(mag1 - mag2) (which is -5) which gives a ratio of 100. */
magnitudeDifferenceToRatio[difference] := (100^(1/5))^-difference

/** We can use the sun's irradiance and stated magnitude to allow us to
calculate the flux-to-magnitude relation. Given the following magnitude
(although it's unclear what distance this magnitude was quoted at; the
earth's orbit is eccentric and the sun's output is variable:)
*/

/** The inverse of the previous calculation. Given a flux given in power/area,
calculate the apparent magnitude. */
magnitude[flux is heat_flux_density] := sunMagnitude - magnitudeDifference[sunIrradiance, flux]

/** Calculate the so-called "absolute magnitude" given an apparent magnitude
and the distance it was measured at. Absolute magnitude is a magnitude
measured at a common distance, generally taken to be 10 parsecs.

For example, to calculate the absolute magnitude of the sun, given that
its apparent magnitude is (sunMagnitude, see above) as seen from earth,
you could write:
absoluteMagnitude[sunMagnitude, earthdist]

/** Calculate the total emitted power from a spherically-symmetrically
radiating object, given the observed flux (that is, power/area) at the
specified distance.
(This just sums the flux over the surface of a sphere at the specified
distance.)
*/
totalEmittedPowerFromFlux[flux is heat_flux_density, distance] := flux 4 pi distance^2

/** Estimate the total emitted power from a spherically-symmetrically
radiating object, given the apparent magnitude at the specified distance.
*/
totalEmittedPowerFromMagnitude[magnitude, distance] := totalEmittedPowerFromFlux[flux[magnitude], distance]

/** Calculate the flux (that is, power/area) at a given distance emitted by a
spherically-symmetrically radiating object, given its total power.
(This just divides the total power over the surface of a sphere at the
specified distance.)
*/
fluxFromTotalEmittedPower[power is power, distance] := power / (4 pi distance^2)

/** Calculate the apparent magnitude at a given distance emitted by a
spherically-symmetrically radiating object, given its total power. */
magnitudeFromTotalEmittedPower[power is power, distance] := magnitude[fluxFromTotalEmittedPower[power, distance]]

/** Calculate the energy of a photon given one of either:

wavelength
frequency
energy

In the following equation, as in physics, h is Planck's constant and
c is the speed of light.
*/
photonEnergy[x] :=
{
if x conforms frequency // given frequency
return h x

if x conforms length // given wavelength
return h (c / x)

if x conforms energy // given energy
return x

return "photonEnergy: No energy known for $x"
}

/** Calculate the frequency of a photon given one of either:

wavelength
frequency
energy

In the following equation, as in physics, h is Planck's constant and
c is the speed of light.
*/
photonFrequency[x] :=
{
if x conforms frequency // given frequency
return x

if x conforms length // given wavelength
return (c / x)

if x conforms energy // given energy
return x / h

return "photonFrequency: No frequency known for $x"
}

/** Calculate the wavelength of a photon given one of either:

wavelength
frequency
energy

In the following equation, as in physics, h is Planck's constant and
c is the speed of light.
*/
photonWavelength[x] :=
{
if x conforms frequency // given frequency
return c / x