Skip to content

Commit

Permalink
#117 Merge pull request from deshima-dev/astropenguin/issue116
Browse files Browse the repository at this point in the history
Update make module
  • Loading branch information
astropenguin authored Nov 7, 2023
2 parents caa9f8d + cd50aa5 commit 61b5cba
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 34 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ message: "If you use this software, please cite it as below."

title: "de:code"
abstract: "DESHIMA code for data analysis"
version: 2.5.2
date-released: 2023-11-06
version: 2.5.3
date-released: 2023-11-07
license: "MIT"
doi: "10.5281/zenodo.3384216"
url: "https://github.com/deshima-dev/decode"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ DESHIMA code for data analysis
## Installation

```shell
pip install decode==2.5.2
pip install decode==2.5.3
```
2 changes: 1 addition & 1 deletion decode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"plot",
"select",
]
__version__ = "2.5.2"
__version__ = "2.5.3"


# submodules
Expand Down
109 changes: 80 additions & 29 deletions decode/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,105 @@
# dependencies
import numpy as np
import xarray as xr
from astropy.units import Quantity
from xarray_dataclasses import AsDataArray, Coord, Data
from astropy.units import Quantity, Unit
from xarray_dataclasses import AsDataArray, Attr, Coordof, Data
from . import convert


# type hints
Angle = Union[Quantity, str]
Chan = Literal["chan"]
Lat = Literal["lat"]
Lon = Literal["lon"]
_ = Tuple[()]
QuantityLike = Union[Quantity, str]
UnitLike = Union[Unit, str]
Ln = Literal["lon"]
Lt = Literal["lat"]
Ch = Literal["chan"]


@dataclass
class Weight:
data: Data[Tuple[Ln, Lt, Ch], float]
long_name: Attr[str] = "Data weights"


@dataclass
class Lon:
data: Data[Ln, float]
long_name: Attr[str] = "Sky longitude"
units: Attr[str] = "deg"


@dataclass
class Lat:
data: Data[Lt, float]
long_name: Attr[str] = "Sky latitude"
units: Attr[str] = "deg"


@dataclass
class Chan:
data: Data[Ch, int]
long_name: Attr[str] = "Channel ID"


@dataclass
class Frame:
data: Data[Tuple[()], str]
long_name: Attr[str] = "Sky coordinate frame"


@dataclass
class D2MkidID:
data: Data[Ch, int]
long_name: Attr[str] = "[DESHIMA 2.0] MKID ID"


@dataclass
class D2MkidType:
data: Data[Ch, str]
long_name: Attr[str] = "[DESHIMA 2.0] MKID type"


@dataclass
class D2MkidFrequency:
data: Data[Ch, float]
long_name: Attr[str] = "[DESHIMA 2.0] MKID center frequency"
units: Attr[str] = "Hz"


@dataclass
class Cube(AsDataArray):
"""Cube of DESHIMA 2.0."""

data: Data[Tuple[Lon, Lat, Chan], Any]
lon: Coord[Lon, float] = 0.0
lat: Coord[Lat, float] = 0.0
frame: Coord[_, str] = "altaz"
d2_mkid_frequency: Coord[Chan, float] = 0.0
d2_mkid_id: Coord[Chan, int] = 0
d2_mkid_type: Coord[Chan, str] = ""
data: Data[Tuple[Ln, Lt, Ch], Any]
weight: Coordof[Weight] = 1.0
lon: Coordof[Lon] = 0.0
lat: Coordof[Lat] = 0.0
chan: Coordof[Chan] = 0
frame: Coordof[Frame] = "altaz"
d2_mkid_frequency: Coordof[D2MkidFrequency] = 0.0
d2_mkid_id: Coordof[D2MkidID] = 0.0
d2_mkid_type: Coordof[D2MkidType] = ""


def cube(
dems: xr.DataArray,
/,
*,
gridsize_lon: Angle = "3 arcsec",
gridsize_lat: Angle = "3 arcsec",
skycoord_grid: QuantityLike = "6 arcsec",
skycoord_units: UnitLike = "arcsec",
) -> xr.DataArray:
"""Make a cube from DEMS.
Args:
dems: Input DEMS DataArray.
gridsize_lon: Grid size of the longitude axis.
gridsize_lat: Grid size of the latitude axis.
dems: Input DEMS DataArray to be converted.
skycoord_grid: Grid size of the sky coordinate axes.
skycoord_units: Units of the sky coordinate axes.
Returns:
Cube DataArray.
"""
dems = dems.copy()
cos = np.cos(np.deg2rad(dems["lat"]))
dems["lon"] -= dems["lon_origin"]
dems["lat"] -= dems["lat_origin"]
dems["lon"] *= cos

dlon = Quantity(gridsize_lon).to("deg").value
dlat = Quantity(gridsize_lat).to("deg").value
dlon = Quantity(skycoord_grid).to(dems.lon.attrs["units"]).values
dlat = Quantity(skycoord_grid).to(dems.lat.attrs["units"]).values
lon_min = np.floor(dems.lon.min() / dlon) * dlon
lon_max = np.ceil(dems.lon.max() / dlon) * dlon
lat_min = np.floor(dems.lat.min() / dlat) * dlat
Expand All @@ -73,15 +121,15 @@ def cube(
j = np.abs(dems.lat - lat).argmin("grid")
index = (i + n_lon * j).compute()

dems = dems.copy()
dems = dems.copy(data=dems.data)
dems.coords.update({"index": index})
grid = dems.groupby("index").mean("time")

temp = np.full([n_lat * n_lon, n_chan], np.nan)
temp[grid.index.values] = grid.values
data = temp.reshape((n_lat, n_lon, n_chan)).swapaxes(0, 1)

return Cube.new(
cube = Cube.new(
data=data,
lon=lon.values,
lat=lat.values,
Expand All @@ -90,3 +138,6 @@ def cube(
d2_mkid_frequency=dems.d2_mkid_frequency.values,
d2_mkid_type=dems.d2_mkid_type.values,
)
cube = convert.units(cube, "lon", skycoord_units)
cube = convert.units(cube, "lat", skycoord_units)
return cube
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "decode"
version = "2.5.2"
version = "2.5.3"
description = "DESHIMA code for data analysis"
authors = ["Akio Taniguchi <taniguchi@a.phys.nagoya-u.ac.jp>"]
keywords = [
Expand Down

0 comments on commit 61b5cba

Please sign in to comment.