Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Rock magnetic data import

This notebook illustrates the two approaches that can be taken to import data from MagIC into a Jupyter notebook:

  • From a locally hosted (i.e. downloaded) MagIC contribution

  • Directly from the MagIC database (using the API)

    • either from a public contribution or

    • from a private contribution

The MagIC file is then unpacked into its constituent files and loaded as a contribution object. The notebook then summarizes the specimens and the experiments that have been run on them within the data. Finally, it is illustrated how data from a single specimen and single experiment on that specimen can be isolated.

In the other rockmagpy notebooks, these import methods are used to bring in contribution data. The filtering methods to access data from specific specimens and experiments are used either in the notebooks or within the rockmag.py functions.

Import scientific python libraries

Run the cell below to import the functions needed for the notebook.

import pmagpy.ipmag as ipmag
import pmagpy.rockmag as rmag
import pmagpy.contribution_builder as cb
import pandas as pd
pd.set_option('display.max_columns', 500)

Download and import data

Download and unpack data from a local MagIC file

Within the folder ./example_data/ECMB, there is a MagIC contribution called 'magic_contribution_20213.txt' that has been downloaded already from the MagIC database. Running the code cell below unpacks that MagIC contribution so that we can visualize and analyze the measurement data. We can then create a contribution object that has all of the MagIC tables. This approach can be applied to MagIC contributions that are downloaded from the database.

# set the directory path (dir_path) to the data
dir_path = './example_data/ECMB'

# set the name of the MagIC file
ipmag.unpack_magic('magic_contribution_20213.txt', 
                     dir_path = dir_path,
                     input_dir_path = dir_path,
                     print_progress=False)

ECMB_contribution = cb.Contribution(dir_path)
1  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/ECMB/contribution.txt
1  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/ECMB/locations.txt
90  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/ECMB/sites.txt
312  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/ECMB/samples.txt
1574  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/ECMB/specimens.txt
17428  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/ECMB/measurements.txt
-I- Using online data model
-I- Getting method codes from earthref.org
-I- Importing controlled vocabularies from https://earthref.org

Download and unpack data directly from a public MagIC contribution

We can also download and unpack a MagIC contribution from a public MagIC contribution by providing the MagIC ID. Let’s do that and pull in data from the paper:

Jackson, M. and Swanson-Hysell, N.L. (2012), Rock Magnetism of Remagnetized Carbonate Rocks: Another Look, In: Elmore, R. D., Muxworthy, A. R., Aldana, M. M. and Mena, M., eds., Remagnetization and Chemical Alteration of Sedimentary Rocks, Geological Society of London Special Publication, 371, doi:10.1144/SP371.3.

The MagIC contribution is here: https://earthref.org/MagIC/16460 with '16460' being the MagIC ID.

# set the MagIC ID for the data set here
magic_id = '16460'

# set where you want the downloaded data to go
dir_path = 'example_data/Jackson2012'

result, magic_file = ipmag.download_magic_from_id(magic_id, directory=dir_path)
ipmag.unpack_magic(magic_file, dir_path, print_progress=False)
J12_contribution = cb.Contribution(dir_path)
Download successful. File saved to: example_data/Jackson2012/magic_contribution_16460.txt
1  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/Jackson2012/contribution.txt
5  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/Jackson2012/locations.txt
8  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/Jackson2012/sites.txt
51  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/Jackson2012/samples.txt
186  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/Jackson2012/specimens.txt
4758  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/Jackson2012/measurements.txt

Download and unpack data directly from a private MagIC contribution

To bring in data from a contribution that has been uploaded to MagIC, but that is not yet publicly published, you need to both set the magic_id ('20100' for the private contribution in this example) and provide the share key for your MagIC contribution which you can find by clicking on the “Share” button in the MagIC database. Here we will do that with data developed by participants in one of the groups from the IRM Summer School.

Description of the image

Copy the share key highlighted in grey in the following image to the share_key parameter in the cell below.

Description of the image
# set the MagIC ID for the data set here
magic_id = '20426'

# provide the share key for the data set
share_key = '5272749a-9af6-413c-8d94-28e1d99befe1'

# set where you want the downloaded data to go
dir_path = 'example_data/SSRM2022C'

result, magic_file = ipmag.download_magic_from_id(magic_id, directory=dir_path, share_key=share_key)
ipmag.unpack_magic(magic_file, dir_path, print_progress=False)
SSRM2022C_contribution = cb.Contribution(dir_path)
Download successful. File saved to: example_data/SSRM2022C/magic_contribution_20426.txt
1  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/SSRM2022C/contribution.txt
6  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/SSRM2022C/locations.txt
6  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/SSRM2022C/sites.txt
16  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/SSRM2022C/samples.txt
47  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/SSRM2022C/specimens.txt
139658  records written to file  /Users/yimingzhang/Github/RockmagPy-notebooks/example_data/SSRM2022C/measurements.txt

Inspect the measurements table

Within the MagIC contribution object, there is a measurements table that we can inspect.

SSRM2022C_measurements = SSRM2022C_contribution.tables['measurements'].df
SSRM2022C_measurements.dropna(axis=1, how='all').head() # see the first 5 measurements without empty columns
Loading...

Generate a table of specimens and experiments

Now that we have imported the measurement data, we can look at the different experiments that have been applied to different specimens. The method codes associated with the experiments are from the controlled vocabularies in the MagIC database https://www2.earthref.org/MagIC/method-codes.

Method CodeDefinition
LP-BCR-BFCoercivity of remanence: Back field method
LP-HYS:LP-HYS-MHysteresis loops
LP-CW-SIRM:LP-MCCycling between cooling and warming: Room temperature SIRM; Measured while cooling
LP-CW-SIRM:LP-MWCycling between cooling and warming: Room temperature SIRM; Measured while warming
LP-FCField cooled: Remanent magnetization measured on warming
LP-ZFCZero field cooled: Remanent magnetization measured on warming
SSRM2022C_experiments = rmag.make_experiment_df(SSRM2022C_measurements)
SSRM2022C_experiments
Loading...

Isolate data from a single experiment

Data for a single experiment can be isolated using the experiment_name.

Here we specify the experiment data, extract the associated data, and look at the first 5 rows.

experiment_name = 'IRM-VSM3-LP-HYS-228041'
DA4_r_hys = SSRM2022C_measurements[SSRM2022C_measurements['experiment'].str.contains(experiment_name, na=False)]
DA4_r_hys = DA4_r_hys.dropna(axis=1, how='all')
DA4_r_hys.head()
Loading...

Isolate data from a single specimen

We can alternatively filter the measurements to get all the data for a specified specimen

specimen_name = 'DX1-5r'
specimen_data = SSRM2022C_measurements[SSRM2022C_measurements["specimen"] == specimen_name]
specimen_data = specimen_data.dropna(axis=1, how='all')
specimen_data.head()
Loading...

Filter specimen data on method code.

Filtering can also be done using the associated method_code.

experiment_method_code = 'LP-FC'
fc_data = specimen_data[specimen_data['method_codes'].str.contains(experiment_method_code, na=False)]
fc_data.head()
Loading...