MATLAB NETCDF Quickstart
This document explains how to open and read a NETCDF file from MATLAB. Please note that MATLAB r2008 or later comes pre-built with NETCDF support.
Please check http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-6011seg02.... or just google "netcdf matlab site:mathworks.com"
First, get a NETCDF file
[anand@leo ~]$ ls -lh atm.nc
-rw-r--r-- 1 anand anand 63M Feb 24 11:08 atm.nc
[anand@leo ~]$
Use a command on the linux prompt and check the structure and variables in the file:
[anand@leo ~]$ ncdump -h atm.nc
netcdf atm {
dimensions:
lon = 198 ;
lat = 93 ;
level = 18 ;
time = UNLIMITED ; // (4 currently)
variables:
float lon(lon) ;
lon:long_name = "Longitude" ;
lon:units = "degrees_east" ;
lon:actual_range = 88.5609f, 141.7089f ;
float lat(lat) ;
lat:long_name = "Latitude" ;
lat:units = "degrees_north" ;
lat:actual_range = -12.18246f, 12.44604f ;
float level(level) ;
level:long_name = "Height_Index" ;
level:units = "level" ;
level:actual_range = 1050.f, 0.f ;
double time(time) ;
time:long_name = "Time" ;
time:units = "hours since 1900-1-1 00:00:0.0" ;
time:actual_range = 885696., 885714. ;
float U(time, level, lat, lon) ;
U:long_name = "Zonal Wind" ;
U:units = "m/s" ;
U:missing_value = -1.e+30f ;
float V(time, level, lat, lon) ;
V:long_name = "Meridional Wind" ;
V:units = "m/s" ;
V:missing_value = -1.e+30f ;
float OMEGA(time, level, lat, lon) ;
OMEGA:long_name = "Omega" ;
OMEGA:units = "hPa" ;
OMEGA:missing_value = -1.e+30f ;
float TK(time, level, lat, lon) ;
TK:long_name = "Temperature" ;
TK:units = "K" ;
TK:missing_value = -1.e+30f ;
float QD(time, level, lat, lon) ;
QD:long_name = "Mixing Ratio" ;
QD:units = "kg/kg" ;
QD:missing_value = -1.e+30f ;
float QC(time, level, lat, lon) ;
QC:long_name = "Cloud Mixing Ratio" ;
QC:units = "kg/kg" ;
QC:missing_value = -1.e+30f ;
float RH(time, level, lat, lon) ;
RH:long_name = "Relative Humidity" ;
RH:units = "fraction" ;
RH:missing_value = -1.e+30f ;
float HGT(time, level, lat, lon) ;
HGT:long_name = "Geopotential Height" ;
HGT:units = "m" ;
HGT:missing_value = -1.e+30f ;
float TH(time, level, lat, lon) ;
TH:long_name = "Potential Temperatur" ;
TH:units = "K" ;
TH:missing_value = -1.e+30f ;
float TD(time, level, lat, lon) ;
TD:long_name = "Dew Point Temperatur" ;
TD:units = "K" ;
TD:missing_value = -1.e+30f ;
float VOR(time, level, lat, lon) ;
VOR:long_name = "Vorticity (Horizonta" ;
VOR:units = "m/s" ;
VOR:missing_value = -1.e+30f ;
float DIV(time, level, lat, lon) ;
DIV:long_name = ;
DIV:units = "m/s" ;
DIV:missing_value = -1.e+30f ;
float PS(time, lat, lon) ;
PS:long_name = "Surface Pressure" ;
PS:units = "hPa" ;
PS:missing_value = -1.e+30f ;
float RT(time, lat, lon) ;
RT:long_name = "Total Precip" ;
RT:units = "mm/day" ;
RT:missing_value = -1.e+30f ;
float TGRND(time, lat, lon) ;
TGRND:long_name = "Ground Temperature" ;
TGRND:units = "K" ;
TGRND:missing_value = -1.e+30f ;
float SMT(time, lat, lon) ;
SMT:long_name = "Total Soil Water" ;
SMT:units = "mm" ;
SMT:missing_value = -1.e+30f ;
float RB(time, lat, lon) ;
RB:long_name = "Base Flow" ;
RB:units = "mm/day" ;
RB:missing_value = -1.e+30f ;
float SLP(time, lat, lon) ;
SLP:long_name = "Sea Level Temperatur" ;
SLP:units = "hPa" ;
SLP:missing_value = -1.e+30f ;
// global attributes:
:domxmin = 88.5609f ;
:domxmax = 141.7089f ;
:domymin = -12.18246f ;
:domymax = 12.44604f ;
:domzmin = 1050.f ;
:domzmax = 0.f ;
}
Start MATLAB:
[anand@leo ~]$ matlab -nodisplay -nojvm
< M A T L A B (R) >
Copyright 1984-2009 The MathWorks, Inc.
Version 7.8.0.347 (R2009a) 64-bit (glnxa64)
February 12, 2009
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>>
Open the NETCDF File:
>> ncid=netcdf.open('atm.nc','NOWRITE')
ncid =
10
>>
Try some queries:
>> [ndims,nvars,ngatts,unlimdimid] = netcdf.inq(ncid)
ndims =
4
nvars =
22
ngatts =
6
unlimdimid =
3
>>
Get the name of the first variable.
>> [varname, xtype, varDimIDs, varAtts] = netcdf.inqVar(ncid,0);
>> varname
varname =
lon
>> xtype
xtype =
5
>> varDimIDs
varDimIDs =
0
>> varAtts
varAtts =
3
>>
Get variable ID of the first variable, given its name.
>> varid = netcdf.inqVarID(ncid,varname);
>> varid
varid =
0
Get the value of the first variable, given its ID.
>> data = netcdf.getVar(ncid,varid)
data =
88.5609
88.8307
89.1005
89.3703
89.6400
89.9098
90.1796
Also try:
>> varid = netcdf.inqVarID(ncid,'lat');
>> data = netcdf.getVar(ncid,varid);
>> data
data =
-12.1825