
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/Creating_GS_Files/plot_csv_magnetics.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_Creating_GS_Files_plot_csv_magnetics.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_Creating_GS_Files_plot_csv_magnetics.py:


Magnetic Raster Dataset
-----------------------

These magnetic data channels were pulled from the Wisconsin SkyTEM example in this repository to demonstrate the relatively simple case of gridded raster files. 

Source Reference: Minsley, B.J, Bloss, B.R., Hart, D.J., Fitzpatrick, W., Muldoon, M.A., Stewart, E.K., Hunt, R.J., James, S.R., Foks, N.L., and Komiskey, M.J., 2022, Airborne electromagnetic and magnetic survey data, northeast Wisconsin (ver. 1.1, June 2022): U.S. Geological Survey data release, https://doi.org/10.5066/P93SY9LI.

.. GENERATED FROM PYTHON SOURCE LINES 11-18

.. code-block:: Python

    import matplotlib.pyplot as plt
    from os.path import join
    import gspy
    from gspy import Survey
    import warnings
    warnings.filterwarnings('ignore')








.. GENERATED FROM PYTHON SOURCE LINES 19-21

Initialize the Survey
^^^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 21-31

.. code-block:: Python


    # Path to example files
    data_path = '..//data_files//magnetics'

    # Survey metadata file
    metadata = join(data_path, "WI_Magnetics_survey_md.yml")

    # Establish the Survey
    survey = Survey.from_dict(metadata)








.. GENERATED FROM PYTHON SOURCE LINES 32-37

.. literalinclude:: /../../examples/data_files/magnetics/WI_Magnetics_survey_md.yml
   :language: yaml
   :linenos:
   :caption: Survey YAML file


.. GENERATED FROM PYTHON SOURCE LINES 40-42

Create a Data Branch
^^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 42-45

.. code-block:: Python

    data_container = survey.gs.add_container('magnetic_data', **dict(content = "raw flightline and gridded magnetic data", comment = "grids were contractor-derived"))









.. GENERATED FROM PYTHON SOURCE LINES 46-48

Add the raw magnetic data to the branch
"""""""""""""""""""""""""""""""""""""""

.. GENERATED FROM PYTHON SOURCE LINES 48-53

.. code-block:: Python


    # Import raw magnetic data from CSV-format.
    d_data1 = join(data_path, 'WI_Magnetics.csv')
    d_supp1 = join(data_path, 'WI_Magnetics_raw_data_md.yml')








.. GENERATED FROM PYTHON SOURCE LINES 54-56

.. code-block:: Python

    rd = data_container.gs.add(key='raw_data', data_filename=d_data1, metadata_file=d_supp1)








.. GENERATED FROM PYTHON SOURCE LINES 57-62

.. literalinclude:: /../../examples/data_files/magnetics/WI_Magnetics_raw_data_md.yml
   :language: yaml
   :linenos:
   :caption: Raw Magnetic Data YAML file


.. GENERATED FROM PYTHON SOURCE LINES 65-67

Add the Gridded Data
""""""""""""""""""""

.. GENERATED FROM PYTHON SOURCE LINES 69-71

Import a tif of gridded mag data.
The metadata file for raster datasets should contain paths to the raster files.

.. GENERATED FROM PYTHON SOURCE LINES 71-75

.. code-block:: Python


    d_supp1 = join(data_path, 'WI_Magnetics_grids_md.yml')
    gd = data_container.gs.add(key='grids', metadata_file=d_supp1)








.. GENERATED FROM PYTHON SOURCE LINES 76-81

.. literalinclude:: /../../examples/data_files/magnetics/WI_Magnetics_grids_md.yml
   :language: yaml
   :linenos:
   :caption: Gridded (Raster) Magnetic YAML file


.. GENERATED FROM PYTHON SOURCE LINES 85-87

Save to NetCDF file
^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 87-91

.. code-block:: Python


    d_out = join(data_path, 'magnetics.nc')
    survey.gs.to_netcdf(d_out)








.. GENERATED FROM PYTHON SOURCE LINES 92-94

Opening a GS NetCDF 
^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 94-97

.. code-block:: Python


    new_survey = gspy.open_datatree(d_out)['survey']








.. GENERATED FROM PYTHON SOURCE LINES 98-100

View the Data Tree
^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 100-103

.. code-block:: Python


    print(new_survey)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    <xarray.DataTree 'survey'>
    Group: /survey
    │   Dimensions:                 ()
    │   Coordinates:
    │       spatial_ref             float64 8B ...
    │   Data variables:
    │       survey_information      float64 8B ...
    │       flightline_information  float64 8B ...
    │       survey_equipment        float64 8B ...
    │   Attributes:
    │       type:          survey
    │       title:         Magnetic data from SkyTEM Airborne Electromagnetic (AEM) S...
    │       institution:   USGS Geology, Geophysics, and Geochemistry Science Center
    │       source:        SkyTEM raw data, USGS processed data
    │       history:       (1) Data acquisition 01/2021 - 02/2021 by SkyTEM Canada In...
    │       references:    Minsley, Burke J., B.R. Bloss, D.J. Hart, W. Fitzpatrick, ...
    │       comment:       This dataset includes minimally processed (raw) AEM and ra...
    │       summary:       Magnetic survey data were collected during January and Feb...
    │       content:       Wisconsin SkyTEM survey information /survey; raw flightlin...
    │       gspy_version:  2.2.4
    │       conventions:   GS-2.0, CF-1.13
    └── Group: /survey/magnetic_data
        │   Dimensions:      ()
        │   Data variables:
        │       spatial_ref  float64 8B ...
        │   Attributes:
        │       content:  raw flightline and gridded magnetic data
        │       comment:  grids were contractor-derived
        │       type:     container
        ├── Group: /survey/magnetic_data/raw_data
        │   │   Dimensions:      (index: 6785)
        │   │   Coordinates:
        │   │     * index        (index) float64 54kB 0.0 1.0 2.0 ... 6.783e+03 6.784e+03
        │   │       spatial_ref  float64 8B ...
        │   │       x            (index) float64 54kB ...
        │   │       y            (index) float64 54kB ...
        │   │       z            (index) float64 54kB ...
        │   │   Data variables: (12/17)
        │   │       fid          (index) float64 54kB ...
        │   │       line         (index) float64 54kB ...
        │   │       lon          (index) float64 54kB ...
        │   │       lat          (index) float64 54kB ...
        │   │       alt          (index) float64 54kB ...
        │   │       height       (index) float64 54kB ...
        │   │       ...           ...
        │   │       mag_raw      (index) float64 54kB ...
        │   │       tmi          (index) float64 54kB ...
        │   │       rmf          (index) float64 54kB ...
        │   │       igrf         (index) float64 54kB ...
        │   │       inc          (index) float64 54kB ...
        │   │       dec          (index) float64 54kB ...
        │   │   Attributes:
        │   │       content:     raw data
        │   │       comment:     Contains flightline magnetic data
        │   │       type:        data
        │   │       structure:   tabular
        │   │       mode:        airborne
        │   │       method:      magnetic, total magnetic field
        │   │       instrument:  cesium vapour
        │   └── Group: /survey/magnetic_data/raw_data/magnetic_system
        │           Dimensions:                           (n_transmitter: 1, n_receiver: 1,
        │                                                  n_couplet: 1, n_base_magnetometer: 1,
        │                                                  base_mag_locations: 2)
        │           Coordinates:
        │             * n_transmitter                     (n_transmitter) float64 8B 0.0
        │             * n_receiver                        (n_receiver) float64 8B 0.0
        │             * n_couplet                         (n_couplet) float64 8B 0.0
        │             * n_base_magnetometer               (n_base_magnetometer) float64 8B 0.0
        │             * base_mag_locations                (base_mag_locations) float64 16B 1.0 2.0
        │           Data variables: (12/29)
        │               transmitter_label                 (n_transmitter) <U7 28B ...
        │               transmitter_description           (n_transmitter) <U142 568B ...
        │               receiver_label                    (n_receiver) <U19 76B ...
        │               receiver_sensor_type              (n_receiver) <U23 92B ...
        │               receiver_sensor_model             (n_receiver) <U6 24B ...
        │               receiver_sensor_manufacturer      (n_receiver) <U10 40B ...
        │               ...                                ...
        │               diurnal_correction                <U110 440B ...
        │               tieline_levelling                 <U33 132B ...
        │               microlevelling                    <U31 124B ...
        │               igrf_model_date                   <U21 84B ...
        │               igrf_model_location               <U54 216B ...
        │               igrf_model_height                 <U69 276B ...
        │           Attributes:
        │               type:        system
        │               mode:        airborne
        │               method:      magnetic
        │               instrument:  Geometrics G-822A cesium‑vapor magnetometer
        │               name:        magnetic_system
        └── Group: /survey/magnetic_data/grids
                Dimensions:       (x: 799, nv: 2, y: 1155)
                Coordinates:
                  * x             (x) float64 6kB 6.551e+05 6.552e+05 ... 7.348e+05 7.349e+05
                  * nv            (nv) float64 16B 0.0 1.0
                  * y             (y) float64 9kB 4.953e+05 4.952e+05 ... 3.8e+05 3.799e+05
                    spatial_ref   float64 8B ...
                Data variables:
                    x_bnds        (x, nv) float64 13kB ...
                    y_bnds        (y, nv) float64 18kB ...
                    magnetic_tmi  (y, x) float64 7MB ...
                    magnetic_rmf  (y, x) float64 7MB ...
                Attributes:
                    content:     gridded magnetic maps
                    comment:     contractor-derived maps
                    type:        data
                    structure:   raster
                    mode:        airborne
                    method:      magnetic, total magnetic field
                    instrument:  cesium vapour
                    property:    magnetic




.. GENERATED FROM PYTHON SOURCE LINES 104-106

Plotting
^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 106-111

.. code-block:: Python


    plt.figure()
    new_survey['magnetic_data/raw_data']['height'].plot()
    plt.tight_layout()




.. image-sg:: /examples/Creating_GS_Files/images/sphx_glr_plot_csv_magnetics_001.png
   :alt: spatial_ref = 0.0
   :srcset: /examples/Creating_GS_Files/images/sphx_glr_plot_csv_magnetics_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 112-117

.. code-block:: Python

    pd = new_survey['magnetic_data/raw_data']['tmi']
    plt.figure()
    pd.plot()
    plt.tight_layout()




.. image-sg:: /examples/Creating_GS_Files/images/sphx_glr_plot_csv_magnetics_002.png
   :alt: spatial_ref = 0.0
   :srcset: /examples/Creating_GS_Files/images/sphx_glr_plot_csv_magnetics_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 118-127

.. code-block:: Python

    m = new_survey['magnetic_data/grids/magnetic_tmi']
    plt.figure()
    m.plot(cmap='jet')
    plt.tight_layout()

    plt.show()






.. image-sg:: /examples/Creating_GS_Files/images/sphx_glr_plot_csv_magnetics_003.png
   :alt: spatial_ref = 0.0
   :srcset: /examples/Creating_GS_Files/images/sphx_glr_plot_csv_magnetics_003.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.531 seconds)


.. _sphx_glr_download_examples_Creating_GS_Files_plot_csv_magnetics.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_csv_magnetics.ipynb <plot_csv_magnetics.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_csv_magnetics.py <plot_csv_magnetics.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_csv_magnetics.zip <plot_csv_magnetics.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
