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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_source_auto_examples_change_units.py>`
        to download the full example code

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

.. _sphx_glr_source_auto_examples_change_units.py:


=====================================
Example for changing units in dataset
=====================================

This is an example of how to change
units in the xarray dataset.

.. GENERATED FROM PYTHON SOURCE LINES 10-62

.. code-block:: default


    import act
    import numpy as np


    def print_summary(obj, variables):
        for var_name in variables:
            print(f"{var_name}: mean={np.nanmean(obj[var_name].values)} "
                  f"units={obj[var_name].attrs['units']}")
        print()


    variables = ['first_cbh', 'second_cbh', 'alt']

    # Read in some example data
    obj = act.io.armfiles.read_netcdf(act.tests.sample_files.EXAMPLE_CEIL1)

    # Print the variable name, mean of values and units
    print('Variables in read data')
    print_summary(obj, variables)

    # Change units of one varible from m to km
    obj.utils.change_units(variables='first_cbh', desired_unit='km')
    print('Variables with one changed to km')
    print_summary(obj, variables)

    # Change units of more than one varible from to km
    obj.utils.change_units(variables=variables, desired_unit='km')
    print('Variables with both changed to km')
    print_summary(obj, variables)

    # Can change all data variables in the dataset that are units of length by not providing
    # a list of variables. Here we are changing back to orginal meters.
    # Also, because it needs to loop over all variables and try to convert, will take
    # longer if we keep the QC variables. Faseter if we exclude them.
    # The method will return a dataset. In this case the dataset returned is the same
    # dataset.
    skip_variables = [ii for ii in obj.data_vars if ii.startswith('qc_')]
    new_obj = obj.utils.change_units(variables=None, desired_unit='m', skip_variables=skip_variables)
    print('Variables changed back to m by looping over all variables in dataset')
    print('Orginal dataset is same as retured dataset:', obj is new_obj)
    print_summary(new_obj, variables)

    # For coordinate variables need to explicitly give coordinage variable name and use
    # the returned dataset. The xarray method used to change values on coordinate
    # values requries returning a new updated dataet.
    var_name = 'range'
    variables.append(var_name)
    new_obj = obj.utils.change_units(variables=variables, desired_unit='km')
    print('Variables and coordinate variable values changed to km')
    print('Orginal dataset is same as retured dataset:', obj is new_obj)
    print_summary(new_obj, variables)


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

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


.. _sphx_glr_download_source_auto_examples_change_units.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

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