Commit 565671ec authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Add examples

parent 9d32a6f2
......@@ -56,8 +56,12 @@ doc/tex/usage.tex -text
examples/Makefile.am -text
examples/Makefile.in -text
examples/cdi_copy.c -text
examples/cdi_copy_f.f -text
examples/cdi_read.c -text
examples/cdi_read_f.f -text
examples/cdi_write.c -text
examples/cdi_write_f.f -text
examples/compf -text
prog/Makefile.am -text
prog/Makefile.in -text
prog/cdi.c -text
......
......@@ -2,21 +2,20 @@
\chapter{\label{appendixa}Examples}
\section{Write variables to a dataset}
\section{Write a dataset}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
#include "cdi.h"
#define NLON 12 /* Number of longitudes */
#define NLAT 6 /* Number of latitudes */
#define NLEV 5 /* Number of levels */
#define NTIME 3 /* Number of time steps */
#define NLON 12 /* Number of longitudes */
#define NLAT 6 /* Number of latitudes */
#define NLEV 5 /* Number of levels */
#define NTIME 3 /* Number of time steps */
int main(void)
{
int gridID, zaxisID1, zaxisID2, taxisID;
int vlistID, varID1, varID2, streamID;
int tsID;
int vlistID, varID1, varID2, streamID, tsID;
int i, nmiss = 0;
double lons[NLON] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
double lats[NLAT] = {-75, -45, -15, 15, 45, 75};
......@@ -24,14 +23,15 @@ int main(void)
double var1[NLON*NLAT];
double var2[NLON*NLAT*NLEV];
/* Create a LONLAT grid */
/* Create a regular lon/lat grid */
gridID = gridCreate(GRID_LONLAT, NLON*NLAT);
gridDefXsize(gridID, NLON);
gridDefYsize(gridID, NLAT);
gridDefXvals(gridID, lons);
gridDefYvals(gridID, lats);
/* Create a surface Z-axis */
/* Create a surface level Z-axis */
zaxisID1 = zaxisCreate(ZAXIS_SURFACE, 1);
/* Create a pressure level Z-axis */
......@@ -42,10 +42,10 @@ int main(void)
vlistID = vlistCreate();
/* Define the variables */
varID1 = vlistDefVar(vlistID, gridID, zaxisID1, TIME_VARIABLE);
varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE);
varID1 = vlistDefVar(vlistID, gridID, zaxisID1, TIME_VARIABLE);
varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE);
/* Define variable name */
/* Define the variable names */
vlistDefVarName(vlistID, varID1, "varname1");
vlistDefVarName(vlistID, varID2, "varname2");
......@@ -55,7 +55,7 @@ int main(void)
/* Assign the Time axis to the variable list */
vlistDefTaxis(vlistID, taxisID);
/* Create a dataset */
/* Create a dataset in netCDF format */
streamID = streamOpenWrite("example.nc", FILETYPE_NC);
/* Assign the variable list to the dataset */
......@@ -71,18 +71,19 @@ int main(void)
/* Define the time step */
streamDefTimestep(streamID, tsID);
/* Write var1 */
for ( i = 0; i < NLON*NLAT; i++ ) var1[i] = 1;
streamWriteVar(streamID, varID1, var1, nmiss);
/* Write var2 */
/* Init var1 and var2 */
for ( i = 0; i < NLON*NLAT; i++ ) var1[i] = 1;
for ( i = 0; i < NLON*NLAT*NLEV; i++ ) var2[i] = 2;
/* Write var1 and var2 */
streamWriteVar(streamID, varID1, var1, nmiss);
streamWriteVar(streamID, varID2, var2, nmiss);
}
/* Close the output stream */
streamClose(streamID);
/* Destroy the created objects */
/* Destroy the objects */
vlistDestroy(vlistID);
taxisDestroy(taxisID);
zaxisDestroy(zaxisID1);
......@@ -93,6 +94,7 @@ int main(void)
}
\end{lstlisting}
\subsection{Result}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
netcdf example {
......@@ -117,10 +119,6 @@ variables:
time:units = "day as %Y%m%d.%f" ;
float varname1(time, lat, lon) ;
float varname2(time, lev, lat, lon) ;
// global attributes:
:CDI = "Climate Data Interface version 1.0.0" ;
:Conventions = "CF-1.0" ;
data:
lon = 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 ;
......@@ -137,12 +135,121 @@ data:
\section{Read a dataset}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
\end{lstlisting}
#include "cdi.h"
#define NLON 12 /* Number of longitudes */
#define NLAT 6 /* Number of latitudes */
#define NLEV 5 /* Number of levels */
#define NTIME 3 /* Number of time steps */
\section{Write variables to a dataset}
int main(void)
{
int taxisID, vlistID, varID1, varID2, streamID, tsID;
int nmiss, vdate, vtime;
double var1[NLON*NLAT];
double var2[NLON*NLAT*NLEV];
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
/* Open the dataset */
streamID = streamOpenRead("example.nc");
/* Get the variable list of the dataset */
vlistID = streamInqVlist(streamID);
/* Set the variable IDs */
varID1 = 0;
varID2 = 1;
/* Get the Time axis form the variable list */
taxisID = vlistInqTaxis(vlistID);
/* Loop over the number of time steps */
for ( tsID = 0; tsID < NTIME; tsID++ )
{
/* Inquire the time step */
streamInqTimestep(streamID, tsID);
/* Get the verification date and time */
vdate = taxisInqVdate(taxisID);
vtime = taxisInqVtime(taxisID);
/* Read var1 and var2*/
streamReadVar(streamID, varID1, var1, &nmiss);
streamReadVar(streamID, varID2, var2, &nmiss);
}
/* Close the input stream */
streamClose(streamID);
return 0;
}
\end{lstlisting}
\section{Copy a dataset}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
#include "cdi.h"
#define NLON 12 /* Number of longitudes */
#define NLAT 6 /* Number of latitudes */
#define NLEV 5 /* Number of levels */
#define NTIME 3 /* Number of time steps */
int main(void)
{
int taxisID, vlistID1, vlistID2, varID1, varID2, streamID1, streamID2, tsID;
int nmiss, vdate, vtime;
double var1[NLON*NLAT];
double var2[NLON*NLAT*NLEV];
/* Open the input dataset */
streamID1 = streamOpenRead("example.nc");
/* Get the variable list of the dataset */
vlistID1 = streamInqVlist(streamID1);
/* Set the variable IDs */
varID1 = 0;
varID2 = 1;
/* Get the Time axis form the variable list */
taxisID = vlistInqTaxis(vlistID1);
/* Open the output dataset */
streamID2 = streamOpenWrite("example.grb", FILETYPE_GRB);
vlistID2 = vlistDuplicate(vlistID1);
streamDefVlist(streamID2, vlistID2);
/* Loop over the number of time steps */
for ( tsID = 0; tsID < NTIME; tsID++ )
{
/* Inquire the input time step */
streamInqTimestep(streamID1, tsID);
/* Get the verification date and time */
vdate = taxisInqVdate(taxisID);
vtime = taxisInqVtime(taxisID);
/* Define the output time step */
streamDefTimestep(streamID2, tsID);
/* Read var1 and var2 */
streamReadVar(streamID1, varID1, var1, &nmiss);
streamReadVar(streamID1, varID2, var2, &nmiss);
/* Write var1 and var2 */
streamWriteVar(streamID2, varID1, var1, nmiss);
streamWriteVar(streamID2, varID2, var2, nmiss);
}
/* Close the streams */
streamClose(streamID1);
streamClose(streamID2);
return 0;
}
\end{lstlisting}
......@@ -10,7 +10,7 @@ CDI library routines:
int gridCreate (intgridtype, intsize);
\end{verbatim}
Create a new Grid.
Create a horizontal Grid.
\section*{\tt \htmlref{gridDefNvertex}{gridDefNvertex}}
......@@ -139,6 +139,15 @@ Define the units of a Y-axis.
Define the values of a Y-axis.
\section*{\tt \htmlref{gridDestroy}{gridDestroy}}
\begin{verbatim}
void gridDestroy (intgridID);
\end{verbatim}
Destroy a horizontal Grid.
\section*{\tt \htmlref{gridDuplicate}{gridDuplicate}}
\begin{verbatim}
......@@ -337,6 +346,15 @@ Get all values of a Y-axis.
Close an open dataset.
\section*{\tt \htmlref{streamDefTimestep}{streamDefTimestep}}
\begin{verbatim}
int streamDefTimestep (intstreamID, inttsID);
\end{verbatim}
Define time step.
\section*{\tt \htmlref{streamDefVlist}{streamDefVlist}}
\begin{verbatim}
......@@ -346,6 +364,24 @@ Close an open dataset.
Define the Vlist for a stream.
\section*{\tt \htmlref{streamInqFiletype}{streamInqFiletype}}
\begin{verbatim}
int streamInqFiletype (intstreamID);
\end{verbatim}
Get the filetype.
\section*{\tt \htmlref{streamInqTimestep}{streamInqTimestep}}
\begin{verbatim}
int streamInqTimestep (intstreamID, inttsID);
\end{verbatim}
Get time step.
\section*{\tt \htmlref{streamInqVlist}{streamInqVlist}}
\begin{verbatim}
......@@ -417,7 +453,7 @@ Write a horizontal slice of a variable.
int taxisCreate (inttimetype);
\end{verbatim}
Create a new T-axis.
Create a Time axis.
\section*{\tt \htmlref{taxisDefCalendar}{taxisDefCalendar}}
......@@ -465,6 +501,15 @@ Define the verification date.
Define the verification time.
\section*{\tt \htmlref{taxisDestroy}{taxisDestroy}}
\begin{verbatim}
void taxisDestroy (inttaxisID);
\end{verbatim}
Destroy a Time axis.
\section*{\tt \htmlref{taxisInqCalendar}{taxisInqCalendar}}
\begin{verbatim}
......@@ -615,7 +660,7 @@ Define the units of a Variable.
void vlistDestroy (intvlistID);
\end{verbatim}
Delete a variable list.
Destroy a variable list.
\section*{\tt \htmlref{vlistDuplicate}{vlistDuplicate}}
......@@ -669,7 +714,7 @@ Number of zaxis in a variable list.
int zaxisCreate (intleveltype, intsize);
\end{verbatim}
Create a new Z-axis.
Create a vertical Z-axis.
\section*{\tt \htmlref{zaxisDefLevel}{zaxisDefLevel}}
......@@ -717,6 +762,15 @@ Define the name of a Z-axis.
Define the units of a Z-axis.
\section*{\tt \htmlref{zaxisDestroy}{zaxisDestroy}}
\begin{verbatim}
void zaxisDestroy (intzaxisID);
\end{verbatim}
Destroy a vertical Z-axis.
\section*{\tt \htmlref{zaxisDuplicate}{zaxisDuplicate}}
\begin{verbatim}
......
......@@ -193,9 +193,6 @@
\input{bib}
\input{c_examples}
\chapter*{Quick Reference\markboth{Quick Reference}{}}
\addcontentsline{toc}{chapter}{Quick Reference}
......@@ -205,6 +202,9 @@ CDI library routines:
\input{c_ref}
\input{c_examples}
\clearpage
\ifpdf
\phantomsection
......
......@@ -202,6 +202,9 @@ CDI library routines:
\input{f_ref}
\input{f_examples}
\clearpage
\ifpdf
\phantomsection
......
......@@ -10,7 +10,7 @@ CDI library routines:
INTEGER FUNCTION gridCreate (INTEGER gridtype, INTEGER size)
\end{verbatim}
Create a new Grid.
Create a horizontal Grid.
\section*{\tt \htmlref{gridDefNvertex}{gridDefNvertex}}
......@@ -139,6 +139,15 @@ Define the units of a Y-axis.
Define the values of a Y-axis.
\section*{\tt \htmlref{gridDestroy}{gridDestroy}}
\begin{verbatim}
SUBROUTINE gridDestroy (INTEGER gridID)
\end{verbatim}
Destroy a horizontal Grid.
\section*{\tt \htmlref{gridDuplicate}{gridDuplicate}}
\begin{verbatim}
......@@ -337,6 +346,15 @@ Get all values of a Y-axis.
Close an open dataset.
\section*{\tt \htmlref{streamDefTimestep}{streamDefTimestep}}
\begin{verbatim}
INTEGER FUNCTION streamDefTimestep (INTEGER streamID, INTEGER tsID)
\end{verbatim}
Define time step.
\section*{\tt \htmlref{streamDefVlist}{streamDefVlist}}
\begin{verbatim}
......@@ -346,6 +364,24 @@ Close an open dataset.
Define the Vlist for a stream.
\section*{\tt \htmlref{streamInqFiletype}{streamInqFiletype}}
\begin{verbatim}
INTEGER FUNCTION streamInqFiletype (INTEGER streamID)
\end{verbatim}
Get the filetype.
\section*{\tt \htmlref{streamInqTimestep}{streamInqTimestep}}
\begin{verbatim}
INTEGER FUNCTION streamInqTimestep (INTEGER streamID, INTEGER tsID)
\end{verbatim}
Get time step.
\section*{\tt \htmlref{streamInqVlist}{streamInqVlist}}
\begin{verbatim}
......@@ -419,7 +455,7 @@ Write a horizontal slice of a variable.
INTEGER FUNCTION taxisCreate (INTEGER timetype)
\end{verbatim}
Create a new T-axis.
Create a Time axis.
\section*{\tt \htmlref{taxisDefCalendar}{taxisDefCalendar}}
......@@ -467,6 +503,15 @@ Define the verification date.
Define the verification time.
\section*{\tt \htmlref{taxisDestroy}{taxisDestroy}}
\begin{verbatim}
SUBROUTINE taxisDestroy (INTEGER taxisID)
\end{verbatim}
Destroy a Time axis.
\section*{\tt \htmlref{taxisInqCalendar}{taxisInqCalendar}}
\begin{verbatim}
......@@ -620,7 +665,7 @@ Define the units of a Variable.
SUBROUTINE vlistDestroy (INTEGER vlistID)
\end{verbatim}
Delete a variable list.
Destroy a variable list.
\section*{\tt \htmlref{vlistDuplicate}{vlistDuplicate}}
......@@ -674,7 +719,7 @@ Number of zaxis in a variable list.
INTEGER FUNCTION zaxisCreate (INTEGER leveltype, INTEGER size)
\end{verbatim}
Create a new Z-axis.
Create a vertical Z-axis.
\section*{\tt \htmlref{zaxisDefLevel}{zaxisDefLevel}}
......@@ -723,6 +768,15 @@ Define the name of a Z-axis.
Define the units of a Z-axis.
\section*{\tt \htmlref{zaxisDestroy}{zaxisDestroy}}
\begin{verbatim}
SUBROUTINE zaxisDestroy (INTEGER zaxisID)
\end{verbatim}
Destroy a vertical Z-axis.
\section*{\tt \htmlref{zaxisDuplicate}{zaxisDuplicate}}
\begin{verbatim}
......
#include "cdi.h"
#define NLON 12 /* Number of longitudes */
#define NLAT 6 /* Number of latitudes */
#define NLEV 5 /* Number of levels */
#define NTIME 3 /* Number of time steps */
#define NLON 12 /* Number of longitudes */
#define NLAT 6 /* Number of latitudes */
#define NLEV 5 /* Number of levels */
#define NTIME 3 /* Number of time steps */
int main(void)
{
int gridID, zaxisID1, zaxisID2, taxisID;
int vlistID, varID1, varID2, streamID;
int tsID;
int i, nmiss = 0;
double lons[NLON] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
double lats[NLAT] = {-75, -45, -15, 15, 45, 75};
double levs[NLEV] = {101300, 92500, 85000, 50000, 20000};
int taxisID, vlistID1, vlistID2, varID1, varID2, streamID1, streamID2, tsID;
int nmiss, vdate, vtime;
double var1[NLON*NLAT];
double var2[NLON*NLAT*NLEV];
/* Create a LONLAT grid */
gridID = gridCreate(GRID_LONLAT, NLON*NLAT);
gridDefXsize(gridID, NLON);
gridDefYsize(gridID, NLAT);
gridDefXvals(gridID, lons);
gridDefYvals(gridID, lats);
/* Create a surface Z-axis */
zaxisID1 = zaxisCreate(ZAXIS_SURFACE, 1);
/* Open the input dataset */
streamID1 = streamOpenRead("example.nc");
/* Create a pressure level Z-axis */
zaxisID2 = zaxisCreate(ZAXIS_PRESSURE, NLEV);
zaxisDefLevels(zaxisID2, levs);
/* Create a variable list */
vlistID = vlistCreate();
/* Get the variable list of the dataset */
vlistID1 = streamInqVlist(streamID1);
/* Define the variables */
varID1 = vlistDefVar(vlistID, gridID, zaxisID1, TIME_VARIABLE);
varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE);
/* Set the variable IDs */
varID1 = 0;
varID2 = 1;
/* Define variable name */
vlistDefVarName(vlistID, varID1, "varname1");
vlistDefVarName(vlistID, varID2, "varname2");
/* Get the Time axis form the variable list */
taxisID = vlistInqTaxis(vlistID1);
/* Create a Time axis */
taxisID = taxisCreate(TAXIS_ABSOLUTE);
/* Open the output dataset */
streamID2 = streamOpenWrite("example.grb", FILETYPE_GRB);
/* Assign the Time axis to the variable list */
vlistDefTaxis(vlistID, taxisID);
vlistID2 = vlistDuplicate(vlistID1);
/* Create a dataset */
streamID = streamOpenWrite("example.nc", FILETYPE_NC);
/* Assign the variable list to the dataset */
streamDefVlist(streamID, vlistID);
streamDefVlist(streamID2, vlistID2);
/* Loop over the number of time steps */
for ( tsID = 0; tsID < NTIME; tsID++ )
{
/* Set the verification date to 1985-01-01 + tsID */
taxisDefVdate(taxisID, 19850101+tsID);
/* Set the verification time to 12:00 */
taxisDefVtime(taxisID, 1200);
/* Define the time step */
streamDefTimestep(streamID, tsID);
/* Write var1 */
for ( i = 0; i < NLON*NLAT; i++ ) var1[i] = 1;
streamWriteVar(streamID, varID1, var1, nmiss);
/* Write var2 */
for ( i = 0; i < NLON*NLAT*NLEV; i++ ) var2[i] = 2;
streamWriteVar(streamID, varID2, var2, nmiss);
}
/* Inquire the input time step */
streamInqTimestep(streamID1, tsID);
/* Get the verification date and time */
vdate = taxisInqVdate(taxisID);
vtime = taxisInqVtime(taxisID);
/* Close the output stream */
streamClose(streamID);
/* Define the output time step */
streamDefTimestep(streamID2, tsID);
/* Read var1 and var2 */
streamReadVar(streamID1, varID1, var1, &nmiss);
streamReadVar(streamID1, varID2, var2, &nmiss);
/* Write var1 and var2 */
streamWriteVar(streamID2, varID1, var1, nmiss);
streamWriteVar(streamID2, varID2, var2, nmiss);
}
/* Destroy the created objects */
vlistDestroy(vlistID);
taxisDestroy(taxisID);
zaxisDestroy(zaxisID1);
zaxisDestroy(zaxisID2);
gridDestroy(gridID);
/* Close the streams */
streamClose(streamID1);
streamClose(streamID2);
return 0;
}
PROGRAM CDICOPY
IMPLICIT NONE