"Fossies" - the Fresh Open Source Software Archive

Member "netcdf-fortran-4.4.5/examples/F90/simple_xy_par_wr2.f90" (31 Jan 2019, 4879 Bytes) of package /linux/misc/netcdf-fortran-4.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Fortran 90 source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "simple_xy_par_wr2.f90": 4.4.4_vs_4.4.5.

    1 !     This is part of the netCDF package.
    2 !     Copyright 2006 University Corporation for Atmospheric Research/Unidata.
    3 !     See COPYRIGHT file for conditions of use.
    4 
    5 !     This is a very simple example which writes a 2D array of sample
    6 !     data. To handle this in netCDF we create two shared dimensions,
    7 !     "x" and "y", and a netCDF variable, called "data". It uses
    8 !     parallel I/O to write the file from all processors at the same
    9 !     time.
   10 
   11 !     This example demonstrates the netCDF Fortran 90 API. This is part
   12 !     of the netCDF tutorial, which can be found at:
   13 !     http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
   14       
   15 !     Full documentation of the netCDF Fortran 90 API can be found at:
   16 !     http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
   17 
   18 !     $Id: simple_xy_par_wr.f90,v 1.3 2010/06/01 15:34:49 ed Exp $
   19 
   20 ! Reto Stockli: added (to demonstrate parallel bug)
   21 ! - added unlimited time dimension (3)
   22 ! - added chunk size for unlimited variable writes
   23 ! - use of MPI module instead of include file
   24 ! - exclude first process from writing data (test independent write). 
   25 ! - include first process for opening/metadata/closing file
   26 
   27 program simple_xy_par_wr
   28 
   29   use netcdf
   30   use mpi
   31 
   32   implicit none
   33  
   34   ! This is the name of the data file we will create.
   35   character (len = *), parameter :: FILE_NAME = "simple_xy_par.nc"
   36 
   37   ! We are writing 2D data.
   38   integer, parameter :: NDIMS = 3
   39 
   40   ! When we create netCDF files, variables and dimensions, we get back
   41   ! an ID for each one.
   42   integer :: ncid, varid, dimids(NDIMS)
   43   integer :: x_dimid, y_dimid, t_dimid
   44 
   45   ! add chunk size for unlimited variables
   46   integer :: chunk_size(NDIMS)
   47 
   48   ! These will tell where in the data file this processor should
   49   ! write.
   50   integer :: start(NDIMS), count(NDIMS)
   51   
   52   ! This is the data array we will write. It will just be filled with
   53   ! the rank of this processor.
   54   integer, allocatable :: data_out(:)
   55 
   56   ! MPI stuff: number of processors, rank of this processor, and error
   57   ! code.
   58   integer :: p, my_rank, ierr
   59 
   60   ! Loop indexes, and error handling.
   61   integer :: x, stat
   62 
   63   ! Initialize MPI, learn local rank and total number of processors.
   64   call MPI_Init(ierr)
   65   call MPI_Comm_rank(MPI_COMM_WORLD, my_rank, ierr)
   66   call MPI_Comm_size(MPI_COMM_WORLD, p, ierr)
   67 
   68   ! Create some pretend data. We just need one row.
   69   allocate(data_out(p), stat = stat)
   70   if (stat .ne. 0) stop 3
   71   do x = 1, p
   72      data_out(x) = my_rank
   73   end do
   74 
   75   ! Create the netCDF file. The NF90_NETCDF4 flag causes a
   76   ! HDF5/netCDF-4 file to be created. The comm and info parameters
   77   ! cause parallel I/O to be enabled. Use either NF90_MPIIO or
   78   ! NF90_MPIPOSIX to select between MPI/IO and MPI/POSIX.
   79   call check(nf90_create(FILE_NAME, IOR(NF90_NETCDF4, NF90_MPIIO), ncid, &
   80        comm = MPI_COMM_WORLD, info = MPI_INFO_NULL))
   81 
   82   ! Define the dimensions. NetCDF will hand back an ID for
   83   ! each. Metadata operations must take place on all processors.
   84   call check(nf90_def_dim(ncid, "x", p, x_dimid))
   85   call check(nf90_def_dim(ncid, "y", p, y_dimid))
   86   call check(nf90_def_dim(ncid, "t", NF90_UNLIMITED, t_dimid))
   87 
   88   ! The dimids array is used to pass the IDs of the dimensions of
   89   ! the variables. Note that in fortran arrays are stored in
   90   ! column-major format.
   91   dimids = (/ y_dimid, x_dimid, t_dimid /)
   92 
   93   ! define the chunk size (1 along unlimited time dimension)
   94   chunk_size = (/ p, 1, 1 /)
   95 
   96   ! Define the variable. The type of the variable in this case is
   97   ! NF90_INT (4-byte integer).
   98   call check(nf90_def_var(ncid, "data", NF90_INT, dimids, varid, chunksizes=chunk_size))
   99 
  100   ! End define mode. This tells netCDF we are done defining
  101   ! metadata. This operation is collective and all processors will
  102   ! write their metadata to disk.
  103   call check(nf90_enddef(ncid))
  104 
  105   ! Write the pretend data to the file. Each processor writes one row.
  106   start = (/ 1, my_rank + 1, 1/)
  107   count = (/ p, 1, 1 /)
  108 
  109   ! Unlimited dimensions require collective writes
  110   call check(nf90_var_par_access(ncid, varid, nf90_collective))
  111 
  112   ! The unlimited axis prevents independent write tests
  113   ! Re-enable the rank test if independent writes are used in the future
  114   !if (my_rank.ne.0) &
  115   call check(nf90_put_var(ncid, varid, data_out, start = start, &
  116        count = count))
  117 
  118   ! Close the file. This frees up any internal netCDF resources
  119   ! associated with the file, and flushes any buffers.
  120   call check( nf90_close(ncid) )
  121 
  122   ! Free my local memory.
  123   deallocate(data_out)
  124 
  125   ! MPI library must be shut down.
  126   call MPI_Finalize(ierr)
  127 
  128   if (my_rank .eq. 0) print *, "*** SUCCESS writing example file ", FILE_NAME, "! "
  129 
  130 contains
  131   subroutine check(status)
  132     integer, intent ( in) :: status
  133     
  134     if(status /= nf90_noerr) then 
  135       print *, trim(nf90_strerror(status))
  136       stop 2
  137     end if
  138   end subroutine check  
  139 end program simple_xy_par_wr
  140