NCL and Python Transition Guide Examples

The main purpose of this page is to help ease the transition from NCL to Python by providing both NCL and Python versions of scripts that read/write ASCII and NetCDF files and/or produce graphics.

The Python scripts use PyNGL to create the graphics and a mix of xarray and PyNIO to read the data.

These NCL and Python scripts are companion examples to the excellent NCL to Python Transition Guide, written by Karin Meier-Fleischer of DKRZ (Deutsches Klimarechenzentrum).

In order to run these examples, we recommend that you use Python version 3.x. The easiest way to install Python and the required packages is to first install conda, and then use conda to install these packages to conda environment:

conda create -n ncl_to_python -c conda-forge xarray netcdf4 scipy pyngl pynio ncl

To activate the "ncl_to_python" conda environment, type:

source activate ncl_to_python

Once you are in the "ncl_to_python" conda environment, you can run the examples with:

ncl TRANS_panel.ncl
python TRANS_panel.py

The data files for these examples can be downloaded from:

http://www.ncl.ucar.edu/Document/Manuals/NCL_User_Guide/Data/

Note that the NCL and Python images may not be identical. This is mostly due to different defaults being used by each package for things like font sizes and labelbar widths and heights.

DKRZ has an extensive suite of graphical examples using NCL, PyNGL, and matplotlib.

basics.ncl / basics.py :

Compares basic language features of NCL and Python.

This example does not produce any graphics.

TRANS_read_netCDF.ncl / TRANS_read_netCDF.py :

Shows how to open a netCDF file, read a variable, and do some basic queries of dimensions, variable names, and variable types.

This example does not produce any graphics.

TRANS_read_multiple_netCDF.ncl / TRANS_read_multiple_netCDF.py :

Shows how to read multiple netCDF files.

This example does not produce any graphics.

TRANS_read_ASCII.ncl / TRANS_read_ASCII.py :

Shows how to read a simple ASCII file.

This example does not produce any graphics.

TRANS_read_ASCII_way2.ncl / TRANS_read_ASCII_way2.py :

Shows how to read an ASCII file with delimiters.

This example does not produce any graphics.

TRANS_read_ASCII_lat_lon_value.ncl / TRANS_read_ASCII_lat_lon_value.py :

Shows how to read a more complicated ASCII file and parse out the different columns of data.

This example does not produce any graphics.

TRANS_read_ASCII_lat_lon_value_way2.ncl / TRANS_read_ASCII_lat_lon_value_way2.py :

Shows how to read an ASCII file and create a contour fill plot on a map.

TRANS_write_netCDF_short.ncl / TRANS_write_netCDF_short.py :

Shows how to write a netCDF file using the easy, but inefficient, method.

This example does not produce any graphics.

TRANS_write_netCDF.ncl / TRANS_write_netCDF.py :

Shows how to write a netCDF file using the more complicated, but efficient method.

This example does not produce any graphics.

TRANS_write_ASCII.ncl / TRANS_write_ASCII.py :

Shows how to write an ASCII file.

This example does not produce any graphics.

TRANS_maps.ncl / TRANS_maps.py :

Shows how to create different map projections.

TRANS_xy_0.ncl / TRANS_xy_0.py :

Shows how to create a single-curve XY plot. To make the line thicker, set xyLineThicknessF to a value larger than 1.0.

TRANS_xy_1.ncl / TRANS_xy_1.py :

Shows how to create a multiple-curve XY plot with a legend.

TRANS_bar_chart.ncl / TRANS_bar_chart.py :

Shows how to create a bar plot, which is simply XY plot that uses bars instead of curves.

TRANS_contour_lines.ncl / TRANS_contour_lines.py :

Shows how to create a simple contour line plot.

TRANS_contour_fill.ncl / TRANS_contour_fill.py :

Shows how to create a simple contour fill plot.

TRANS_contour_lines_on_map.ncl / TRANS_contour_lines_on_map.py :

Shows how to create a contour line plot on a map.

TRANS_contour_fill_on_map.ncl / TRANS_contour_fill_on_map.py :

Shows how to create a contour fill plot on a map.

TRANS_vectors.ncl / TRANS_vectors.py :

Shows how to create a vector plot.

TRANS_streamline.ncl / TRANS_streamline.py :

Shows how to create a streamline plot, which is very similar to vector plots.

TRANS_slice.ncl / TRANS_slice.py :

Shows how to create a slice plot at latitude=40.

TRANS_scatter.ncl / TRANS_scatter.py :

Shows how to create a scatter plot, which is an XY plot drawn with markers instead of lines.

TRANS_overlay.ncl / TRANS_overlay.py :

Shows how to overlay a contour fill plot and a contour line plot over a map.

TRANS_panel.ncl / TRANS_panel.py :

Shows how to create mutiple plots on one page (called a "panel" plot).

TRANS_annotations_0.ncl / TRANS_annotations_0.py :

Shows how to change the labels on the X and Y axes.

TRANS_polyline_polygon_polymarker.ncl / TRANS_polyline_polygon_polymarker.py :

Shows how to add polylines, polygons and polymarkers to a map.

TRANS_masking.ncl / TRANS_masking.py :

Shows how to mask data over particular countries using map resources.

TRANS_shapefile.ncl / TRANS_shapefile.py :

Shows how to draw shapefile outlines on a map.