Skip to content

Commit

Permalink
#139 Merge pull request from deshima-dev/astropenguin/issue135
Browse files Browse the repository at this point in the history
Update qlook module
  • Loading branch information
astropenguin committed Nov 25, 2023
2 parents 9a1f508 + ad9ee4d commit e8bdfff
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 381 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
@@ -1,7 +1,7 @@
{
"name": "decode",
"image":"python:3.11",
"onCreateCommand": "pip install poetry==1.6.1",
"onCreateCommand": "pip install poetry==1.7.1",
"postCreateCommand": "poetry install",
"containerEnv": {
"POETRY_VIRTUALENVS_CREATE": "false"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pypi.yaml
Expand Up @@ -16,4 +16,4 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- run: pip install poetry && poetry publish --build
- run: pip install poetry==1.7.1 && poetry publish --build
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
- run: pip install poetry && poetry install
- run: pip install poetry==1.7.1 && poetry install
- run: black --check decode docs tests
- run: pyright decode docs tests
- run: pytest -v tests
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
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.8.0
date-released: 2023-11-12
version: 2.9.0
date-released: 2023-11-25
license: "MIT"
doi: "10.5281/zenodo.3384216"
url: "https://github.com/deshima-dev/decode"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,5 +11,5 @@ DESHIMA code for data analysis
## Installation

```shell
pip install decode==2.8.0
pip install decode==2.9.0
```
2 changes: 1 addition & 1 deletion decode/__init__.py
Expand Up @@ -10,7 +10,7 @@
"select",
"utils",
]
__version__ = "2.8.0"
__version__ = "2.9.0"


# submodules
Expand Down
18 changes: 9 additions & 9 deletions decode/assign.py
Expand Up @@ -14,7 +14,7 @@ def scan(
dems: xr.DataArray,
/,
*,
by: Literal["state"] = "state",
by: Literal["beam", "scan", "state"] = "state",
dt: Optional[np.timedelta64] = None,
inplace: bool = False,
) -> xr.DataArray:
Expand All @@ -33,16 +33,16 @@ def scan(
"""
if not inplace:
dems = dems.copy()
# deepcopy except for data
dems = dems.copy(data=dems.data)

is_cut = np.zeros_like(dems.scan, dtype=bool)
is_div = xr.zeros_like(dems.scan, dtype=bool)

if by == "state":
state = dems.state.values
is_cut |= np.hstack([False, state[1:] != state[:-1]])
ref = dems.coords[by].data
is_div[1:] |= ref[1:] != ref[:-1]

if dt is not None:
is_cut |= np.hstack([False, np.diff(dems.time) >= dt])
is_div[1:] |= np.diff(dems.time) >= dt

dems.scan.values[:] = np.cumsum(is_cut)
return dems
new_scan = is_div.cumsum().astype(dems.scan.dtype)
return dems.assign_coords(scan=new_scan)
85 changes: 44 additions & 41 deletions decode/convert.py
Expand Up @@ -2,7 +2,7 @@


# standard library
from typing import Optional, Union
from typing import Optional, Sequence, TypeVar, Union


# dependencies
Expand All @@ -12,90 +12,90 @@


# type hints
UnitLike = Union[Unit, str]
T = TypeVar("T")
Multiple = Union[Sequence[T], T]
UnitLike = Union[xr.DataArray, Unit, str]


def coord_units(
da: xr.DataArray,
coord_name: str,
coord_names: Multiple[str],
new_units: UnitLike,
/,
*,
equivalencies: Optional[Equivalency] = None,
) -> xr.DataArray:
"""Convert units of a coordinate of a DataArray.
"""Convert the units of coordinate(s) of a DataArray.
Args:
da: Input DataArray.
coord_name: Name of the coordinate to be converted.
coord_names: Name(s) of the coordinate(s) to be converted.
new_units: Units to be converted from the current ones.
A DataArray that has units attribute is also accepted.
equivalencies: Optional Astropy equivalencies.
Returns:
DataArray with the units of the coordinate converted.
"""
new_coord = units(da[coord_name], new_units, equivalencies=equivalencies)
return da.assign_coords({coord_name: new_coord})
# deepcopy except for data
da = da.copy(data=da.data)

if isinstance(coord_names, str):
coord_names = [coord_names]

def frame(
dems: xr.DataArray,
new_frame: str,
/,
*,
inplace: bool = False,
) -> xr.DataArray:
"""Convert skycoord frame of DEMS.
for coord_name in coord_names:
coord = da.coords[coord_name]
new_coord = units(coord, new_units, equivalencies)
da = da.assign_coords({coord_name: new_coord})

return da


def frame(da: xr.DataArray, new_frame: str, /) -> xr.DataArray:
"""Convert the skycoord frame of a DataArray.
Args:
dems: Target DEMS DataArray.
new_frame: Skycoord frame to be converted from the current ones.
inplace: Whether the skycoord frame are converted in-place.
da: Input DataArray.
new_frame: Frame to be converted from the current one.
Returns:
DEMS DataArray with the skycoord frame converted.
DataArray with the skycoord frame converted.
"""
if not inplace:
# deepcopy except for data
dems = dems.copy(data=dems.data)
# deepcopy except for data
da = da.copy(data=da.data)

if not new_frame == "relative":
raise ValueError("Relative is only available.")

lon, lon_origin = dems["lon"], dems["lon_origin"]
lat, lat_origin = dems["lat"], dems["lat_origin"]
cos = np.cos(Quantity(lat, lat.attrs["units"]).to("rad"))

if lon.attrs["units"] != lon_origin.attrs["units"]:
raise ValueError("Units of lon and lon_origin must be same.")

if lat.attrs["units"] != lat_origin.attrs["units"]:
raise ValueError("Units of lat and lat_origin must be same.")
lon = da.coords["lon"]
lat = da.coords["lat"]
lon_origin = da.coords["lon_origin"]
lat_origin = da.coords["lat_origin"]

# do not change the order below!
lon -= lon_origin
lon *= cos
lat -= lat_origin
lon_origin[:] = 0.0
lat_origin[:] = 0.0
lon -= units(lon_origin, lon)
lon *= np.cos(units(lat, "rad"))
lat -= units(lat_origin, lat)
lon_origin *= 0.0
lat_origin *= 0.0

return dems.assign_coords(frame=dems.frame.copy(False, new_frame))
new_frame = da.frame.copy(data=new_frame)
return da.assign_coords(frame=new_frame)


def units(
da: xr.DataArray,
new_units: UnitLike,
/,
*,
equivalencies: Optional[Equivalency] = None,
) -> xr.DataArray:
"""Convert units of a DataArray.
"""Convert the units of a DataArray.
Args:
da: Input DataArray.
new_units: Units to be converted from the current ones.
A DataArray that has units attribute is also accepted.
equivalencies: Optional Astropy equivalencies.
Returns:
Expand All @@ -105,5 +105,8 @@ def units(
if (units := da.attrs.get("units")) is None:
raise ValueError("Units must exist in DataArray attrs.")

if isinstance(new_units, xr.DataArray):
new_units = new_units.attrs["units"]

new_data = Quantity(da, units).to(new_units, equivalencies)
return da.copy(False, new_data).assign_attrs(units=str(new_units))
return da.copy(data=new_data).assign_attrs(units=new_units)

0 comments on commit e8bdfff

Please sign in to comment.