
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/Creating_GS_Files/plot_aseg_tempest.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_aseg_tempest.py>`
        to download the full example code.

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

.. _sphx_glr_examples_Creating_GS_Files_plot_aseg_tempest.py:


ASEG-GDF (Tempest AEM)
----------------------

This example demonstrates the workflow for creating a GS file from the `ASEG <https://www.aseg.org.au/sites/default/files/pdf/ASEG-GDF2-REV4.pdf>`_ file format, as well as how to add multiple associated datasets to the Survey. Specifically, this AEM survey contains the following datasets:

1. Raw AEM data, from the Tempest system
2. Inverted resistivity models
3. An interpolated map of total magnetic intensity

Source Reference: Minsley, B.J., James, S.R., Bedrosian, P.A., Pace, M.D., Hoogenboom, B.E., and Burton, B.L., 2021, Airborne electromagnetic, magnetic, and radiometric survey of the Mississippi Alluvial Plain, November 2019 - March 2020: U.S. Geological Survey data release, https://doi.org/10.5066/P9E44CTQ.

.. GENERATED FROM PYTHON SOURCE LINES 16-20

.. code-block:: Python

    import matplotlib.pyplot as plt
    from os.path import join
    import gspy








.. GENERATED FROM PYTHON SOURCE LINES 21-23

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

.. GENERATED FROM PYTHON SOURCE LINES 23-33

.. code-block:: Python


    # Path to example files
    data_path = "..//data_files//tempest_aseg"

    # Survey Metadata file
    metadata = join(data_path, "data//Tempest_survey_md.yml")

    # Establish survey instance
    survey = gspy.Survey.from_dict(metadata)








.. GENERATED FROM PYTHON SOURCE LINES 34-39

.. literalinclude:: /../../examples/data_files/tempest_aseg/data/Tempest_survey_md.yml
   :language: yaml
   :linenos:
   :caption: Survey YAML file


.. GENERATED FROM PYTHON SOURCE LINES 42-44

Create a branch and attach data leaves
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 46-47

Create the first branch (container) called "data"

.. GENERATED FROM PYTHON SOURCE LINES 47-49

.. code-block:: Python

    data_container = survey.gs.add_container('data', **dict(content = "raw data"))








.. GENERATED FROM PYTHON SOURCE LINES 50-51

1. Raw Data

.. GENERATED FROM PYTHON SOURCE LINES 53-54

Import raw AEM data from ASEG-GDF2 format.

.. GENERATED FROM PYTHON SOURCE LINES 54-63

.. code-block:: Python


    d_data = join(data_path, 'data//Tempest.dat')
    d_supp = join(data_path, 'data//Tempest_data_md.yml')

    # Add the raw AEM data to the data branch
    rd = data_container.gs.add(key='raw_data', 
                               data_filename=d_data, 
                               metadata_file=d_supp)








.. GENERATED FROM PYTHON SOURCE LINES 64-65

Note for ASEG-GDF2 files, variable metadata is pulled directly from the DFN file associated with the DAT file. Any additional variable metadata, or desired overwrites to the DFN values, can be passed through the YAML. 

.. GENERATED FROM PYTHON SOURCE LINES 67-68

In this example, multiple systems are defined in the Tempest_data_md.yml metadata file:

.. GENERATED FROM PYTHON SOURCE LINES 70-75

.. literalinclude:: /../../examples/data_files/tempest_aseg/data/Tempest_data_md.yml
   :language: yaml
   :linenos:
   :caption: Raw Data YAML file


.. GENERATED FROM PYTHON SOURCE LINES 78-80

Create a 2nd branch and attach model data leaves
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 80-95

.. code-block:: Python


    model_container = survey.gs.add_container('models', **dict(content = "inverted 1-D electrical resistivity models"))

    # Define Path to inverted AEM models and corresponding metadata file
    m_data = join(data_path, 'model//Tempest_model.dat')
    m_supp = join(data_path, 'model//Tempest_model_md.yml')

    # Add models to the model container, note this example contains a "parameters" group that
    # is added as a leaflet below the model group.
    mod = model_container.gs.add(key='inverted_models', 
                                 data_filename=m_data, 
                                 metadata_file=m_supp, 
                                 system=rd.tempest_system,
                                 derived_from=rd)








.. GENERATED FROM PYTHON SOURCE LINES 96-101

.. literalinclude:: /../../examples/data_files/tempest_aseg/model/Tempest_model_md.yml
   :language: yaml
   :linenos:
   :caption: Inverted Model YAML file


.. GENERATED FROM PYTHON SOURCE LINES 104-106

Create a 3rd branch for the magnetic intensity map
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. GENERATED FROM PYTHON SOURCE LINES 106-116

.. code-block:: Python


    # Create a new branch for the contractor-derived total magnetic intensity map
    map_container = survey.gs.add_container('derived_maps', **dict(content = "derived maps"))

    # Import the magnetic data from TIF-format.
    d_supp = join(data_path, 'data//Tempest_raster_md.yml')

    # Add the magnetic map to the data
    maps = map_container.gs.add(key='maps', metadata_file = d_supp)








.. GENERATED FROM PYTHON SOURCE LINES 117-118

Note: raster data files are defined within the metadata file

.. GENERATED FROM PYTHON SOURCE LINES 120-125

.. literalinclude:: /../../examples/data_files/tempest_aseg/data/Tempest_raster_md.yml
   :language: yaml
   :linenos:
   :caption: Magnetic Raster Map YAML file


.. GENERATED FROM PYTHON SOURCE LINES 128-130

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

.. GENERATED FROM PYTHON SOURCE LINES 130-134

.. code-block:: Python


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








.. GENERATED FROM PYTHON SOURCE LINES 135-136

Read back in the NetCDF file

.. GENERATED FROM PYTHON SOURCE LINES 136-138

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 139-141

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

.. GENERATED FROM PYTHON SOURCE LINES 141-143

.. 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 ...
    │       survey_units            float64 8B ...
    │       flightline_information  float64 8B ...
    │       survey_equipment        float64 8B ...
    │   Attributes:
    │       type:          survey
    │       title:         Example Tempest Airborne Electromagnetic (AEM) Dataset
    │       institution:   USGS Geology, Geophysics, & Geochemistry Science Center
    │       source:        Contractor provided ASEG-GDF2 formatted data
    │       history:       This example dataset includes the raw AEM data and gridded...
    │       references:    Minsley, B.J., James, S.R., Bedrosian, P.A., Pace, M.D., H...
    │       comment:       This dataset is incomplete and has been downsampled for th...
    │       content:       Tempest AEM Survey from the Mississippi Alluvial Plain /su...
    │       gspy_version:  2.2.4
    │       conventions:   GS-2.0, CF-1.13
    ├── Group: /survey/data
    │   │   Dimensions:      ()
    │   │   Data variables:
    │   │       spatial_ref  float64 8B ...
    │   │   Attributes:
    │   │       content:  raw data
    │   │       type:     container
    │   └── Group: /survey/data/raw_data
    │       │   Dimensions:         (index: 2001, gate_times: 15)
    │       │   Coordinates:
    │       │     * index           (index) float64 16kB 0.0 1.0 2.0 ... 1.999e+03 2e+03
    │       │     * gate_times      (gate_times) float64 120B 1.085e-05 3.255e-05 ... 0.01338
    │       │       spatial_ref     float64 8B ...
    │       │       x               (index) float64 16kB ...
    │       │       y               (index) float64 16kB ...
    │       │       z               (index) float64 16kB ...
    │       │   Data variables: (12/58)
    │       │       line            (index) float64 16kB ...
    │       │       flight          (index) float64 16kB ...
    │       │       fiducial        (index) float64 16kB ...
    │       │       proj_cgg        (index) float64 16kB ...
    │       │       proj_client     (index) float64 16kB ...
    │       │       date            (index) float64 16kB ...
    │       │       ...              ...
    │       │       z_primaryfield  (index) float64 16kB ...
    │       │       z_vlf1          (index) float64 16kB ...
    │       │       z_vlf2          (index) float64 16kB ...
    │       │       z_vlf3          (index) float64 16kB ...
    │       │       z_vlf4          (index) float64 16kB ...
    │       │       z_geofact       (index) float64 16kB ...
    │       │   Attributes:
    │       │       content:     raw AEM data
    │       │       comment:     This dataset includes minimally processed (raw) AEM data
    │       │       type:        data
    │       │       mode:        airborne
    │       │       method:      ['magnetic', 'radiometric', 'electromagnetic, time domain']
    │       │       instrument:  30Hz Tempest
    │       │       structure:   tabular
    │       └── Group: /survey/data/raw_data/tempest_system
    │               Dimensions:                       (gate_times: 15, nv: 2, n_transmitter: 1,
    │                                                  waveform_time: 7, n_receiver: 2,
    │                                                  n_couplet: 2, dim_0: 1)
    │               Coordinates:
    │                 * nv                            (nv) float64 16B 0.0 1.0
    │                 * n_transmitter                 (n_transmitter) float64 8B 0.0
    │                 * waveform_time                 (waveform_time) float64 56B -0.01667 ... 0....
    │                 * n_receiver                    (n_receiver) float64 16B 0.0 1.0
    │                 * n_couplet                     (n_couplet) float64 16B 0.0 1.0
    │               Dimensions without coordinates: dim_0
    │               Data variables: (12/26)
    │                   gate_times_bnds               (gate_times, nv) float64 240B ...
    │                   transmitter_label             (n_transmitter) <U1 4B ...
    │                   transmitter_area              (n_transmitter) int64 8B ...
    │                   transmitter_waveform_type     (n_transmitter) <U6 24B ...
    │                   transmitter_waveform_current  (waveform_time) float64 56B ...
    │                   transmitter_scale_factor      (n_transmitter) float64 8B ...
    │                   ...                            ...
    │                   couplet_txrx_dz               (n_couplet) int64 16B ...
    │                   data_normalized               (dim_0) bool 1B ...
    │                   output_data_type              <U1 4B ...
    │                   reference_frame               <U24 96B ...
    │                   output_sample_frequency       (dim_0) int64 8B ...
    │                   digitization_frequency        (dim_0) int64 8B ...
    │               Attributes:
    │                   type:        system
    │                   mode:        airborne
    │                   method:      electromagnetic, time domain
    │                   instrument:  30Hz Tempest
    │                   name:        tempest_system
    ├── Group: /survey/models
    │   │   Dimensions:      ()
    │   │   Data variables:
    │   │       spatial_ref  float64 8B ...
    │   │   Attributes:
    │   │       content:  inverted 1-D electrical resistivity models
    │   │       type:     container
    │   └── Group: /survey/models/inverted_models
    │       │   Dimensions:                  (layer_depth: 30, nv: 2, gate_times: 15,
    │       │                                 index: 2001)
    │       │   Coordinates:
    │       │     * layer_depth              (layer_depth) float64 240B 1.5 4.65 ... 424.2 467.5
    │       │     * nv                       (nv) float64 16B 0.0 1.0
    │       │     * gate_times               (gate_times) float64 120B 1.085e-05 ... 0.01338
    │       │     * index                    (index) float64 16kB 0.0 1.0 ... 1.999e+03 2e+03
    │       │       spatial_ref              float64 8B ...
    │       │       x                        (index) float64 16kB ...
    │       │       y                        (index) float64 16kB ...
    │       │       z                        (index) float64 16kB ...
    │       │   Data variables: (12/46)
    │       │       layer_depth_bnds         (layer_depth, nv) float64 480B ...
    │       │       gate_times_bnds          (gate_times, nv) float64 240B ...
    │       │       uniqueid                 (index) float64 16kB ...
    │       │       survey                   (index) float64 16kB ...
    │       │       date                     (index) float64 16kB ...
    │       │       flight                   (index) float64 16kB ...
    │       │       ...                       ...
    │       │       phic                     (index) float64 16kB ...
    │       │       phit                     (index) float64 16kB ...
    │       │       phig                     (index) float64 16kB ...
    │       │       phis                     (index) float64 16kB ...
    │       │       lambda                   (index) float64 16kB ...
    │       │       iterations               (index) float64 16kB ...
    │       │   Attributes:
    │       │       content:     inverted resistivity models
    │       │       comment:     This dataset includes inverted resistivity models derived fr...
    │       │       type:        model
    │       │       method:      electromagnetic, time domain
    │       │       instrument:  30Hz Tempest
    │       │       mode:        airborne
    │       │       property:    electrical conductivity
    │       │       structure:   tabular
    │       ├── Group: /survey/models/inverted_models/tempest_system
    │       │       Dimensions:                       (gate_times: 15, nv: 2, n_transmitter: 1,
    │       │                                          waveform_time: 7, n_receiver: 2,
    │       │                                          n_couplet: 2, dim_0: 1)
    │       │       Coordinates:
    │       │         * n_transmitter                 (n_transmitter) float64 8B 0.0
    │       │         * waveform_time                 (waveform_time) float64 56B -0.01667 ... 0....
    │       │         * n_receiver                    (n_receiver) float64 16B 0.0 1.0
    │       │         * n_couplet                     (n_couplet) float64 16B 0.0 1.0
    │       │       Dimensions without coordinates: dim_0
    │       │       Data variables: (12/26)
    │       │           gate_times_bnds               (gate_times, nv) float64 240B ...
    │       │           transmitter_label             (n_transmitter) <U1 4B ...
    │       │           transmitter_area              (n_transmitter) int64 8B ...
    │       │           transmitter_waveform_type     (n_transmitter) <U6 24B ...
    │       │           transmitter_waveform_current  (waveform_time) float64 56B ...
    │       │           transmitter_scale_factor      (n_transmitter) float64 8B ...
    │       │           ...                            ...
    │       │           couplet_txrx_dz               (n_couplet) int64 16B ...
    │       │           data_normalized               (dim_0) bool 1B ...
    │       │           output_data_type              <U1 4B ...
    │       │           reference_frame               <U24 96B ...
    │       │           output_sample_frequency       (dim_0) int64 8B ...
    │       │           digitization_frequency        (dim_0) int64 8B ...
    │       │       Attributes:
    │       │           type:        system
    │       │           mode:        airborne
    │       │           method:      electromagnetic, time domain
    │       │           instrument:  30Hz Tempest
    │       │           name:        tempest_system
    │       └── Group: /survey/models/inverted_models/inversion_parameters
    │               Dimensions:             ()
    │               Data variables:
    │                   software            <U61 244B ...
    │                   software_reference  <U298 1kB ...
    │                   description         <U1402 6kB ...
    │                   doi_calculation     <U592 2kB ...
    │                   phid_cut            <U256 1kB ...
    │               Attributes:
    │                   type:        parameters
    │                   method:      electromagnetic, time domain
    │                   instrument:  30Hz Tempest
    │                   mode:        airborne
    │                   property:    electrical conductivity
    │                   name:        inversion_parameters
    └── Group: /survey/derived_maps
        │   Dimensions:      ()
        │   Data variables:
        │       spatial_ref  float64 8B ...
        │   Attributes:
        │       content:  derived maps
        │       type:     container
        └── Group: /survey/derived_maps/maps
            │   Dimensions:       (x: 599, nv: 2, y: 1212)
            │   Coordinates:
            │     * x             (x) float64 5kB 2.928e+05 2.934e+05 ... 6.51e+05 6.516e+05
            │     * nv            (nv) float64 16B 0.0 1.0
            │     * y             (y) float64 10kB 1.607e+06 1.606e+06 ... 8.808e+05 8.802e+05
            │       spatial_ref   float64 8B ...
            │   Data variables:
            │       x_bnds        (x, nv) float64 10kB ...
            │       y_bnds        (y, nv) float64 19kB ...
            │       magnetic_tmi  (y, x) float64 6MB ...
            │   Attributes:
            │       comment:     contractor-derived product
            │       content:     gridded map of total magnetic intensity
            │       type:        data
            │       mode:        airborne
            │       method:      magnetic
            │       instrument:  Scintrex CS-3 cesium vapor magnetometer
            │       structure:   raster
            └── Group: /survey/derived_maps/maps/magnetic_system
                    Dimensions:                             (base_mag_locations: 6, nv: 2,
                                                             n_transmitter: 1, n_receiver: 1,
                                                             n_couplet: 1, n_base_magnetometer: 1)
                    Coordinates:
                      * base_mag_locations                  (base_mag_locations) float64 48B 1.0 ...
                      * 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
                    Data variables: (12/35)
                        base_mag_locations_bnds             (base_mag_locations, nv) float64 96B ...
                        transmitter_label                   (n_transmitter) <U7 28B ...
                        transmitter_description             (n_transmitter) <U107 428B ...
                        receiver_label                      (n_receiver) <U19 76B ...
                        receiver_sensor_type                (n_receiver) <U12 48B ...
                        receiver_sensor_model               (n_receiver) <U4 16B ...
                        ...                                  ...
                        diurnal_correction                  <U142 568B ...
                        igrf_model_date                     <U10 40B ...
                        igrf_model_height                   <U7 28B ...
                        igrf_removed_model_epoch            <U6 24B ...
                        tieline_levelling                   <U137 548B ...
                        deliverables                        <U63 252B ...
                    Attributes:
                        type:        system
                        mode:        airborne
                        method:      magnetic
                        instrument:  Scintrex CS-3 c cesium-vapor magnetometer
                        name:        magnetic_system




.. GENERATED FROM PYTHON SOURCE LINES 144-145

Once the survey is read in, we can access variables like a standard xarray dataset.

.. GENERATED FROM PYTHON SOURCE LINES 147-148

Option A:

.. GENERATED FROM PYTHON SOURCE LINES 148-149

.. code-block:: Python

    print(new_survey['derived_maps/maps'].magnetic_tmi)




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

 .. code-block:: none

    <xarray.DataArray 'magnetic_tmi' (y: 1212, x: 599)> Size: 6MB
    [725988 values with dtype=float64]
    Coordinates:
      * y            (y) float64 10kB 1.607e+06 1.606e+06 ... 8.808e+05 8.802e+05
      * x            (x) float64 5kB 2.928e+05 2.934e+05 ... 6.51e+05 6.516e+05
        spatial_ref  float64 8B ...
    Attributes:
        system_couplet:  passive_scalar_magnetometer
        standard_name:   total_magnetic_intensity
        long_name:       Total magnetic intensity, diurnally corrected and filtered
        units:           nT
        valid_range:     [-17504.6640625   11490.32324219]
        grid_mapping:    spatial_ref




.. GENERATED FROM PYTHON SOURCE LINES 150-151

Option B:

.. GENERATED FROM PYTHON SOURCE LINES 151-153

.. code-block:: Python

    print(new_survey['derived_maps/maps']['magnetic_tmi'])





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

 .. code-block:: none

    <xarray.DataArray 'magnetic_tmi' (y: 1212, x: 599)> Size: 6MB
    [725988 values with dtype=float64]
    Coordinates:
      * y            (y) float64 10kB 1.607e+06 1.606e+06 ... 8.808e+05 8.802e+05
      * x            (x) float64 5kB 2.928e+05 2.934e+05 ... 6.51e+05 6.516e+05
        spatial_ref  float64 8B ...
    Attributes:
        system_couplet:  passive_scalar_magnetometer
        standard_name:   total_magnetic_intensity
        long_name:       Total magnetic intensity, diurnally corrected and filtered
        units:           nT
        valid_range:     [-17504.6640625   11490.32324219]
        grid_mapping:    spatial_ref




.. GENERATED FROM PYTHON SOURCE LINES 154-155

Option C:

.. GENERATED FROM PYTHON SOURCE LINES 155-157

.. code-block:: Python

    print(new_survey['derived_maps']['maps']['magnetic_tmi'])





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

 .. code-block:: none

    <xarray.DataArray 'magnetic_tmi' (y: 1212, x: 599)> Size: 6MB
    [725988 values with dtype=float64]
    Coordinates:
      * y            (y) float64 10kB 1.607e+06 1.606e+06 ... 8.808e+05 8.802e+05
      * x            (x) float64 5kB 2.928e+05 2.934e+05 ... 6.51e+05 6.516e+05
        spatial_ref  float64 8B ...
    Attributes:
        system_couplet:  passive_scalar_magnetometer
        standard_name:   total_magnetic_intensity
        long_name:       Total magnetic intensity, diurnally corrected and filtered
        units:           nT
        valid_range:     [-17504.6640625   11490.32324219]
        grid_mapping:    spatial_ref




.. GENERATED FROM PYTHON SOURCE LINES 158-161

Plotting Examples
^^^^^^^^^^^^^^^^^
demonstrating different ways to access and plot variables

.. GENERATED FROM PYTHON SOURCE LINES 161-166

.. code-block:: Python


    # Make a scatter plot of a specific tabular variable, using GSPy's plotter
    plt.figure()
    new_survey['data']['raw_data'].gs.scatter(x='x', hue='tx_height', cmap='jet')




.. image-sg:: /examples/Creating_GS_Files/images/sphx_glr_plot_aseg_tempest_001.png
   :alt: plot aseg tempest
   :srcset: /examples/Creating_GS_Files/images/sphx_glr_plot_aseg_tempest_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 167-168

Make a 2-D map plot of a specific raster variable, using Xarrays's plotter

.. GENERATED FROM PYTHON SOURCE LINES 168-170

.. code-block:: Python

    plt.figure()
    new_survey['derived_maps/maps']['magnetic_tmi'].plot(cmap='jet', robust=True)
    plt.show()


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






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

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


.. _sphx_glr_download_examples_Creating_GS_Files_plot_aseg_tempest.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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