An open API service indexing awesome lists of open source software.

https://github.com/mgduda/mpas-plotting

References and Examples for Plotting MPAS Output
https://github.com/mgduda/mpas-plotting

atmospheric-science meterology mpas plotting python

Last synced: 5 months ago
JSON representation

References and Examples for Plotting MPAS Output

Awesome Lists containing this project

README

          

MPAS Plotting
=============
This repository contains a number of examples related to plotting MPAS model
output with Python. It contains examples using MatPlotLib and will later
contain examples using NCAR's PyNGO as well as any other NCAR NCL Python
modules that appear.

The goal of this repository is to function as a set of examples and reference
for Python Modules that can be used to create model output plots.

Note: Files that are named with 'clean' are the same as the files with
the same name, but have less documentation and code comments.

**Tutorial Contents**
* mpas_patches - Tutorial for plotting each indivual MPAS Grid Cell
* ll-plotting - Tutorial for plotting a normal lat, lon grid (using convert_mpas).

**Readme Contents**
* [Introduction to Python Modules](#Introduction)
* [Numpy](#Numpy)
* [Python NetCDF Modules and other Met Datatypes](#NetCDF)
* [Cartopy and Basemap](#Basemap)
* [Python on Cheyenne and Casper](#venv)
* [Python 2 and Python 3](#version)
* [Quick Introduction to MatPlotLib](#MPL-Intro)
* [Helpful References and Guides](#Module-References)

# Introduction to Python Modules

Plotting Meteorological data in Python is a multi-module endeavour and is
undoubtedly a bit overwhelming. Some modules even contain references to each
other, which can be even more consufing. I hope to break this process down
into more manageable chunks, and also point you in the right direction to
find the information you need.

**Numpy**

One feature that Fortran has over vanilla Python are its n-dimensional arrays and its
operations around those arrays. Out of the box Python allows you to have
multidimensional 'lists', but these list of lists are large and
inefficient for vector and scaler operations.

The Numpy module enables smaller, more efficient n-dimensional arrays in a very similar
approach to Fortran n-d arrays. With useful intrinsics such as `shape`,
`size`, `ndim` and others, which allow you to inspect data as you see fit.

It also enables a load of optimized mathematical functions that you can dream of:
.

**Python NetCDF Modules and other Met Datatypes**

There are a few modules out there that can support reading, creating and
manipulating NetCDF datafiles. These include: Scipy NetCDF, NetCDF4 Dataset, xArray, as
well as NCAR's PyNIO (which is an NCL wrapper).

There are multitude of modules to choose from, but I recommend starting with
Scipy's NetCDF module to start. It contains the most examples and is the best
documented; however, it cannot open NetCDF4, but you can use UNIDATA's NetCDF4
Dataset instead, its also easy to use, but its documentation could be better. The
examples in this tutorial use the UNIDATA's NetCDF4 module.

If you need to open other Datatypes such as Grib, HDF, etc. PyNIO is currently
your best choice: . However, with the
shift to Python and PyNIO going into maintenance mode, it may be worth becoming
familiar with Python alternatives. Here is a list of common met datatypes and
modules that can open them:

* GRIB - [cfgrib](https://github.com/ecmwf/cfgrib)
* HDFS - [PyTables](http://www.pytables.org/index.html)
* HDF-EOS - [PyHDF](https://www.hdfeos.org/software/pyhdf.php)

(Note: Besides Scipy's NetCDF and Unidata's NetCDF4 I don't know how many of
these are on cheyenne or capser)

**MatPlotLib (MPL)**

The free alternative to creating plots in MATLAB, MPL was created by
neurobiologist to plot EEG of his patients, it has perhaps the largest
open source python modules out there.

It is flexible beyond belief to fit a variety of application and can be used to
create publication-quality figures (though you may disagree coming from NCL) to
real time forecasting plots or overlays.

It has been extended to be used Geogrpahically/Meterographically with Basemap
and Cartopy.

**Basemap and Cartopy**

These two modules are built on top of MatPlotLib and allow the complicated
handling of projections and converting between projections.

Cartopy is a newer module, but is still in alpha and lacking in features and
documentation which makes it difficult to use, but it appears to be developing
rapidly.

Basemap, the original of the two modules, is much better documented and contains
more features and more examples and is much easier to use at the current
moment. I recommend using Basemap while Cartopy gains documentation and
features.

Examples within this tutorial use Basemap; however, once I see Cartopy
gaining more traction and better documentation (and I get more familar with it)
I will create examples using it.

**Python on Cheyenne and Casper**

To use Python with the modules above on Cheyenne and Casper. We'll need to
create a Python virtual environment that contains these modules. Thankfully,
CISL has created a command that will do that for us: `ncar_pylib`. Once you
have loaded a Python (version >=3.6.4) run `ncar_pylib` in your terminal window
and you'll have access to all the above modules.

**Python 2, Python 3**

Python 2 will soon not be maintained. All of these examples use Python 3. Use
Python 3.

# Quick Introduction to MatPlotLib

MatPlotLib can be a confusing module to use and understand at first, but with
practice and understanding of how its mechanisms work, plotting becomes much
easier.

Anatomy1
Anatomy2

Above, are two images that I wish I had when I started using MPL. They both
give an overview of the anatomy of a MPL Figure and its parts.

You'll see on the image on the left, those two main parts are the Figure, and
the Axes/Subplot. The Figure, is highest in the hierarchy of MPL, and it
contains 1 or more Axes/Subplots. An Axes/Subplot represent an individual plot
or graphic.

So, for instance, the plots above contain a single Axes/Subplot, but are both,
separately, their own figure. Often times, it is needed to create multiple
subplots in a single figure, which MPL allows, such as: Subplots

If you are wanting to create a plot that is not within this tutorial. I
recommend taking a look at the [MPL Example Gallery][MPL Example Gallery] and
the [MPL Tutorial Page][MPL Tutorial Page] for a load of examples and tutorials
on everything MPL.

# Module and other Helpful References

* Plotting
* [MatPlotLib](https://matplotlib.org/)
* Meteorological and Geographic Plotting
* [Basemap](https://matplotlib.org/basemap/)
* [Cartopy](https://scitools.org.uk/cartopy/docs/latest/)
* [Gold-Standard Basemap Tutorial](https://basemaptutorial.readthedocs.io/en/latest/)
* Python NetCDF Tools
* [Scipy Netcdf](https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.io.netcdf.netcdf_file.html)
* [Unidata's NetCDF4 Dataset](https://unidata.github.io/netcdf4-python/netCDF4/index.html)
* [xArray](http://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_netcdf.html)
* Numpy
* [Numpy Basics](https://docs.scipy.org/doc/numpy/user/index.html)
* [Numpy Reference](https://docs.scipy.org/doc/numpy/reference/)

Helpful References and Guides
* [Anatomy Of MatplotLib](https://github.com/matplotlib/AnatomyOfMatplotlib)
* [MPL Tutorial Page][MPL Tutorial Page]
* [MPL Example Gallery][MPL Example Gallery]
* [MPL Color Maps Reference](https://matplotlib.org/users/colormaps.html)
* [MPL Plot Style Reference](https://matplotlib.org/gallery/style_sheets/style_sheets_reference.html)
* [Figure Documentation](https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html)
* [Axes Documentation](https://matplotlib.org/api/axes_api.html)

# References & Credits
* [For examples and the Antomy of MatPlotLib Image used in and Introduction to MatPlotLib][1]
* [For help creating mpas_patches][2]
* [Anatomy of A Figure Image](https://matplotlib.org/gallery/showcase/anatomy.html)

[1]: https://github.com/matplotlib/AnatomyOfMatplotlib
[2]: https://github.com/lmadaus/mpas_python
[MPL Example Gallery]: https://matplotlib.org/gallery/index.html
[MPL Tutorial Page]: https://matplotlib.org/tutorials/index.html

# Todo:
1. Add more examples (espcially 2D examples)
* Histogram, Barplots, Scatter Plots etc.
* Add a Subplot example (possibly using grid spacing)
* Add an example longitude or latitude 'slize' of a variable or two.
2. Clean up the helpful reference and guide list into one list and not two
3. Add documentation to `mpas_patches.py`
4. Find a way to apply patches to Cartopy