Skip to content

Commit

Permalink
#189 Add df/f-to-brightness conversion to load.dems
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Aug 9, 2024
1 parent 817b953 commit 48104c1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
31 changes: 30 additions & 1 deletion decode/convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ["coord_units", "frame", "units"]
__all__ = ["coord_units", "dfof_to_brightness", "frame", "units"]


# standard library
Expand All @@ -17,6 +17,11 @@
UnitLike = Union[xr.DataArray, Unit, str]


# constants
DEFAULT_T_ATM = 273.0 # K
DEFAULT_T_ROOM = 293.0 # K


def coord_units(
da: xr.DataArray,
coord_names: Multiple[str],
Expand Down Expand Up @@ -51,6 +56,30 @@ def coord_units(
return da


def dfof_to_brightness(da: xr.DataArray, /) -> xr.DataArray:
"""Convert a DataArray of df/f to that of brightness."""
if np.isnan(T_atm := da.temperature.mean().data):
T_atm = DEFAULT_T_ATM

if np.isnan(T_room := da.aste_cabin_temperature.mean().data):
T_room = DEFAULT_T_ROOM

fwd = da.d2_resp_fwd.data
p0 = da.d2_resp_p0.data
T0 = da.d2_resp_t0.data

return (
da.copy(
deep=True,
data=(da.data + p0 * np.sqrt(T_room + T0)) ** 2 / (p0**2 * fwd)
- T0 / fwd
- (1 - fwd) / fwd * T_atm,
)
.astype(da.dtype)
.assign_attrs(long_name="Brightness", units="K")
)


def frame(da: xr.DataArray, new_frame: str, /) -> xr.DataArray:
"""Convert the skycoord frame of a DataArray.
Expand Down
33 changes: 26 additions & 7 deletions decode/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
import pandas as pd
import xarray as xr
from . import convert


# constants
Expand Down Expand Up @@ -66,20 +67,24 @@ def atm(*, type: Literal["eta", "tau"] = "tau") -> xr.DataArray:
raise ValueError("Type must be either eta or tau.")


def dems(dems: Union[Path, str], /, **options: Any) -> xr.DataArray:
def dems(
dems: Union[Path, str],
/,
measure: Literal["brightness", "df/f"] = "brightness",
**options: Any,
) -> xr.DataArray:
"""Load a DEMS file as a DataArray.
Args:
dems: Path of the DEMS file.
measure: Measure of the DataArray (either brightness or df/f).
**options: Options to be passed to ``xarray.open_dataarray``.
Keyword Args:
options: Arguments to be passed to ``xarray.open_dataarray``.
Return:
Returns:
Loaded DEMS DataArray.
Raises:
ValueError: Raised if the file type is not supported.
ValueError: Raised if file type or measure is not supported.
"""
suffixes = Path(dems).suffixes
Expand All @@ -101,4 +106,18 @@ def dems(dems: Union[Path, str], /, **options: Any) -> xr.DataArray:
"Use netCDF (.nc) or Zarr (.zarr, .zarr.zip)."
)

return xr.open_dataarray(dems, **options)
da = xr.open_dataarray(dems, **options)

if da.long_name == "Brightness" and measure == "brightness":
return da

if da.long_name == "df/f" and measure == "df/f":
return da

if da.long_name == "df/f" and measure == "brightness":
return convert.dfof_to_brightness(da)

if da.long_name == "Brightness" and measure == "df/f":
raise ValueError("Brightness-to-df/f conversion is not supported.")

raise ValueError("Measure must be either brightness or df/f.")

0 comments on commit 48104c1

Please sign in to comment.