Reading NetCDF in C Software Documentation
Reading NetCDF in C Software Documentation
Usage
cdf2c [-v] [-o outputFile] inputFilePath
This procedure creates a c routine to read in the data from a NetCDF
file. The default mode, non-verbose output, outputs only the varaible
data to the c routine.
The variables and global attributes have identical names to the
variable and global attribute names in the NetCDF file. The
attributes associated with a given variable have names preceded by
the associated variable name.
[-v]
If this flag is set then the cdf2c program creates verbose output.
All variable and global attributes in the netCDF file will be included
in the resultant c routine.
[-o outputFile]
This flag allows you to enter an optional output filename for the
routine produced by the program. The default is for the output to
be written to stdout.
inputFilePath
The full path to the netCDF file for which you wish to create a
c routine to read the data.
Prerequisites
This program CANNOT be compiled without the netCDF library. The source
code for the netCDF library can be obtained via anonymous ftp to
ftp.unidata.ucar.edu.
The necessary files are located in the /pub/netcdf directory.
Additionally, the Makefile provided uses the gcc compiler to compile the
program. You may substitute for your local compiler, but if you do, you
will need to modify the Makefile to use the flags appropriate to your
particular compiler. You may also obtain the gcc compiler, if you do not
have it, via anonymous ftp to
prep.ai.mit.edu. The gcc
compiler is located in the /pub/gnu directory along with a suite
of other free goodies.
Please read the Readme included with this release to ensure that the code
compiles properly.
Example
Assuming you have met all of the prerequisites, and that you have followed
the instructions in the Readme file.
your_machine 1 > make
your_machine 2 > cdf2c -v -o netCDF_reader.c /path_to_data_file/data_file.cdf
The resulting c routine will be placed in the file netCDF_reader.c
netCDF_reader.c
//-------------------------------------------------------------------------
//
// This code was generated by cdf2c to read the netCDF data from the
// file.cdf file into memory.
//
// NOTE: The code creates global variables, but you may move the variable
// definitions inside the read_netCDF_file procedure if you wish.
// The code REQUIRES the netCDF C libraries to run, and assumes that
// netcdf.h is in the common include path. DON'T FORGET to link with
// -lnetcdf -lsun when you compile the code.
//
// This file contains the following routines:
//
// read_netCDF_file - reads the data from the given netCDF file into memory
//
// History:
// Date long_name Action
// --------- ------------ -----------------------------------------------
// ?? ??? ?? CIDS Work in Progress.
//
//-------------------------------------------------------------------------
#include
// Variable definitions. The comments contain the dimension names.
double llt_lon[20]; // numofsamples
char llt_lon_long_name[10];
char llt_lon_units[8];
double llt_lat[20]; // numofsamples
char llt_lat_long_name[9];
char llt_lat_units[8];
double llt_time[20]; // numofsamples
char llt_time_long_name[5];
char llt_time_units[8];
double variable1[20]; // numofsamples
char variable1_long_name[19];
char variable1_units[27];
long i_iets[5][2]; // nof, two
char i_iets_long_name[27];
char i_iets_units[8];
char srcflnms[5][300]; // nof, noc
char srcflnms_long_name[18];
char srcflnms_units[5];
char platform[11];
char instrument[13];
char original_data_source[39];
char original_data_date[24];
char original_data_file[60];
char conversion_software[43];
char conversion_technician[39];
char conversion_date[24];
char source_file[61];
void
read_netCDF_file ()
{
//# func_description
// This routine reads the data from file.cdf into memory.
int fd;
long start[MAX_NC_VARS];
long end[MAX_NC_VARS];
// Open the netCDF file.
fd = ncopen("file.cdf", NC_NOWRITE);
// Initialize start so all elements are zero.
bzero(start, MAX_NC_VARS * sizeof(long));
// Read in the variables and their attributes.
// Read: llt_lon.
end[0] = 20;
ncvarget(fd, 0, start, end, llt_lon);
// Read: llt_lon_long_name.
ncattget(fd, 0, "long_name", llt_lon_long_name);
// Read: llt_lon_units.
ncattget(fd, 0, "units", llt_lon_units);
// Read: llt_lat.
end[0] = 20;
ncvarget(fd, 1, start, end, llt_lat);
// Read: llt_lat_long_name.
ncattget(fd, 1, "long_name", llt_lat_long_name);
// Read: llt_lat_units.
ncattget(fd, 1, "units", llt_lat_units);
// Read: llt_time.
end[0] = 20;
ncvarget(fd, 2, start, end, llt_time);
// Read: llt_time_long_name.
ncattget(fd, 2, "long_name", llt_time_long_name);
// Read: llt_time_units.
ncattget(fd, 2, "units", llt_time_units);
// Read: variable1.
end[0] = 20;
ncvarget(fd, 3, start, end, variable1);
// Read: variable1_long_name.
ncattget(fd, 3, "long_name", variable1_long_name);
// Read: variable1_units.
ncattget(fd, 3, "units", variable1_units);
// Read: i_iets.
end[0] = 5; end[1] = 2;
ncvarget(fd, 6, start, end, i_iets);
// Read: i_iets_long_name.
ncattget(fd, 6, "long_name", i_iets_long_name);
// Read: i_iets_units.
ncattget(fd, 6, "units", i_iets_units);
// Read: srcflnms.
end[0] = 5; end[1] = 300;
ncvarget(fd, 7, start, end, srcflnms);
// Read: srcflnms_long_name.
ncattget(fd, 7, "long_name", srcflnms_long_name);
// Read: srcflnms_units.
ncattget(fd, 7, "units", srcflnms_units);
// Read in the Global Attributes.
// Read: platform.
ncattget(fd, 0, "platform", platform);
// Read: instrument.
ncattget(fd, 1, "instrument", instrument);
// Read: original_data_source.
ncattget(fd, 2, "original_data_source", original_data_source);
// Read: original_data_date.
ncattget(fd, 3, "original_data_date", original_data_date);
// Read: original_data_file.
ncattget(fd, 4, "original_data_file", original_data_file);
// Read: conversion_software.
ncattget(fd, 5, "conversion_software", conversion_software);
// Read: conversion_technician.
ncattget(fd, 6, "conversion_technician", conversion_technician);
// Read: conversion_date.
ncattget(fd, 7, "conversion_date", conversion_date);
// Read: source-file.
ncattget(fd, 8, "source-file", source_file);
// Close the netCDF file.
ncclose(fd);
}
You may now add a main and any other functions you wish to manipulate the
data to the routine in netCDF_reader.c.
Send your comments to the CIDS Aministrator.
Back to CIDS Page.