diff --git a/CITATION.cff b/CITATION.cff index 669669c..f8e228c 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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" diff --git a/README.md b/README.md index 8c41f89..4bc9933 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,5 @@ DESHIMA code for data analysis ## Installation ```shell -pip install decode==2.5.2 +pip install decode==2.5.3 ``` diff --git a/decode/__init__.py b/decode/__init__.py index 8f39cf2..71bd62b 100644 --- a/decode/__init__.py +++ b/decode/__init__.py @@ -8,7 +8,7 @@ "plot", "select", ] -__version__ = "2.5.2" +__version__ = "2.5.3" # submodules diff --git a/decode/make.py b/decode/make.py index 518c463..fa0f710 100644 --- a/decode/make.py +++ b/decode/make.py @@ -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 @@ -73,7 +121,7 @@ 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") @@ -81,7 +129,7 @@ def cube( 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, @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 13c1a04..91e4137 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] keywords = [