一个简单算例:
http://www.unidata.ucar.edu/software/netcdf/examples/programs/
! This is part of the netCDF package.
! Copyright 2006 University Corporation for Atmospheric Research/Unidata.
! See COPYRIGHT file for conditions of use.
! This is a very simple example which writes a 2D array of
! sample data. To handle this in netCDF we create two shared
! dimensions, "x" and "y", and a netCDF variable, called "data".
! This example demonstrates the netCDF Fortran 90 API. This is part
! of the netCDF tutorial, which can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
! Full documentation of the netCDF Fortran 90 API can be found at:
! http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
! $Id: simple_xy_wr.f90,v 1.7 2006/12/09 18:44:58 russ Exp $
program simple_xy_wr
use typeSizes
use netcdf90
implicit none
include 'netcdf.inc'
! This is the name of the data file we will create.
character (len = *), parameter :: FILE_NAME = "simple_xy.nc"
! We are writing 2D data, a 6 x 12 grid.
integer, parameter :: NDIMS = 2
integer, parameter :: NX = 6, NY = 12
! When we create netCDF files, variables and dimensions, we get back
! an ID for each one.
integer :: ncid, varid, dimids(NDIMS)
integer :: x_dimid, y_dimid
! This is the data array we will write. It will just be filled with
! a progression of integers for this example.
integer :: data_out(NY, NX)
! Loop indexes, and error handling.
integer :: x, y
! Create some pretend data. If this wasn't an example program, we
! would have some real data to write, for example, model output.
do x = 1, NX
do y = 1, NY
data_out(y, x) = (x - 1) * NY + (y - 1)
end do
end do
! Always check the return code of every netCDF function call. In
! this example program, wrapping netCDF calls with "call check()"
! makes sure that any return which is not equal to nf90_noerr (0)
! will print a netCDF error message and exit.
! Create the netCDF file. The nf90_clobber parameter tells netCDF to
! overwrite this file, if it already exists.
call check( nf90_create(FILE_NAME, NF90_CLOBBER, ncid) )
! Define the dimensions. NetCDF will hand back an ID for each.
call check( nf90_def_dim(ncid, "x", NX, x_dimid) )
call check( nf90_def_dim(ncid, "y", NY, y_dimid) )
! The dimids array is used to pass the IDs of the dimensions of
! the variables. Note that in fortran arrays are stored in
! column-major format.
dimids = (/ y_dimid, x_dimid /)
! Define the variable. The type of the variable in this case is
! NF90_INT (4-byte integer).
call check( nf90_def_var(ncid, "data", NF90_INT, dimids, varid) )
! End define mode. This tells netCDF we are done defining metadata.
call check( nf90_enddef(ncid) )
! Write the pretend data to the file. Although netCDF supports
! reading and writing subsets of data, in this case we write all the
! data in one operation.
call check( nf90_put_var(ncid, varid, data_out) )
! Close the file. This frees up any internal netCDF resources
! associated with the file, and flushes any buffers.
call check( nf90_close(ncid) )
print *, "*** SUCCESS writing example file simple_xy.nc! "
contains
subroutine check(status)
integer, intent ( in) :: status
if(status /= nf90_noerr) then
print *, trim(nf90_strerror(status))
stop "Stopped"
end if
end subroutine check
end program simple_xy_wr
运行结束,产生simple_xy.nc文件,运行 ncdump simple_xy.nc > simplile_xy.cdl可将.nc文件转换为.cdl文件。 cdl文件可用记事本打开,内容如下:
netcdf simple_xy {
dimensions:
x = 6 ;
y = 12 ;
variables:
int data(x, y) ;
data:
data =
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71 ;
}