.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/gallery/pyspedas_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_gallery_pyspedas_demo.py: ==================================== pySPEDAS Demo ==================================== Written by Nick Hatzigeorgiu and Shawn Polson. The purpose of this demo is to demonstrate some of the capabilities of the pySPEDAS package. .. GENERATED FROM PYTHON SOURCE LINES 13-20 The pySPEDAS package contains functions for downloading data from NASA missions and tools for data analysis and data plotting. It works together with the pytplot and the cdflib packages. pySPEDAS is a python implementation of the SPEDAS software which is written in the IDL programming language. For more information on SPEDAS, see: http://spedas.org/wiki/ .. GENERATED FROM PYTHON SOURCE LINES 20-27 .. code-block:: default import pyspedas import pytplot from pyspedas import clean_spikes, version from pytplot import del_data, get_data, store_data, tplot_options, tplot_names, tplot import os import pickle .. GENERATED FROM PYTHON SOURCE LINES 28-30 Load and plot THEMIS data ################################################################################### .. GENERATED FROM PYTHON SOURCE LINES 32-39 Note: pySPEDAS uses an environment variable SPEDAS_DATA_DIR to determine the local path for saving data files. SPEDAS_DATA_DIR acts as a root data directory for all missions, but mission-specific data directories (e.g., MMS_DATA_DIR for MMS, THM_DATA_DIR for THEMIS) can also be set, and these will override SPEDAS_DATA_DIR. We can find the version of the installed pyspedas package using `version()`. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: default version() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none pyspedas version: 1.2.8 .. GENERATED FROM PYTHON SOURCE LINES 42-43 Delete any existing pytplot variables. .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: default del_data() .. GENERATED FROM PYTHON SOURCE LINES 46-47 Define a time range. Here, we pick a time range that spans one day. .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: default time_range = ['2015-10-16', '2015-10-17'] .. GENERATED FROM PYTHON SOURCE LINES 50-55 You can load data into tplot variables by calling `pyspedas.mission.instrument()`. E.g., to load and plot our one day of THEMIS FGM data for probe 'd': (This following function downloads all the necessary files, loads data, and time-clips data to the specified time range.) .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: default thm_fgm = pyspedas.themis.fgm(trange=time_range, probe='d') .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 29-Nov-21 11:04:43: Downloading remote index: http://themis.ssl.berkeley.edu/data/themis/thd/l2/fgm/2015/ 29-Nov-21 11:04:44: File is current: themis_data/thd/l2/fgm/2015/thd_l2_fgm_20151016_v01.cdf .. GENERATED FROM PYTHON SOURCE LINES 58-64 Mission-specific information and examples can be found in the READMEs of each mission directory in the pySPEDAS GitHub repo. E.g., THEMIS: https://github.com/spedas/pyspedas/tree/master/pyspedas/themis Get data from pytplot object into python variables. This is useful when we want to work on the data using standard python libraries. .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: default all_data = get_data("thd_fgs_gse") time = all_data[0] data = all_data[1] .. GENERATED FROM PYTHON SOURCE LINES 69-71 After working with the data, we can store a new pytplot variable. We can store any data in the pytplot object. .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: default store_data("new_thd_fgs_gse", data={'x': time, 'y': data}) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 74-76 We plot the data using the pyqtgraph library (the default). Another option is to plot using the bokeh library. .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: default pytplot.options('thd_fgs_gse', 'color', 'red') pytplot.options('thd_fgs_gsm', 'color', 'blue') tplot(['thd_fgs_gse', 'thd_fgs_gsm']) .. GENERATED FROM PYTHON SOURCE LINES 81-83 Load and plot MMS data ################################################################################### .. GENERATED FROM PYTHON SOURCE LINES 85-86 Delete any existing pytplot variables, and define a time range. .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: default del_data() time_range = ['2015-10-16/13:05:30', '2015-10-16/13:07:30'] .. GENERATED FROM PYTHON SOURCE LINES 90-95 Load and plot two minutes of MMS burst mode FGM data: First we make an `mms_auth_info` pickle file with blank credentials in the home directory—this skips a prompt that would ask for an SDC username (a prompt that you could just submit blank). .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: default with open(os.sep.join([os.path.expanduser('~'), 'mms_auth_info.pkl']), 'wb') as auth_file: pickle.dump({'user': '', 'passwd': ''}, auth_file) mms_fgm = pyspedas.mms.fgm(trange=['2015-10-16/13:05:30', '2015-10-16/13:07:30'], data_rate='brst') .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 29-Nov-21 11:05:23: Loading pydata/mms1/fgm/brst/l2/2015/10/16/mms1_fgm_brst_l2_20151016130524_v4.18.1.cdf :228: RuntimeWarning: numpy.ndarray size changed, may indicate binary incompatibility. Expected 80 from C header, got 88 from PyObject The lengths of x and y do not match! mms1_fgm_rdeltahalf_brst_l2 is currently not in pytplot. mms1_fgm_rdeltahalf_brst_l2 is currently not in pytplot. mms1_fgm_rdeltahalf_brst_l2 is currently not in pytplot. That name is currently not in pytplot That name is currently not in pytplot Loaded variables: Epoch mms1_fgm_b_gse_brst_l2 mms1_fgm_b_gsm_brst_l2 mms1_fgm_b_dmpa_brst_l2 mms1_fgm_b_bcs_brst_l2 mms1_fgm_flag_brst_l2 Epoch_state mms1_fgm_hirange_brst_l2 mms1_fgm_bdeltahalf_brst_l2 mms1_fgm_stemp_brst_l2 mms1_fgm_etemp_brst_l2 mms1_fgm_mode_brst_l2 mms1_fgm_rdeltahalf_brst_l2 mms1_fgm_b_dmpa_brst_l2_bvec mms1_fgm_b_dmpa_brst_l2_btot mms1_fgm_b_gse_brst_l2_bvec mms1_fgm_b_gse_brst_l2_btot mms1_fgm_b_gsm_brst_l2_bvec mms1_fgm_b_gsm_brst_l2_btot mms1_fgm_b_bcs_brst_l2_bvec mms1_fgm_b_bcs_brst_l2_btot .. GENERATED FROM PYTHON SOURCE LINES 101-103 The names of the loaded tplot variables are printed. You can print the names of all currently-loaded tplot variables at any time using `pyspedas.tnames()`: .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: default pyspedas.tnames() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['Epoch', 'mms1_fgm_b_gse_brst_l2', 'mms1_fgm_b_gsm_brst_l2', 'mms1_fgm_b_dmpa_brst_l2', 'mms1_fgm_b_bcs_brst_l2', 'mms1_fgm_flag_brst_l2', 'Epoch_state', 'mms1_fgm_hirange_brst_l2', 'mms1_fgm_bdeltahalf_brst_l2', 'mms1_fgm_stemp_brst_l2', 'mms1_fgm_etemp_brst_l2', 'mms1_fgm_mode_brst_l2', 'mms1_fgm_b_dmpa_brst_l2_bvec', 'mms1_fgm_b_dmpa_brst_l2_btot', 'mms1_fgm_b_gse_brst_l2_bvec', 'mms1_fgm_b_gse_brst_l2_btot', 'mms1_fgm_b_gsm_brst_l2_bvec', 'mms1_fgm_b_gsm_brst_l2_btot', 'mms1_fgm_b_bcs_brst_l2_bvec', 'mms1_fgm_b_bcs_brst_l2_btot'] .. GENERATED FROM PYTHON SOURCE LINES 106-108 pySPEDAS has a number of helpful analysis routines under `pyspedas.analysis`. E.g., if we want to clean spikes from the data: .. GENERATED FROM PYTHON SOURCE LINES 108-110 .. code-block:: default clean_spikes(['mms1_fgm_b_gse_brst_l2', 'mms1_fgm_b_gsm_brst_l2']) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none mms1_fgm_b_gse_brst_l2 copied to mms1_fgm_b_gse_brst_l2-despike mms1_fgm_b_gse_brst_l2-despike copied to mms1_fgm_b_gse_brst_l2-despike_tmp_data mms1_fgm_b_gse_brst_l2-despike_tmp_data copied to mms1_fgm_b_gse_brst_l2-despike_tmp_data-s tsmooth was applied to: mms1_fgm_b_gse_brst_l2-despike_tmp_data-s clean_spikes was applied to: mms1_fgm_b_gse_brst_l2-despike mms1_fgm_b_gsm_brst_l2 copied to mms1_fgm_b_gsm_brst_l2-despike mms1_fgm_b_gsm_brst_l2-despike copied to mms1_fgm_b_gsm_brst_l2-despike_tmp_data mms1_fgm_b_gsm_brst_l2-despike_tmp_data copied to mms1_fgm_b_gsm_brst_l2-despike_tmp_data-s tsmooth was applied to: mms1_fgm_b_gsm_brst_l2-despike_tmp_data-s clean_spikes was applied to: mms1_fgm_b_gsm_brst_l2-despike .. GENERATED FROM PYTHON SOURCE LINES 111-116 Plot the (despiked) MMS data. Use the bokeh library — the plots will appear in the web browser. Note how we use pytplot options to set the line colors. See the full list of options at: https://pytplot.readthedocs.io/en/latest/_modules/pytplot/options.html .. GENERATED FROM PYTHON SOURCE LINES 116-120 .. code-block:: default pytplot.options('mms1_fgm_b_gse_brst_l2-despike', 'color', 'red') pytplot.options('mms1_fgm_b_gsm_brst_l2-despike', 'color', 'blue') tplot(['mms1_fgm_b_gse_brst_l2-despike', 'mms1_fgm_b_gsm_brst_l2-despike'], bokeh=True) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO:bokeh.io.state:Session output file '/var/folders/4f/t664wvxn4nd8864qfqwknh7r008q5h/T/temp.html' already exists, will be overwritten. .. GENERATED FROM PYTHON SOURCE LINES 121-124 Note: The HTML web page for this example may be missing the plots but this is a limitation of the platform for this particular gallery — if you run the python code locally, the plots will appear. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 47.418 seconds) .. _sphx_glr_download_generated_gallery_pyspedas_demo.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/HeliophysicsPy/gallery/gh-pages?urlpath=lab/tree/notebooks/generated/gallery/pyspedas_demo.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pyspedas_demo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pyspedas_demo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_