Commit 8c0677bd authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

add f_examples.tex

parent 48501709
......@@ -34,6 +34,7 @@ doc/tex/c_zaxis.tex -text
doc/tex/cdi_cman.tex -text
doc/tex/cdi_fman.tex -text
doc/tex/cleanup -text
doc/tex/f_examples.tex -text
doc/tex/f_grid.tex -text
doc/tex/f_link.tex -text
doc/tex/f_quick_ref.tex -text
......
\appendix
\chapter{\label{appendixa}Examples}
\section{Write a dataset}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
PROGRAM CDIWRITE
IMPLICIT NONE
INTEGER NLON, NLAT, NLEV, NTIME
PARAMETER (NLON = 12) ! Number of longitudes
PARAMETER (NLAT = 6) ! Number of latitudes
PARAMETER (NLEV = 5) ! Number of levels
PARAMETER (NTIME = 3) ! Number of time steps
INTEGER gridID, zaxisID1, zaxisID2, taxisID
INTEGER vlistID, varID1, varID2, streamID, tsID
INTEGER i, nmiss, status
REAL*8 lons(NLON), lats(NLAT), levs(NLEV)
REAL*8 var1(NLON*NLAT), var2(NLON*NLAT*NLEV)
DATA lons /0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330/
DATA lats /-75, -45, -15, 15, 45, 75/
DATA levs /101300, 92500, 85000, 50000, 20000/
INCLUDE 'cdi.inc'
nmiss = 0
! Create a regular lon/lat grid
gridID = gridCreate(GRID_LONLAT, NLON*NLAT)
CALL gridDefXsize(gridID, NLON)
CALL gridDefYsize(gridID, NLAT)
CALL gridDefXvals(gridID, lons)
CALL gridDefYvals(gridID, lats)
! Create a surface level Z-axis
zaxisID1 = zaxisCreate(ZAXIS_SURFACE, 1)
! Create a pressure level Z-axis
zaxisID2 = zaxisCreate(ZAXIS_PRESSURE, NLEV)
CALL zaxisDefLevels(zaxisID2, levs)
! Create a variable list
vlistID = vlistCreate()
! Define the variables
varID1 = vlistDefVar(vlistID, gridID, zaxisID1, TIME_VARIABLE)
varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE)
! Define the variable names
CALL vlistDefVarName(vlistID, varID1, "varname1")
CALL vlistDefVarName(vlistID, varID2, "varname2")
! Create a Time axis
taxisID = taxisCreate(TAXIS_ABSOLUTE)
! Assign the Time axis to the variable list
CALL vlistDefTaxis(vlistID, taxisID)
! Create a dataset in netCDF format
streamID = streamOpenWrite("example.nc", FILETYPE_NC)
! Assign the variable list to the dataset
CALL streamDefVlist(streamID, vlistID)
! Loop over the number of time steps
DO tsID = 0, NTIME-1
! Set the verification date to 1985-01-01 + tsID
CALL taxisDefVdate(taxisID, 19850101+tsID)
! Set the verification time to 12:00
CALL taxisDefVtime(taxisID, 1200)
! Define the time step
status = streamDefTimestep(streamID, tsID)
! Init var1 and var2
DO i = 1, NLON*NLAT
var1(i) = 1
END DO
DO i = 1, NLON*NLAT*NLEV
var2(i) = 2
END DO
! Write var1 and var2
CALL streamWriteVar(streamID, varID1, var1, nmiss)
CALL streamWriteVar(streamID, varID2, var2, nmiss)
END DO
! Close the output stream
CALL streamClose(streamID)
! Destroy the objects
CALL vlistDestroy(vlistID)
CALL taxisDestroy(taxisID)
CALL zaxisDestroy(zaxisID1)
CALL zaxisDestroy(zaxisID2)
CALL gridDestroy(gridID)
END
\end{lstlisting}
\subsection{Result}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
netcdf example {
dimensions:
lon = 12 ;
lat = 6 ;
lev = 5 ;
time = UNLIMITED ; // (3 currently)
variables:
double lon(lon) ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:standard_name = "longitude" ;
double lat(lat) ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:standard_name = "latitude" ;
double lev(lev) ;
lev:long_name = "pressure" ;
lev:units = "Pa" ;
double time(time) ;
time:units = "day as %Y%m%d.%f" ;
float varname1(time, lat, lon) ;
float varname2(time, lev, lat, lon) ;
data:
lon = 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 ;
lat = -75, -45, -15, 15, 45, 75 ;
lev = 101300, 92500, 85000, 50000, 20000 ;
time = 19850101.5, 19850102.5, 19850103.5 ;
}
\end{lstlisting}
\section{Read a dataset}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
PROGRAM CDIREAD
IMPLICIT NONE
INTEGER NLON, NLAT, NLEV, NTIME
PARAMETER (NLON = 12) ! Number of longitudes
PARAMETER (NLAT = 6) ! Number of latitudes
PARAMETER (NLEV = 5) ! Number of levels
PARAMETER (NTIME = 3) ! Number of time steps
INTEGER gridID, zaxisID1, zaxisID2, taxisID
INTEGER vlistID, varID1, varID2, streamID, tsID
INTEGER nmiss, status, vdate, vtime
REAL*8 var1(NLON*NLAT), var2(NLON*NLAT*NLEV)
INCLUDE 'cdi.inc'
! 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
DO tsID = 0, NTIME-1
! Inquire the time step
status = streamInqTimestep(streamID, tsID)
! Get the verification date and time
vdate = taxisInqVdate(taxisID)
vtime = taxisInqVtime(taxisID)
! Read var1 and var2
CALL streamReadVar(streamID, varID1, var1, nmiss)
CALL streamReadVar(streamID, varID2, var2, nmiss)
END DO
! Close the input stream
CALL streamClose(streamID)
END
\end{lstlisting}
\section{Copy a dataset}
\begin{lstlisting}[frame=single, backgroundcolor=\color{zebg}, basicstyle=\footnotesize]
PROGRAM CDICOPY
IMPLICIT NONE
INTEGER NLON, NLAT, NLEV, NTIME
PARAMETER (NLON = 12) ! Number of longitudes
PARAMETER (NLAT = 6) ! Number of latitudes
PARAMETER (NLEV = 5) ! Number of levels
PARAMETER (NTIME = 3) ! Number of time steps
INTEGER gridID, zaxisID1, zaxisID2, taxisID, tsID
INTEGER vlistID1, vlistID2, varID1, varID2, streamID1, streamID2
INTEGER i, nmiss, status, vdate, vtime
REAL*8 var1(NLON*NLAT), var2(NLON*NLAT*NLEV)
INCLUDE 'cdi.inc'
! 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)
CALL streamDefVlist(streamID2, vlistID2)
! Loop over the number of time steps
DO tsID = 0, NTIME-1
! Inquire the input time step */
status = streamInqTimestep(streamID1, tsID)
! Get the verification date and time
vdate = taxisInqVdate(taxisID)
vtime = taxisInqVtime(taxisID)
! Define the output time step
status = streamDefTimestep(streamID2, tsID)
! Read var1 and var2
CALL streamReadVar(streamID1, varID1, var1, nmiss)
CALL streamReadVar(streamID1, varID2, var2, nmiss)
! Write var1 and var2
CALL streamWriteVar(streamID2, varID1, var1, nmiss)
CALL streamWriteVar(streamID2, varID2, var2, nmiss)
END DO
! Close the streams
CALL streamClose(streamID1)
CALL streamClose(streamID2)
END
\end{lstlisting}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment