Editing Data#

Spectra rarely come ready-to-use out of the box. You might need to update metadata, rescale flux values, isolate specific regions, or combine multiple exposures. This tutorial covers the Edit menu, which serves as the “Swiss Army knife” for these operations.

1. History Management (Undo/Redo)#

Astrocook tracks every operation you perform in a session. If you make a mistake or want to test a different parameter:

  • Undo: Press Ctrl+Z (or Cmd+Z) or select Edit > Undo.

  • Redo: Press Ctrl+Y (or Cmd+Shift+Z) or select Edit > Redo.

2. Setting Session Properties#

Before running complex algorithms, ensure your session metadata is correct. Many Astrocook recipes (like the Ly-\(\alpha\) forest analysis) rely on the emission redshift (\(z_\mathrm{em}\)) to determine rest-frame wavelengths.

  1. Select your session in the Session List (left sidebar).

  2. Go to Edit > Set Properties….

  3. A dialog will appear, showing the current values. Here you can update:

    • Name: The label used in the list (it cannot be empty).

    • Object: The target name (often from the FITS header).

    • Emission Redshift (\(z_\mathrm{em}\)): Set this carefully!

    • Flux unit (yunit): The unit of the flux, declared without changing the values: a flux density per unit wavelength (e.g. erg/(Angstrom s cm^2), 1e-17 erg/(Angstrom s cm^2)), or arb_flux for arbitrary units. It is given to the flux, its error, the continuum and the model. Use it when the file did not say the unit (see the unit of the flux); to convert a flux that has a unit, use Convert Y axis.

    • Telluric correction (telluric_corrected): Whether the flux is already corrected for the telluric absorption. no (as for every spectrum that does not say otherwise): the fits normalize the flux by the continuum times the telluric transmission, the telluric column, when the spectrum has one. yes: the tellurics are already divided out of the flux, and the normalization is the continuum alone; the telluric column is kept, as a record of where the correction was made. The choice is kept in the archive (the flag TELLURIC_CORRECTED of the metadata).

    • Resolution (\(R\)): You can enter a resolving power (e.g., 50000) or a pixel width (e.g., 3px, with the x axis in wavelength). The resolution is stored in the resol column of the spectrum, one value per pixel, and the fits use the value of each pixel. If the resolution varies with wavelength (e.g. after co-adding spectra of different resolution), the field is left empty, with the median as a hint, and the column is kept: if you type a value there, Astrocook asks whether to replace the column with it.

    A field left empty keeps the current value.

The Set Properties dialog

3. Arithmetic on Columns#

Astrocook allows you to perform mathematical operations on any data column (Flux F, Error dF, Wavelength λ, etc.) using NumPy-style syntax.

Note

The names of the columns. Everywhere a column is named (expressions, the fields of the recipes, scripts, the MCP server) the names are those of the table of the GUI: λ, λmin, λmax, F, dF, and the other columns by their own name (cont, model, resol, …). Where a plain ASCII name is handier, λ can be written lam (lammin, lammax). The names that the data have in the archives (and had in the CSV files of Export ASCII before version 2.7), x, xmin, xmax, y, dy, are accepted too, so the logs and the scripts written with them still work.

General Arithmetic#

You can modify existing columns or create new ones.

  1. Go to Edit > Apply Expression….

  2. Target Column: Enter the name of the column to overwrite (e.g., F) or create (e.g., F_corr): letters, digits and _, not starting with a digit. The two fields start empty.

  3. Expression: Enter your formula (e.g., F * 1e17).

  4. Click Run.

The expressions are evaluated by NumExpr: the arithmetic operators, the comparisons, and its functions (sqrt, exp, log, log10, abs, sin, cos, where, and sum, prod, min, max, which reduce the whole column to one number). The axis (λ, λmin, λmax) cannot be the target: use Convert X axis to change its unit. A new column has no unit, whatever the expression.

Tip

The recipe dialog features toolbars above the input fields. Click buttons like F, λ, log10, or sqrt to instantly insert them into your expression syntax error-free.

Creating New Columns#

You can also create new auxiliary columns. For example, to create a column representing the Signal-to-Noise Ratio:

  1. Target Column: snr (A new name).

  2. Expression: F / dF.

  3. Once created, you can visualize this new column using the Auxiliary column dropdown in the Plot Controls.

Applying a mathematical expression to a column

4. Advanced Operations#

The Edit menu provides specialized tools for cleaning and organizing your data structures.

Masking Data#

To exclude bad data points (e.g., dead pixels or negative errors) without deleting rows:

  1. Go to Edit > Mask by Expression….

  2. Target Column: The column to mask (e.g., F); not the axis (λ, λmin, λmax).

  3. Expression: A boolean condition (e.g., dF <= 0 or λ < 350).

  4. Values satisfying this condition will be set to NaN.

Converting the Axes#

  • Edit > Convert X axis…: converts the axis to another unit of wavelength (nm, Angstrom) or to velocity (km/s). The zero of the velocity is the Lyman-α emission at the reference redshift, 121.567 nm × (1 + z_ref); z_ref is \(z_\mathrm{em}\) if left at _current_.

  • Edit > Convert Y axis…: converts the flux, its error and the columns in the same unit (cont, model, …) to another unit of flux density per unit wavelength, e.g. erg/(Angstrom s cm^2). A unit per frequency (Jy) is not converted. A continuum or a model without a unit (in archives saved by earlier versions) is taken in the unit of the flux and converted with it. A flux in arbitrary units cannot be converted: declare its unit with Set Properties, or calibrate it.

Warning

The spectrum does not remember the z_ref of a conversion to velocity: to go back to wavelength, convert with the same z_ref, and without changing \(z_\mathrm{em}\) in between if z_ref was left at _current_; otherwise the wavelengths come back shifted.

Smoothing Single Columns#

While the Flux menu smooths the whole spectrum, you might want to smooth only a specific auxiliary column (like a noise array or a background fit).

  • Edit > Smooth Column…: Applies a Gaussian filter to a specific target column (e.g., running_std).

Deleting Elements#

To clean up your session:

  • Edit > Delete Elements…: Enter a comma-separated list of items to remove.

    • Column names (e.g., cont, snr) to remove data arrays. The columns of every spectrum (λ, λmin, λmax, F, dF) cannot be removed, and a name that is not a column of the spectrum stops the recipe (the message lists the columns).

    • Keywords lines or systems to clear the entire absorption system list.

Importing Systems#

If you have analyzed the same object in a different session (e.g., a different exposure), you can copy its absorption lines to the current one.

  1. Go to File > Import Systems from Session….

  2. Source Session: Select the other open session by name (required).

  3. Append: Check this to add to your current list; uncheck to replace it entirely.

5. Slicing Sessions (Split & Extract)#

Sometimes you only want to work on a specific piece of a spectrum, such as the Ly-\(\alpha\) forest or a specific metal line.

Manual Split#

To extract a custom range into a new session:

  1. Go to Edit > Split Out Region….

  2. Enter a boolean expression (the field starts empty). Use the dialog toolbar to insert the wavelength variable λ.

    • Example: (λ > 400) & (λ < 500)

  3. A new session containing only that data range will appear in your list.

You can also select the range with the mouse: choose the Select tool in the plot toolbar, drag over the region and pick Split region (see Navigating the Plot), or right-click the plot and choose Split from Current Zoom to split what the plot shows.

Quick Extraction (Presets)#

Astrocook knows standard quasar regions. If your \(z_\mathrm{em}\) is set:

  1. Go to Edit > Extract Preset Region….

  2. Region: Choose a preset (lya_forest, all_ly_forest, red_side) or enter multiple presets separated by commas to extract them all at once (e.g., lya_forest, red_side). A name that is not a preset stops the recipe.

  3. The software automatically calculates the observed bounds and creates new sessions for each region.

Note

This recipe requires Emission Redshift (\(z_{em}\)) to be defined. If missing, Astrocook will automatically prompt you to set them via the Set Properties dialog before proceeding.

6. Combining Sessions (Stitch & Co-add)#

If you have multiple exposures of the same object, you can combine them directly from the Session List.

The recipes that combine sessions name the other sessions by the names of the Session List, separated by commas; a name that is not that of an open session stops the recipe. In a Python script, where no other session is open, give the sessions themselves (e.g. session.edit.stitch([red, nir]), session.edit.coadd([session, other])); the session log records their names.

Units. The sessions are combined in the units of one of them, the reference (this session for Stitch, the bluest arm for Equalize and Stitch, the first session listed for Co-add): the others are converted to the unit of its axis (e.g. Å to nm) and of its flux (e.g. erg cm⁻² s⁻¹ nm⁻¹ to erg cm⁻² s⁻¹ Å⁻¹) before they are scaled or joined. A flux in arbitrary units (arb_flux, see Setting Session Properties) cannot be converted to a physical one: it is combined with it only when it is scaled to it (by Equalize and Stitch, or by Co-add with equalize_order not None), and then takes its unit. A normalized flux (no unit) is never combined with one that is not. An axis in velocity is converted to wavelengths first with Convert X axis.

Co-adding Spectra#

This operation defines common grid and rebins all spectra at once into it (weighting the contributions by their inverse variance and the amount of overlap with the final bins). The errors are those of the weighted mean, and the optional kappa clipping rejects only upward outliers (spikes), as in Flux > Rebin Spectrum (see Flux Operations).

  1. Load all the spectra you wish to combine.

  2. In the Session List, hold Ctrl (or Cmd on Mac) and click to select multiple sessions.

  3. Right-click on the selection.

  4. Choose Co-add N sessions….

  5. A dialog will ask for the grid parameters (e.g., step size dx; xstart must be below xend).

  6. A new, combined session will be created.

The first session listed is the reference: the others are scaled to its flux by a polynomial of order equalize_order (0, a constant factor, the default, up to 5), or not scaled if it is None (the session logs written before version 2.7 say -1, which is the same).

Note

You can also access this tool via Edit > Co-add Spectra…. This opens a dialog where you can manually type the names of the sessions to combine (comma-separated, this session among them), which is useful for scripting.

Stitching#

If you have spectra covering different wavelength ranges (e.g., Blue and Red arms of a spectrograph), use Stitch:

  1. Select the sessions as above.

  2. Right-click and choose Stitch N sessions….

  3. This concatenates the data arrays without resampling.

    Note

    The recipe automatically cleans up NaN values (e.g., gaps between detectors) to ensure the resulting plot is continuous and does not suffer from “scatter-plot” artifacts. If the stitched spectra overlap, the pixels of the overlap region are interleaved: the result is shown as a scatter plot instead of a line (for up to 20000 visible points; zoom in on longer spectra), so that the overlap is clearly visible. Rebinning or co-adding returns to the line view.

Equalize and Stitch Arms#

When working with multi-arm spectrographs (like X-Shooter), the flux calibration between adjacent arms (e.g., UVB and VIS) might slightly mismatch in the overlap region. This tool automatically scales the flux and stitches the arms at precise cut-off wavelengths.

  1. Load the \(N\) sessions representing your spectral arms.

  2. In the Session List, hold Ctrl (or Cmd on Mac) and click to select them.

  3. Right-click and choose Equalize and Stitch N Arms….

  4. A dialog will appear asking for exact parameters to guide the combination:

    • stitch_wavelengths: Enter the precise wavelengths (in nm) where one arm should stop and the next should begin (e.g., 550, 1000), increasing. You must provide exactly \(N-1\) values, and each arm must have pixels between its cuts.

    • equalize_ranges: (Optional) Specify the exact overlap window (e.g., 545-555, 995-1005) Astrocook should use to calculate the flux scaling factor. Leave as auto to attempt a global match.

    • manual_factors: (Optional) If you already know the exact flux multipliers (positive), enter them here to override automatic calculation.

Astrocook automatically sorts the arms from bluest to reddest before processing, ensuring a mathematically continuous final spectrum.

Context menu for co-adding multiple sessions

Resolution Tracking during Operations#

When combining multiple spectra, Astrocook rigorously tracks instrumental resolving power (\(R\)) pixel-by-pixel:

  • Stitching: When stitching spectra end-to-end, their respective resol columns are seamlessly concatenated.

  • Co-adding: When co-adding overlapping spectra, Astrocook calculates the new effective resolution using an Inverse Variance Weighted (IVW) average of the input resolutions (weighted by \(1/R^2\)). Because co-addition combines signals in parallel, the lower-resolution data will safely dominate the overlap region, preserving the physical accuracy of the Line Spread Function.

  • Safety First: To prevent scientifically invalid models, co-addition enforces an “all or nothing” rule. If you attempt to co-add a mix of spectra where some have known resolutions and others do not, Astrocook will warn you and safely drop the resol column from the final output.