mirror of
https://github.com/Richard-Sti/csiborgtools_public.git
synced 2025-05-24 03:21:10 +00:00
Box units conversion (#3)
* linting * fix long line * rename nb * rename import * add group catalog * move imports out of functions * add array_to_structured * add references * fix subsampling * fix coord bug * add 2M++ dists * save nb * fix comment * add snapshot path * add snapshot path * add read_info * Move transforms * add import radec * expand docs * Move flipcols * update nb * add flip_cols * create file * add blank line * Move units transfs * add blank line * add units import * rm imports * add import * add box_units * add comments
This commit is contained in:
parent
c7665b8136
commit
942c36b142
18 changed files with 6730 additions and 1574 deletions
|
@ -13,6 +13,6 @@
|
|||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
from .recarray_manip import (cols_to_structured, add_columns, rm_columns,
|
||||
list_to_ndarray)
|
||||
from .transforms import cartesian_to_radec
|
||||
from .recarray_manip import (cols_to_structured, add_columns, rm_columns, # noqa
|
||||
list_to_ndarray, array_to_structured, # noqa
|
||||
flip_cols) # noqa
|
||||
|
|
|
@ -12,11 +12,14 @@
|
|||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
"""
|
||||
Utilility functions for manipulation structured arrays.
|
||||
"""
|
||||
|
||||
"""Utilility functions for manipulation structured arrays."""
|
||||
|
||||
import numpy
|
||||
|
||||
|
||||
def cols_to_structured(N, cols):
|
||||
"""
|
||||
Allocate a structured array from `cols`.
|
||||
|
@ -84,6 +87,7 @@ def add_columns(arr, X, cols):
|
|||
|
||||
return out
|
||||
|
||||
|
||||
def rm_columns(arr, cols):
|
||||
"""
|
||||
Remove columns `cols` from a record array `arr`. Creates a new array.
|
||||
|
@ -143,7 +147,7 @@ def list_to_ndarray(arrs, cols):
|
|||
raise TypeError("`arrs` must be a list of structured arrays.")
|
||||
cols = [cols] if isinstance(cols, str) else cols
|
||||
|
||||
Narr = len(arrs)
|
||||
Narr = len(arrs)
|
||||
Nobj_max = max([arr.size for arr in arrs])
|
||||
Ncol = len(cols)
|
||||
# Preallocate the array and fill it
|
||||
|
@ -153,3 +157,55 @@ def list_to_ndarray(arrs, cols):
|
|||
for j in range(Ncol):
|
||||
out[i, :Nobj, j] = arrs[i][cols[j]]
|
||||
return out
|
||||
|
||||
|
||||
def array_to_structured(arr, cols):
|
||||
"""
|
||||
Create a structured array from a 2-dimensional array.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
arr : 2-dimensional array
|
||||
Original array of shape `(n_samples, n_cols)`.
|
||||
cols : list of str
|
||||
Columns of the structured array
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : structured array
|
||||
The output structured array.
|
||||
"""
|
||||
cols = [cols] if isinstance(cols, str) else cols
|
||||
if arr.ndim != 2 and arr.shape[1] != len(cols):
|
||||
raise TypeError("`arr` must be a 2-dimensional array of "
|
||||
"shape `(n_samples, n_cols)`.")
|
||||
|
||||
dtype = {"names": cols, "formats": [arr.dtype] * len(cols)}
|
||||
out = numpy.full(arr.shape[0], numpy.nan, dtype=dtype)
|
||||
for i, col in enumerate(cols):
|
||||
out[col] = arr[:, i]
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def flip_cols(arr, col1, col2):
|
||||
"""
|
||||
Flip values in columns `col1` and `col2`. `arr` is passed by reference and
|
||||
is not explicitly returned back.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
arr : structured array
|
||||
The array whose columns are to be converted.
|
||||
col1 : str
|
||||
The first column name.
|
||||
col2 : str
|
||||
The second column name.
|
||||
|
||||
Returns
|
||||
-------
|
||||
nothing
|
||||
"""
|
||||
dum = numpy.copy(arr[col1])
|
||||
arr[col1] = arr[col2]
|
||||
arr[col2] = dum
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
# Copyright (C) 2022 Richard Stiskalek
|
||||
# This program is free software; you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License as published by the
|
||||
# Free Software Foundation; either version 3 of the License, or (at your
|
||||
# option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
# Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
import numpy
|
||||
|
||||
|
||||
def cartesian_to_radec(arr, xpar="peak_x", ypar="peak_y", zpar="peak_z", degrees=True):
|
||||
"""
|
||||
Extract `x`, `y`, and `z` coordinates from a record array `arr` and
|
||||
calculate their spherical coordinates representation.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
arr : record array
|
||||
Record array with the Cartesian coordinates.
|
||||
xpar : str, optional
|
||||
Name of the x coordinate in the record array.
|
||||
ypar : str, optional
|
||||
Name of the y coordinate in the record array.
|
||||
zpar : str, optional
|
||||
Name of the z coordinate in the record array.
|
||||
degrees : bool, optional
|
||||
Whether to return angles in degrees. By default `True`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
dist : 1-dimensional array
|
||||
Radial distance.
|
||||
ra : 1-dimensional array
|
||||
Right ascension.
|
||||
dec : 1-dimensional array
|
||||
Declination.
|
||||
"""
|
||||
x, y, z = arr[xpar], arr[ypar], arr[zpar]
|
||||
|
||||
dist = numpy.sqrt(x**2 + y**2 + z**2)
|
||||
dec = numpy.arcsin(z / dist)
|
||||
ra = numpy.arctan2(y, x)
|
||||
|
||||
if degrees:
|
||||
dec = numpy.rad2deg(dec)
|
||||
ra = numpy.rad2deg(ra)
|
||||
|
||||
return dist, ra, dec
|
Loading…
Add table
Add a link
Reference in a new issue