Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed May 21, 2024
1 parent 02ed77f commit d96e8c2
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 134 deletions.
19 changes: 13 additions & 6 deletions maria/atmosphere/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def _initialize_2d_atmosphere(
)
self.atmosphere.layers = []

depths = enumerate(self.turbulent_layer_depths)
if self.verbose:
depths = tqdm(depths, desc="Initializing atmospheric layers")
depths = tqdm(
self.turbulent_layer_depths,
desc="Initializing atmospheric layers",
disable=not self.verbose,
)

for _, layer_depth in depths:
for layer_depth in depths:
layer_res = (
self.instrument.physical_fwhm(z=layer_depth).min()
/ min_atmosphere_beam_res
Expand Down Expand Up @@ -72,7 +74,12 @@ def _simulate_2d_turbulence(self):
(len(self.atmosphere.layers), self.instrument.n_dets, self.plan.n_time)
)

pbar = tqdm(enumerate(self.atmosphere.layers), disable=not self.verbose)
pbar = tqdm(
enumerate(self.atmosphere.layers),
desc="Generating atmosphere",
disable=not self.verbose,
)

for layer_index, layer in pbar:
layer.generate()
layer_data[layer_index] = sp.interpolate.interp1d(
Expand All @@ -83,7 +90,7 @@ def _simulate_2d_turbulence(self):
bounds_error=False,
fill_value="extrapolate",
)(self.boresight.time)
pbar.set_description(f"Generating atmosphere (z={layer.depth:.00f}m)")
# pbar.set_description(f"Generating atmosphere (z={layer.depth:.00f}m)")

return layer_data

Expand Down
8 changes: 6 additions & 2 deletions maria/instrument/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

INSTRUMENT_CONFIGS = read_yaml(f"{here}/configs.yml")

for name, config in INSTRUMENT_CONFIGS.items():
config["aliases"] = config.get("aliases", [])
config["aliases"].append(name.lower())

INSTRUMENT_DISPLAY_COLUMNS = [
"description",
# "field_of_view",
Expand All @@ -50,7 +54,7 @@ def get_instrument(instrument_name="default", **kwargs):
"""
if instrument_name:
for key, config in INSTRUMENT_CONFIGS.items():
if instrument_name in config.get("aliases", []):
if instrument_name.lower() in config.get("aliases", []):
instrument_name = key
if instrument_name not in INSTRUMENT_CONFIGS.keys():
raise InvalidInstrumentError(instrument_name)
Expand Down Expand Up @@ -149,7 +153,7 @@ def get_subarrays(instrument_config):
subarray["bands"][band_name] = all_bands[band_name]

for param in subarray_params_to_inherit:
if param in config:
if (param in config) and (param not in subarray):
subarray[param] = config[param]

if "bands" not in subarray:
Expand Down
95 changes: 55 additions & 40 deletions maria/instrument/bands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@
import pandas as pd

from ...atmosphere.spectrum import Spectrum
from ...constants import T_CMB, c
from ...functions import planck_spectrum, rayleigh_jeans_spectrum
from ...constants import T_CMB, c, k_B
from ...functions import planck_spectrum
from ...io import flatten_config, read_yaml

BAND_FIELDS = {
"center": {"units": "GHz", "dtype": "float"},
"width": {"units": "GHz", "dtype": "float"},
"shape": {"units": None, "dtype": "str"},
"efficiency": {"units": None, "dtype": "float"},
"sensitivity": {"units": "K sqrt(s)", "dtype": "float"},
"NEP": {"units": "pW sqrt(s)", "dtype": "float"},
}

here, this_filename = os.path.split(__file__)

FIELD_FORMATS = pd.read_csv(f"{here}/format.csv", index_col=0)

all_bands = {}
for path in glob.glob(f"{here}/configs/*.yml"):
tag = os.path.split(path)[1].split(".")[0]
Expand Down Expand Up @@ -115,16 +108,35 @@ def __init__(
sensitivity, kind=sensitivity_kind
) # this sets the NEP automatically

@property
def summary(self):
summary = pd.Series(index=FIELD_FORMATS.index, dtype=str)

for field, entry in FIELD_FORMATS.iterrows():
value = getattr(self, field)

if entry["dtype"] == "float":
if entry["format"] == "e":
s = f"{value:.02e}"
else:
s = f"{value}"

if entry.units != "none":
s = f"{s} {entry.units}"

elif entry["dtype"] == "str":
s = f"{value}"

summary[field] = s

return summary

def __repr__(self):
summary = self.summary
parts = []
for field, d in BAND_FIELDS.items():
value = getattr(self, field)
if d["dtype"] == "str":
s = f"{field}='{value}'"
elif d["units"] is not None:
s = f"{field}={value} {d['units']}"
else:
s = f"{field}={value}"
for field, entry in FIELD_FORMATS.iterrows():
value = summary[field]
s = f"{field}='{value}'" if entry["dtype"] == str else f"{field}={value}"
parts.append(s)

return f"Band({', '.join(parts)})"
Expand Down Expand Up @@ -219,36 +231,40 @@ def passband(self, nu):
@property
def dP_dTRJ(self) -> float:
"""
In watts per kelvin Rayleigh-Jeans, assuming perfect transmission.
In picowatts per kelvin Rayleigh-Jeans, assuming perfect transmission.
"""

nu = np.linspace(self.nu_min, self.nu_max, 256)

dI_dTRJ = rayleigh_jeans_spectrum(
nu=1e9 * nu, T=1
) # this is the same as the derivative
dP_dTRJ = np.trapz(dI_dTRJ * self.passband(nu), 1e9 * nu)
# dI_dTRJ = rayleigh_jeans_spectrum(nu=1e9 * nu, T=1) # this is the same as the derivative
# dP_dTRJ = np.trapz(dI_dTRJ * self.passband(nu), 1e9 * nu)
dP_dTRJ = k_B * np.trapz(self.passband(nu), 1e9 * nu)

return self.efficiency * dP_dTRJ
return 1e12 * self.efficiency * dP_dTRJ

@property
def dP_dTCMB(self) -> float:
"""
In watts per kelvin CMB, assuming perfect transmission.
In picowatts per kelvin CMB, assuming perfect transmission.
"""

eps = 1e-3
eps = 1e-2
delta_T = np.array([-eps / 2, eps / 2])

nu = np.linspace(self.nu_min, self.nu_max, 256)
TRJ = (
planck_spectrum(nu=1e9 * nu, T=T_CMB + delta_T[:, None])
* c**2
/ (2 * k_B * (1e9 * nu) ** 2)
)

delta_T = np.array([-eps / 2, eps / 2])
dI_dTCMB = (
np.diff(planck_spectrum(nu=1e9 * nu, T=T_CMB + delta_T[:, None]), axis=0)[0]
return (
1e12
* self.efficiency
* k_B
* np.diff(np.trapz(TRJ * self.passband(nu), 1e9 * nu))[0]
/ eps
)
dP_dTCMB = self.efficiency * np.trapz(dI_dTCMB * self.passband(nu), 1e9 * nu)

return self.efficiency * dP_dTCMB

@property
def wavelength(self):
Expand Down Expand Up @@ -321,12 +337,11 @@ def names(self):

@property
def summary(self) -> pd.DataFrame:
table = pd.DataFrame(columns=list(BAND_FIELDS.keys()), index=self.names)
summary = pd.DataFrame(index=self.names)

for attr, d in BAND_FIELDS.items():
dtype = d["dtype"]
for band in self.bands:
table.at[band.name, attr] = getattr(band, attr)
table[attr] = table[attr].astype(dtype)
for band in self.bands:
band_summary = band.summary
for field, entry in FIELD_FORMATS.iterrows():
summary.loc[band.name, field] = band_summary[field]

return table
return summary
2 changes: 1 addition & 1 deletion maria/instrument/bands/configs/abs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ f150:
center: 150
width: 30
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
24 changes: 12 additions & 12 deletions maria/instrument/bands/configs/act.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
pa4:
f150:
center: 150
width: 50
width: 30
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
f220:
center: 220
width: 60
width: 40
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
pa5:
f090:
center: 90
width: 30
width: 20
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
f150:
center: 150
width: 50
width: 30
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
pa6:
f090:
center: 90
width: 30
width: 20
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
f150:
center: 150
width: 50
width: 30
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 0.5
40 changes: 20 additions & 20 deletions maria/instrument/bands/configs/alma.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,87 @@ f043:
width: 16
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f078:
center: 78
width: 22
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f100:
center: 100
width: 32
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f144:
center: 144
width: 38
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f187:
center: 187
width: 48
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f243:
center: 243
width: 64
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f324:
center: 324
width: 98
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f447:
center: 447
width: 114
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f661:
center: 661
width: 118
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5

f869:
center: 869
width: 163
shape: gaussian
time_constant: 0
NEP: 1.0 # pW / sqrt(s)
NEP: 3.e-5 # pW / sqrt(s)
knee: 1.0 # Hz
efficiency: 1.0
efficiency: 0.5
Loading

0 comments on commit d96e8c2

Please sign in to comment.