"Fossies" - the Fresh Open Source Software Archive

Member "netcdf-fortran-4.5.2/examples/F77/simple_xy_wr.f" (18 Sep 2019, 3890 Bytes) of package /linux/misc/netcdf-fortran-4.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Fortran 77 source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 C     This is part of the netCDF package.
    2 C     Copyright 2006 University Corporation for Atmospheric Research/Unidata.
    3 C     See COPYRIGHT file for conditions of use.
    4 
    5 C     This is a very simple example which writes a 2D array of
    6 C     sample data. To handle this in netCDF we create two shared
    7 C     dimensions, "x" and "y", and a netCDF variable, called "data".
    8 
    9 C     This example demonstrates the netCDF Fortran 77 API. This is part
   10 C     of the netCDF tutorial, which can be found at:
   11 C     http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
   12       
   13 C     Full documentation of the netCDF Fortran 77 API can be found at:
   14 C     http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f77
   15 
   16 C     $Id: simple_xy_wr.f,v 1.11 2008/08/20 22:29:56 russ Exp $
   17 
   18       program simple_xy_wr
   19       implicit none
   20       include 'netcdf.inc'
   21 
   22 C     This is the name of the data file we will create.
   23       character*(*) FILE_NAME
   24       parameter (FILE_NAME='simple_xy.nc')
   25 
   26 C     We are writing 2D data, a 12 x 6 grid. 
   27       integer NDIMS
   28       parameter (NDIMS=2)
   29       integer NX, NY
   30       parameter (NX = 6, NY = 12)
   31 
   32 C     When we create netCDF files, variables and dimensions, we get back
   33 C     an ID for each one.
   34       integer ncid, varid, dimids(NDIMS)
   35       integer x_dimid, y_dimid
   36 
   37 C     This is the data array we will write. It will just be filled with
   38 C     a progression of integers for this example.
   39       integer data_out(NY, NX)
   40 
   41 C     Loop indexes, and error handling.
   42       integer x, y, retval
   43 
   44 C     Create some pretend data. If this wasn't an example program, we
   45 C     would have some real data to write, for example, model output.
   46       do x = 1, NX
   47          do y = 1, NY
   48             data_out(y, x) = (x - 1) * NY + (y - 1)
   49          end do
   50       end do
   51 
   52 C     Always check the return code of every netCDF function call. In
   53 C     this example program, any retval which is not equal to nf_noerr
   54 C     (0) will call handle_err, which prints a netCDF error message, and
   55 C     then exits with a non-zero return code.
   56 
   57 C     Create the netCDF file. The nf_clobber parameter tells netCDF to
   58 C     overwrite this file, if it already exists.
   59       retval = nf_create(FILE_NAME, NF_CLOBBER, ncid)
   60       if (retval .ne. nf_noerr) call handle_err(retval)
   61 
   62 C     Define the dimensions. NetCDF will hand back an ID for each. 
   63       retval = nf_def_dim(ncid, "x", NX, x_dimid)
   64       if (retval .ne. nf_noerr) call handle_err(retval)
   65       retval = nf_def_dim(ncid, "y", NY, y_dimid)
   66       if (retval .ne. nf_noerr) call handle_err(retval)
   67 
   68 C     The dimids array is used to pass the IDs of the dimensions of
   69 C     the variables. Note that in fortran arrays are stored in
   70 C     column-major format.
   71       dimids(2) = x_dimid
   72       dimids(1) = y_dimid
   73 
   74 C     Define the variable. The type of the variable in this case is
   75 C     NF_INT (4-byte integer).
   76       retval = nf_def_var(ncid, "data", NF_INT, NDIMS, dimids, varid)
   77       if (retval .ne. nf_noerr) call handle_err(retval)
   78 
   79 C     End define mode. This tells netCDF we are done defining metadata.
   80       retval = nf_enddef(ncid)
   81       if (retval .ne. nf_noerr) call handle_err(retval)
   82 
   83 C     Write the pretend data to the file. Although netCDF supports
   84 C     reading and writing subsets of data, in this case we write all the
   85 C     data in one operation.
   86       retval = nf_put_var_int(ncid, varid, data_out)
   87       if (retval .ne. nf_noerr) call handle_err(retval)
   88 
   89 C     Close the file. This frees up any internal netCDF resources
   90 C     associated with the file, and flushes any buffers.
   91       retval = nf_close(ncid)
   92       if (retval .ne. nf_noerr) call handle_err(retval)
   93 
   94       print *,'*** SUCCESS writing example file simple_xy.nc!'
   95       end
   96 
   97       subroutine handle_err(errcode)
   98       implicit none
   99       include 'netcdf.inc'
  100       integer errcode
  101 
  102       print *, 'Error: ', nf_strerror(errcode)
  103       stop 2
  104       end