Skip to content

Commit

Permalink
patches
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed Jun 4, 2024
1 parent 198ce37 commit 7fcdf77
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 71 deletions.
116 changes: 66 additions & 50 deletions docs/source/tutorials/custom-map-simulations.ipynb

Large diffs are not rendered by default.

59 changes: 43 additions & 16 deletions maria/map/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from astropy.io import fits
from astropy.wcs import WCS
from matplotlib.colors import ListedColormap
from maria import units

from ..units import KbrightToJyPix

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

Expand All @@ -20,6 +21,11 @@

mpl.colormaps.register(cmb_cmap)

UNITS_CONFIG = {
"K_RJ": {"long_name": "Rayleigh-Jeans Temperature [K]"},
"Jy/pixel": {"long_name": "Janskies per pixel"},
}


class Map:
"""
Expand All @@ -36,8 +42,11 @@ def __init__(
center: Tuple[float, float] = (0.0, 0.0),
frame: str = "ra_dec",
degrees: bool = True,
units: str ='K_RJ'
units: str = "K_RJ",
):
if units not in UNITS_CONFIG:
raise ValueError(f"'units' must be one of {list(UNITS_CONFIG.keys())}.")

self.data = data if data.ndim > 2 else data[None]
self.weight = np.ones(self.data.shape) if weight is None else weight
self.center = tuple(np.radians(center)) if degrees else center
Expand All @@ -59,8 +68,8 @@ def __init__(
f"frequency dimension of the supplied map ({self.n_f})."
)

if self.units == 'Jy/pixel':
self.to('K_RJ')
if self.units == "Jy/pixel":
self.to("K_RJ")

self.header = ap.io.fits.header.Header()

Expand Down Expand Up @@ -115,17 +124,35 @@ def x_side(self):
def y_side(self):
y = self.resolution * np.arange(self.n_y)
return y - y.mean()


def to(self, unit):

if unit is 'K_RJ':
self.data /= units.KbrightToJyPix(self.frequency*1e9, np.degrees(self.resolution), np.degrees(self.resolution))
elif unit is 'Jy/pixel':
self.data *= units.KbrightToJyPix(self.frequency*1e9, np.degrees(self.resolution), np.degrees(self.resolution))
else:
raise ValueError(f"Unit {self.unit} not implemented.")
def to(self, units, inplace=False):
if units == self.units:
data = self.data

if units == "K_RJ":
data = self.data / KbrightToJyPix(
self.frequency * 1e9, np.degrees(self.resolution)
)
elif units == "Jy/pixel":
data = self.data * KbrightToJyPix(
self.frequency * 1e9, np.degrees(self.resolution)
)
else:
raise ValueError(f"Units '{units}' not implemented.")

if inplace:
self.data = data
else:
return Map(
data=data,
weight=self.weight,
resolution=self.resolution,
frequency=self.frequency,
center=self.center,
frame=self.frame,
degrees=False,
units=units,
)

def plot(
self, cmap="cmb", rel_vmin=0.001, rel_vmax=0.999, units="degrees", **kwargs
Expand Down Expand Up @@ -195,7 +222,7 @@ def plot(
ax.set_ylabel(rf"$\Delta\,\theta_y$ [{units}]")

cbar = fig.colorbar(map_im, ax=ax, shrink=1.0)
cbar.set_label("RJ temperature [K]")
cbar.set_label(UNITS_CONFIG[self.units]["long_name"])

def to_fits(self, filepath):
self.header = ap.io.fits.header.Header()
Expand Down Expand Up @@ -227,11 +254,11 @@ def to_fits(self, filepath):
self.header["BTYPE"] = "Jy/pixel"

elif self.units == "K_RJ":
self.header["BTYPE"] = "Kelvin RJ"
self.header["BTYPE"] = "Kelvin RJ"

fits.writeto(
filename=filepath,
data=self.data,
header=self.header,
overwrite=True,
)
)
4 changes: 2 additions & 2 deletions maria/plan/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def raster(time, radius=1, height=None, speed=0.5, n=16, turnaround_time=0.5):
)


def back_and_forth(time, speed, width, turnaround_time=5): # noqa
def back_and_forth(time, speed=1, radius=5, turnaround_time=5): # noqa
return raster(
time, speed=speed, width=width, height=0, turnaround_time=turnaround_time
time, speed=speed, radius=radius, height=0, turnaround_time=turnaround_time
)


Expand Down
2 changes: 1 addition & 1 deletion maria/sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(
self.noise_kwargs = noise_kwargs

if map:
self.map = map
self.map = map.to(units="K_RJ")
...

if atmosphere:
Expand Down
6 changes: 4 additions & 2 deletions maria/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ def KcmbToKbright(freq):

# Kelvin brightness to Jy/pixel
# ----------------------------------------------------------------------
def KbrightToJyPix(freq, ipix, jpix):
def KbrightToJyPix(freq, ipix, jpix=None):
jpix = jpix or ipix
return KcmbToJyPix(freq, ipix, jpix) / KcmbToKbright(freq)


# Kelvin CMB to Jy/pixel
# ----------------------------------------------------------------------
def KcmbToJyPix(freq, ipix, jpix):
def KcmbToJyPix(freq, ipix, jpix=None):
jpix = jpix or ipix
x = getx(freq)
factor = getJynorm() / Tcmb
factor *= (x**4) * np.exp(x) / (np.expm1(x) ** 2)
Expand Down

0 comments on commit 7fcdf77

Please sign in to comment.