Skip to content

Commit

Permalink
Merge pull request #25 from nickssl/master
Browse files Browse the repository at this point in the history
Updated imports,  various other small changes.
  • Loading branch information
nickssl authored Dec 9, 2023
2 parents e2bb203 + 624b229 commit 9ed1b80
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 143 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@

This project contains general examples and turorials for pySPEDAS.


## Requirements

Python 3.8+ is required.


## Installation

To install the examples, use the following command:
```bash
pip install pyspedas_examples
```

To upgrade the examples from a previous version, use the following command:
```bash
pip install pyspedas_examples --upgrade
```

Installing pyspedas_examples will also install pyspedas and all other required packages.


### Running the examples

To run one of the examples using the python command line, use the following commands:

```bash
from pyspedas_examples import ex_basic
ex_basic()
```

Alternatively, you can open the example file using an editor (like Visual Studio Code) and run it directly.


## Additional examples

Additional, mission-specific examples, can be found in the repositories:

Magnetospheric Multiscale Mission (MMS):
Expand All @@ -11,6 +46,7 @@ https://github.com/spedas/mms-examples
Time History of Events and Macroscale Interactions during Substorms (THEMIS):
https://github.com/spedas/themis-examples


## Additional Information

For documentation on PySPEDAS, see: https://pyspedas.readthedocs.io/
Expand Down
18 changes: 17 additions & 1 deletion pyspedas_examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
from .version import version
from .version import version
from .examples.ex_analysis import ex_analysis
from .examples.ex_avg import ex_avg, ex_avg2
from .examples.ex_basic import ex_basic
from .examples.ex_cdagui import ex_cdagui
from .examples.ex_cdasws import ex_cdasws
from .examples.ex_colors import ex_colors
from .examples.ex_cotrans import ex_cotrans, ex_cotrans1
from .examples.ex_deriv import ex_deriv, ex_deriv1
from .examples.ex_dsl2gse import ex_dsl2gse
from .examples.ex_gmag import ex_gmag
from .examples.ex_mpause_2 import ex_mpause_2
from .examples.ex_mpause_t96 import ex_mpause_t96
from .examples.ex_smooth import ex_smooth
from .examples.ex_spectra import ex_spectra
from .examples.ex_spikes import ex_spikes
from .examples.ex_wavelet import ex_wavelet
18 changes: 9 additions & 9 deletions pyspedas_examples/examples/ex_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
"""

import pyspedas
import pytplot
from pytplot import del_data, subtract_average, subtract_median, tplot
from pyspedas.themis import state


def ex_analysis(plot=True):
"""Create a plot with THEMIS data."""
# Delete any existing pytplot variables
pytplot.del_data()
del_data()

# Download THEMIS state data for 2015-12-31
time_range = ['2015-12-31 00:00:00', '2015-12-31 23:59:59']
pyspedas.themis.state(probe='a', trange=time_range)
state(probe='a', trange=time_range)

# Use some analysis functions on tplot variables
pyspedas.subtract_average('tha_pos')
pyspedas.subtract_median('tha_pos')
subtract_average('tha_pos')
subtract_median('tha_pos')

# Plot
if plot:
pytplot.tplot(["tha_pos", "tha_pos-d", "tha_pos-m"])
tplot(["tha_pos", "tha_pos-d", "tha_pos-m"])

# Return 1 as indication that the example finished without problems.
return 1


# Run the example code
if __name__ == '__main__':
ex_analysis()
# Run the example code
# ex_analysis()
30 changes: 15 additions & 15 deletions pyspedas_examples/examples/ex_avg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
"""
import random
import pytplot
import pyspedas
from pyspedas.analysis.avg_data import avg_data
from pytplot import del_data, get_data, store_data, tplot_options, tplot, subtract_average
from pyspedas import avg_data
from pyspedas.themis import gmag


def ex_avg(plot=True):
"""Load GMAG data and average over 5 min intervals."""
# Delete any existing pytplot variables.
pytplot.del_data()
del_data()

# Define a time rage as a list
trange = ['2007-03-23', '2007-03-23']

# Download gmag files and load data into pytplot variables.
sites = ['ccnv']
var = 'thg_mag_ccnv'
pyspedas.themis.gmag(sites=sites, trange=trange, varnames=[var])
pytplot.tplot_options('title', 'GMAG data, thg_mag_ccnv 2007-03-23')
pyspedas.subtract_average(var, median=1)
gmag(sites=sites, trange=trange, varnames=[var])
tplot_options('title', 'GMAG data, thg_mag_ccnv, 2007-03-23')
subtract_average(var, median=1)
var += '-m'

# Five minute average using time dt.
Expand All @@ -34,7 +34,7 @@ def ex_avg(plot=True):

# Plot.
if plot:
pytplot.tplot([var, var + '-avg', var + '-avg2'])
tplot([var, var + '-avg', var + '-avg2'])

# Return 1 as indication that the example finished without problems.
return 1
Expand All @@ -59,22 +59,22 @@ def ex_avg2():
print(yi)

print("y: ", str(y[0:4]))
pytplot.store_data('test', data={'x': t, 'y': y})
d0 = pytplot.get_data('test')
store_data('test', data={'x': t, 'y': y})
d0 = get_data('test')
print('time before: ', d0[0])
print('data before: ', d0[1])

avg_data('test', width=5)
d = pytplot.get_data('test-avg')
d = get_data('test-avg')
print('time after: ', d[0])
print('data after: ', d[1])
print('first 4 results:', d[1][0:4])

# Return data for automated testing:
# 1044.22 1063.034 1034.58 1054.46

return d[1][0:4]


# Run the example code
ex_avg2()
if __name__ == '__main__':
ex_avg()
ex_avg2()
print("IDL results: ", [1044.22, 1063.034, 1034.58, 1054.46])
1 change: 1 addition & 0 deletions pyspedas_examples/examples/ex_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def ex_basic(plot=True):
alldata = get_data("tha_pos")
time = alldata[0]
data = alldata[1]

# Here we could work on the data before saving a new tplot variable.
# For example, we could convert the data to km:
data = data / 1000.0
Expand Down
32 changes: 19 additions & 13 deletions pyspedas_examples/examples/ex_cotrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
See also: http://spedas.org/wiki/index.php?title=Cotrans
"""

import pyspedas
import pytplot
from pyspedas.utilities.cotrans import cotrans
from pyspedas.utilities.cotrans_lib import submag2geo
from pytplot import options, tplot_options, tplot
from pyspedas import cotrans
from pyspedas.cotrans.cotrans_lib import submag2geo
from pyspedas.themis import state


def ex_cotrans():
Expand All @@ -25,20 +25,24 @@ def ex_cotrans():
pos_out = "tha_pos_gse"
vel_in = "tha_vel"
vel_out = "tha_vel_gse"
pyspedas.themis.state(probe=probe, trange=trange, time_clip=True,
varnames=[pos_in, vel_in])
state(probe=probe, trange=trange, time_clip=True,
varnames=[pos_in, vel_in])

# Coordinate transformation.
cotrans(name_in=pos_in, name_out=pos_out, coord_in="gei", coord_out="gse")
cotrans(name_in=vel_in, name_out=vel_out, coord_in="gei", coord_out="gse")

# Plot.
pytplot.tplot_options('title', 'Themis pos and vel in GEI and GSE')
pytplot.options(pos_in, 'ytitle', pos_in)
pytplot.options(pos_out, 'ytitle', pos_out)
pytplot.options(vel_in, 'ytitle', vel_in)
pytplot.options(vel_out, 'ytitle', vel_out)
pytplot.tplot([pos_in, vel_in, pos_out, vel_out])
tplot_options('title', 'Themis pos and vel in GEI and GSE')
options(pos_in, 'ytitle', pos_in)
options(pos_in, 'legend_names', ["x, GEI", "y, GEI", "z, GEI"])
options(pos_out, 'ytitle', pos_out)
options(pos_out, 'legend_names', ["x, GSE", "y, GSE", "z, GSE"])
options(vel_in, 'ytitle', vel_in)
options(vel_in, 'legend_names', ["Vx, GEI", "Vy, GEI", "Vz, GEI"])
options(vel_out, 'ytitle', vel_out)
options(vel_out, 'legend_names', ["Vx, GSE", "Vy, GSE", "Vz, GSE"])
tplot([pos_in, vel_in, pos_out, vel_out])

# Return 1 as indication that the example finished without problems.
return 1
Expand Down Expand Up @@ -78,4 +82,6 @@ def ex_cotrans1():


# Run the example code
# ex_cotrans()
if __name__ == '__main__':
ex_cotrans()
ex_cotrans1()
55 changes: 0 additions & 55 deletions pyspedas_examples/examples/ex_create.py

This file was deleted.

6 changes: 4 additions & 2 deletions pyspedas_examples/examples/ex_deriv.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def ex_deriv(plot=True):
return 1


def ex_deriv2(plot=True):
def ex_deriv1(plot=True):
"""Find the derivative of sinx."""
# Delete any existing pytplot variables
pytplot.del_data()
Expand All @@ -64,4 +64,6 @@ def ex_deriv2(plot=True):


# Run the example code
# ex_deriv()
if __name__ == '__main__':
ex_deriv()
ex_deriv1()
3 changes: 2 additions & 1 deletion pyspedas_examples/examples/ex_dsl2gse.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ def ex_dsl2gse(plot=True):


# Run the example code
# ex_dsl2gse()
if __name__ == '__main__':
ex_dsl2gse()
29 changes: 16 additions & 13 deletions pyspedas_examples/examples/ex_gmag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@
Download and plot THEMIS GMAG data.
Shows how to get the names of GMAG stations and how to download GMAG data
either from a single station or gram a GMAG group.
either from a single station or a GMAG group.
"""
import pyspedas
import pytplot
from pytplot import del_data, tplot_options, tplot, tplot_names
from pyspedas import subtract_average, tnames
from pyspedas.themis.ground.gmag import gmag, gmag_list


def ex_gmag(plot=True):
"""Demonstrate how to use gmag functions."""
# Delete any existing pytplot variables
pytplot.del_data()
del_data()

# Define a time rage as a list
trange = ['2015-12-31', '2015-12-31']

# Get a list of EPO gmag stations
sites = pyspedas.themis.ground.gmag.gmag_list('epo')
sites = gmag_list('epo')

# Download gmag files and load data into pytplot variables
pyspedas.themis.gmag(sites=sites, trange=trange)
gmag(sites=sites, trange=trange)

# Get a list of loaded sites
sites_loaded = pyspedas.tnames()
sites_loaded = tnames()

# Subtract mean values
pyspedas.subtract_average(sites_loaded, '')
subtract_average(sites_loaded, '')

# Download AE index data
# pyspedas.load_data('gmag', time_list, ['idx'], '', '')
pyspedas.themis.gmag(sites='idx', trange=trange)
gmag(sites='idx', trange=trange)

# Plot
sites_loaded = pytplot.tplot_names()
pytplot.tplot_options('title', 'EPO GMAG 2015-12-31')
sites_loaded = tplot_names()
tplot_options('title', 'EPO GMAG 2015-12-31')

if plot:
pytplot.tplot(sites_loaded)
tplot(sites_loaded)

# Return 1 as indication that the example finished without problems.
return 1


# Run the example code
# ex_gmag()
if __name__ == '__main__':
ex_gmag()
Loading

0 comments on commit 9ed1b80

Please sign in to comment.