Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert netcdf to image

Tags:

python

netcdf

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

like image 695
Rimbaud Avatar asked Jan 14 '12 19:01

Rimbaud


People also ask

Is NetCDF a raster or vector?

See Creating a mosaic dataset using the Vector Field template to create a multidimensional mosaic dataset. ArcGIS currently supports three multidimensional raster types—GRIB, HDF, and NetCDF—which correspond to multidimensional raster data stored in those formats.

How do I open a NC file in Python?

To install with anaconda (conda) simply type conda install netCDF4 . Alternatively, you can install with pip . To be sure your netCDF4 module is properly installed start an interactive session in the terminal (type python and press 'Enter'). Then import netCDF4 as nc .


2 Answers

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

like image 179
Rich Signell Avatar answered Sep 19 '22 00:09

Rich Signell


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

like image 45
EnE_ Avatar answered Sep 18 '22 00:09

EnE_