From 07afdf4cc03bf31d7370fec6e9617b603418447c Mon Sep 17 00:00:00 2001 From: Russell Stoneback Date: Thu, 19 May 2022 14:54:59 -0500 Subject: [PATCH] ENH: Add occurrence prob demo --- ...ysatSeasons - Occurrence Probability.ipynb | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 pysatTutorials/pysatSeasons - Occurrence Probability.ipynb diff --git a/pysatTutorials/pysatSeasons - Occurrence Probability.ipynb b/pysatTutorials/pysatSeasons - Occurrence Probability.ipynb new file mode 100644 index 0000000..df18cdf --- /dev/null +++ b/pysatTutorials/pysatSeasons - Occurrence Probability.ipynb @@ -0,0 +1,207 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "8b83f8e1", + "metadata": {}, + "outputs": [], + "source": [ + "import datetime as dt\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import pandas as pds\n", + "import warnings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5fcb0375", + "metadata": {}, + "outputs": [], + "source": [ + "import pysat\n", + "import pysatSeasons\n", + "\n", + "# Set data directory if user hasn't already set one\n", + "if len(pysat.params['data_dirs']) == 0:\n", + " # Set a directory for pysat to use for data\n", + " pysat.params['data_dirs'] = '~/pysatDemo'\n", + "else:\n", + " print('pysat directory has been set previously. Leaving unchanged.')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d54ac16", + "metadata": {}, + "outputs": [], + "source": [ + "# Register pysatNASA ICON IVM data plug-in. Only required once per install.\n", + "import pysatNASA\n", + "pysat.utils.registry.register(['pysatNASA.instruments.icon_ivm'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52fc4186", + "metadata": {}, + "outputs": [], + "source": [ + "# Orbit breakdown info\n", + "orbit_info = {'kind': 'lt', 'index':'Magnetic_Local_Time'}\n", + "\n", + "# We will employ a centered time-based calculation. Engage pysat's data padding.\n", + "pad = pds.DateOffset(seconds=30)\n", + "\n", + "# Instantiate IVM instrument data and \n", + "ivm = pysat.Instrument('icon', 'ivm', inst_id='a', orbit_info=orbit_info,\n", + " pad=pad)\n", + "\n", + "# Analysis date range\n", + "sdate = dt.datetime(2020, 1, 1)\n", + "edate = dt.datetime(2020, 1, 15)\n", + "\n", + "# Download data\n", + "ivm.download(sdate, edate)\n", + "\n", + "# Set the range of dates for the analysis.\n", + "ivm.bounds = (sdate, edate)\n", + "\n", + "# Improvements for loading ICON metadata are currently in \n", + "# https://github.com/pysat/pysatNASA/pull/100. \n", + "warnings.simplefilter('ignore', UserWarning)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd3bded4", + "metadata": {}, + "outputs": [], + "source": [ + "# Modify data as it is loaded to add a log ion density measurement\n", + "def add_std_dens(inst):\n", + " \"\"\"Calculate Standard Deviation in Ion Density.\n", + " \n", + " Parameters\n", + " ----------\n", + " inst : pysat.Instrument\n", + " Instrument object to operate upon.\n", + " \n", + " \"\"\"\n", + " inst['Ion_Density_STD'] = inst['Ion_Density'].rolling('30s',\n", + " center=True).std()\n", + " \n", + " return\n", + "\n", + "# Attach to IVM\n", + "ivm.custom_attach(add_std_dens)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba558515", + "metadata": {}, + "outputs": [], + "source": [ + "# Load some data to get a sense of values\n", + "ivm.load(2020, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e2012fd", + "metadata": {}, + "outputs": [], + "source": [ + "# Plot standard deviation\n", + "ivm['Ion_Density_STD'].plot()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eec99502", + "metadata": {}, + "outputs": [], + "source": [ + "# Run Occurrence Probability\n", + "answer = pysatSeasons.occur_prob.by_orbit2D(ivm, [0, 360, 24], 'Longitude',\n", + " [-20, 20, 10], 'Magnetic_Latitude',\n", + " ['Ion_Density_STD'], [5.E3],\n", + " return_bins=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a8adaba", + "metadata": {}, + "outputs": [], + "source": [ + "# A dict indexed by data_label is returned.\n", + "ans = answer['Ion_Density_STD']\n", + "\n", + "# Plot occurrence probability\n", + "f, axarr = plt.subplots(2, 1, sharex=True, sharey=True, figsize=(8.5, 11))\n", + "\n", + "# Mask for locations not observed.\n", + "masked = np.ma.array(ans['prob'], mask=np.isnan(ans['prob']))\n", + "\n", + "# Plot occurrence probability\n", + "im = axarr[0].pcolor(ans['bin_x'], ans['bin_y'], masked)\n", + "axarr[0].set_title('Occurrence Probability Delta-N > 7.E3 N/cc')\n", + "axarr[0].set_ylabel('Latitude (Degrees)')\n", + "axarr[0].set_yticks((-20, -15, -10, -5, 0, 5, 10, 15, 20))\n", + "axarr[0].set_ylim((ans['bin_y'][0], ans['bin_y'][-1]))\n", + "plt.colorbar(im, ax=axarr[0], label='Occurrence Probability')\n", + "\n", + "# Plot number of orbits per bin.\n", + "im = axarr[1].pcolor(ans['bin_x'], ans['bin_y'], ans['count'])\n", + "axarr[1].set_title('Number of Orbits in Bin')\n", + "axarr[1].set_xlabel('Longitude (Degrees)')\n", + "axarr[1].set_xticks((0, 60, 120, 180, 240, 300, 360))\n", + "axarr[1].set_xlim((ans['bin_x'][0], ans['bin_x'][-1]))\n", + "axarr[1].set_ylabel('Latitude')\n", + "plt.colorbar(im, ax=axarr[1], label='Counts')\n", + "\n", + "f.tight_layout()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "090642e0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file