lezargus.library.conversion module#
Functions to convert things into something else.
Any and all generic conversions (string, units, or otherwise) can be found in here. Extremely standard conversion functions are welcome in here, but, sometimes, a simple multiplication factor is more effective.
- lezargus.library.conversion.convert_units(value: float | hint.NDArray, value_unit: hint.Unit | str, result_unit: hint.Unit | str) float | hint.NDArray [source]#
Convert a value from one unit to another unit.
We convert values using Astropy, however, we only convert raw numbers and so we do not handle Quantity variables. The unit arguments are parsed with
parse_astropy_unit()
if it is not a unit. This function is vectorized properly, of course, as it is generally just multiplication.- Parameters:
value (float or ndarray) – The value to convert.
value_unit (Unit or str) – The unit of the value we are converting. Parsing is attempted if it is not an Astropy Unit.
result_unit (Unit or str) – The unit that we are converting to. Parsing is attempted if it is not an Astropy Unit.
- Returns:
result – The result after the unit conversion.
- Return type:
float or ndarray
- lezargus.library.conversion.parse_astropy_unit(unit_string: str | hint.Unit | None) hint.Unit [source]#
Parse a unit string to an Astropy Unit class.
Although for most cases, it is easier to use the Unit instantiation class directly, Astropy does not properly understand some unit conventions so we need to parse them in manually. Because of this, we just build a unified interface for all unit strings in general.
- Parameters:
unit_string (str or Astropy Unit or None.) – The unit string to parse into an Astropy unit. If it is None, then we return a dimensionless quantity unit.
- Returns:
unit_instance – The unit instance after parsing.
- Return type:
Unit
- lezargus.library.conversion.parse_numpy_dtype(dtype_string: str | type) type [source]#
Parse a data type string to an Numpy data type.
We only support built-in normal Python types and the Numpy types. Unsupported types will not be converted by this function, and will raise an error.
- Parameters:
dtype_string (str) – The data type, either as a string representation (typical) or a type (for compatibility reasons). Only Numpy canonical names are accepted.
- Returns:
numpy_type – The data type after the conversion.
- Return type:
type