njtbx-icon
Netcdf-Java Toolbox
previous Environment Setup
User Guide Next

 
 
Quick Tutorial

Often you just want to extract some data and attributes from a local NetCDF file or an OpenDAP URL. With the NJ Toolbox there are several ways to do this.

One way is to call NJ high level convenience functions as a one line command, specifying the data using a syntax similar to the NetCDF Fortran Interface: [data]=nj_varget(file,variable,corner,edge,stride); using 1-based indexing and using "Inf" to specify the maximum value. Thus

 ncRef = 'http://coast-enviro.er.usgs.gov/models/adria/bora_feb_001.nc'
 data = nj_varget(ncRef,'temp',[inf 19 1 1],[1 2 inf inf],[1 1 2 2]);
 att = nj_attget(ncRef,'temp');
returns the last time step of temperature from a ROMS ocean model result, but from only the 19th and 20th vertical level, and also subsamples the entire lon,lat field by a factor of two. The second command returns the attributes of "temp" as a structure.

You could also accomplish the same thing in the NJ Toolbox by using matlab subcripted referencing syntax (i.e. use of '{}', '(), & '.') familiar to users of the NetCDF Toolbox by Chuck Denham:
 ncRef = 'http://coast-enviro.er.usgs.gov/models/test/bora_feb.nc'
 nc = mDataset(ncRef);
 data = nc{'temp'}(end,19:20,1:2:end,1:2:end).data;  % nc{'temp'}(end,19:20,1:2:end,1:2:end)  for non-gridded var
 close(nc)
If the dataset you are trying to access has CF Compliant metadata, you can use the NJ Toolbox to return the geospatial coordinates (e.g. lon,lat, z and time) along with the data you requested by addding a ".grid" onto the end of the data request. So in the above example, we could also do:
  ncRef = 'http://coast-enviro.er.usgs.gov/models/test/bora_feb.nc'
  nc = mDataset(ncRef);
  data = nc{'temp'}(end,19:20,1:2:end,1:2:end).data;
  grid = nc{'temp'}(end,19:20,1:2:end,1:2:end).grid;
  units = nc{'temp'}.units;    %return the "units" attribute of the variable "temp"
  close(nc);
to also return an object called "grid" containing the "time", "z", "lat" and "lon" values associated with data selected. This removes the need to explicitly determine which coordinates contain latitude and longitude, how to compute vertical positions from stretched coordinate models, and parse units of time. As an example, having retrieved the grid object, you could plot just the surface layer temp and title with the time by doing:
  pcolor(grid.lon,grid.lat,double(squeeze(data(end,:,:))));shading flat
  title(['Surface Layer Temp (' units ') at ' datestr(grid.time(end))]);
You could also return the data and grid at a particular time step and level with the routine "nj_tslice". We added these functions just as a convenient shortcut for some common operations. We could return the 3D data and grid for the last time step by doing:
  ncRef = 'http://coast-enviro.er.usgs.gov/models/test/bora_feb.nc'
  [data,grid]=nj_tslice(ncRef,'temp',inf);
For in-depth help on njTBX API and NJ function usage continue to user guide section.

previous Environment Setup
User Guide Next
© 2006-2009   Mississippi State University   • License Terms   • Credits