Pseudo code for netCDF in land surface model


This document provides an example to incorporate the netCDF library in a source code of a land surface model. The model executable treats the number of grid boxes as a map, allowing a clean separation between I/O, model calculations and writing of results.

In the code listings below, only the netCDF reading/writing routines are included. A series of dots represents code that is model specific and taken out here for clarity. A series of dots followed by (etc) represents a repetition of a similar sequence of netCDF code found somewhere earlier in the example. A standard routine cdf_encode has been designed (but not tested) that defines a new 3D variable in a netCDF output field. Similar routines could be made for 4D fields (for instance, soil temperatures), but that has not been included in this example.

The code in the examples below is Fortran90 fixed format. Implicit type is used throughout, which implies that, in order to have a clean description of the code you should add, at the top of each routine, the declaration statements of logical and character array/scalars. Compilation on double precision is assumed. Output precision can be specified (either NCFLOAT or NCDOUBLE).

The code uses some Fortran90 intrinsic procedures, e.g. those that pack/unpack an array ((UN)PACK). The command

A1 = PACK (A2, LMASK)

creates a new array A1 containing only the results of A2 for cells where the corresponding elements of logical array LMASK are true. Thus, the size of A1 is smaller or equal to the size of A2, depending on the number of elements in LMASK that are true. The UNPACK command does the reverse.

For questions or remarks please refer to Bart van den Hurk (KNMI), hurkvd@knmi.nl.


Tree sketch of the code

mainmain routine
setupsetting of constants, timing etc
setup_poutsetup of netCDF output files with prognostic quantities
setup_doutsetup of netCDF output files with diagnostic quantities
setup_physsetup of physical parameters etc.
initializeinitialization of variables
rd_coorread input coordinates
rd_climread climate fields (vegetation, soil etc.)
rd_progread initial prognostic quantities
rd_forcread forcings
time_controltiming routine
intp_forcinterpolation of forcings
wr_progwriting of prognostic quantities
call_physcall to physics
filtertime filtering of diagnostic output
wr_diagwriting of diagnostic quantities
endend routine
f_closeclose output files