Ticket #42414: essai.f90

File essai.f90, 3.3 KB (added by jsalort (Julien Salort), 11 years ago)

Example source file

Line 
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
6!     sample data. To handle this in netCDF we create two shared
7!     dimensions, "x" and "y", and a netCDF variable, called "data".
8
9!     This example demonstrates the netCDF Fortran 90 API. This is part
10!     of the netCDF tutorial, which can be found at:
11!     http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
12     
13!     Full documentation of the netCDF Fortran 90 API can be found at:
14!     http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90
15
16!     $Id: simple_xy_wr.f90,v 1.7 2006/12/09 18:44:58 russ Exp $
17
18program simple_xy_wr
19  use netcdf
20  implicit none
21
22  ! This is the name of the data file we will create.
23  character (len = *), parameter :: FILE_NAME = "simple_xy.nc"
24
25  ! We are writing 2D data, a 6 x 12 grid.
26  integer, parameter :: NDIMS = 2
27  integer, parameter :: NX = 6, NY = 12
28
29  ! When we create netCDF files, variables and dimensions, we get back
30  ! an ID for each one.
31  integer :: ncid, varid, dimids(NDIMS)
32  integer :: x_dimid, y_dimid
33 
34  ! This is the data array we will write. It will just be filled with
35  ! a progression of integers for this example.
36  integer :: data_out(NY, NX)
37
38  ! Loop indexes, and error handling.
39  integer :: x, y
40
41  ! Create some pretend data. If this wasn't an example program, we
42  ! would have some real data to write, for example, model output.
43  do x = 1, NX
44     do y = 1, NY
45        data_out(y, x) = (x - 1) * NY + (y - 1)
46     end do
47  end do
48
49  ! Always check the return code of every netCDF function call. In
50  ! this example program, wrapping netCDF calls with "call check()"
51  ! makes sure that any return which is not equal to nf90_noerr (0)
52  ! will print a netCDF error message and exit.
53
54  ! Create the netCDF file. The nf90_clobber parameter tells netCDF to
55  ! overwrite this file, if it already exists.
56  call check( nf90_create(FILE_NAME, NF90_CLOBBER, ncid) )
57
58  ! Define the dimensions. NetCDF will hand back an ID for each.
59  call check( nf90_def_dim(ncid, "x", NX, x_dimid) )
60  call check( nf90_def_dim(ncid, "y", NY, y_dimid) )
61
62  ! The dimids array is used to pass the IDs of the dimensions of
63  ! the variables. Note that in fortran arrays are stored in
64  ! column-major format.
65  dimids =  (/ y_dimid, x_dimid /)
66
67  ! Define the variable. The type of the variable in this case is
68  ! NF90_INT (4-byte integer).
69  call check( nf90_def_var(ncid, "data", NF90_INT, dimids, varid) )
70
71  ! End define mode. This tells netCDF we are done defining metadata.
72  call check( nf90_enddef(ncid) )
73
74  ! Write the pretend data to the file. Although netCDF supports
75  ! reading and writing subsets of data, in this case we write all the
76  ! data in one operation.
77  call check( nf90_put_var(ncid, varid, data_out) )
78
79  ! Close the file. This frees up any internal netCDF resources
80  ! associated with the file, and flushes any buffers.
81  call check( nf90_close(ncid) )
82
83  print *, "*** SUCCESS writing example file simple_xy.nc! "
84
85contains
86  subroutine check(status)
87    integer, intent ( in) :: status
88   
89    if(status /= nf90_noerr) then
90      print *, trim(nf90_strerror(status))
91      stop "Stopped"
92    end if
93  end subroutine check 
94end program simple_xy_wr