From 2b8dda20b1737794c82ab29a0221e3ffe96cd26c Mon Sep 17 00:00:00 2001
From: Aaron LI <aly@aaronly.me>
Date: Thu, 1 Feb 2018 21:54:52 +0800
Subject: utils/convert.py: Add flux_to_power() to calculate power from flux
 density

---
 fg21sim/utils/convert.py | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

(limited to 'fg21sim')

diff --git a/fg21sim/utils/convert.py b/fg21sim/utils/convert.py
index 0fd08b4..85240f8 100644
--- a/fg21sim/utils/convert.py
+++ b/fg21sim/utils/convert.py
@@ -1,11 +1,14 @@
-# Copyright (c) 2016-2017 Weitian LI <weitian@aaronly.me>
+# Copyright (c) 2016-2018 Weitian LI <weitian@aaronly.me>
 # MIT license
 
 """
 Utilities for conversion among common astronomical quantities.
 """
 
+import numpy as np
+
 from .units import (UnitConversions as AUC, Constants as AC)
+from ..share import COSMO
 
 
 def Sb_to_Tb(Sb, freq):
@@ -108,3 +111,33 @@ def JyPerPix_to_K(freq, pixelsize):
     """
     factor = Fnu_to_Tb(Fnu=1.0, omega=pixelsize**2, freq=freq)
     return factor
+
+
+def flux_to_power(flux, z, index=1.0):
+    """
+    Calculate the (spectral) power from the measured flux density at
+    the same frequency with K-correction taking into account, if the
+    spectral index is given.
+
+    Parameters
+    ----------
+    flux : float
+        The measured flux density at a frequency.
+        Unit: [mJy]
+    z : float
+        The redshift to the source.
+    index : float, optional
+        The spectral index (α) of the source flux: S(ν) ∝ ν^(-α)
+        If given, the K-correction is taken into account.
+
+    Returns
+    -------
+    power : float
+        The calculated (spectral) power at the same frequency as
+        the flux density measured.
+        Unit: [W/Hz]
+    """
+    flux *= 1e-29  # [mJy] -> [W/Hz/m^2]
+    DL = COSMO.DL(z) * AUC.Mpc2m  # [m]
+    power = 4*np.pi * DL**2 * (1+z)**(index-1) * flux  # [W/Hz]
+    return power
-- 
cgit v1.2.2