.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/Interacting_With_GS_Files/plot_xarray_methods.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_Interacting_With_GS_Files_plot_xarray_methods.py: Basic Class Structure --------------------- The three primary classes (Survey, Tabular, and Raster) all contain data and metadata within `Xarray `_ Datasets. This example demonstrates how to access the xarray object for each class, and methods for exploring the data and metadata. This example uses ASEG-formatted raw AEM data from the Tempest system, and a 2-D GeoTiFF of magnetic data. Dataset 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 14-20 .. code-block:: Python import matplotlib.pyplot as plt from os.path import join import gspy from gspy import Survey from pprint import pprint .. GENERATED FROM PYTHON SOURCE LINES 21-22 First open the netcdf GS standard survey file. .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python survey = gspy.open_datatree("../../../../example_material/example_2/data/Tempest.nc")['survey'] .. GENERATED FROM PYTHON SOURCE LINES 26-28 Accessing the Xarray object +++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 30-31 Survey .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python # The Survey's metadata is accessed through the xarray property print('Survey:') print(survey) .. rst-class:: sphx-glr-script-out .. code-block:: none 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: │ title: Example Tempest Airborne Electromagnetic (AEM) Dataset │ institution: USGS Geology, Geophysics, & Geochemistry Science Center │ source: Contractor provided ASEG-formatted data │ history: │ references: │ comment: │ content: │ content: gridded magnetic map └── Group: /survey/models │ Dimensions: () │ Data variables: │ spatial_ref float64 8B ... │ Attributes: │ content: inverted models │ type: container └── Group: /survey/models/inverted_models Dimensions: (layer_depth: 30, nv: 2, gate_times: 15, index: 20701) Coordinates: * layer_depth (layer_depth) float64 240B 1.5 4.65 ... 424.2 467.5 * nv (nv) int64 16B 0 1 * gate_times (gate_times) float64 120B 1.085e-05 ... 0.01338 * index (index) int32 83kB 0 1 2 3 ... 20698 20699 20700 spatial_ref float64 8B ... x (index) float64 166kB ... y (index) float64 166kB ... z (index) float64 166kB ... Data variables: (12/49) layer_depth_bnds (layer_depth, nv) float64 480B ... gate_times_bnds (gate_times, nv) float64 240B ... uniqueid (index) int32 83kB ... survey (index) int32 83kB ... date (index) int32 83kB ... flight (index) int32 83kB ... ... ... phic (index) float64 166kB ... phit (index) float64 166kB ... phig (index) float64 166kB ... phis (index) float64 166kB ... lambda (index) float64 166kB ... iterations (index) int32 83kB ... Attributes: content: inverted resistivity models comment: This dataset includes inverted resistivity models derived from ... .. GENERATED FROM PYTHON SOURCE LINES 37-38 To look just at the attributes .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python print('Survey Attributes:\n') pprint(survey.attrs) .. rst-class:: sphx-glr-script-out .. code-block:: none Survey Attributes: {'comment': '', 'content': '', 'conventions': 'CF-1.8, GS-0.0', 'created_by': 'gspy==0.0.1', 'history': '', 'institution': 'USGS Geology, Geophysics, & Geochemistry Science Center', 'references': '', 'source': 'Contractor provided ASEG-formatted data', 'title': 'Example Tempest Airborne Electromagnetic (AEM) Dataset', 'type': 'survey'} .. GENERATED FROM PYTHON SOURCE LINES 42-43 Or expand a specific variable .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python print('Survey Information:\n') print(survey['survey_information']) .. rst-class:: sphx-glr-script-out .. code-block:: none Survey Information: Size: 8B [1 values with dtype=float64] Coordinates: spatial_ref float64 8B ... Attributes: contractor_project_number: 603756FWA contractor: CGG Canada Services Ltd. client: U.S. Geological Survey survey_type: electromagneticmagneticradiometric survey_area_name: Mississippi Alluvial Plain (MAP) state: MOARTNMSLAILKY country: USA acquisition_start: 20191120 acquisition_end: 20200307 dataset_created: 20200420 .. GENERATED FROM PYTHON SOURCE LINES 47-49 Datasets Get the list of datasets attached to the survey .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python print(survey.items) .. rst-class:: sphx-glr-script-out .. code-block:: none 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: │ title: Example Tempest Airborne Electromagnetic (AEM) Dataset │ institution: USGS Geology, Geophysics, & Geochemistry Science Center │ source: Contractor provided ASEG-formatted data │ history: │ references: │ comment: │ content: │ content: gridded magnetic map └── Group: /survey/models │ Dimensions: () │ Data variables: │ spatial_ref float64 8B ... │ Attributes: │ content: inverted models │ type: container └── Group: /survey/models/inverted_models Dimensions: (layer_depth: 30, nv: 2, gate_times: 15, index: 20701) Coordinates: * layer_depth (layer_depth) float64 240B 1.5 4.65 ... 424.2 467.5 * nv (nv) int64 16B 0 1 * gate_times (gate_times) float64 120B 1.085e-05 ... 0.01338 * index (index) int32 83kB 0 1 2 3 ... 20698 20699 20700 spatial_ref float64 8B ... x (index) float64 166kB ... y (index) float64 166kB ... z (index) float64 166kB ... Data variables: (12/49) layer_depth_bnds (layer_depth, nv) float64 480B ... gate_times_bnds (gate_times, nv) float64 240B ... uniqueid (index) int32 83kB ... survey (index) int32 83kB ... date (index) int32 83kB ... flight (index) int32 83kB ... ... ... phic (index) float64 166kB ... phit (index) float64 166kB ... phig (index) float64 166kB ... phis (index) float64 166kB ... lambda (index) float64 166kB ... iterations (index) int32 83kB ... Attributes: content: inverted resistivity models comment: This dataset includes inverted resistivity models derived from ...> .. GENERATED FROM PYTHON SOURCE LINES 52-54 Datasets are attached to the Survey regardless of their format whether unstructured tabular data or image-type raster data .. GENERATED FROM PYTHON SOURCE LINES 54-63 .. code-block:: Python # Tabular print('Tabular:\n') print(survey['data']) # Raster print('\nRaster:\n') print(survey['data/derived_maps/maps']) .. rst-class:: sphx-glr-script-out .. code-block:: none Tabular: Group: /survey/data │ Dimensions: () │ Data variables: │ spatial_ref float64 8B ... │ Attributes: │ content: raw and processed data │ type: container ├── Group: /survey/data/raw_data │ │ Dimensions: (index: 20701, gate_times: 15) │ │ Coordinates: │ │ * index (index) int32 83kB 0 1 2 3 4 ... 20697 20698 20699 20700 │ │ * gate_times (gate_times) float64 120B 1.085e-05 3.255e-05 ... 0.01338 │ │ spatial_ref float64 8B ... │ │ x (index) float64 166kB ... │ │ y (index) float64 166kB ... │ │ z (index) float64 166kB ... │ │ Data variables: (12/61) │ │ line (index) int32 83kB ... │ │ flight (index) int32 83kB ... │ │ fiducial (index) float64 166kB ... │ │ proj_cgg (index) int32 83kB ... │ │ proj_client (index) int32 83kB ... │ │ date (index) int32 83kB ... │ │ ... ... │ │ z_primaryfield (index) float64 166kB ... │ │ z_vlf1 (index) float64 166kB ... │ │ z_vlf2 (index) float64 166kB ... │ │ z_vlf3 (index) float64 166kB ... │ │ z_vlf4 (index) float64 166kB ... │ │ z_geofact (index) float64 166kB ... │ │ Attributes: │ │ content: raw data │ │ comment: This dataset includes minimally processed (raw) AEM data │ ├── Group: /survey/data/raw_data/tempest_system │ │ Dimensions: (gate_times: 15, nv: 2, n_transmitter: 1, │ │ waveform_time: 7, n_receiver: 2, │ │ n_component: 2) │ │ Coordinates: │ │ * nv (nv) int64 16B 0 1 │ │ * n_transmitter (n_transmitter) int64 8B 0 │ │ * waveform_time (waveform_time) float64 56B -0.01667 ... 0.... │ │ * n_receiver (n_receiver) int64 16B 0 1 │ │ * n_component (n_component) int64 16B 0 1 │ │ Data variables: (12/21) │ │ gate_times_bnds (gate_times, nv) float64 240B ... │ │ transmitter_label (n_transmitter) content: gridded magnetic map Raster: Group: /survey/data/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) int64 16B 0 1 * 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: content: gridded magnetic map .. GENERATED FROM PYTHON SOURCE LINES 64-65 and the second is located at index 1 .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python print('Second Tabular Group:\n') print(survey['models/inverted_models']) .. rst-class:: sphx-glr-script-out .. code-block:: none Second Tabular Group: Group: /survey/models/inverted_models Dimensions: (layer_depth: 30, nv: 2, gate_times: 15, index: 20701) Coordinates: * layer_depth (layer_depth) float64 240B 1.5 4.65 ... 424.2 467.5 * nv (nv) int64 16B 0 1 * gate_times (gate_times) float64 120B 1.085e-05 ... 0.01338 * index (index) int32 83kB 0 1 2 3 ... 20698 20699 20700 spatial_ref float64 8B ... x (index) float64 166kB ... y (index) float64 166kB ... z (index) float64 166kB ... Data variables: (12/49) layer_depth_bnds (layer_depth, nv) float64 480B ... gate_times_bnds (gate_times, nv) float64 240B ... uniqueid (index) int32 83kB ... survey (index) int32 83kB ... date (index) int32 83kB ... flight (index) int32 83kB ... ... ... phic (index) float64 166kB ... phit (index) float64 166kB ... phig (index) float64 166kB ... phis (index) float64 166kB ... lambda (index) float64 166kB ... iterations (index) int32 83kB ... Attributes: content: inverted resistivity models comment: This dataset includes inverted resistivity models derived from ... .. GENERATED FROM PYTHON SOURCE LINES 69-71 Coordinates, Dimensions, and Attributes +++++++++++++++++++++++++++++++++++++++ .. GENERATED FROM PYTHON SOURCE LINES 73-74 All data variables must have dimensions, coordinate, and attributes .. GENERATED FROM PYTHON SOURCE LINES 76-78 Dimensions ^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 80-82 Tabular data are typicaly 1-D or 2-D variables with the primary dimension being ``index``, which corresponds to the rows of the input text file representing individual measurements. .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python print(survey['models/inverted_models']['index']) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 83kB array([ 0, 1, 2, ..., 20698, 20699, 20700], shape=(20701,), dtype=int32) Coordinates: * index (index) int32 83kB 0 1 2 3 4 ... 20696 20697 20698 20699 20700 spatial_ref float64 8B ... x (index) float64 166kB ... y (index) float64 166kB ... z (index) float64 166kB ... Attributes: standard_name: index long_name: Index of individual data points units: not_defined null_value: not_defined valid_range: [ 0. 20700.] grid_mapping: spatial_ref .. GENERATED FROM PYTHON SOURCE LINES 85-88 If a dimension is not discrete, meaning it represents ranges (such as depth layers), then the bounds on each dimension value also need to be defined, and are linked to the dimension through the "bounds" attribute. .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: Python print('example non-discrete dimension:\n') print(survey['models/inverted_models']['gate_times']) print('\n\ncorresponding bounds on non-discrete dimension:\n') print(survey['models/inverted_models']['gate_times_bnds']) .. rst-class:: sphx-glr-script-out .. code-block:: none example non-discrete dimension: Size: 120B array([1.085000e-05, 3.255000e-05, 5.426000e-05, 8.681000e-05, 1.410600e-04, 2.278700e-04, 3.689300e-04, 5.859500e-04, 9.114800e-04, 1.410630e-03, 2.191900e-03, 3.418070e-03, 5.338690e-03, 8.301020e-03, 1.337928e-02]) Coordinates: * gate_times (gate_times) float64 120B 1.085e-05 3.255e-05 ... 0.01338 spatial_ref float64 8B ... Attributes: standard_name: gate_times long_name: receiver gate times units: seconds null_value: not_defined valid_range: [1.085000e-05 1.337928e-02] grid_mapping: spatial_ref bounds: gate_times_bnds corresponding bounds on non-discrete dimension: Size: 240B [30 values with dtype=float64] Coordinates: * gate_times (gate_times) float64 120B 1.085e-05 3.255e-05 ... 0.01338 * nv (nv) int64 16B 0 1 spatial_ref float64 8B ... Attributes: standard_name: gate_times_bounds long_name: receiver gate times cell boundaries null_value: not_defined valid_range: [-0.00012479 0.01351492] grid_mapping: spatial_ref .. GENERATED FROM PYTHON SOURCE LINES 93-95 Coordinates ^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 97-102 Coordinates define the spatial and temporal positioning of the data (X Y Z T). Additionally, all dimensions are by default classified as a coordinate. This means a dataset can have both dimensional and non-dimensional coordinates. Dimensional coordinates are noted with a * (or bold text) in printed output of the xarray, such as ``index``, ``gate_times``, ``nv`` in this example: .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python print(survey['data'].dataset.coords) .. rst-class:: sphx-glr-script-out .. code-block:: none Coordinates: *empty* .. GENERATED FROM PYTHON SOURCE LINES 105-106 Tabular Coordinates .. GENERATED FROM PYTHON SOURCE LINES 108-113 In Tabular data, coordinates are typically non-dimensional, since the primary dataset dimension is ``index``. By default, we define the spatial coordinates, ``x`` and ``y``, based on the longitude and latitude (or easting/northing) data variables. If relevant, ``z`` and ``t`` coordinate variables can also be defined, representing the vertical and temporal coordinates of the data points. .. GENERATED FROM PYTHON SOURCE LINES 115-116 Note: All coordinates must match the coordinate reference system defined in the Survey. .. GENERATED FROM PYTHON SOURCE LINES 118-119 Raster Coordinates .. GENERATED FROM PYTHON SOURCE LINES 121-124 Raster data are gridded, typically representing maps or multi-dimensional models. Therefore, Raster data almost always have dimensional coordinates, i.e., the data dimensions correspond directly to either spatial or temporal coordinates (``x``, ``y``, ``z``, ``t``). .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python print(survey['data/derived_maps/maps'].coords) .. rst-class:: sphx-glr-script-out .. code-block:: none Coordinates: * x (x) float64 5kB 2.928e+05 2.934e+05 ... 6.51e+05 6.516e+05 * nv (nv) int64 16B 0 1 * y (y) float64 10kB 1.607e+06 1.606e+06 ... 8.808e+05 8.802e+05 spatial_ref float64 8B ... .. GENERATED FROM PYTHON SOURCE LINES 127-128 The Spatial Reference Coordinate .. GENERATED FROM PYTHON SOURCE LINES 130-133 the ``spatial_ref`` coordinate variable is a non-dimensional coordinate that contains information on the coordinate reference system. For more information, see :ref:`Coordinate Reference Systems `. .. GENERATED FROM PYTHON SOURCE LINES 135-137 Attributes ^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 139-142 Both datasets and data variables have attributes (metadata fields). Certain attributes are required, see our documentation on :ref:`the GS standard `. for more details. .. GENERATED FROM PYTHON SOURCE LINES 144-145 Dataset attributes .. GENERATED FROM PYTHON SOURCE LINES 147-151 Dataset attributes provide users a way to document and describe supplementary information about a dataset group as a whole, such as model inversion parameters or other processing descriptions. At a minimum, a ``content`` attribute should contain a brief summary of the contents of the dataset. .. GENERATED FROM PYTHON SOURCE LINES 151-153 .. code-block:: Python pprint(survey['models/inverted_models'].attrs) .. rst-class:: sphx-glr-script-out .. code-block:: none {'comment': 'This dataset includes inverted resistivity models derived from ' 'processed AEM data produced by USGS', 'content': 'inverted resistivity models'} .. GENERATED FROM PYTHON SOURCE LINES 154-155 Variable attributes .. GENERATED FROM PYTHON SOURCE LINES 157-159 Each data variable must contain attributes detailing the metadata of that individual variable. These follow the `Climate and Forecast (CF) metadata conventions `_. .. GENERATED FROM PYTHON SOURCE LINES 159-159 .. code-block:: Python pprint(survey['models/inverted_models']['conductivity'].attrs) .. rst-class:: sphx-glr-script-out .. code-block:: none {'format': '30e15.6', 'grid_mapping': 'spatial_ref', 'long_name': 'Layer conductivity', 'null_value': 'not_defined', 'standard_name': 'conductivity', 'units': 'S/m', 'valid_range': array([1.e-04, 1.e+01])} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.146 seconds) .. _sphx_glr_download_examples_Interacting_With_GS_Files_plot_xarray_methods.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_xarray_methods.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_xarray_methods.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_xarray_methods.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_