Convert netcdf to image

Rimbaud picture Rimbaud · Jan 14, 2012 · Viewed 7.5k times · Source

I have a netcdf file that I would like to convert to an image (joed, png, gif) using a command line tool.

Is someone could please help me with the library name and possibly a link to how it is done.

Regards David

Answer

Rich Signell picture Rich Signell · Feb 8, 2013

Others have mentioned commercial solutions with ArcGIS, IDL and Matlab, but here's one way to do it using Python, using the netCDF4 module to read the netcdf file, and matplotlib to create the image. The netCDF4 module will read both NetCDF3, NetCDF4 files, and also read remote NetCDF (or other files) served via the OPeNDAP service. Below I read topography data using the OPeNDAP service, so you should be able to run the program without changes. The netCDF4 module can be a bit difficult to build, but it's included in the Python(x,y), Enthought Canopy and Continuum Anaconda distributions.

import matplotlib.pyplot as plt
import netCDF4

# open a local NetCDF file or remote OPeNDAP URL
url = 'http://www.ngdc.noaa.gov/thredds/dodsC/relief/ETOPO1/thredds/ETOPO1_Bed_g_gmt4.nc'
nc = netCDF4.Dataset(url)

# examine the variables
print nc.variables.keys()
print nc.variables['z']

# sample every 10th point of the 'z' variable
topo = nc.variables['z'][::10,::10]

# make image
plt.figure(figsize=(10,10))
plt.imshow(topo,origin='lower') 
plt.title(nc.title)
plt.savefig('image.png', bbox_inches=0)

which produces this image:enter image description here