astrocook.recipes.file module#

class astrocook.recipes.file.RecipeFile(session_v2: Session)[source]#

Bases: object

Recipes for file input/output operations.

This class manages exporting session data (spectra and system lists) to external formats like ASCII/CSV, and importing modified data back into the session. It operates immutably where applicable to support Undo/Redo.

__init__(session_v2: Session)[source]#
export_ascii(targets: str, folder: str, prefix: str, overwrite: bool = False) → bool[source]#

Export selected components of the session to ASCII/CSV files.

Generates distinct files for spectrum data (_spec.csv) and system lists (_systems.csv) based on the requested targets. Spectrum exports automatically include the wavelength array (lam), while system exports automatically include the transition name (series) and redshift (z).

The spectrum is written as ECSV (the CSV format of astropy, phase 6-quater, PR 8-bis, F129): a header of comment lines with the unit of each column and the emission redshift, then the names and the values separated by commas (pandas.read_csv(path, comment='#') reads it). The columns have the names of the GUI in ASCII: lam, lammin, lammax, F, dF and the others by their own name (before: x, xmin, xmax, y, dy, without units). Opened again, the file gives the same columns and units.

Parameters:
  • targets (str) – Comma-separated list of structures or columns to export (e.g., 'spec, systems' or 'F, cont, logN').

  • folder (str) – The folder where the files are written (path until phase 6-quater, PR 7: the old name is still accepted).

  • prefix (str) – The base filename prefix for the exported files.

  • overwrite (bool, optional) – Replace the files if they exist. If False (the default) and a file exists, nothing is written and the recipe fails naming the files (F106: they were overwritten without a word outside the GUI, which asks before and passes True).

Returns:

True if at least one file was saved, False if the targets name nothing to export.

Return type:

bool

Raises:

RecipeError – If a file cannot be written (phase 6-bis, PR 3: it returned False).

export_ascii_outputs(targets: str) → List[str][source]#

The files that export_ascii() writes for these targets.

The GUI asks this before exporting, to suggest a file name and to check for files that would be overwritten (F82: it used to decide it by itself, without the names of the columns shown in the GUI).

Parameters:

targets (str) – Comma-separated list of structures or columns, as for export_ascii().

Returns:

'spec' (the _spec.csv file) and/or 'systems' (the _systems.csv file), in this order.

Return type:

list of str

import_ascii(file_path: str) → Session[source]#

Import columns from an ASCII/CSV file into the current session.

Reads external data and overwrites or appends columns in the spectrum based on matching column headers. The imported file must have the exact same number of rows as the current spectrum to ensure grid alignment.

Parameters:

file_path (str) – The full path to the ASCII/CSV file to import.

Returns:

A new session instance containing the imported data.

Return type:

Session

Raises:

RecipeError – If the file cannot be read, or its length is not that of the spectrum.

import_model(file_path: str = '', source_session: str | Session = '', role: str = 'telluric', column: str = '', renorm_model: bool = True, normalized: bool = False) → Session[source]#

Import an external model into the spectrum (phase 7-ter, PR T2).

A model is a column of the spectrum with a role. The model is read from a file or from another session, brought onto the grid of the spectrum and written in the column of its role, replacing it if it exists (undo, log and archive as for any recipe):

  • the grid: a copy when the axis of the model is that of the spectrum within a tenth of a pixel, otherwise the average of the model over each pixel of the spectrum (xmin-xmax), each pixel of the model weighted by its overlap (astrocook.core.model_grid); a pixel the model does not cover in full gets the value of the role beyond the model;

  • the role telluric: a transmission (dimensionless, 0-1) written in the column telluric, 1 beyond the model; the normalization is cont × telluric unless the flux is flagged as corrected (set_properties(telluric_corrected=...));

  • the role cont (T3): a continuum, in the unit of the flux, written in the column cont, NaN beyond the model (there the flux is not normalized; a WARNING says on how many pixels). A unit of the source is converted to that of the flux, refused if it cannot be; without a unit the values are taken in the unit of the flux, and refused when their median and that of the flux, over the pixels covered, differ by more than a factor 10 (a normalized continuum under a physical flux). The model of the spectrum follows the new continuum if renorm_model, as in fit_continuum (kept as it was where there is no continuum);

  • the role model (T4): an absorption model written in the column model, the normalization of the spectrum (cont × telluric) beyond it (NaN where the spectrum has none; a WARNING says on how many pixels). In the unit of the flux, as a continuum (converted from a unit, refused if it cannot be; without a unit refused when it is not on the scale of the flux); or normalized (normalized=True: dimensionless, 0-1), multiplied by the normalization at the import, so the spectrum needs a continuum. The column is not tied to the system list: a fit, or a change of the components that redraws the model (add_component, update_component, delete_component, …), overwrites it with the model of the components.

The models are taken in the frame of the spectrum: no shift. A file is read by the names of its columns (astrocook.io.tables): a text table separated by spaces, tabs or commas with a header, also commented (# x xmin xmax trans_molecfit dy), the CSV of export_ascii, a FITS table (Astrocook, the Molecfit outputs with mtrans); the axis is x, lam, lambda, wave or wavelength (in nm in a text file without units), its edges xmin-xmax (lammin-lammax) if the file has them. The other columns are ignored (the dy of a Molecfit model is the error of the science spectrum, not of the model).

Parameters:
  • file_path (str) – The file of the model; empty if it is in another session.

  • source_session (str or Session) – The open session that holds the model (its name; in a script also the session itself); empty if it is in a file.

  • role (str) – 'telluric', 'cont' or 'model'.

  • column (str) – The column of the model in the source; empty: found by the names of the role (for 'telluric': telluric, telluric_model, trans_molecfit, mtrans; for 'cont': cont, continuum; for 'model': model; ignoring the case).

  • renorm_model (bool) – With the role 'cont', rescale the model of the spectrum to the new continuum (the GUI asks); ignored by the other roles.

  • normalized (bool) – With the role 'model': the model is normalized (0-1) and is multiplied by the normalization of the spectrum; refused with the other roles.

Returns:

A new session with the model in its column.

Return type:

Session

Raises:

RecipeError – If neither or both sources are given, the source cannot be read or has no column of the model, its values cannot be this model, or the model does not overlap the spectrum.