astrocook.recipes.edit module#

class astrocook.recipes.edit.RecipeEdit(session_v2: Session)[source]#

Bases: object

Recipes for general structure editing and arithmetic operations.

These methods are accessed via the session.edit attribute and delegate heavy logic to the Core structures Spectrum and SystemList.

__init__(session_v2: Session)[source]#
set_properties(name: str = '_current_', object: str = '_current_', z_em: float | str = '_current_', resol: str = '_current_', overwrite_resol_col: bool = False, yunit: str = '_current_', telluric_corrected: str = '_current_') → Session | None[source]#

Set session properties.

Set core session properties like name, object, redshift, and resolution. Delegates to astrocook.core.spectrum.Spectrum.with_properties().

Parameters:
  • name (str, optional) – The session Name (display label). Defaults to '_current_' (no change).

  • object (str, optional) – The object Name (header target). Defaults to '_current_' (no change).

  • z_em (float or str, optional) – Emission Redshift, above -1 and finite. Defaults to '_current_' (no change).

  • resol (str, optional) – Resolving Power R (e.g., 50000) OR Pixel FWHM (e.g., '3px'), positive and finite. Defaults to '_current_' (no change); an empty string keeps the current resolution too (FT8).

  • overwrite_resol_col (bool, optional) – If True, a resol column that varies with wavelength is replaced by a flat one at the new resolution; if False it is kept (a flat or missing column is always set). Defaults to False (#526: before, a variable column was flattened even with resol='_current_').

  • yunit (str, optional) – The unit of the flux, declared without converting the values (phase 6-quater, PR 8-bis): a flux density per unit wavelength (e.g. 'erg/(Angstrom s cm^2)') or 'arb_flux', the arbitrary units of Astrocook. Defaults to '_current_' (no change). y_convert() converts a flux that has a unit.

  • telluric_corrected (str, optional) – Whether the flux is already corrected for the telluric absorption (phase 7-ter, T1): 'yes' sets the flag TELLURIC_CORRECTED of the metadata, and the normalization is the continuum alone; 'no' clears it, and the normalization is the continuum times the telluric column, when there is one. Defaults to '_current_' (no change).

Returns:

A new Session with updated properties.

Return type:

Session

x_convert(z_ref: float | str = '_current_', xunit: str = 'km/s') → Session | None[source]#

Convert X axis.

Convert the x axis units and zero point to wavelength or velocity units. Delegates to astrocook.core.spectrum.Spectrum.x_convert().

Parameters:
  • z_ref (float or str, optional) – Reference redshift for velocity conversion, above -1 and finite; '_current_' uses the session’s z_em. Defaults to '_current_', as the GUI.

  • xunit (str, optional) – Unit of wavelength or velocity (e.g., 'nm', 'km/s'). Defaults to 'km/s'.

Returns:

A new Session with the converted X-axis.

Return type:

Session

y_convert(yunit: str = 'erg/(nm s cm^2)') → Session | None[source]#

Convert Y axis units.

Converts the flux and error arrays to the specified unit. Delegates to astrocook.core.spectrum.Spectrum.y_convert().

Parameters:

yunit (str, optional) – Target unit of flux density. Defaults to 'erg/(nm s cm^2)'.

Returns:

A new Session with the converted Y-axis.

Return type:

Session

apply_expression(target_col: str, expression: str, alias_map: Dict[str, str] | None = None) → Session[source]#

Apply expression to columns.

Apply a NumPy-style expression. Use column names (λ, F, cont…) as variables. Supports multi-session arithmetic via aliases. Delegates to astrocook.core.spectrum.Spectrum.apply_expression().

Parameters:
  • target_col (str) – Target column (to be overwritten or created).

  • expression (str) – Expression to evaluate (e.g., 'F / dF', 'F * 2.0').

  • alias_map (dict, optional) – Mapping of aliases to session names (e.g. {'s1': 'Session 1'}).

Returns:

A new Session with the computed column.

Return type:

Session

mask_expression(target_col: str, expression: str, alias_map: Dict[str, str] | None = None) → Session[source]#

Mask column by expression.

Mask a target column by setting values to NaN where the expression is True. Delegates to astrocook.core.spectrum.Spectrum.mask_expression().

Parameters:
  • target_col (str) – Target column to mask (e.g., 'F').

  • expression (str) – Boolean expression (e.g., '(λ < 300) | (λ > 400)', 'dF <= 0').

  • alias_map (dict, optional) – Mapping of aliases to session names.

Returns:

A new Session with the masked column.

Return type:

Session

smooth_column(target_col: str = 'dF', sigma_kms: float = 100.0, renorm_model: bool = False) → Session[source]#

Smooth a single column.

Apply a Gaussian filter to a single data column. Delegates to astrocook.core.spectrum.Spectrum.smooth_column().

Parameters:
  • target_col (str, optional) – Target column to smooth. Defaults to 'dF'.

  • sigma_kms (float, optional) – Standard deviation for Gaussian kernel (km/s), positive. Defaults to 100.0.

  • renorm_model (bool, optional) – If True and smoothing 'cont', the 'model' column is re-normalized. Defaults to False.

Returns:

A new Session with the smoothed column.

Return type:

Session

import_systems(source_session: str | Session, append: bool = True, refit: bool = False) → Session[source]#

Import systems from session.

Copy absorption systems from another open session into this one, filtering those that fall outside the current spectral range. Delegates filtering to astrocook.core.system_list.SystemList.filter_by_range().

Parameters:
  • source_session (str) – Name of the open session to import from (required; in a script, also the session itself).

  • append (bool, optional) – If True, adds to existing systems. If False, replaces them. Defaults to True.

  • refit (bool, optional) – If True, runs a refit on the imported systems. Defaults to False.

Returns:

A new Session with imported systems.

Return type:

Session

import_systems_ascii(file_path: str, append: bool = True, refit: bool = False) → Session[source]#

Import systems from an ASCII file (issue #480).

Reads a table with a header, in any format astropy can guess (CSV, whitespace-separated, …), e.g. the _systems.csv written by astrocook.recipes.file.RecipeFile.export_ascii(). Required columns: series and z; optional: logN (default 13.0), b (default 10.0 km/s), btur (default 0), dz, dlogN, db. Other columns are ignored. Components outside the spectral range are skipped.

Parameters:
  • file_path (str) – Path of the file.

  • append (bool, optional) – If True, add to the existing systems; otherwise replace them. Defaults to True.

  • refit (bool, optional) – If True, refit the systems after importing. Defaults to False.

Returns:

A new session with the imported systems (the session as it was if the file holds no valid row).

Return type:

Session

Raises:

RecipeFailed – If the file cannot be read or lacks the required columns.

delete(targets: str) → Session[source]#

Delete elements.

Remove columns from the spectrum or clear the line list. Delegates to astrocook.core.spectrum.Spectrum.remove_columns().

Parameters:

targets (str) – Comma-separated list of items to delete (e.g. 'cont, model'), columns of the spectrum other than its axis, flux and error. Use 'systems' (or 'lines') to clear the component list.

Returns:

A new Session with the elements removed.

Return type:

Session

Raises:

InvalidParameter – If targets names nothing, a column of every spectrum (λ, λmin, λmax, F, dF) or a column that the spectrum does not have (phase 6-quater, PR 9: each gave a WARNING and an unchanged state, recorded as done).

split(expression: str, alias_map: Dict[str, str] | None = None) → Session[source]#

Split spectrum by expression.

Extract a portion of the spectrum into a new session using a boolean expression. Only rows where the expression is True are kept. Delegates to astrocook.core.spectrum.Spectrum.split().

Parameters:
  • expression (str) – Boolean expression to select data (e.g., '(λ > 400) & (λ < 500)'); required (no default since phase 6-quater, PR 9).

  • alias_map (dict, optional) – Mapping for multi-session variables.

Returns:

A new Session containing the subset of data.

Return type:

Session

extract_preset(region: str = 'lya_forest') → Session | List[Session][source]#

Extract region by preset.

Extract a specific standard region (like the Ly-alpha forest) into a new session, based on the current emission redshift (z_em). Delegates to astrocook.core.spectrum.Spectrum.get_region_bounds() and split().

Parameters:

region (str, optional) – Preset region identifier. Options: 'lya_forest', 'all_ly_forest', 'red_side'. Can be a comma-separated list. Defaults to 'lya_forest'.

Returns:

One or more new Session instances.

Return type:

Session or List[Session]

Raises:

InvalidParameter – If region names no region, or one that is not a preset (phase 6-quater, PR 9: an unknown one was skipped with a WARNING).

trim_common(other_sessions: str | List[Session | str], z_target: float | str, trans_self: str, trans_others: str, window_kms: float = 500.0) → Session[source]#

Trim to common velocity coverage.

Trims the current session to the velocity range shared by a list of other sessions (intersection). Delegates to astrocook.core.spectrum.Spectrum.calc_intersection_expression().

Parameters:
  • other_sessions (str or list) – Comma-separated names of other sessions (in a script, also a list of the sessions themselves). others_names until phase 6-quater, PR 9, still accepted.

  • z_target (float) – Target redshift, above -1 and finite.

  • trans_self (str) – Transition for current session (e.g. 'Ly_a'), as named in the atomic data.

  • trans_others (str) – Comma-separated transitions for other sessions, one per session.

  • window_kms (float, optional) – Symmetric window width in km/s, positive. Defaults to 500.0.

Returns:

A new Session trimmed to the common coverage.

Return type:

Session

Raises:

InvalidParameter – If a transition is not in the atomic data, or the transitions are not one per session (phase 6-quater, PR 9: an unknown transition of this session gave “no range in common”, one of another session was ignored).

stitch(other_sessions: str | List[Session | str], sort: bool = True) → Session[source]#

Merge multiple sessions.

Concatenates the current session with a list of other sessions. Delegates to astrocook.core.spectrum.Spectrum.stitch().

Parameters:
  • other_sessions (str or list) – Comma-separated names of the other sessions, or a list of names or (in a script) of Session objects. Their flux and axis are converted to the units of this session.

  • sort (bool, optional) – If True, sorts the final spectrum by wavelength. Defaults to True.

Returns:

A new Session containing the merged data.

Return type:

Session

Raises:

RecipeFailed – If a name is not that of an open session (phase 6-quater, PR 9: skipped with a WARNING), or a flux or an axis cannot be converted to the units of this session.

equalize_and_stitch(other_sessions: str | List[Session | str], stitch_wavelengths: str, equalize_ranges: str = 'auto', manual_factors: str = 'auto') → Session[source]#

Equalize and stitch arms.

Equalize flux levels and stitch multiple sessions at specific cut-off wavelengths. This recipe is designed to seamlessly combine data from multi-arm spectrographs (e.g., X-Shooter’s UVB, VIS, and NIR arms).

The procedure enforces a strict blue-to-red processing order regardless of input order. For $N$ total sessions, exactly $N-1$ parameters must be provided for cuts, ranges, and factors. The arms are converted to the units of the axis and of the flux of the bluest (an arm in arbitrary units of flux takes those of the bluest once scaled; phase 6-quater, PR 9, F132, F134).

Parameters:
  • other_sessions (str or list) – Comma-separated names of the other sessions to stitch to the current one (in a script, also a list of the sessions themselves). (This parameter is hidden in the GUI dialog).

  • stitch_wavelengths (str) – Comma-separated cut-off wavelengths (in nm), increasing, where the arms join. Data bluer than a cut is taken from the bluer arm; data redder is taken from the redder arm. Requires $N-1$ values.

  • equalize_ranges (str, optional) – Comma-separated overlap ranges formatted as 'min-max, min-max' (in nm) used to calculate the scaling factor between adjacent arms. If set to 'auto', the recipe attempts a global match. Defaults to 'auto'.

  • manual_factors (str, optional) – Comma-separated fixed positive multipliers to scale adjacent arms. If provided, these override the equalize_ranges calculation. Defaults to 'auto'.

Returns:

A new Session containing the equalized and stitched spectrum.

Return type:

Session

coadd(session_names: str | List[Session | str], xstart: float | None = None, xend: float | None = None, dx: float = 10.0, xunit: str = 'km/s', kappa: float | None = 5.0, equalize_order: int | None = 0) → Session[source]#

Co-add multiple sessions.

Stitches multiple sessions together and rebins them onto a single common grid in one pass (Inverse Variance Weighting). Delegates to astrocook.core.spectrum.Spectrum.coadd().

Parameters:
  • session_names (str or list) – Comma-separated names of sessions to co-add, this one among them (in a script, also a list of the sessions themselves). The first is the reference: the others are converted to the units of its axis and flux, and scaled to its flux (F133, F134).

  • xstart (float or None, optional) – Start wavelength (nm). None for auto. Defaults to None.

  • xend (float or None, optional) – End wavelength (nm), above xstart. None for auto. Defaults to None.

  • dx (float, optional) – Step size for the final grid, positive. Defaults to 10.0 (km/s), as the GUI: choose the native pixel size for high-resolution data (e.g. ~2.5 km/s for UVES, ~0.8 km/s for ESPRESSO).

  • xunit (str, optional) – Unit for the step size: a velocity or a wavelength (e.g., 'km/s' or 'nm'). Defaults to 'km/s'.

  • kappa (float or None, optional) – Sigma clipping threshold, positive. None for off. Defaults to 5.0.

  • equalize_order (int or None, optional) – Order of the polynomial (0 to 5) that scales each session to the first: 0 a constant factor, None no scaling (-1, the value of no scaling until phase 6-quater, PR 9, F123, is read as None). Defaults to 0.

Returns:

A new Session containing the co-added spectrum.

Return type:

Session

Raises:
  • InvalidParameter – If session_names names no session or not this one, xstart is not below xend (an empty spectrum) or xunit is neither a velocity nor a wavelength (phase 6-quater, PR 9).

  • RecipeFailed – If a name is not that of an open session (it was skipped without a word), or a flux or an axis cannot be converted.