aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2017-09-01 15:27:12 +0800
committerAaron LI <aly@aaronly.me>2017-09-01 15:27:12 +0800
commite35fc3ce66a7dfe0099a17fdab83952460f52f62 (patch)
tree64c0a42429e4b1eee64b6e3c0e291d5c4b9a440b
parent90569a862f81e057ee023b012e0339771f4f1a9b (diff)
downloadfg21sim-e35fc3ce66a7dfe0099a17fdab83952460f52f62.tar.bz2
utils/convert.py: Remove the slow, astropy.units based functions
-rw-r--r--fg21sim/utils/convert.py88
1 files changed, 24 insertions, 64 deletions
diff --git a/fg21sim/utils/convert.py b/fg21sim/utils/convert.py
index 9eb9061..0fd08b4 100644
--- a/fg21sim/utils/convert.py
+++ b/fg21sim/utils/convert.py
@@ -5,75 +5,14 @@
Utilities for conversion among common astronomical quantities.
"""
-import astropy.units as au
-
from .units import (UnitConversions as AUC, Constants as AC)
-def Fnu_to_Tb(Fnu, omega, freq):
- """
- Convert flux density to brightness temperature, using the
- Rayleigh-Jeans law, in the Rayleigh-Jeans limit.
-
- Parameters
- ----------
- Fnu : `~astropy.units.Quantity`
- Input flux density, e.g., `1.0*au.Jy`
- omega : `~astropy.units.Quantity`
- Source angular size/area, e.g., `100*au.arcsec**2`
- freq : `~astropy.units.Quantity`
- Frequency where the flux density measured, e.g., `120*au.MHz`
-
- Returns
- -------
- Tb : `~astropy.units.Quantity`
- Brightness temperature, with default unit `au.K`
-
- References
- ----------
- - Brightness and Flux
- http://www.cv.nrao.edu/course/astr534/Brightness.html
- - Wikipedia: Brightness Temperature
- https://en.wikipedia.org/wiki/Brightness_temperature
- - NJIT: Physics 728: Introduction to Radio Astronomy: Lecture #1
- https://web.njit.edu/~gary/728/Lecture1.html
- - Astropy: Equivalencies: Brightness Temperature / Flux Density
- http://docs.astropy.org/en/stable/units/equivalencies.html
- """
- equiv = au.brightness_temperature(omega, freq)
- Tb = Fnu.to(au.K, equivalencies=equiv)
- return Tb
-
-
def Sb_to_Tb(Sb, freq):
"""
Convert surface brightness to brightness temperature, using the
Rayleigh-Jeans law, in the Rayleigh-Jeans limit.
- Parameters
- ----------
- Sb : `~astropy.units.Quantity`
- Input surface brightness, e.g., `1.0*(au.Jy/au.arcsec**2)`
- freq : `~astropy.units.Quantity`
- Frequency where the flux density measured, e.g., `120*au.MHz`
-
- Returns
- -------
- Tb : `~astropy.units.Quantity`
- Brightness temperature, with default unit `au.K`
- """
- omega = 1.0 * au.arcsec**2
- Fnu = (Sb * omega).to(au.Jy) # [Jy]
- return Fnu_to_Tb(Fnu, omega, freq)
-
-
-def Sb_to_Tb_fast(Sb, freq):
- """
- Convert surface brightness to brightness temperature, using the
- Rayleigh-Jeans law, in the Rayleigh-Jeans limit.
-
- Avoid using `astropy.units` to optimize the speed.
-
Tb = Sb * c^2 / (2 * k_B * nu^2)
where `Sb` is the surface brightness density measured at a certain
@@ -81,6 +20,16 @@ def Sb_to_Tb_fast(Sb, freq):
1 [Jy] = 1e-23 [erg/s/cm^2/Hz] = 1e-26 [W/m^2/Hz]
+ NOTE
+ ----
+ It is very easy to use ``astropy.units`` for the conversion:
+ equiv = au.brightness_temperature(omega, freq)
+ Tb = Fnu.to(au.K, equivalencies=equiv)
+
+ WARNING:
+ Using `astropy.units` for the conversion may be much slower.
+ This can be used as a cross check for the calculation.
+
Parameters
----------
Sb : float
@@ -95,6 +44,17 @@ def Sb_to_Tb_fast(Sb, freq):
Tb : float
Calculated brightness temperature
Unit: [K]
+
+ References
+ ----------
+ - Brightness and Flux
+ http://www.cv.nrao.edu/course/astr534/Brightness.html
+ - Wikipedia: Brightness Temperature
+ https://en.wikipedia.org/wiki/Brightness_temperature
+ - NJIT: Physics 728: Introduction to Radio Astronomy: Lecture #1
+ https://web.njit.edu/~gary/728/Lecture1.html
+ - Astropy: Equivalencies: Brightness Temperature / Flux Density
+ http://docs.astropy.org/en/stable/units/equivalencies.html
"""
# NOTE: [rad] & [sr] are dimensionless
arcsec2 = AUC.arcsec2rad ** 2 # [sr]
@@ -104,7 +64,7 @@ def Sb_to_Tb_fast(Sb, freq):
return Tb
-def Fnu_to_Tb_fast(Fnu, omega, freq):
+def Fnu_to_Tb(Fnu, omega, freq):
"""
Convert flux density to brightness temperature, using the
Rayleigh-Jeans law, in the Rayleigh-Jeans limit.
@@ -130,7 +90,7 @@ def Fnu_to_Tb_fast(Fnu, omega, freq):
Unit: [K]
"""
Sb = Fnu / omega # [Jy/arcsec^2]
- return Sb_to_Tb_fast(Sb, freq)
+ return Sb_to_Tb(Sb, freq)
def JyPerPix_to_K(freq, pixelsize):
@@ -146,5 +106,5 @@ def JyPerPix_to_K(freq, pixelsize):
The pixel size.
Unit: [arcsec]
"""
- factor = Fnu_to_Tb_fast(Fnu=1.0, omega=pixelsize**2, freq=freq)
+ factor = Fnu_to_Tb(Fnu=1.0, omega=pixelsize**2, freq=freq)
return factor