{ "cells": [ { "cell_type": "markdown", "id": "7fb27b941602401d91542211134fc71a", "metadata": {}, "source": [ "# Solar wind example (SWACE + multiple models)\n", "\n", "This notebook shows how to use `SWACE` and `read_solar_wind_from_multiple_models` with **synthetic** solar wind data. It writes small fake data files to a local folder so you can run without downloads.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "44257f7a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(PosixPath('/PAGER/FLAG/code/external_data/SWVO'),\n", " PosixPath('/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind'))" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datetime import datetime, timezone\n", "from pathlib import Path\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from swvo.io.solar_wind import ( # noqa: E402\n", " SWACE,\n", " SWOMNI,\n", " read_solar_wind_from_multiple_models,\n", ")\n", "from swvo.logger import setup_logging\n", "\n", "setup_logging()\n", "\n", "\n", "def find_repo_root(start: Path) -> Path:\n", " for candidate in [start, *start.parents]:\n", " if (candidate / \"swvo\").exists():\n", " return candidate\n", " raise RuntimeError(\"Could not locate repo root containing 'swvo' package.\")\n", "\n", "\n", "ROOT = find_repo_root(Path.cwd())\n", "DATA_ROOT = ROOT / \"docs\" / \"_notebook_data\" / \"solar_wind\"\n", "ACE_DIR = DATA_ROOT / \"ACE_RT\"\n", "OMNI_DIR = DATA_ROOT / \"OMNI_HIGH_RES\"\n", "ACE_DIR.mkdir(parents=True, exist_ok=True)\n", "OMNI_DIR.mkdir(parents=True, exist_ok=True)\n", "\n", "ROOT, DATA_ROOT" ] }, { "cell_type": "code", "execution_count": 2, "id": "e54b3871", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(PosixPath('/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT/2024/11/ACE_SW_NOWCAST_20241120.csv'),\n", " PosixPath('/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES/2024/OMNI_HIGH_RES_1min_202411.csv'))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generate synthetic ACE (processed) and OMNI files\n", "rng = np.random.default_rng(42)\n", "start = datetime(2024, 11, 20, 0, 0, tzinfo=timezone.utc)\n", "end = datetime(2024, 11, 20, 6, 0, tzinfo=timezone.utc)\n", "idx = pd.date_range(start, end, freq=\"1min\", tz=\"UTC\")\n", "\n", "ace_df = pd.DataFrame(\n", " {\n", " \"t\": idx,\n", " \"bavg\": 5 + rng.normal(0, 0.3, len(idx)),\n", " \"bx_gsm\": rng.normal(0, 1.0, len(idx)),\n", " \"by_gsm\": rng.normal(0, 1.0, len(idx)),\n", " \"bz_gsm\": rng.normal(0, 1.0, len(idx)),\n", " \"proton_density\": 6 + rng.normal(0, 0.5, len(idx)),\n", " \"speed\": 420 + rng.normal(0, 20.0, len(idx)),\n", " \"temperature\": 1.2e5 + rng.normal(0, 1.0e4, len(idx)),\n", " }\n", ")\n", "# Add a small gap to demonstrate model fallback\n", "ace_df.iloc[30:50, ace_df.columns.get_indexer([\"speed\", \"proton_density\"])] = np.nan\n", "Path(ACE_DIR / \"2024\" / \"11\").mkdir(parents=True, exist_ok=True)\n", "ace_path = Path(ACE_DIR / \"2024\" / \"11\") / \"ACE_SW_NOWCAST_20241120.csv\"\n", "ace_df.to_csv(ace_path, index=False)\n", "\n", "omni_df = pd.DataFrame(\n", " {\n", " \"timestamp\": idx,\n", " \"bavg\": 5.2 + rng.normal(0, 0.2, len(idx)),\n", " \"bx_gsm\": rng.normal(0, 1.0, len(idx)),\n", " \"by_gsm\": rng.normal(0, 1.0, len(idx)),\n", " \"bz_gsm\": rng.normal(0, 1.0, len(idx)),\n", " \"speed\": 430 + rng.normal(0, 15.0, len(idx)),\n", " \"proton_density\": 6.5 + rng.normal(0, 0.4, len(idx)),\n", " \"temperature\": 1.1e5 + rng.normal(0, 8.0e3, len(idx)),\n", " }\n", ")\n", "omni_df[\"pdyn\"] = 2e-6 * omni_df[\"proton_density\"] * omni_df[\"speed\"] ** 2\n", "omni_df[\"sym-h\"] = -10 + rng.normal(0, 5.0, len(idx))\n", "Path(OMNI_DIR / \"2024\").mkdir(parents=True, exist_ok=True)\n", "omni_path = Path(OMNI_DIR / \"2024\") / \"OMNI_HIGH_RES_1min_202411.csv\"\n", "omni_df.to_csv(omni_path, index=False)\n", "\n", "ace_path, omni_path" ] }, { "cell_type": "markdown", "id": "acae54e37e7d407bbb7b55eff062a284", "metadata": {}, "source": [ "## Read ACE data directly with `SWACE`\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "9a63283cbaf04dbcab1f6479b197f3a8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.solar_wind.ace - ACE data directory: /PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT\u001b[0m\n" ] }, { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "datetime64[ns, UTC]", "type": "unknown" }, { "name": "bavg", "rawType": "float64", "type": "float" }, { "name": "bx_gsm", "rawType": "float64", "type": "float" }, { "name": "by_gsm", "rawType": "float64", "type": "float" }, { "name": "bz_gsm", "rawType": "float64", "type": "float" }, { "name": "file_name", "rawType": "object", "type": "unknown" }, { "name": "proton_density", "rawType": "float64", "type": "float" }, { "name": "speed", "rawType": "float64", "type": "float" }, { "name": "temperature", "rawType": "float64", "type": "float" } ], "ref": "973f2c15-e364-46fe-a439-5636e22e286d", "rows": [ [ "2024-11-20 00:00:00+00:00", "5.091415123926329", "0.3833938643534951", "-2.280737845371923", "0.0619164836799591", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT/2024/11/ACE_SW_NOWCAST_20241120.csv", "5.9279861699726615", "381.5816814820964", "148126.49248238723" ], [ "2024-11-20 00:01:00+00:00", "4.688004768127851", "0.9998242469102946", "-1.4966386899033264", "0.1103957256122185", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT/2024/11/ACE_SW_NOWCAST_20241120.csv", "6.145020198241768", "429.5407025445292", "102907.73896089844" ], [ "2024-11-20 00:02:00+00:00", "5.225135358741937", "-1.058536081978409", "-0.9228864418952676", "-0.4083331628266692", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT/2024/11/ACE_SW_NOWCAST_20241120.csv", "7.162889895940842", "419.9887380575408", "112616.23419122893" ], [ "2024-11-20 00:03:00+00:00", "5.282169414917364", "-0.1250090303195776", "1.461178866720329", "-1.3981096597988507", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT/2024/11/ACE_SW_NOWCAST_20241120.csv", "6.758220050294701", "420.79387053195126", "113838.24915825477" ], [ "2024-11-20 00:04:00+00:00", "4.414689443403849", "1.4814555476589732", "0.2825869829192965", "-1.5436252025220816", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT/2024/11/ACE_SW_NOWCAST_20241120.csv", "5.846717189356471", "439.2146861518893", "111491.80377617308" ] ], "shape": { "columns": 8, "rows": 5 } }, "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bavgbx_gsmby_gsmbz_gsmfile_nameproton_densityspeedtemperature
2024-11-20 00:00:00+00:005.0914150.383394-2.2807380.061916/PAGER/FLAG/code/external_data/SWVO/docs/_note...5.927986381.581681148126.492482
2024-11-20 00:01:00+00:004.6880050.999824-1.4966390.110396/PAGER/FLAG/code/external_data/SWVO/docs/_note...6.145020429.540703102907.738961
2024-11-20 00:02:00+00:005.225135-1.058536-0.922886-0.408333/PAGER/FLAG/code/external_data/SWVO/docs/_note...7.162890419.988738112616.234191
2024-11-20 00:03:00+00:005.282169-0.1250091.461179-1.398110/PAGER/FLAG/code/external_data/SWVO/docs/_note...6.758220420.793871113838.249158
2024-11-20 00:04:00+00:004.4146891.4814560.282587-1.543625/PAGER/FLAG/code/external_data/SWVO/docs/_note...5.846717439.214686111491.803776
\n", "
" ], "text/plain": [ " bavg bx_gsm by_gsm bz_gsm \\\n", "2024-11-20 00:00:00+00:00 5.091415 0.383394 -2.280738 0.061916 \n", "2024-11-20 00:01:00+00:00 4.688005 0.999824 -1.496639 0.110396 \n", "2024-11-20 00:02:00+00:00 5.225135 -1.058536 -0.922886 -0.408333 \n", "2024-11-20 00:03:00+00:00 5.282169 -0.125009 1.461179 -1.398110 \n", "2024-11-20 00:04:00+00:00 4.414689 1.481456 0.282587 -1.543625 \n", "\n", " file_name \\\n", "2024-11-20 00:00:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:01:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:02:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:03:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:04:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "\n", " proton_density speed temperature \n", "2024-11-20 00:00:00+00:00 5.927986 381.581681 148126.492482 \n", "2024-11-20 00:01:00+00:00 6.145020 429.540703 102907.738961 \n", "2024-11-20 00:02:00+00:00 7.162890 419.988738 112616.234191 \n", "2024-11-20 00:03:00+00:00 6.758220 420.793871 113838.249158 \n", "2024-11-20 00:04:00+00:00 5.846717 439.214686 111491.803776 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "swace = SWACE(data_dir=ACE_DIR)\n", "ace = swace.read(start, end, download=False)\n", "ace.head()" ] }, { "cell_type": "markdown", "id": "8dd0d8092fe74a7c96281538738b07e2", "metadata": {}, "source": [ "## Read from multiple models (ACE first, OMNI fills gaps)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "72eea5119410473aa328ad9291626812", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.solar_wind.ace - ACE data directory: /PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/ACE_RT\u001b[0m\n", "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.omni.omni_high_res - OMNI high resolution data directory: /PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES\u001b[0m\n", "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.solar_wind.read_solar_wind_from_multiple_models - Reading ace from 2024-11-20 00:00:00+00:00 to 2024-11-20 06:00:00+00:00\u001b[0m\n", "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.solar_wind.ace - Shiting start day by -1 day to account for propagation\u001b[0m\n", "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.utils - Percentage of NaNs in data frame: 176.45%\u001b[0m\n", "\u001b[32m[INFO ] 2026-02-11 16:15:41 - swvo.io.solar_wind.read_solar_wind_from_multiple_models - Reading omni from 2024-11-20 00:00:00+00:00 to 2024-11-20 06:00:00+00:00\u001b[0m\n", "\u001b[32m[INFO ] 2026-02-11 16:15:42 - swvo.io.utils - Percentage of NaNs in data frame: 0.00%\u001b[0m\n" ] }, { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "datetime64[ns, UTC]", "type": "unknown" }, { "name": "bavg", "rawType": "float64", "type": "float" }, { "name": "bx_gsm", "rawType": "float64", "type": "float" }, { "name": "by_gsm", "rawType": "float64", "type": "float" }, { "name": "bz_gsm", "rawType": "float64", "type": "float" }, { "name": "file_name", "rawType": "object", "type": "unknown" }, { "name": "proton_density", "rawType": "float64", "type": "float" }, { "name": "speed", "rawType": "float64", "type": "float" }, { "name": "temperature", "rawType": "float64", "type": "float" }, { "name": "model", "rawType": "object", "type": "string" } ], "ref": "c1c9040c-9c98-418f-9397-ace1a6c1a9b4", "rows": [ [ "2024-11-20 00:00:00+00:00", "5.125716452783586", "-0.3971943099170739", "0.1858345775189155", "0.6451126876611214", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES/2024/OMNI_HIGH_RES_1min_202411.csv", "7.111424325717546", "433.2910400463086", "104055.0367395644", "omni" ], [ "2024-11-20 00:01:00+00:00", "5.416470385958347", "-0.3324425343397702", "1.1493768409578178", "-0.7654953010422492", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES/2024/OMNI_HIGH_RES_1min_202411.csv", "6.908617546392595", "433.5189337134016", "109031.7726737602", "omni" ], [ "2024-11-20 00:02:00+00:00", "5.0916033199547615", "-0.8175583983299655", "-0.0447329341878679", "-0.5794400855816993", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES/2024/OMNI_HIGH_RES_1min_202411.csv", "6.730933096713872", "455.5118913764247", "112888.00882905153", "omni" ], [ "2024-11-20 00:03:00+00:00", "5.429651284903198", "0.4216168299098296", "-1.4051643606769604", "-0.4634446136253962", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES/2024/OMNI_HIGH_RES_1min_202411.csv", "6.158006087636648", "419.9749244746795", "115729.5659177014", "omni" ], [ "2024-11-20 00:04:00+00:00", "5.308439057144851", "-0.0940151740851555", "0.966129681004106", "-1.852727861690278", "/PAGER/FLAG/code/external_data/SWVO/docs/_notebook_data/solar_wind/OMNI_HIGH_RES/2024/OMNI_HIGH_RES_1min_202411.csv", "6.560635937927809", "434.4415088443491", "109248.86538785307", "omni" ] ], "shape": { "columns": 9, "rows": 5 } }, "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bavgbx_gsmby_gsmbz_gsmfile_nameproton_densityspeedtemperaturemodel
2024-11-20 00:00:00+00:005.125716-0.3971940.1858350.645113/PAGER/FLAG/code/external_data/SWVO/docs/_note...7.111424433.291040104055.036740omni
2024-11-20 00:01:00+00:005.416470-0.3324431.149377-0.765495/PAGER/FLAG/code/external_data/SWVO/docs/_note...6.908618433.518934109031.772674omni
2024-11-20 00:02:00+00:005.091603-0.817558-0.044733-0.579440/PAGER/FLAG/code/external_data/SWVO/docs/_note...6.730933455.511891112888.008829omni
2024-11-20 00:03:00+00:005.4296510.421617-1.405164-0.463445/PAGER/FLAG/code/external_data/SWVO/docs/_note...6.158006419.974924115729.565918omni
2024-11-20 00:04:00+00:005.308439-0.0940150.966130-1.852728/PAGER/FLAG/code/external_data/SWVO/docs/_note...6.560636434.441509109248.865388omni
\n", "
" ], "text/plain": [ " bavg bx_gsm by_gsm bz_gsm \\\n", "2024-11-20 00:00:00+00:00 5.125716 -0.397194 0.185835 0.645113 \n", "2024-11-20 00:01:00+00:00 5.416470 -0.332443 1.149377 -0.765495 \n", "2024-11-20 00:02:00+00:00 5.091603 -0.817558 -0.044733 -0.579440 \n", "2024-11-20 00:03:00+00:00 5.429651 0.421617 -1.405164 -0.463445 \n", "2024-11-20 00:04:00+00:00 5.308439 -0.094015 0.966130 -1.852728 \n", "\n", " file_name \\\n", "2024-11-20 00:00:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:01:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:02:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:03:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "2024-11-20 00:04:00+00:00 /PAGER/FLAG/code/external_data/SWVO/docs/_note... \n", "\n", " proton_density speed temperature model \n", "2024-11-20 00:00:00+00:00 7.111424 433.291040 104055.036740 omni \n", "2024-11-20 00:01:00+00:00 6.908618 433.518934 109031.772674 omni \n", "2024-11-20 00:02:00+00:00 6.730933 455.511891 112888.008829 omni \n", "2024-11-20 00:03:00+00:00 6.158006 419.974924 115729.565918 omni \n", "2024-11-20 00:04:00+00:00 6.560636 434.441509 109248.865388 omni " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "combined = read_solar_wind_from_multiple_models(\n", " start_time=start,\n", " end_time=end,\n", " model_order=[SWACE(ACE_DIR), SWOMNI(OMNI_DIR)],\n", " historical_data_cutoff_time=end,\n", " download=False,\n", ")\n", "combined.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "8edb47106e1a46a883d545849b8ab81b", "metadata": {}, "outputs": [ { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "model", "rawType": "object", "type": "string" }, { "name": "count", "rawType": "int64", "type": "integer" } ], "ref": "9f473ebd-0b7c-4792-94d2-e8b42ecef187", "rows": [ [ "ace", "270" ], [ "omni", "91" ] ], "shape": { "columns": 1, "rows": 2 } }, "text/plain": [ "model\n", "ace 270\n", "omni 91\n", "Name: count, dtype: int64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "combined[\"model\"].value_counts(dropna=False)" ] }, { "cell_type": "markdown", "id": "10185d26023b46108eb7d9f57d49d2b3", "metadata": {}, "source": [ "### Notes\n", "- `read_solar_wind_from_multiple_models` fills missing values by model priority.\n", "- In this example, ACE is preferred and OMNI fills the synthetic gaps.\n", "- For real data, pass `download=True` and set the environment variables in the class docs (e.g. `RT_SW_ACE_STREAM_DIR`).\n" ] } ], "metadata": { "kernelspec": { "display_name": "verb4d", "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.11.13" } }, "nbformat": 4, "nbformat_minor": 5 }