User Tools

Site Tools


Back to WGV Group Logbook: Welcome to the WGV Group Logbook

Visualizing NetCDF Files with Python

Configuring YT for NetCDF Files

June 1-2, 2014: Donald Willcox

Visualizing NetCDF Files with Python

Once you've installed netcdf, you can access data in netcdf files from Python by installing numpy and ScientificPython. You should be able to get these by searching your package manager of choice or visiting their project sites:

I go into more detail on building and installing ScientificPython in the following section for adding it to yt if you're interested in using yt to plot your data. Otherwise you can use python to access the netcdf files by referencing the NetCDF/python documentation here:

If you aren't using yt but do want to use python and need ScientificPython, follow the procedure in the next section except the source command for activating yt.

To load the necessary modules in your python script, use the following:

import Scientific.IO.NetCDF

from numpy import *

from Scientific.IO.NetCDF import *

Configuring YT for NetCDF Files

yt is great for visualizing large multidimensional datasets from astrophysics codes such as FLASH, and it can be obtained from yt returned errors in interpreting the netcdf files output by VH-1 when I tried opening them using its load function, so here's how I added NetCDF support to my yt distribution.

First, make sure you've set the NETCDF_PREFIX environment variable as in the section on configuring NetCDF:

Setting up Parallel VH-1 with NetCDF

I installed the NetCDF libraries so my include and lib directories are as follows:

Include dir: /home/dwillcox/local/netcdf/include

Lib dir: /home/dwillcox/local/netcdf/lib

So I set the following environment variable to point to the netcdf directory:

export NETCDF_PREFIX=/home/dwillcox/local/netcdf

I have installed yt to the following location: /home/dwillcox/codes/local/yt-x86_64

To use NetCDF, yt needs numpy and ScientificPython installed, and it already comes with numpy. So the only thing to do is download ScientificPython:

Extract the tarball and cd into its directory. Activate the yt package by executing:

source /home/dwillcox/codes/local/yt-x86_64/bin/activate

Now execute the following:

python build

python install

And ScientificPython should be added to your yt distribution!

For a sample script using NetCDF with yt, see in my github repo:

june_1-2_2014_configuring_python_and_yt_to_work_with_netcdf_files.txt · Last modified: 2014/06/03 00:27 by willcox