Rimbaud
Rimbaud

Reputation: 61

Convert netcdf to image

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

Upvotes: 6

Views: 12207

Answers (7)

ClimateUnboxed
ClimateUnboxed

Reputation: 8087

If you want a commandline facility to enable to produce a simple plot without needing to do any coding there is

1. ncview

ncview file.nc 

which can produce a ps plot with "print to file" but it is not very helpful as the colorbar legend is not included

2. Panoply

A second point and click method is to use

panoply 

which is available here: https://www.giss.nasa.gov/tools/panoply/

3. CDO

The third method is to use CDO from the command line, (but make sure you have compiled it with MAGICS++ support included).

For example, you can make a simple PNG map of data in a netcdf file with the following command:

cdo shaded,device="png" in.nc plot

which will provide a file called "plot_variablename.png"

See the documentation here: https://code.mpimet.mpg.de/projects/cdo/wiki/Tutorial#Plotting

Upvotes: 0

Robert Wilson
Robert Wilson

Reputation: 3407

This can be done using my new package ncplot (available through pypi or conda-forge). It works as either a command line tool or in Python, and will generate interactive plots from the NetCDF files.

On the command line just do the following:

ncplot infile.nc

In Python (preferably in a Jupyter notebook). Just do the following:

from ncplot import ncplot
ncplot("infile.nc")

Upvotes: 2

Rich Signell
Rich Signell

Reputation: 16375

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

Upvotes: 16

mdsumner
mdsumner

Reputation: 29515

GDAL can do this and you can control the output colour palettes in a variety of ways, and the rgdal package provides an R interface to some of this.

R has support to read NetCDF files and write to image files via a number of contributed packages, namely ncdf, RNetCDF and ncdf4.

Upvotes: 0

Raphael Roth
Raphael Roth

Reputation: 27373

this may help:

http://gmt.soest.hawaii.edu/gmt/html/man/grdimage.html

here some examples: web.ics.purdue.edu/~ecalais/teaching/gmt/GMT_6.pdf

Upvotes: 0

GPSmaster
GPSmaster

Reputation: 914

IDV is a good visualization tool for NetCDF, but, as far as I know, there is no command line interface.

I would recommend Matlab. It has read and write functions for NetCDF as well as an extensive plotting library...probably one of the best. You can then compile the matlab code and run it from the command line.

Upvotes: 0

EnE_
EnE_

Reputation: 541

Here is the official list of software for manipulating NetCDF.

http://www.unidata.ucar.edu/software/netcdf/software.html

If there is anything it is probably there. But I have a couple of comments on the subject if you are interested.

What platform are you using? Linux, Windows etc? Whatever the answer I think the answer is you will be struggling to find a command line tool with out creating it yourself.

It is probably relatively easy to create something using Java or Python and some GDAL libraries or the like. I have made something similar using ArcGIS if you have this but it is not command line because that is quite difficult to achieve.

Part of the problem you will face is that in using a command line you will need to have additional information as to how it is exported setup before hand but this information does not lend itself to a non-GUI environment.

Questions such as is it going to be grey scale or colour. If colour which colours because these will need to be defined. Say we use blue to red colour ramp then is red a high value or a low value. How is the colour going to be assigned to the values. Will it be gradual or will it be stepped e.g. values 0 - 10 correspond to a single colour then 10 - 20 to another colour.

It's not command line but 'NcView' may work for you.

http://meteora.ucsd.edu/~pierce/ncview_home_page.html

Upvotes: 3

Related Questions