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

.. only:: html

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

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

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

.. _sphx_glr_dataFormatRequirements_netCDF_examples_cartesianGridExample.py:


cartesianGridExample.py
=====================
This script applies the CF Conventions to a basic NetCDF file so Vapor can read it.
It performs the the following:
   - Creates "Coordinate Variables" for the spatial and time dimensions
   - Adds the "axis" attributes to these new Coordinate Variables
   - Adds the "units" attribute to these new Coordinate Variables

The sample data for this script can be downloaded here:
    https://github.com/NCAR/VAPOR-Data/blob/main/netCDF/simple.nc

.. GENERATED FROM PYTHON SOURCE LINES 17-18

Import packages:

.. GENERATED FROM PYTHON SOURCE LINES 18-24

.. code-block:: Python


    import xarray as xr
    import numpy as np
    from pathlib import Path
    # sphinx_gallery_thumbnail_path = '_images/cartesian.png'


.. GENERATED FROM PYTHON SOURCE LINES 25-27

Open sample data as an xarray dataset.
Modify the 'home' variable to point to the directory containing sample data

.. GENERATED FROM PYTHON SOURCE LINES 27-33

.. code-block:: Python

    home = str(Path.home())
    simpleNC = home + "/simple.nc"
    ds = xr.open_dataset(simpleNC)

    ds.info()


.. GENERATED FROM PYTHON SOURCE LINES 34-35

Create a Coordinate Variable for our 'time' dimension and assign a value to it

.. GENERATED FROM PYTHON SOURCE LINES 35-37

.. code-block:: Python

    ds['time'] = np.linspace(start=0, stop=0, num=1);


.. GENERATED FROM PYTHON SOURCE LINES 38-42

Generate Coordinate Variables for our x, y, z dimensions.  The coordinate
values are monotonically increasing and equally spaced.  They are generated
with numpy's linspace function.
https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

.. GENERATED FROM PYTHON SOURCE LINES 42-49

.. code-block:: Python

  
    ds['y'] = np.linspace(start=0, stop=100, num=48)
    ds['x'] = np.linspace(start=0, stop=100, num=48)
    ds['z'] = np.linspace(start=0, stop=50, num=24)

    ds.info() 


.. GENERATED FROM PYTHON SOURCE LINES 50-52

Give our Coordinate Variables 'axis' attributes so Vapor knows which axes they
apply to

.. GENERATED FROM PYTHON SOURCE LINES 52-58

.. code-block:: Python


    ds.time.attrs['axis']      = 'T'
    ds.x.attrs['axis']         = 'X'
    ds.y.attrs['axis']         = 'Y'
    ds.z.attrs['axis']         = 'Z'


.. GENERATED FROM PYTHON SOURCE LINES 59-60

Give our Coordinate Variables 'units' attributes

.. GENERATED FROM PYTHON SOURCE LINES 60-67

.. code-block:: Python


    ds.time.attrs['units']     = 'seconds since 2000-0101'
    ds.x.attrs['units']        = 'm'
    ds.y.attrs['units']        = 'm'
    ds.z.attrs['units']        = 'm'



.. GENERATED FROM PYTHON SOURCE LINES 68-69

It's optional but advisable to give our scalar variables a 'units' attribute

.. GENERATED FROM PYTHON SOURCE LINES 69-73

.. code-block:: Python

    ds.temperature.attrs['units'] = 'K'

    ds.info()


.. GENERATED FROM PYTHON SOURCE LINES 74-75

Save our file for reading into Vapor

.. GENERATED FROM PYTHON SOURCE LINES 75-77

.. code-block:: Python

    ds.to_netcdf( home + "/regularCompliant.nc")


.. GENERATED FROM PYTHON SOURCE LINES 78-79

Plot a 2D cross section of temperature

.. GENERATED FROM PYTHON SOURCE LINES 79-80

.. code-block:: Python

    ds.isel(time=0, z=0).temperature.plot(size=6, robust=True);


.. _sphx_glr_download_dataFormatRequirements_netCDF_examples_cartesianGridExample.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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