ESBO-ETC/esbo_etc/classes/optical_component/Atmosphere.py

164 lines
5.9 KiB
Python
Raw Normal View History

2020-04-16 09:35:24 +02:00
from .AOpticalComponent import AOpticalComponent
from ..IRadiant import IRadiant
from ..SpectralQty import SpectralQty
2020-05-08 15:06:13 +02:00
from ..Entry import Entry
2020-09-30 21:53:09 +02:00
from ...lib.logger import logger
2020-04-14 13:12:33 +02:00
import astropy.units as u
2020-09-30 21:53:09 +02:00
from astropy.io import ascii
from astropy.modeling.models import BlackBody
2020-05-08 15:06:13 +02:00
from typing import Union
2020-04-14 13:12:33 +02:00
class Atmosphere(AOpticalComponent):
"""
A class to model the atmosphere including the atmosphere's spectral transmittance and emission.
"""
2020-04-16 09:35:24 +02:00
2020-09-30 21:53:09 +02:00
def __init__(self, **kwargs):
2020-04-14 13:12:33 +02:00
"""
Initialize a new atmosphere model
2020-09-30 21:53:09 +02:00
Parameters
----------
parent : IRadiant
The parent element of the atmosphere from which the electromagnetic radiation is received.
transmittance : str
Path to the file containing the spectral transmittance-coefficients of the atmosphere.
The format of the file will be guessed by `astropy.io.ascii.read()`.
2020-10-02 15:10:34 +02:00
atran : str
Path to the ATRAN output file containing the spectral transmittance-coefficients of the atmosphere.
2020-09-30 21:53:09 +02:00
emission : str
Path to the file containing the spectral radiance of the atmosphere.
The format of the file will be guessed by `astropy.io.ascii.read()`.
2020-10-02 15:10:34 +02:00
temp : u.Quantity
The atmospheric temperature for the atmosphere's black body radiation.
2020-09-30 21:53:09 +02:00
"""
args = dict()
2020-10-02 15:10:34 +02:00
if "atran" in kwargs:
2020-09-30 21:53:09 +02:00
args = self._fromATRAN(**kwargs)
elif "transmittance" in kwargs:
args = self._fromFiles(**kwargs)
else:
logger.error("Wrong parameters for class Atmosphere.")
super().__init__(parent=args["parent"], transreflectivity=args["transmittance"], noise=args["emission"])
def _fromFiles(self, parent: IRadiant, transmittance: str, emission: str = None):
"""
Initialize a new atmosphere model from two files
2020-04-14 13:12:33 +02:00
Parameters
----------
2020-04-16 09:35:24 +02:00
parent : IRadiant
2020-04-14 13:12:33 +02:00
The parent element of the atmosphere from which the electromagnetic radiation is received.
transmittance : str
Path to the file containing the spectral transmittance-coefficients of the atmosphere.
The format of the file will be guessed by `astropy.io.ascii.read()`.
emission : str
Path to the file containing the spectral radiance of the atmosphere.
The format of the file will be guessed by `astropy.io.ascii.read()`.
2020-10-02 15:10:34 +02:00
Returns
-------
args : dict
The arguments for the class instantiation.
2020-04-14 13:12:33 +02:00
"""
# Read the transmittance
2020-09-30 21:53:09 +02:00
transmittance = SpectralQty.fromFile(transmittance, wl_unit_default=u.nm,
qty_unit_default=u.dimensionless_unscaled)
2020-04-14 13:12:33 +02:00
if emission is None:
2020-09-30 21:53:09 +02:00
emission = 0
2020-04-14 13:12:33 +02:00
else:
2020-09-30 21:53:09 +02:00
emission = SpectralQty.fromFile(emission, wl_unit_default=u.nm,
qty_unit_default=u.W / (u.m ** 2 * u.nm * u.sr))
return {"parent": parent, "transmittance": transmittance, "emission": emission}
2020-10-02 15:10:34 +02:00
def _fromATRAN(self, parent: IRadiant, atran: str, temp: u.Quantity = None):
"""
Initialize a new atmosphere model from an ATRAN output file
Parameters
----------
parent : IRadiant
The parent element of the atmosphere from which the electromagnetic radiation is received.
atran : str
Path to the ATRAN output file containing the spectral transmittance-coefficients of the atmosphere.
temp : u.Quantity
The atmospheric temperature for the atmosphere's black body radiation.
Returns
-------
args : dict
The arguments for the class instantiation.
"""
2020-09-30 21:53:09 +02:00
# Read the file
2020-10-02 15:10:34 +02:00
data = ascii.read(atran, format=None)
2020-09-30 21:53:09 +02:00
# Set units
data["col2"].unit = u.um
data["col3"].unit = u.dimensionless_unscaled
# Create spectral quantity
transmittance = SpectralQty(data["col2"].quantity, data["col3"].quantity)
2020-10-02 15:10:34 +02:00
if temp is not None:
# Create black body
bb = self.__gb_factory(temp)
# Calculate emission
emission = SpectralQty(transmittance.wl, bb(transmittance.wl)) * transmittance
else:
emission = 0
2020-09-30 21:53:09 +02:00
return {"parent": parent, "transmittance": transmittance, "emission": emission}
2020-05-08 15:06:13 +02:00
@staticmethod
def check_config(conf: Entry) -> Union[None, str]:
"""
Check the configuration for this class
Parameters
----------
conf : Entry
The configuration entry to be checked.
Returns
-------
mes : Union[None, str]
The error message of the check. This will be None if the check was successful.
"""
2020-10-02 15:10:34 +02:00
if hasattr(conf, "transmittance"):
mes = conf.check_file("transmittance")
2020-05-08 15:06:13 +02:00
if mes is not None:
return mes
2020-10-02 15:10:34 +02:00
if hasattr(conf, "emission"):
mes = conf.check_file("emission")
if mes is not None:
return mes
else:
mes = conf.check_file("atran")
if mes is not None:
return mes
if hasattr(conf, "temp"):
mes = conf.check_quantity("temp", u.K)
if mes is not None:
return mes
2020-09-30 21:53:09 +02:00
@staticmethod
@u.quantity_input(temp=[u.Kelvin, u.Celsius])
def __gb_factory(temp: u.Quantity, em: Union[int, float] = 1):
"""
Factory for a grey body lambda-function.
Parameters
----------
temp : Quantity in Kelvin / Celsius
The temperature fo the grey body.
em : Union[int, float]
Emissivity of the the grey body
Returns
-------
bb : Callable
The lambda function for the grey body.
"""
bb = BlackBody(temperature=temp, scale=em * u.W / (u.m ** 2 * u.nm * u.sr))
return lambda wl: bb(wl)