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_to_fits_header_types(input_data: object) str | int | float | bool | Undefined[source]#

Convert any input into something FITS headers allow.

Per the FITS standard, the allowable data types which values entered in FITS headers is a subset of what Python can do. As such, this function converts any type of reasonable input into something the FITS headers would allow. Note, we mostly do basic checking and conversions. If the object is too exotic, it may cause issues down the line.

In general, only strings, integers, floating point, boolean, and no values are allowed. Astropy usually will handle further conversion from the basic Python types so we only convert up to there.

Parameters:

input_data (object) – The input to convert into an allowable FITS header keyword.

Returns:

header_output – The output after conversion. Note the None is not actually a None type itself, but Astropy’s header None/Undefined type.

Return type:

str, int, float, bool, or None

lezargus.library.conversion.convert_units(value: float | ndarray, value_unit: Unit | str, result_unit: Unit | str) float | 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) 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) – 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