### Install ChaosMagPy from PyPI distribution archives Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/installation.rst Install ChaosMagPy from the built wheel or source distribution downloaded from PyPI. Replace x.x with the relevant version. These commands are alternatives for installing from distribution archives. ```pycon >>> pip install chaosmagpy-x.x-py3-none-any.whl ``` ```pycon >>> pip install chaosmagpy-x.x.tar.gz ``` -------------------------------- ### Install ChaosMagPy from wheel distribution Source: https://github.com/ancklo/chaosmagpy/blob/master/INSTALL.rst Installs ChaosMagPy from a built wheel distribution downloaded from PyPI. Replace x.x with the version number. ```bash pip install chaosmagpy-x.x-py3-none-any.whl ``` -------------------------------- ### Install ChaosMagPy from source distribution Source: https://github.com/ancklo/chaosmagpy/blob/master/INSTALL.rst Installs ChaosMagPy from a source distribution (tar.gz) downloaded from PyPI. Replace x.x with the version number. ```bash pip install chaosmagpy-x.x.tar.gz ``` -------------------------------- ### Install ChaosMagPy with pip Source: https://github.com/ancklo/chaosmagpy/blob/master/INSTALL.rst Installs the ChaosMagPy package from PyPI using pip. ```bash pip install chaosmagpy ``` -------------------------------- ### Install dependencies and ChaosMagPy with conda and pip Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/installation.rst Install all required and optional dependencies using conda and pip, then install ChaosMagPy with pip. The conda command installs core packages (python, numpy, scipy, pandas, pyshp, h5py, matplotlib, lxml), while pip installs hdf5storage and apexpy. This sequence is the recommended installation path. ```pycon >>> conda install python numpy scipy pandas pyshp h5py matplotlib lxml ``` ```pycon >>> pip install hdf5storage apexpy ``` ```pycon >>> pip install chaosmagpy ``` -------------------------------- ### Install remaining dependencies with pip Source: https://github.com/ancklo/chaosmagpy/blob/master/INSTALL.rst Installs hdf5storage and apexpy using pip, which are not available via the conda command above. ```bash pip install hdf5storage apexpy ``` -------------------------------- ### Load configuration from JSON file Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/configuration.rst Loads a configuration from a JSON file into `basicConfig`. Place at the start of a script to apply saved settings. ```python cp.basicConfig.load('myconfig.json') ``` -------------------------------- ### Install dependencies with conda Source: https://github.com/ancklo/chaosmagpy/blob/master/INSTALL.rst Installs the core and optional dependencies (except hdf5storage and apexpy) using conda. Requires conda package manager. ```bash conda install python numpy scipy pandas pyshp h5py matplotlib lxml ``` -------------------------------- ### Convert Modified Julian Date to Decimal Year Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Convert a modified Julian date to a decimal year using chaosmagpy.data_utils.mjd_to_dyear(). The example shows the output for input 412. ```pycon >>> cp.data_utils.mjd_to_dyear(412.) 2001.1260273972603 ``` -------------------------------- ### Convert Decimal Year to Modified Julian Date Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Convert a decimal year to a modified Julian date using chaosmagpy.data_utils.dyear_to_mjd(). The example shows the output for input 2001.25. ```pycon >>> cp.data_utils.dyear_to_mjd(2001.25) 457.25 ``` -------------------------------- ### Set Earth's surface radius parameter Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/configuration.rst Changes the Earth's surface radius parameter `params.r_surf` to 6371 km. This is an example of modifying a configuration parameter during a Python session before saving. ```python import chaosmagpy as cp cp.basicConfig['params.r_surf'] = 6371 ``` -------------------------------- ### Convert Modified Julian Date to Numpy datetime Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Convert a modified Julian date to a Numpy datetime object using chaosmagpy.data_utils.timestamp(). The example shows the output for input 412. ```pycon >>> cp.data_utils.timestamp(412.) numpy.datetime64('2001-02-16T00:00:00.000000') ``` -------------------------------- ### Convert Year, Month, Day, Hour to Modified Julian Date Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Convert a date given as integers (year, month, day, hour) to a modified Julian date using chaosmagpy.data_utils.mjd2000(). The example shows the output for 2002-01-19 15:00:00. ```pycon >>> cp.data_utils.mjd2000(2002, 1, 19, 15) # 2002-01-19 15:00:00 749.625 ``` -------------------------------- ### Convert Numpy datetime to Modified Julian Date Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Convert a Numpy datetime object to a modified Julian date using chaosmagpy.data_utils.mjd2000(). The example shows the output for a specific datetime. ```pycon >>> cp.data_utils.mjd2000(np.datetime64('2001-02-01T12:00:00')) 397.5 ``` -------------------------------- ### View basicConfig parameters Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/configuration.rst Prints the contents of the `basicConfig` dictionary-like container, which holds parameters and filepaths used by ChaosMagPy. Useful for viewing the parameters that can be changed, such as `params.r_surf`. ```python import chaosmagpy as cp print(cp.basicConfig) ``` -------------------------------- ### Import ChaosMagPy and Numpy Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Import the required libraries for using ChaosMagPy's time conversion functions. ```python import chaosmagpy as cp import numpy as np ``` -------------------------------- ### Save configuration to JSON file Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/configuration.rst Saves the current `basicConfig` dictionary to a JSON file. Use after changing configuration parameters to persist them. ```python cp.basicConfig.save('myconfig.json') ``` -------------------------------- ### Set custom RC-index file path Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/configuration.rst Sets the path to a custom RC-index file in `basicConfig`. Must be done at the top of the script after imports, otherwise ChaosMagPy uses the builtin RC-index file. ```python cp.basicConfig['file.RC_index'] = './my_RC_file.h5' ``` -------------------------------- ### Save RC-index as HDF5 file Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/configuration.rst Downloads the latest RC-index from the RC website and saves it locally as an HDF5 file. Recommended for use with the latest CHAOS model; there is no significant speed difference between TXT and HDF5 formats. ```python import chaosmagpy as cp cp.data_utils.save_RC_h5file('my_RC_file.h5') ``` -------------------------------- ### Synthesize Gauss coefficient timeseries with ChaosMagPy Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Loads a CHAOS model from a .mat file, creates a time vector in modified Julian date, computes Gauss coefficients for the internal field (MF, SV, SA) using synth_coeffs_tdep(), and saves them to text files with decimal year time points. Requires the CHAOS-6-x7.mat file and numpy. ```python import numpy as np import chaosmagpy as cp # load the CHAOS model from the mat-file model = cp.load_CHAOS_matfile('CHAOS-6-x7.mat') print('Model timespan is:', model.model_tdep.breaks[[0, -1]]) # create vector of time points in modified Julian date from 2000 to 2004 time = np.linspace(0., 4*365.25, 10) # 10 equally-spaced time instances # compute the Gauss coefficients of the MF, SV and SA of the internal field coeffs_MF = model.synth_coeffs_tdep(time, nmax=13, deriv=0) # shape: (10, 195) coeffs_SV = model.synth_coeffs_tdep(time, nmax=14, deriv=1) # shape: (10, 224) coeffs_SA = model.synth_coeffs_tdep(time, nmax=9, deriv=2) # shape: (10, 99) # save time and coefficients to a txt-file: each column starts with the time # point in decimal years followed by the Gauss coefficients in # natural order, i.e. g(n,m): g(1,0), g(1, 1), h(1, 1), ... dyear = cp.data_utils.mjd_to_dyear(time) # convert mjd2000 to decimal year np.savetxt('MF.txt', np.concatenate([dyear[None, :], coeffs_MF.T]), fmt='%10.5f', delimiter=' ') np.savetxt('SV.txt', np.concatenate([dyear[None, :], coeffs_SV.T]), fmt='%10.5f', delimiter=' ') np.savetxt('SA.txt', np.concatenate([dyear[None, :], coeffs_SA.T]), fmt='%10.5f', delimiter=' ') ``` -------------------------------- ### Compute time-dependent internal field components on a grid Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Loads a CHAOS model from a MAT-file and computes the internal field components (B_radius, B_theta, B_phi) on a full grid using synth_values_tdep. Requires the model file 'CHAOS-6-x7.mat' in the current directory. ```python import numpy as np import chaosmagpy as cp # create full grid radius = 3485. # km, core-mantle boundary theta = np.linspace(0., 180., num=181) # colatitude in degrees phi = np.linspace(-180., 180., num=361) # longitude in degrees phi_grid, theta_grid = np.meshgrid(phi, theta) radius_grid = radius*np.ones(phi_grid.shape) time = cp.data_utils.mjd2000(2000, 1, 1) # modified Julian date # create a "model" instance by loading the CHAOS model from a mat-file model = cp.load_CHAOS_matfile('CHAOS-6-x7.mat') # compute field components on the grid using the method "synth_values_tdep" B_radius, B_theta, B_phi = model.synth_values_tdep(time, radius_grid, theta_grid, phi_grid) ``` -------------------------------- ### Plot maps of time-dependent internal field Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Load a CHAOS model and plot the first time-derivative of the internal field on the core-mantle boundary at a specific time. ```python import chaosmagpy as cp model = cp.load_CHAOS_matfile('CHAOS-6-x7.mat') radius = 3485.0 # km, here core-mantle boundary time = 0.0 # mjd2000, here Jan 1, 2000 0:00 UTC model.plot_maps_tdep(time, radius, nmax=16, deriv=1) # plots the SV up to degree 16 ``` -------------------------------- ### Compute field components on a regular grid with grid=True Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Uses the grid=True option for speed when using a regular grid. It internally computes a grid in theta and phi similar to numpy.meshgrid() while saving time, and uses theta and phi directly instead of meshgrid arrays. ```python B_radius, B_theta, B_phi = model.synth_values_tdep(time, radius, theta, phi, grid=True) ``` -------------------------------- ### Plot and save static internal field maps Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Loads a CHAOS model from a .mat file and plots the static internal field on a map with radius 6371.2 km and spherical harmonic degree up to 85. The second block saves the static field to an .shc file. ```python import chaosmagpy as cp model = cp.load_CHAOS_matfile('CHAOS-6-x7.mat') model.plot_maps_static(radius=6371.2, nmax=85) ``` ```python model.save_shcfile('CHAOS-6-x7_static.shc', model='static') ``` -------------------------------- ### Save time-dependent internal field to shc file Source: https://github.com/ancklo/chaosmagpy/blob/master/docs/source/usage.rst Save the Gauss coefficients of the time-dependent internal field to a file in shc format. ```python model.save_shcfile('CHAOS-6-x7_tdep.shc', model='tdep') ```