Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilburlewis committed Dec 8, 2023
2 parents 5970038 + bf1ec3e commit 631c1ad
Show file tree
Hide file tree
Showing 6 changed files with 2,384 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pyspedas_examples/examples/ex_mpause_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This example generates a plot of the magnetopause locations using the mpause_2 function from the pyspedas.utilities module.
The plot includes grid, labels, and title. The x-axis is inverted to match the IDL behavior.
This is similar to the IDL example crib_magnetopause.pro (first part).
"""

import matplotlib.pyplot as plt
from pyspedas.utilities.mpause_2 import mpause_2


def ex_mpause_2():

# Call the function without parameters to use default values
xmp, ymp = mpause_2()

# Set the range for the axes
x_limits = (-300, 100)
y_limits = (-100, 100)

# Create the plot
plt.figure()
plt.plot(xmp, ymp)

# Set the limits of x and y axes
plt.xlim(x_limits)
plt.ylim(y_limits)

# Invert the x-axis to match the IDL behavior
plt.gca().invert_xaxis()

# Add grid, labels, and title
plt.grid(True)
plt.xlabel("XMP")
plt.ylabel("YMP")
plt.title("Magnetopause Locations")

# Display the plot
plt.show()


# To run this function, uncomment the following line:
# ex_mpause_2()
54 changes: 54 additions & 0 deletions pyspedas_examples/examples/ex_mpause_t96.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
This example loads position data for THEMIS thd probe for a specified time range,
converts the position data from GEI to GSM coordinates,
calls the mpause_t96 function with the position data, and plots the magnetopause.
This is similar to the IDL example crib_magnetopause.pro (second part).
"""

from pyspedas.themis import state
from pyspedas.cotrans.cotrans import cotrans
from pyspedas.utilities.mpause_t96 import mpause_t96
import matplotlib.pyplot as plt
from pytplot import get_data


def ex_mpause_t96():

# Set the date and load one day of data
date_start = '2019-01-05/00:00:00'
date_end = '2019-01-06/00:00:00'

# Load position data for THEMIS thd probe for the specified time range
state(trange=[date_start, date_end], probe='d', get_support_data=True)

# Transform the position data from GEI to GSM coordinates
cotrans(name_in='thd_pos', name_out='thd_pos_gsm',
coord_in='gei', coord_out='gsm')

# Get the position data in GSM coordinates
pos_gsm_data = get_data('thd_pos_gsm')

# Convert position from kilometers to Earth radii
pos_gsm = pos_gsm_data.y / 6378.0

# Define solar wind dynamic pressure
dynp = 2.0

# Call the mpause_t96 function with the position data
xmgnp, ymgnp, zmgnp, id, distan = mpause_t96(
pd=dynp, xgsm=pos_gsm[:, 0], ygsm=pos_gsm[:, 1], zgsm=pos_gsm[:, 2])

# Plot xmgnp vs ymgnp
plt.figure()
plt.plot(xmgnp, ymgnp)
plt.xlim(20, -60)
plt.ylim(-30, 30)
plt.xlabel('X (Re)')
plt.ylabel('Y (Re)')
plt.title('Magnetopause Boundary')
plt.grid(True)
plt.show()

# To run this function, uncomment the following line:
# ex_mpause_t96()
181 changes: 181 additions & 0 deletions pyspedas_examples/notebooks/BARREL_background_model.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## BARREL Create Background Model\n",
"\n",
"\n",
"### Setup\n",
"Start by importing libraries and loading data from a potentially interesting event.\n",
"\n",
"In this guide, we are going to use interactive plots, so `%matplotlib ipympl` should be set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib ipympl\n",
"import pyspedas, pytplot, pprint, numpy\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For our example, we will look at data form flight 1G from January 17th - 19th, 2013.\n",
"\n",
"FSPC and SSPC data can be downloaded with the `pyspedas.barrel` helper functions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trange=['2013-01-17', '2013-01-19']\n",
"\n",
"pyspedas.barrel.fspc(\n",
" trange=trange,\n",
" probe='1g'\n",
")\n",
"\n",
"pyspedas.barrel.sspc(\n",
" trange=trange,\n",
" probe='1g'\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Background subtraction\n",
"Plot FSPC1 for the loaded data and visually determine the start and stop locations for the background selection."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pytplot.tplot('brl1G_SSPC')\n",
"pytplot.tplot('brl1G_FSPC1')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"By moving the mouse cursor over a quiet area of the plot, we can estimate values for the start and stop times.\n",
"In this way we can find one or more periods of time to use for background calculation.\n",
"\n",
"These start and stop times can be stored in a list of tuples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"background_periods=[(\"2013-01-17/17:25\", \"2013-01-17/20:35\"), (\"2013-01-18/09:35\", \"2013-01-18/12:04\")]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we can use the plot to estimate the time period of the event that we are interested in:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"event_period=(\"2013-01-17/01:54\", \"2013-01-17/03:24\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we will extract the SSPC data and send it to the `pyspedas.barrel.average_event_spectrum` function. This will return a background subtracted average spectrum of the event. This data is stored in a new tplot variable with the x axis set to show the energy levels."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ts, cnts, energy_levels = pytplot.get_data('brl1G_SSPC')\n",
"spec = pyspedas.barrel.average_event_spectrum(ts, cnts, energy_levels, background_periods, event_period)\n",
"pytplot.store_data(\"brl1G_Event_Spec\", data={'x':energy_levels, 'y':spec})\n",
"pytplot.options(\"brl1G_Event_Spec\", opt_dict={\"name\": \"Average Event Spectrum\", \"ytitle\": \"cnts/keV/sec\"})\n",
"pytplot.tplot(\"brl1G_Event_Spec\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to plotting the event spectrum, we can generate a background-subtracted spectrogram using the `pyspedas.barrel.background_subtracted_spectrogram` function. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#The background subtracted spectrogram function only takes the background time periods, not the event time periods.\n",
"bg_sub_spectrogram = pyspedas.barrel.background_subtracted_spectrogram(ts, cnts, energy_levels, background_periods)\n",
"pytplot.store_data(\"brl1G_SSPC_BKG_SUB\", data={'x':ts, 'y':bg_sub_spectrogram, 'v':energy_levels})\n",
"pytplot.options(\"brl1G_SSPC_BKG_SUB\", \"name\", \"Background Subtracted SSPC\")\n",
"\n",
"#If the option for the spectrogram plot isn't set, it will plot a stack of line plots\n",
"pytplot.options(\"brl1G_SSPC_BKG_SUB\", \"Spec\", 1) \n",
"\n",
"#We can guess at the y axis range by looking at the event stectum above. Setting the upper limit to 500keV will capture all of the counts\n",
"pytplot.options(\"brl1G_SSPC_BKG_SUB\", \"yrange\", [0, 500])\n",
"\n",
"#Use the estimated event period to set the time range for the plot\n",
"pytplot.tlimit(list(event_period))\n",
"\n",
"pytplot.tplot(\"brl1G_SSPC_BKG_SUB\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 631c1ad

Please sign in to comment.