The first example program, written in C, creates a CDF with 2-dimensional rVariables using only CDF library function calls. The CDF created will contain the data and metadata values used in the example presented earlier in this chapter (minus some of the vAttributes/rEntries). An input file, example.dat, whose format is similar to that of Table 1.1 will be read and its data values written into the CDF.
/******************************************************************************
*
* NSSDC/CDF Create an example CDF (without using a skeleton table).
*
* Version 1.0, 5-Jan-94, CDF, Inc.
*
* Modification history:
*
* V1.0 5-Jan-94, Joe Programmer Original version.
*
******************************************************************************/
/******************************************************************************
*
* Note(s):
*
* This program would have to be modified to run on a DEC Alpha because the
* C language `long' data type is 8 bytes rather than 4 (the CDF data type of
* CDF_INT4 is always 4 bytes).
*
******************************************************************************/
/******************************************************************************
* Necessary include files.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "cdf.h"
/******************************************************************************
* Status handler.
******************************************************************************/
void StatusHandler (status)
CDFstatus status;
{
char message[CDF_ERRTEXT_LEN+1];
if (status < CDF_WARN) {
printf ("An error has occurred, halting...\n");
CDFerror (status, message);
printf ("%s\n", message);
exit (status);
}
else
if (status < CDF_OK) {
printf ("Warning, function may not have completed as expected...\n");
CDFerror (status, message);
printf ("%s\n", message);
}
else
if (status > CDF_OK) {
printf ("Function completed successfully, but be advised that...\n");
CDFerror (status, message);
printf ("%s\n", message);
}
return;
}
/******************************************************************************
* MAIN.
******************************************************************************/
main () {
CDFid id; /* CDF identifier. */
CDFstatus status; /* CDF completion status. */
FILE *fp; /* File pointer - used to read input data file. */
long numDims = 2; /* Number of dimensions, rVariables. */
static long dimSizes[2] = {2,2}; /* Dimension sizes, rVariables. */
long dimVarys[2]; /* Dimension variances. */
long indices[2]; /* Dimension indices. */
long recNum; /* Record number. */
long attrNum; /* Attribute number. */
long TimeVarNum; /* 'Time' rVariable number. */
long LonVarNum; /* 'Longitude' rVariable number. */
long LatVarNum; /* 'Latitude' rVariable number. */
long TmpVarNum; /* 'Temperature' rVariable number. */
long Time; /* 'Time' rVariable value. */
float Lat; /* 'Latitude' rVariable value. */
float Lon; /* 'Longitude' rVariable value. */
float Tmp; /* 'Temperature' rVariable value. */
long TimeValidMin = 0; /* 'Time' valid minimum (0000). */
long TimeValidMax = 2359; /* 'Time' valid maximum (2359). */
float LonValidMin = -180.0; /* 'Longitude' valid minimum. */
float LonValidMax = 180.0; /* 'Longitude' valid maximum. */
float LatValidMin = -90.0; /* 'Latitude' valid minimum. */
float LatValidMax = 90.0; /* 'Latitude' valid maximum. */
float TmpValidMin = -40.0; /* 'Temperature' valid minimum. */
float TmpValidMax = 50.0; /* 'Temperature' valid maximum. */
/****************************************************************************
* Create the CDF.
****************************************************************************/
status = CDFcreate ("example1", numDims, dimSizes, NETWORK_ENCODING,
ROW_MAJOR, &id);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Create rVariables.
****************************************************************************/
dimVarys[0] = NOVARY;
dimVarys[1] = NOVARY;
status = CDFvarCreate (id, "Time", CDF_INT4, 1L, VARY, dimVarys,
&TimeVarNum);
if (status != CDF_OK) StatusHandler (status);
dimVarys[0] = VARY;
dimVarys[1] = NOVARY;
status = CDFvarCreate (id, "Longitude", CDF_REAL4, 1L, NOVARY, dimVarys,
&LonVarNum);
if (status != CDF_OK) StatusHandler (status);
dimVarys[0] = NOVARY;
dimVarys[1] = VARY;
status = CDFvarCreate (id, "Latitude", CDF_REAL4, 1L, NOVARY, dimVarys,
&LatVarNum);
if (status != CDF_OK) StatusHandler (status);
dimVarys[0] = VARY;
dimVarys[1] = VARY;
status = CDFvarCreate (id, "Temperature", CDF_REAL4, 1L, VARY, dimVarys,
&TmpVarNum);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Create attributes.
****************************************************************************/
status = CDFattrCreate (id, "TITLE", GLOBAL_SCOPE, &attrNum);
if (status != CDF_OK) StatusHandler (status);
status = CDFattrCreate (id, "VALIDMIN", VARIABLE_SCOPE, &attrNum);
if (status != CDF_OK) StatusHandler (status);
status = CDFattrCreate (id, "VALIDMAX", VARIABLE_SCOPE, &attrNum);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Write TITLE gAttribute gEntry.
****************************************************************************/
status = CDFattrPut (id, CDFattrNum(id,"TITLE"), 0L, CDF_CHAR, 50L,
"An example CDF (1). ");
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Write vAttribute rEntries for 'Time' rVariable.
****************************************************************************/
status = CDFattrPut (id, CDFattrNum(id,"VALIDMIN"),
CDFvarNum(id,"Time"), CDF_INT4, 1L, &TimeValidMin);
if (status != CDF_OK) StatusHandler (status);
status = CDFattrPut (id, CDFattrNum(id,"VALIDMAX"),
CDFvarNum(id,"Time"), CDF_INT4, 1L, &TimeValidMax);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Write vAttribute rEntries for 'Longitude' rVariable.
****************************************************************************/
status = CDFattrPut (id, CDFattrNum(id,"VALIDMIN"),
CDFvarNum(id,"Longitude"), CDF_REAL4, 1L, &LonValidMin);
if (status != CDF_OK) StatusHandler (status);
status = CDFattrPut (id, CDFattrNum(id,"VALIDMAX"),
CDFvarNum(id,"Longitude"), CDF_REAL4, 1L, &LonValidMax);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Write vAttribute rEntries for 'Latitude' rVariable.
****************************************************************************/
status = CDFattrPut (id, CDFattrNum(id,"VALIDMIN"),
CDFvarNum(id,"Latitude"), CDF_REAL4, 1L, &LatValidMin);
if (status != CDF_OK) StatusHandler (status);
status = CDFattrPut (id, CDFattrNum(id,"VALIDMAX"),
CDFvarNum(id,"Latitude"), CDF_REAL4, 1L, &LatValidMax);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Write vAttribute rEntries for 'Temperature' rVariable.
****************************************************************************/
status = CDFattrPut (id, CDFattrNum(id,"VALIDMIN"),
CDFvarNum(id,"Temperature"), CDF_REAL4, 1L,
&TmpValidMin);
if (status != CDF_OK) StatusHandler (status);
status = CDFattrPut (id, CDFattrNum(id,"VALIDMAX"),
CDFvarNum(id,"Temperature"), CDF_REAL4, 1L,
&TmpValidMax);
if (status != CDF_OK) StatusHandler (status);
/****************************************************************************
* Read input values for rVariables and write them to the CDF. Not
* every value must be written to the CDF - many of the values are redundant.
* The 'Time' value only has to be written once per CDF record (every 4 input
* records). The 'Longitude' and 'Latitude' values are only written to the
* first CDF record (and only at the appropriate indices). Each 'Temperature'
* value read is written to the CDF.
****************************************************************************/
fp = fopen ("example.dat", "r");
if (fp == NULL) {
printf ("Error opening input file.\n");
exit (-1);
}
for (recNum = 0; recNum < 24; recNum++) {
for (indices[0] = 0; indices[0] < 2; indices[0]++) {
for (indices[1] = 0; indices[1] < 2; indices[1]++) {
fscanf (fp, "%d %f %f %f", &Time, &Lon, &Lat, &Tmp);
if (indices[0] == 0 && indices[1] == 0) {
status = CDFvarPut (id, TimeVarNum, recNum, indices, &Time);
if (status != CDF_OK) StatusHandler (status);
}
if (recNum == 0 && indices[1] == 0) {
status = CDFvarPut (id, LonVarNum, recNum, indices, &Lon);
if (status != CDF_OK) StatusHandler (status);
}
if (recNum == 0 && indices[0] == 0) {
status = CDFvarPut (id, LatVarNum, recNum, indices, &Lat);
if (status != CDF_OK) StatusHandler (status);
}
status = CDFvarPut (id, TmpVarNum, recNum, indices, &Tmp);
if (status != CDF_OK) StatusHandler (status);
}
}
}
fclose (fp);
/****************************************************************************
* Close CDF.
****************************************************************************/
status = CDFclose (id);
if (status != CDF_OK) StatusHandler (status);
return;
}