{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 0\n", "\n", "## Generation of Example Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial generates example data to be used in some of the other tutorials. The example data can be downloaded from [here](https://github.com/almoulla/arve/tree/main/docs/tutorials/example_data)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We begin by importing the `ARVE` package and other useful packages." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import packages\n", "import arve\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Synthetic Stellar Spectral Time Series" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this part, we simulate a stellar spectral time series. The spectral time series consists of 100 observations each containing the same 100 spectral lines. 95% of the lines are displaced with a sinusoidal signal of 10 m/s amplitude, whereas the remaining 5% of the lines are randomly shifted by RVs drawn from a $\\mathcal{G}(\\mu=100\\,\\mathrm{m/s}, \\sigma=100\\,\\mathrm{m/s})$ distribution acting as spurious outliers (from, e.g., telluric contamination). The spectra are thereafter noised to a S/N of 200." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Parameters\n", "N_spec = 100 # nr. of spectra\n", "N_line = 100 # nr. of lines\n", "\n", "# Constants\n", "c = 2.99792458e5 # [km/s] speed of light in vacuum\n", "\n", "# Initiate ARVE object\n", "example = arve.ARVE()\n", "example.star.target = \"Sun\"\n", "example.star.get_stellar_parameters()\n", "example.data.add_data(wave_val=np.arange(3000,3100,0.01).reshape(1,-1))\n", "example.data.get_aux_data()\n", "\n", "# Get line mask\n", "mask = example.data.aux_data[\"mask\"][0]\n", "mask = mask[:N_line]\n", "wc = mask[\"wave\" ].values\n", "dc = mask[\"depth\"].values\n", "\n", "# Wavelength grid\n", "wave_min = wc[ 0] - 0.5\n", "wave_max = wc[-1] + 0.5\n", "wave_val = np.arange(wave_min, wave_max, 0.01)\n", "N_wave = len(wave_val)\n", "\n", "# Good and bad lines\n", "frac_good = 0.95\n", "N_line_good = int(N_line*frac_good)\n", "N_line_bad = N_line - N_line_good\n", "idx_line_good = np.sort(np.random.default_rng().choice(N_line, N_line_good, replace=False))\n", "\n", "# RVs of good and bad lines\n", "time_val = np.arange(N_spec)\n", "time_span = time_val[-1] - time_val[0]\n", "vrad_val_good = 10e-3*np.sin(2*np.pi*time_val/time_span)\n", "vrad_val_bad = np.random.normal(100e-3, 100e-3, N_spec)\n", "\n", "# Flux matrix\n", "flux_val = np.ones((N_spec, N_wave))\n", "\n", "# Loop spectra\n", "for i in range(N_spec):\n", "\n", " # Loop lines\n", " for j in range(N_line):\n", "\n", " # RV shift\n", " if j in idx_line_good:\n", " v = vrad_val_good[i]\n", " else:\n", " v = vrad_val_bad[i]\n", "\n", " # Shifted wavelength\n", " wc_shift = wc[j]*(1+v/c)\n", "\n", " # Line width in velocity\n", " sig_v = 3\n", "\n", " # Line width in wavelength\n", " sig_w = wc[j]*sig_v/c\n", "\n", " # Add line to spectrum\n", " flux_val[i] -= dc[j]*np.exp(-(wave_val-wc_shift)**2/(2*sig_w**2))\n", "\n", "# Degrade spectrum\n", "snr_val = 200\n", "cont_val_abs = snr_val**2\n", "flux_val_abs = flux_val*cont_val_abs\n", "flux_val_abs = np.random.poisson(flux_val_abs)\n", "flux_err_abs = np.sqrt(flux_val_abs)\n", "flux_val = flux_val_abs/cont_val_abs\n", "flux_err = flux_err_abs/cont_val_abs\n", "\n", "# Save spectra\n", "for i in range(N_spec):\n", " np.savez(f\"example_data/tutorial_2/spec_{i:0>2}.npz\",\n", " time_val=time_val[i],\n", " wave_val=wave_val ,\n", " flux_val=flux_val[i],\n", " flux_err=flux_err[i])\n", "\n", "# Save RVs\n", "df = pd.DataFrame()\n", "df[\"time_val\" ] = time_val\n", "df[\"vrad_val_good\"] = vrad_val_good\n", "df[\"vrad_val_bad\" ] = vrad_val_bad\n", "df.to_csv(\"example_data/tutorial_2/vrad.csv\", index=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Synthetic Radial Velocities" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this part, we use `ARVE` to simulate realistic radial velocities (RVs) of stellar oscillations and granulation. White noise is thereafter added to the RVs." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Simulate RVs with ARVE\n", "example = arve.ARVE()\n", "example.star.target = \"Sun\"\n", "example.star.get_stellar_parameters()\n", "example.star.add_vpsd_components()\n", "example.star.simulate_vrad_from_vpsd_components(time_start=0, time_stop=10, time_step=1/(24*60))\n", "\n", "# Extract RVs from ARVE object and add white noise\n", "time_val = example.data.vrad_components[\"time\"]\n", "vrad_val = example.data.vrad_components[\"total\"]\n", "vrad_err = 1e-3\n", "vrad_val += np.random.normal(0, vrad_err, len(time_val))\n", "vrad_err = np.ones_like(time_val)*vrad_err\n", "\n", "# Save RVs in CSV file\n", "df = pd.DataFrame()\n", "df[\"time_val\"] = time_val\n", "df[\"vrad_val\"] = vrad_val\n", "df[\"vrad_err\"] = vrad_err\n", "df.to_csv(\"example_data/tutorial_3/vrad.csv\", index=False)" ] } ], "metadata": { "kernelspec": { "display_name": "arve", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 2 }