From d1eb1c5738746774efcf6efe32cce5360f0db2f7 Mon Sep 17 00:00:00 2001 From: LukasK13 Date: Thu, 16 Apr 2020 09:35:24 +0200 Subject: [PATCH] Code clean up --- esbo_etc/classes/IRadiant.py | 31 +++++++++++++++++++ esbo_etc/classes/ITransmissive.py | 12 ------- esbo_etc/classes/__init__.py | 2 +- esbo_etc/classes/config.py | 29 ++++++----------- .../optical_component/AHotOpticalComponent.py | 8 ++--- .../optical_component/AOpticalComponent.py | 13 ++++---- .../classes/optical_component/Atmosphere.py | 15 ++++----- .../classes/optical_component/BeamSplitter.py | 6 ++-- esbo_etc/classes/optical_component/Filter.py | 18 +++++------ esbo_etc/classes/optical_component/Lens.py | 6 ++-- esbo_etc/classes/optical_component/Mirror.py | 6 ++-- .../classes/optical_component/StrayLight.py | 13 ++++---- esbo_etc/classes/target/ATarget.py | 8 ++--- esbo_etc/classes/target/BlackBodyTarget.py | 2 +- esbo_etc/classes/target/FileTarget.py | 6 ++-- esbo_etc/lib/helpers.py | 6 +--- tests/data/mirror/mirror_emissivity.csv | 5 +++ .../test_AHotOpticalComponent.py | 20 +++++++----- .../test_AOpticalComponent.py | 2 +- tests/optical_component/test_Atmosphere.py | 2 +- tests/optical_component/test_BeamSplitter.py | 2 +- tests/optical_component/test_Filter.py | 2 +- tests/optical_component/test_Lens.py | 2 +- tests/optical_component/test_Mirror.py | 2 +- tests/optical_component/test_StrayLight.py | 2 +- tests/test_SpectralQty.py | 2 -- 26 files changed, 118 insertions(+), 104 deletions(-) create mode 100644 esbo_etc/classes/IRadiant.py delete mode 100644 esbo_etc/classes/ITransmissive.py create mode 100644 tests/data/mirror/mirror_emissivity.csv diff --git a/esbo_etc/classes/IRadiant.py b/esbo_etc/classes/IRadiant.py new file mode 100644 index 0000000..d2bdc16 --- /dev/null +++ b/esbo_etc/classes/IRadiant.py @@ -0,0 +1,31 @@ +from abc import ABC, abstractmethod +from .SpectralQty import SpectralQty + + +class IRadiant(ABC): + """ + Interface for getting the signal and the noise of a emitting, reflecting or transmitting component in the beam. + """ + @abstractmethod + def calcSignal(self) -> SpectralQty: + """ + Calculate the signal coming from the component + + Returns + ------- + signal : SpectralQty + The emitted, reflected or transmitted signal + """ + pass + + @abstractmethod + def calcNoise(self) -> SpectralQty: + """ + Calculate the noise coming from the component + + Returns + ------- + signal : SpectralQty + The emitted, reflected or transmitted noise + """ + pass diff --git a/esbo_etc/classes/ITransmissive.py b/esbo_etc/classes/ITransmissive.py deleted file mode 100644 index ef42128..0000000 --- a/esbo_etc/classes/ITransmissive.py +++ /dev/null @@ -1,12 +0,0 @@ -from abc import ABC, abstractmethod -from .SpectralQty import SpectralQty - - -class ITransmissive(ABC): - @abstractmethod - def calcSignal(self) -> SpectralQty: - pass - - @abstractmethod - def calcNoise(self) -> SpectralQty: - pass diff --git a/esbo_etc/classes/__init__.py b/esbo_etc/classes/__init__.py index 6e9c53e..c2515b0 100644 --- a/esbo_etc/classes/__init__.py +++ b/esbo_etc/classes/__init__.py @@ -1,5 +1,5 @@ from esbo_etc.classes.config import * -from esbo_etc.classes.ITransmissive import * +from esbo_etc.classes.IRadiant import * from esbo_etc.classes.SpectralQty import * from esbo_etc.classes.target import * from esbo_etc.classes.optical_component import * diff --git a/esbo_etc/classes/config.py b/esbo_etc/classes/config.py index f163983..198f8c0 100644 --- a/esbo_etc/classes/config.py +++ b/esbo_etc/classes/config.py @@ -3,23 +3,28 @@ import numpy as np import astropy.units as u import os import logging -from esbo_etc.lib.helpers import error +from ..lib.helpers import error +from typing import Union class Entry(object): """ A class used to represent a configuration entry. - Copied from ExoSim (https://github.com/ExoSim/ExoSimPublic) + Taken from ExoSim (https://github.com/ExoSim/ExoSimPublic) """ + val: Union[str, bool, u.Quantity] def __call__(self): return self.val if hasattr(self, "val") else None - def parse(self, xml): + def parse(self, xml: eT.Element): """ Parse attributes of a XML element - :param xml: XML element to parse the attributes from + Parameters + ---------- + xml : xml.etree.ElementTree.Element + XML element to parse the attributes from """ # Copy the XML attributes to object attributes for attrib in xml.attrib.keys(): @@ -30,8 +35,6 @@ class Entry(object): try: self.val = u.Quantity(list(map(float, self.val.split(','))), self.units) - # if self.units == 'deg': - # self.val = [val * pq.rad for val in self.val] # workaround for qt unit conversion if len(self.val) == 1: self.val = self.val[0] except (ValueError, LookupError): @@ -52,7 +55,7 @@ class Configuration(object): """ conf = None - def __init__(self, filename="esbo-etc_defaults.xml", default_path=None): + def __init__(self, filename="esbo-etc_defaults.xml"): """ Parse a XML configuration file. @@ -60,8 +63,6 @@ class Configuration(object): ---------- filename : str configuration file to parse - default_path : str - default path to use for relative paths """ # Check if configuration file exists @@ -111,18 +112,12 @@ class Configuration(object): def calc_metaoptions(self): """ Calculate additional attributes e.g. the wavelength grid - Returns - ------- - """ self.calc_metaoption_wl_delta() def calc_metaoption_wl_delta(self): """ Calculate the wavelength grid used for the calculations. - Returns - ------- - """ if hasattr(self.conf.common, "wl_delta"): wl_delta = self.conf.common.wl_delta() @@ -131,7 +126,3 @@ class Configuration(object): setattr(self.conf.common, 'wl_bins', np.arange(self.conf.common.wl_min().to(u.micron).value, self.conf.common.wl_max().to(u.micron).value, wl_delta.to(u.micron).value) * u.micron) - - -if __name__ == "__main__": - conf = Configuration() diff --git a/esbo_etc/classes/optical_component/AHotOpticalComponent.py b/esbo_etc/classes/optical_component/AHotOpticalComponent.py index 2ca36f7..033bf3c 100644 --- a/esbo_etc/classes/optical_component/AHotOpticalComponent.py +++ b/esbo_etc/classes/optical_component/AHotOpticalComponent.py @@ -1,5 +1,5 @@ from esbo_etc.classes.optical_component.AOpticalComponent import AOpticalComponent -from esbo_etc.classes.ITransmissive import ITransmissive +from esbo_etc.classes.IRadiant import IRadiant from esbo_etc.classes.SpectralQty import SpectralQty from abc import abstractmethod import astropy.units as u @@ -13,14 +13,14 @@ class AHotOpticalComponent(AOpticalComponent): """ @abstractmethod @u.quantity_input(wl_bins='length', temp=[u.Kelvin, u.Celsius], obstruction_temp=[u.Kelvin, u.Celsius]) - def __init__(self, parent: ITransmissive, emissivity: Union[SpectralQty, int, float, str], temp: u.Quantity, + def __init__(self, parent: IRadiant, emissivity: Union[SpectralQty, int, float, str], temp: u.Quantity, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): """ Initialize a new optical component with thermal emission Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. emissivity : Union[SpectralQty, int, float, str] The spectral emissivity coefficient for the optical surface. @@ -47,7 +47,7 @@ class AHotOpticalComponent(AOpticalComponent): elif isinstance(emissivity, str): em = SpectralQty.fromFile(emissivity, u.nm, u.dimensionless_unscaled) bb = self._gb_factory(temp) - self._noise = SpectralQty(emissivity.wl, bb(emissivity.wl)) * em + self._noise = SpectralQty(em.wl, bb(em.wl)) * em else: bb = self._gb_factory(temp, emissivity) self._noise = bb diff --git a/esbo_etc/classes/optical_component/AOpticalComponent.py b/esbo_etc/classes/optical_component/AOpticalComponent.py index 386ed8f..9874428 100644 --- a/esbo_etc/classes/optical_component/AOpticalComponent.py +++ b/esbo_etc/classes/optical_component/AOpticalComponent.py @@ -1,20 +1,20 @@ -from ..ITransmissive import ITransmissive -from abc import abstractmethod +from ..IRadiant import IRadiant from ..SpectralQty import SpectralQty -from esbo_etc.lib.helpers import error, isLambda +from ...lib.helpers import error +from abc import abstractmethod import astropy.units as u from astropy.modeling.models import BlackBody from typing import Union, Callable -class AOpticalComponent(ITransmissive): +class AOpticalComponent(IRadiant): """ Abstract super class for an optical component """ @abstractmethod @u.quantity_input(obstructor_temp=[u.K, u.Celsius]) - def __init__(self, parent: ITransmissive, transreflectivity: Union[SpectralQty, int, float, u.Quantity] = None, + def __init__(self, parent: IRadiant, transreflectivity: Union[SpectralQty, int, float, u.Quantity] = None, noise: Union[SpectralQty, int, float, u.Quantity] = None, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): """ @@ -22,7 +22,7 @@ class AOpticalComponent(ITransmissive): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received transreflectivity : SpectralQty The spectral transmission / reflectivity coefficient of the component. This coefficient is multiplied with @@ -79,7 +79,6 @@ class AOpticalComponent(ITransmissive): noise = parent * (1. - self._obstruction) return noise + self.ownNoise() - def propagate(self, sqty: SpectralQty) -> SpectralQty: """ Propagate incoming radiation through the optical component diff --git a/esbo_etc/classes/optical_component/Atmosphere.py b/esbo_etc/classes/optical_component/Atmosphere.py index e42ecf7..fc614c2 100644 --- a/esbo_etc/classes/optical_component/Atmosphere.py +++ b/esbo_etc/classes/optical_component/Atmosphere.py @@ -1,6 +1,6 @@ -from esbo_etc.classes.optical_component.AOpticalComponent import AOpticalComponent -from esbo_etc.classes.ITransmissive import ITransmissive -from esbo_etc.classes.SpectralQty import SpectralQty +from .AOpticalComponent import AOpticalComponent +from ..IRadiant import IRadiant +from ..SpectralQty import SpectralQty import astropy.units as u @@ -8,13 +8,14 @@ class Atmosphere(AOpticalComponent): """ A class to model the atmosphere including the atmosphere's spectral transmittance and emission. """ - def __init__(self, parent: ITransmissive, transmittance: str, emission: str = None) -> "Atmosphere": + + def __init__(self, parent: IRadiant, transmittance: str, emission: str = None): """ Initialize a new atmosphere model Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the atmosphere from which the electromagnetic radiation is received. This element is usually of type Target or StrayLight. transmittance : str @@ -26,13 +27,13 @@ class Atmosphere(AOpticalComponent): """ # Read the transmittance transmittance_sqty = SpectralQty.fromFile(transmittance, wl_unit_default=u.nm, - qty_unit_default=u.dimensionless_unscaled) + qty_unit_default=u.dimensionless_unscaled) if emission is None: # No emission is given, initialize the super class super().__init__(parent, transmittance_sqty) else: # Read the emission emission_sqty = SpectralQty.fromFile(emission, wl_unit_default=u.nm, - qty_unit_default=u.W / (u.m**2 * u.nm * u.sr)) + qty_unit_default=u.W / (u.m ** 2 * u.nm * u.sr)) # Initialize the super class super().__init__(parent, transmittance_sqty, emission_sqty) diff --git a/esbo_etc/classes/optical_component/BeamSplitter.py b/esbo_etc/classes/optical_component/BeamSplitter.py index 44a4853..4ee9269 100644 --- a/esbo_etc/classes/optical_component/BeamSplitter.py +++ b/esbo_etc/classes/optical_component/BeamSplitter.py @@ -1,6 +1,6 @@ from .AHotOpticalComponent import AHotOpticalComponent from ..SpectralQty import SpectralQty -from ..ITransmissive import ITransmissive +from ..IRadiant import IRadiant from astropy import units as u from typing import Union @@ -10,7 +10,7 @@ class BeamSplitter(AHotOpticalComponent): A class to model the optical characteristics of a beam splitter. """ @u.quantity_input(temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def __init__(self, parent: ITransmissive, transmittance: str, + def __init__(self, parent: IRadiant, transmittance: str, emissivity: Union[int, float, str] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): """ @@ -18,7 +18,7 @@ class BeamSplitter(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. transmittance : str The spectral transmittance coefficients of the filter. diff --git a/esbo_etc/classes/optical_component/Filter.py b/esbo_etc/classes/optical_component/Filter.py index e304353..0576b41 100644 --- a/esbo_etc/classes/optical_component/Filter.py +++ b/esbo_etc/classes/optical_component/Filter.py @@ -1,6 +1,6 @@ from .AHotOpticalComponent import AHotOpticalComponent from ..SpectralQty import SpectralQty -from ..ITransmissive import ITransmissive +from ..IRadiant import IRadiant from ...lib.helpers import error from astropy import units as u from typing import Union, Callable @@ -17,7 +17,7 @@ class Filter(AHotOpticalComponent): H=dict(cwl=1630 * u.nm, bw=400 * u.nm), K=dict(cwl=2190 * u.nm, bw=600 * u.nm)) @u.quantity_input(temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def __init__(self, parent: ITransmissive, transmittance: Union[SpectralQty, Callable], + def __init__(self, parent: IRadiant, transmittance: Union[SpectralQty, Callable], emissivity: Union[int, float, str] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): """ @@ -25,7 +25,7 @@ class Filter(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. transmittance : Union[SpectralQty, Callable] The spectral transmittance coefficients of the filter. @@ -48,7 +48,7 @@ class Filter(AHotOpticalComponent): @classmethod # @u.quantity_input(temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def fromBand(cls, parent: ITransmissive, band: str, emissivity: Union[str, int, float] = 1, + def fromBand(cls, parent: IRadiant, band: str, emissivity: Union[str, int, float] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1) -> "Filter": """ @@ -57,7 +57,7 @@ class Filter(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. band : str The spectral band of the filter. Can be one of [U, B, V, R, I, J, H, K]. @@ -88,7 +88,7 @@ class Filter(AHotOpticalComponent): @classmethod # @u.quantity_input(temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def fromFile(cls, parent: ITransmissive, transmittance: str, emissivity: Union[str, int, float] = 1, + def fromFile(cls, parent: IRadiant, transmittance: str, emissivity: Union[str, int, float] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1) -> "Filter": """ @@ -96,7 +96,7 @@ class Filter(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. transmittance : str Path to the file containing the spectral transmittance-coefficients of the filter element. @@ -125,7 +125,7 @@ class Filter(AHotOpticalComponent): @classmethod # @u.quantity_input(start="length", end="length", temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def fromRange(cls, parent: ITransmissive, start: u.Quantity, end: u.Quantity, + def fromRange(cls, parent: IRadiant, start: u.Quantity, end: u.Quantity, emissivity: Union[str, int, float] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1) -> "Filter": """ @@ -134,7 +134,7 @@ class Filter(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. start : length-quantity Start wavelength of the pass-band diff --git a/esbo_etc/classes/optical_component/Lens.py b/esbo_etc/classes/optical_component/Lens.py index c643306..e76c688 100644 --- a/esbo_etc/classes/optical_component/Lens.py +++ b/esbo_etc/classes/optical_component/Lens.py @@ -1,6 +1,6 @@ from .AHotOpticalComponent import AHotOpticalComponent from ..SpectralQty import SpectralQty -from ..ITransmissive import ITransmissive +from ..IRadiant import IRadiant from astropy import units as u from typing import Union @@ -10,7 +10,7 @@ class Lens(AHotOpticalComponent): A class to model the optical characteristics of a lens. """ @u.quantity_input(temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def __init__(self, parent: ITransmissive, transmittance: str, + def __init__(self, parent: IRadiant, transmittance: str, emissivity: Union[int, float, str] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): """ @@ -18,7 +18,7 @@ class Lens(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. transmittance : str The spectral transmittance coefficients of the filter. diff --git a/esbo_etc/classes/optical_component/Mirror.py b/esbo_etc/classes/optical_component/Mirror.py index adf597f..e931635 100644 --- a/esbo_etc/classes/optical_component/Mirror.py +++ b/esbo_etc/classes/optical_component/Mirror.py @@ -1,6 +1,6 @@ from .AHotOpticalComponent import AHotOpticalComponent from ..SpectralQty import SpectralQty -from ..ITransmissive import ITransmissive +from ..IRadiant import IRadiant from astropy import units as u from typing import Union @@ -10,7 +10,7 @@ class Mirror(AHotOpticalComponent): A class to model the optical characteristics of a mirror. """ @u.quantity_input(temp=[u.Kelvin, u.Celsius], obstructor_temp=[u.Kelvin, u.Celsius]) - def __init__(self, parent: ITransmissive, reflectance: str, + def __init__(self, parent: IRadiant, reflectance: str, emissivity: Union[int, float, str] = 1, temp: u.Quantity = 0 * u.K, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): """ @@ -18,7 +18,7 @@ class Mirror(AHotOpticalComponent): Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element of the optical component from which the electromagnetic radiation is received. reflectance : str The spectral transmittance coefficients of the filter. diff --git a/esbo_etc/classes/optical_component/StrayLight.py b/esbo_etc/classes/optical_component/StrayLight.py index d2b476d..b6cd36b 100644 --- a/esbo_etc/classes/optical_component/StrayLight.py +++ b/esbo_etc/classes/optical_component/StrayLight.py @@ -1,6 +1,6 @@ -from esbo_etc.classes.optical_component.AOpticalComponent import AOpticalComponent -from esbo_etc.classes.ITransmissive import ITransmissive -from esbo_etc.classes.SpectralQty import SpectralQty +from .AOpticalComponent import AOpticalComponent +from ..IRadiant import IRadiant +from ..SpectralQty import SpectralQty import astropy.units as u @@ -8,13 +8,14 @@ class StrayLight(AOpticalComponent): """ A class to model additional stray light sources e.g. zodiacal light """ - def __init__(self, parent: ITransmissive, emission: str = None) -> "Atmosphere": + + def __init__(self, parent: IRadiant, emission: str = None): """ Initialize a new stray light source Parameters ---------- - parent : ITransmissive + parent : IRadiant The parent element from which the electromagnetic radiation is received. This element is usually of type Target or StrayLight. emission : str @@ -23,6 +24,6 @@ class StrayLight(AOpticalComponent): """ # Read the emission emission_sqty = SpectralQty.fromFile(emission, wl_unit_default=u.nm, - qty_unit_default=u.W / (u.m**2 * u.nm * u.sr)) + qty_unit_default=u.W / (u.m ** 2 * u.nm * u.sr)) # Initialize the super class super().__init__(parent, 1.0, emission_sqty) diff --git a/esbo_etc/classes/target/ATarget.py b/esbo_etc/classes/target/ATarget.py index cd1b8b5..da2c451 100644 --- a/esbo_etc/classes/target/ATarget.py +++ b/esbo_etc/classes/target/ATarget.py @@ -1,10 +1,11 @@ from abc import abstractmethod -from ..ITransmissive import ITransmissive +from ..IRadiant import IRadiant from ..SpectralQty import SpectralQty import astropy.units as u +import numpy as np -class ATarget(ITransmissive): +class ATarget(IRadiant): """ Abstract super class for target models """ @@ -30,7 +31,7 @@ class ATarget(ITransmissive): noise : SpectralQty The spectral radiance of the target's noise """ - return SpectralQty(self._sfd.wl, [0] * len(self._sfd.wl) << u.W / (u.m**2 * u.nm * u.sr)) + return SpectralQty(self._sfd.wl, np.repeat(0, len(self._sfd.wl)) << u.W / (u.m**2 * u.nm * u.sr)) def calcSignal(self) -> SpectralQty: """ @@ -42,4 +43,3 @@ class ATarget(ITransmissive): The spectral flux density of the target's signal """ return self._sfd - diff --git a/esbo_etc/classes/target/BlackBodyTarget.py b/esbo_etc/classes/target/BlackBodyTarget.py index 0c690a2..5105712 100644 --- a/esbo_etc/classes/target/BlackBodyTarget.py +++ b/esbo_etc/classes/target/BlackBodyTarget.py @@ -2,7 +2,7 @@ from ..target.ATarget import ATarget from ..SpectralQty import SpectralQty import astropy.units as u from astropy.modeling.models import BlackBody -from esbo_etc.lib.helpers import error +from ...lib.helpers import error class BlackBodyTarget(ATarget): diff --git a/esbo_etc/classes/target/FileTarget.py b/esbo_etc/classes/target/FileTarget.py index 0593998..872eeda 100644 --- a/esbo_etc/classes/target/FileTarget.py +++ b/esbo_etc/classes/target/FileTarget.py @@ -1,8 +1,6 @@ from ..target.ATarget import ATarget from ..SpectralQty import SpectralQty import astropy.units as u -from astropy.io import ascii -import re class FileTarget(ATarget): @@ -19,8 +17,8 @@ class FileTarget(ATarget): file : str The file to read the spectral flux density values from. The file needs to provide two columns: wavelength and the corresponding spectral flux density. The format of the file will be guessed by - `astropy.io.ascii.read(). If the file doesn't provide units via astropy's enhanced CSV format, the units will - be read from the column headers or otherwise assumed to be *nm* and *W / m^2 / nm*. + `astropy.io.ascii.read(). If the file doesn't provide units via astropy's enhanced CSV format, the units + will be read from the column headers or otherwise assumed to be *nm* and *W / m^2 / nm*. """ # Create spectral quantity from file sfd = SpectralQty.fromFile(file, u.nm, u.W / (u.m ** 2 * u.nm)) diff --git a/esbo_etc/lib/helpers.py b/esbo_etc/lib/helpers.py index f889d19..99c3707 100644 --- a/esbo_etc/lib/helpers.py +++ b/esbo_etc/lib/helpers.py @@ -1,9 +1,6 @@ import logging import sys import traceback -import astropy.units as u -from astropy.modeling.models import BlackBody -from typing import Union def error(msg: str, exit_: bool = True): @@ -41,5 +38,4 @@ def isLambda(v: object): res : bool Result of the check """ - LAMBDA = lambda: 0 - return isinstance(v, type(LAMBDA)) and v.__name__ == LAMBDA.__name__ + return isinstance(v, type(lambda: None)) and v.__name__ == (lambda: None).__name__ diff --git a/tests/data/mirror/mirror_emissivity.csv b/tests/data/mirror/mirror_emissivity.csv new file mode 100644 index 0000000..668d16d --- /dev/null +++ b/tests/data/mirror/mirror_emissivity.csv @@ -0,0 +1,5 @@ +wavelength,spectral emissivity +201,0.5 +202,0.5 +203,0.5 +204,0.5 diff --git a/tests/optical_component/test_AHotOpticalComponent.py b/tests/optical_component/test_AHotOpticalComponent.py index 1341773..1d45c84 100644 --- a/tests/optical_component/test_AHotOpticalComponent.py +++ b/tests/optical_component/test_AHotOpticalComponent.py @@ -1,15 +1,16 @@ from unittest import TestCase from esbo_etc.classes.optical_component.AHotOpticalComponent import AHotOpticalComponent -from esbo_etc.classes.ITransmissive import ITransmissive +from esbo_etc.classes.IRadiant import IRadiant from esbo_etc.classes.SpectralQty import SpectralQty from esbo_etc.classes.target.FileTarget import FileTarget import astropy.units as u import numpy as np +from typing import Union class HotOpticalComponent(AHotOpticalComponent): - def __init__(self, parent: ITransmissive, emissivity: SpectralQty, temp: u.Quantity, obstruction: float = 0, - obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): + def __init__(self, parent: IRadiant, emissivity: Union[SpectralQty, int, float, str], temp: u.Quantity, + obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 1): super().__init__(parent, emissivity, temp, obstruction, obstructor_temp, obstructor_emissivity) def propagate(self, sqty: SpectralQty) -> SpectralQty: @@ -20,9 +21,14 @@ class TestAHotOpticalComponent(TestCase): wl = np.arange(201, 205, 1) << u.nm def setUp(self): - self.target = FileTarget("../data/target/target_demo_1.csv") - self.comp = HotOpticalComponent(self.target, SpectralQty(self.wl, [0.5] * 4), temp=300 * u.K) + self.target = FileTarget("data/target/target_demo_1.csv") def test___init__(self): - self.assertEqual(self.comp.calcNoise(), SpectralQty(self.wl, [4.31413931e-96, 1.37122214e-95, 4.30844544e-95, - 1.33846280e-94] << u.W / (u.m**2 * u.nm * u.sr))) + comp = HotOpticalComponent(self.target, SpectralQty(self.wl, np.repeat(0.5, 4) << u.dimensionless_unscaled), + temp=300 * u.K) + self.assertEqual(comp.calcNoise(), SpectralQty(self.wl, [4.31413931e-96, 1.37122214e-95, 4.30844544e-95, + 1.33846280e-94] << u.W / (u.m ** 2 * u.nm * u.sr))) + + comp = HotOpticalComponent(self.target, "data/mirror/mirror_emissivity.csv", temp=300 * u.K) + self.assertEqual(comp.calcNoise(), SpectralQty(self.wl, [4.31413931e-96, 1.37122214e-95, 4.30844544e-95, + 1.33846280e-94] << u.W / (u.m ** 2 * u.nm * u.sr))) diff --git a/tests/optical_component/test_AOpticalComponent.py b/tests/optical_component/test_AOpticalComponent.py index 0e074ce..1fd01e0 100644 --- a/tests/optical_component/test_AOpticalComponent.py +++ b/tests/optical_component/test_AOpticalComponent.py @@ -5,7 +5,7 @@ import numpy as np class OpticalComponent(AOpticalComponent): - def __init__(self, parent: ITransmissive, transreflectivity: SpectralQty = None, + def __init__(self, parent: IRadiant, transreflectivity: SpectralQty = None, noise: SpectralQty = None, obstruction: float = 0, obstructor_temp: u.Quantity = 0 * u.K, obstructor_emissivity: float = 0): super().__init__(parent, transreflectivity, noise, obstruction, obstructor_temp, obstructor_emissivity) diff --git a/tests/optical_component/test_Atmosphere.py b/tests/optical_component/test_Atmosphere.py index 8303c0c..13bc0ba 100644 --- a/tests/optical_component/test_Atmosphere.py +++ b/tests/optical_component/test_Atmosphere.py @@ -6,7 +6,7 @@ import astropy.units as u class TestAtmosphere(TestCase): def setUp(self): - self.target = FileTarget("../data/target/target_demo_1.csv") + self.target = FileTarget("data/target/target_demo_1.csv") self.atmosphere = Atmosphere(self.target, "data/atmosphere/atmosphere_transmittance_1.csv", "data/atmosphere/atmosphere_emission_1.csv") diff --git a/tests/optical_component/test_BeamSplitter.py b/tests/optical_component/test_BeamSplitter.py index f933298..4fb5d8a 100644 --- a/tests/optical_component/test_BeamSplitter.py +++ b/tests/optical_component/test_BeamSplitter.py @@ -10,7 +10,7 @@ class TestBeamSplitter(TestCase): wl = np.arange(201, 205, 1) << u.nm def setUp(self): - self.target = FileTarget("../data/target/target_demo_1.csv") + self.target = FileTarget("data/target/target_demo_1.csv") self.splitter = BeamSplitter(self.target, "data/beamsplitter/beamsplitter_transmittance.csv", 0.5, temp=300 * u.K) diff --git a/tests/optical_component/test_Filter.py b/tests/optical_component/test_Filter.py index 88871e4..4eed642 100644 --- a/tests/optical_component/test_Filter.py +++ b/tests/optical_component/test_Filter.py @@ -13,7 +13,7 @@ class TestFilter(TestCase): 0.0, 0.0] << u.W / (u.m ** 2 * u.nm))) def test_fromFile(self): - target = FileTarget("../data/target/target_demo_1.csv") + target = FileTarget("data/target/target_demo_1.csv") filt = Filter.fromFile(target, "data/filter/filter_transmittance.csv") self.assertEqual(filt.calcSignal(), SpectralQty(np.arange(200, 210, 1) << u.nm, [1.10e-15, 1.20e-15, 1.30e-15, 1.40e-15, 1.35e-15, 1.44e-15, diff --git a/tests/optical_component/test_Lens.py b/tests/optical_component/test_Lens.py index ecbb3a7..71d12ab 100644 --- a/tests/optical_component/test_Lens.py +++ b/tests/optical_component/test_Lens.py @@ -10,7 +10,7 @@ class TestLens(TestCase): wl = np.arange(201, 205, 1) << u.nm def setUp(self): - self.target = FileTarget("../data/target/target_demo_1.csv") + self.target = FileTarget("data/target/target_demo_1.csv") self.lens = Lens(self.target, "data/lens/lens_transmittance.csv", 0.5, temp=300 * u.K) def test___init__(self): diff --git a/tests/optical_component/test_Mirror.py b/tests/optical_component/test_Mirror.py index cc85f46..20060d4 100644 --- a/tests/optical_component/test_Mirror.py +++ b/tests/optical_component/test_Mirror.py @@ -10,7 +10,7 @@ class TestMirror(TestCase): wl = np.arange(201, 205, 1) << u.nm def setUp(self): - self.target = FileTarget("../data/target/target_demo_1.csv") + self.target = FileTarget("data/target/target_demo_1.csv") self.mirror = Mirror(self.target, "data/mirror/mirror_reflectance.csv", 0.5, temp=300 * u.K) def test___init__(self): diff --git a/tests/optical_component/test_StrayLight.py b/tests/optical_component/test_StrayLight.py index dd3ac83..032b645 100644 --- a/tests/optical_component/test_StrayLight.py +++ b/tests/optical_component/test_StrayLight.py @@ -6,7 +6,7 @@ import astropy.units as u class TestStrayLight(TestCase): def setUp(self): - self.target = FileTarget("../data/target/target_demo_1.csv") + self.target = FileTarget("data/target/target_demo_1.csv") self.zodiac = StrayLight(self.target, "data/straylight/zodiacal_emission_1.csv") def test_calcSignal(self): diff --git a/tests/test_SpectralQty.py b/tests/test_SpectralQty.py index 6444699..ab3e4b5 100644 --- a/tests/test_SpectralQty.py +++ b/tests/test_SpectralQty.py @@ -116,6 +116,4 @@ class TestSpectralQty(TestCase): self.assertEqual(sqty, res) sqty = SpectralQty.fromFile("data/target/target_demo_2.csv", u.nm, u.W / (u.m ** 2 * u.nm)) - print(sqty.wl) - print(sqty.qty) self.assertEqual(sqty, res)