File: ftst_vars2.F

package info (click to toggle)
netcdf-fortran 4.4.4%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,420 kB
  • ctags: 8,797
  • sloc: fortran: 51,087; f90: 20,357; sh: 11,601; ansic: 7,034; makefile: 548; pascal: 313; xml: 173
file content (137 lines) | stat: -rw-r--r-- 4,588 bytes parent folder | download | duplicates (4)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
C     This is part of the netCDF package.
C     Copyright 2008 University Corporation for Atmospheric Research/Unidata.
C     See COPYRIGHT file for conditions of use.

C     This program tests netCDF-4 variable functions from fortran, even
C     more.

C     $Id: ftst_vars2.F,v 1.6 2010/01/20 15:21:46 ed Exp $

      program ftst_vars2
      implicit none
      include 'netcdf.inc'

C     This is the name of the data file we will create.
      character*(*) FILE_NAME
      parameter (FILE_NAME='ftst_vars2.nc')

C     We are writing 3D data, a 3 x 5 x 2 grid. Why do I use "x, y, z,"
C     and then do everything in order "z, y, x?" Because I am a C
C     programmer, and everything in Fortran seems backwards!
      integer NDIMS, NTYPES
      parameter (NDIMS = 3, NTYPES = 5)
      integer NX, NY, NZ
      parameter (NX = 3, NY = 5, NZ = 2)

C     These will be used to set the per-variable chunk cache.
      integer CACHE_SIZE, CACHE_NELEMS, CACHE_PREEMPTION
      parameter (CACHE_SIZE = 8, CACHE_NELEMS = 571)
      parameter (CACHE_PREEMPTION = 42)

C     These will be used to check the setting of the per-variable chunk
C     cache.
      integer cache_size_in, cache_nelems_in, cache_preemption_in

C     NetCDF IDs.
      integer ncid, varid(NTYPES), dimids(NDIMS), typeid(NTYPES)

C     Name of the variable is stored here.
      character*80 var_name

C     This is the data array we will write, and a place to store it when
C     we read it back in. Z is the fastest varying dimension.
      integer data_out(NZ, NY, NX), data_in(NZ, NY, NX)

C     Loop indexes, and error handling.
      integer i, x, y, z, retval

C     Create some pretend data.
      do x = 1, NX
         do y = 1, NY
            do z = 1, NZ
               data_out(z, y, x) = (x-1) * NY * NZ + (y-1) * NZ + (z-1)
            end do
         end do
      end do

      print *, ''
      print *,'*** Testing netCDF-4 vars from F77 with new types.'

C     Create the netCDF file.
      retval = nf_create(FILE_NAME, NF_NETCDF4, ncid)
      if (retval .ne. nf_noerr) call handle_err(retval)

C     Define the dimensions.
      retval = nf_def_dim(ncid, "z", NZ, dimids(3))
      if (retval .ne. nf_noerr) call handle_err(retval)
      retval = nf_def_dim(ncid, "y", NY, dimids(2))
      if (retval .ne. nf_noerr) call handle_err(retval)
      retval = nf_def_dim(ncid, "x", NX, dimids(1))
      if (retval .ne. nf_noerr) call handle_err(retval)

C     These are the types of vars that will be written.
      typeid(1) = NF_UBYTE
      typeid(2) = NF_USHORT
      typeid(3) = NF_UINT
      typeid(4) = NF_INT64
      typeid(5) = NF_UINT64

C     Define the variables. 
      do i = 1, NTYPES
         write(var_name, 1001) i
 1001    format('var', I1)
         retval = nf_def_var(ncid, var_name, typeid(i), NDIMS, 
     &        dimids, varid(i))
         if (retval .ne. nf_noerr) call handle_err(retval)

C        Set variable caches.
         retval = nf_set_var_chunk_cache(ncid, varid(i), CACHE_SIZE, 
     &        CACHE_NELEMS, CACHE_PREEMPTION)
         if (retval .ne. nf_noerr) call handle_err(retval)
      end do

C     Check the per-variable cache to make sure it is what we think it
C     is.
      do i = 1, NTYPES
         retval = nf_get_var_chunk_cache(ncid, varid(i), cache_size_in, 
     &        cache_nelems_in, cache_preemption_in)
         if (retval .ne. nf_noerr) call handle_err(retval)
         if (cache_size_in .ne. CACHE_SIZE .or. cache_nelems_in .ne. 
     &        CACHE_NELEMS .or. cache_preemption .ne. CACHE_PREEMPTION)
     &        stop 8
         
      end do

C     Write the pretend data to the file.
      do i = 1, NTYPES
         retval = nf_put_var_int(ncid, varid(i), data_out)
         if (retval .ne. nf_noerr) call handle_err(retval)
      end do

C     Close the file. 
      retval = nf_close(ncid)
      if (retval .ne. nf_noerr) call handle_err(retval)

C     Reopen the file and check again.
      retval = nf_open(FILE_NAME, NF_NOWRITE, ncid)
      if (retval .ne. nf_noerr) call handle_err(retval)

C     Read the data and check it.
      do i = 1, NTYPES
         retval = nf_get_var_int(ncid, varid(i), data_in)
         if (retval .ne. nf_noerr) call handle_err(retval)
         do x = 1, NX
            do y = 1, NY
               do z = 1, NZ
                  if (data_in(z, y, x) .ne. data_out(z, y, x)) stop 2
               end do
            end do
         end do
      end do

C     Close the file. 
      retval = nf_close(ncid)
      if (retval .ne. nf_noerr) call handle_err(retval)

      print *,'*** SUCCESS!'
      end