File: netcdf4_variables.f90

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 (360 lines) | stat: -rw-r--r-- 13,600 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  ! ----- 
  ! Variable definitions and inquiry
  ! ----- 
  function nf90_def_var_Scalar(ncid, name, xtype, varid)
    integer, intent( in) :: ncid
    character (len = *), intent( in) :: name
    integer, intent(in) :: xtype
    integer, intent(out) :: varid
    integer :: nf90_def_var_Scalar
    
    ! Dummy - shouldn't get used
    integer, dimension(1) :: dimids

    ! These may not be used with scalars, but it causes an interface
    ! violation if they are not optional arguments.
    
    nf90_def_var_Scalar = nf_def_var(ncid, name, xtype, 0, dimids, varid)
  end function nf90_def_var_Scalar
  ! ----- 
  function nf90_def_var_oneDim(ncid, name, xtype, dimids, varid, contiguous, &
       chunksizes, deflate_level, shuffle, fletcher32, endianness, &
       cache_size, cache_nelems, cache_preemption)
    integer, intent( in) :: ncid
    character (len = *), intent( in) :: name
    integer, intent(in) :: xtype
    integer, intent(in) :: dimids
    integer, intent(out) :: varid
    logical, optional, intent(in) :: contiguous
    integer, optional, intent(in) :: chunksizes
    integer, optional, intent(in) :: deflate_level
    logical, optional, intent(in) :: shuffle, fletcher32
    integer, optional, intent(in) :: endianness
    integer, optional, intent(in) :: cache_size, cache_nelems, cache_preemption
    integer :: nf90_def_var_oneDim
    
    integer, dimension(1) :: dimidsA, chunksizes1
    integer :: size1 = -1, nelems1 = -1, preemption1 = -1
    integer :: contiguous1

    ! Put this int into an array, where all decent folk keep ids.
    dimidsA(1) = dimids

    ! This is forbidden! Don't even think about it.
    if (present(contiguous)) then
       if (contiguous .and. present(chunksizes)) then
          nf90_def_var_oneDim = nf90_einval
          return
       end if
    end if
    if (present(contiguous)) then
       if (.not. contiguous .and. .not. present(chunksizes)) then
          nf90_def_var_oneDim = nf90_einval
          return
       end if
    end if
    

    ! Define the variable. 
    nf90_def_var_oneDim = nf_def_var(ncid, name, xtype, 1, dimidsA, varid)
    if (nf90_def_var_oneDim .ne. nf90_noerr) return

    ! Handle chunksizes and contiguous.
    if (present(chunksizes) .or. present(contiguous)) then
       if (present(contiguous)) then
          if (contiguous) then
             contiguous1 = nf90_contiguous
          else
             contiguous1 = nf90_notcontiguous
          endif
       endif
       if (present(chunksizes)) then
          contiguous1 = 0
          chunksizes1(1) = chunksizes
       endif
       nf90_def_var_oneDim = nf_def_var_chunking(ncid, varid, contiguous1, chunksizes1)       
    endif
    if (present(contiguous)) then
       if (contiguous) then
          chunksizes1(1) = 0
          nf90_def_var_oneDim = nf_def_var_chunking(ncid, varid, 1, chunksizes1(1:1))       
       endif
    endif
    if (nf90_def_var_oneDim .ne. nf90_noerr) return
    
    ! Handle deflate and shuffle.
    if (present(deflate_level)) then
       if (deflate_level .gt. 0) then
          if (present(shuffle)) then
             if (shuffle) then
                nf90_def_var_oneDim = nf_def_var_deflate(ncid, varid, 1, 1, deflate_level)       
             else
                nf90_def_var_oneDim = nf_def_var_deflate(ncid, varid, 0, 1, deflate_level)       
             end if
             if (nf90_def_var_oneDim .ne. nf90_noerr) return
          end if
       end if
    endif

    ! Handle fletcher32.
    if (present(fletcher32)) then
       if (fletcher32) then
          nf90_def_var_oneDim = nf_def_var_fletcher32(ncid, varid, 1)       
          if (nf90_def_var_oneDim .ne. nf90_noerr) return
       endif
    endif

    ! Handle endianness.
    if (present(endianness)) then
       nf90_def_var_oneDim = nf_def_var_endian(ncid, varid, endianness)       
       if (nf90_def_var_oneDim .ne. nf90_noerr) return
    endif

    ! Set the cache if the user wants to.
    if (present(cache_size) .or. present(cache_nelems) .or. &
         present(cache_preemption)) then
       ! Negative values mean leave it alone.
       if (present(cache_size)) size1 = cache_size
       if (present(cache_nelems)) nelems1 = cache_nelems
       if (present(cache_preemption)) preemption1 = cache_preemption
       
       nf90_def_var_oneDim = nf_set_var_chunk_cache(ncid, varid, &
            size1, nelems1, preemption1)
       if (nf90_def_var_oneDim .ne. nf90_noerr) return
    endif

  end function nf90_def_var_oneDim
  ! ----- 
  function nf90_def_var_ManyDims(ncid, name, xtype, dimids, varid, contiguous, &
       chunksizes, deflate_level, shuffle, fletcher32, endianness, cache_size, &
       cache_nelems, cache_preemption)
    integer, intent(in) :: ncid
    character (len = *), intent(in) :: name
    integer, intent( in) :: xtype
    integer, dimension(:), intent(in) :: dimids
    integer, intent(out) :: varid
    logical, optional, intent(in) :: contiguous
    integer, optional, dimension(:), intent(in) :: chunksizes
    integer, optional, intent(in) :: deflate_level
    logical, optional, intent(in) :: shuffle, fletcher32
    integer, optional, intent(in) :: endianness
    integer, optional, intent(in) :: cache_size, cache_nelems, cache_preemption
    integer :: nf90_def_var_ManyDims

    ! Local variables.
    integer :: contiguous1, d
    integer :: size1 = -1, nelems1 = -1, preemption1 = -1
    integer, dimension(nf90_max_dims) :: chunksizes1

    ! This is forbidden!
    if (present(contiguous)) then
       if (contiguous .and. present(chunksizes)) then
          nf90_def_var_ManyDims = nf90_einval
          return
       end if
    end if
    if (present(contiguous)) then
       if (.not. contiguous .and. .not. present(chunksizes)) then
          nf90_def_var_ManyDims = nf90_einval
          return
       endif
    end if

    ! Be nice and check array size.
    if (present(chunksizes)) then
       if (size(chunksizes) .ne. size(dimids)) then
          nf90_def_var_ManyDims = nf90_einval
          return
       end if
    end if
    
    ! Define the variable. 
    nf90_def_var_ManyDims = nf_def_var(ncid, name, xtype, size(dimids), dimids, varid)
    if (nf90_def_var_ManyDims .ne. nf90_noerr) return

    ! Handle chunksizes and contiguous.
    if (present(chunksizes) .or. present(contiguous)) then
       if (present(contiguous)) then
          if (contiguous) then
             contiguous1 = nf90_contiguous
          else
             contiguous1 = nf90_notcontiguous
          endif
       endif
       if (present(chunksizes)) then
          contiguous1 = 0
          do d = 1, size(dimids)
             chunksizes1(d) = chunksizes(d)
          end do
       endif
       nf90_def_var_ManyDims = nf_def_var_chunking(ncid, varid, contiguous1, chunksizes1)       
    endif
    if (present(contiguous)) then
       if (contiguous) then
          chunksizes1(1) = 0
          nf90_def_var_ManyDims = nf_def_var_chunking(ncid, varid, 1, chunksizes1(1:1))       
       endif
    endif
    if (nf90_def_var_ManyDims .ne. nf90_noerr) return
    
    ! Handle deflate and shuffle.
    if (present(deflate_level)) then
       if (deflate_level .gt. 0) then
          if (present(shuffle)) then
             if (shuffle) then
                nf90_def_var_ManyDims = nf_def_var_deflate(ncid, varid, 1, 1, deflate_level)       
             else
                nf90_def_var_ManyDims = nf_def_var_deflate(ncid, varid, 0, 1, deflate_level)       
             end if
          else
             nf90_def_var_ManyDims = nf_def_var_deflate(ncid, varid, 0, 1, deflate_level)       
          end if
       end if
    endif
    if (nf90_def_var_ManyDims .ne. nf90_noerr) return

    ! Handle fletcher32.
    if (present(fletcher32)) then
       if (fletcher32) then
          nf90_def_var_ManyDims = nf_def_var_fletcher32(ncid, varid, 1)       
       endif
    endif
    if (nf90_def_var_ManyDims .ne. nf90_noerr) return

    ! Handle endianness.
    if (present(endianness)) then
       nf90_def_var_ManyDims = nf_def_var_endian(ncid, varid, endianness)       
    endif

    ! Set the cache if the user wants to.
    if (present(cache_size) .or. present(cache_nelems) .or. &
         present(cache_preemption)) then
       ! Negative values mean leave it alone.
       if (present(cache_size)) size1 = cache_size
       if (present(cache_nelems)) nelems1 = cache_nelems
       if (present(cache_preemption)) preemption1 = cache_preemption
       
       nf90_def_var_ManyDims = nf_set_var_chunk_cache(ncid, varid, &
            size1, nelems1, preemption1)
       if (nf90_def_var_ManyDims .ne. nf90_noerr) return
    endif

  end function nf90_def_var_ManyDims
  ! ----- 
  function nf90_inq_varid(ncid, name, varid)
    integer, intent(in) :: ncid
    character (len = *), intent( in) :: name
    integer, intent(out) :: varid
    integer :: nf90_inq_varid
    
    nf90_inq_varid = nf_inq_varid(ncid, name, varid)
    if (nf90_inq_varid .ne. nf90_noerr) return

  end function nf90_inq_varid
  ! ----- 
  function nf90_set_var_chunk_cache(ncid, varid, size, nelems, preemption)
    integer, intent(in) :: ncid, varid, size, nelems, preemption
    integer :: nf90_set_var_chunk_cache
    
    nf90_set_var_chunk_cache = nf_set_var_chunk_cache(ncid, varid, &
         size, nelems, preemption)
    if (nf90_set_var_chunk_cache .ne. nf90_noerr) return

  end function nf90_set_var_chunk_cache
  ! ----- 
  function nf90_inquire_variable(ncid, varid, name, xtype, ndims, dimids, nAtts, &
       contiguous, chunksizes, deflate_level, shuffle, fletcher32, endianness, &
       cache_size, cache_nelems, cache_preemption)
    integer, intent(in) :: ncid, varid
    character (len = *), optional, intent(out) :: name
    integer, optional, intent(out) :: xtype, ndims 
    integer, dimension(:), optional, intent(out) :: dimids
    integer, optional, intent(out) :: nAtts
    logical, optional, intent(out) :: contiguous
    integer, optional, dimension(:), intent(out) :: chunksizes
    integer, optional, intent(out) :: deflate_level
    logical, optional, intent(out) :: shuffle, fletcher32
    integer, optional, intent(out) :: endianness
    integer, optional, intent(out) :: cache_size, cache_nelems, cache_preemption
    integer :: nf90_inquire_variable
    
    ! Local variables
    character (len = nf90_max_name) :: varName = ''
    integer :: externalType, numDimensions
    integer, dimension(nf90_max_var_dims) :: dimensionIDs
    integer :: numAttributes
    integer :: deflate1, deflate_level1, contiguous1, shuffle1, fletcher321
    integer, dimension(nf90_max_dims) :: chunksizes1
    integer :: size1, nelems1, preemption1
    integer :: d

    ! Learn the basic facts.
    nf90_inquire_variable = nf_inq_var(ncid, varid, varName, externalType, &
                                       numDimensions, dimensionIDs, numAttributes)
    if (nf90_inquire_variable .ne. nf90_noerr) return
    
    ! Tell the user what he wants to know.
    if (present(name)) name = trim(varName)
    if (present(xtype)) xtype = externalType
    if (present(ndims)) ndims = numDimensions
    if (present(dimids)) then
       if (size(dimids) .ge. numDimensions) then
          dimids(:numDimensions) = dimensionIDs(:numDimensions)
       else
          nf90_inquire_variable = nf90_einval
       endif
    endif
    if (present(nAtts)) nAtts = numAttributes

    ! Get the chunksizes and contiguous settings, if desired.
    if (present(chunksizes) .or. present(contiguous)) then
       nf90_inquire_variable = nf_inq_var_chunking(ncid, varid, contiguous1, chunksizes1)
       if (nf90_inquire_variable .ne. nf90_noerr) return
       if (present(contiguous)) contiguous = contiguous1 .ne. nf90_notcontiguous
       if (present(chunksizes)) then
          do d = 1, numDimensions
             chunksizes(d) = chunksizes1(d)
          end do
       endif
    endif

    ! Get the fletcher32 settings, if desired.
    if (present(fletcher32)) then
       nf90_inquire_variable = nf_inq_var_fletcher32(ncid, varid, fletcher321)
       if (nf90_inquire_variable .ne. nf90_noerr) return
       fletcher32 = fletcher321 .gt. 0
    endif
    
    ! Get the deflate and shuffle settings, if desired.
    if (present(deflate_level) .or. present(shuffle)) then
       nf90_inquire_variable = nf_inq_var_deflate(ncid, varid, shuffle1, deflate1, deflate_level1)
       if (nf90_inquire_variable .ne. nf90_noerr) return
       if (present(deflate_level)) deflate_level = deflate_level1
       if (present(shuffle)) shuffle = shuffle1 .ne. 0
    endif

    ! And the endianness...
    if (present(endianness)) then
       nf90_inquire_variable = nf_inq_var_endian(ncid, varid, endianness)
       if (nf90_inquire_variable .ne. nf90_noerr) return
    endif

    ! Does the user want cache settings?
    if (present(cache_size) .or. present(cache_nelems) .or. present(cache_preemption)) then
       nf90_inquire_variable = nf_get_var_chunk_cache(ncid, varid, &
            size1, nelems1, preemption1)
       if (nf90_inquire_variable .ne. nf90_noerr) return
       if (present(cache_size)) cache_size = size1
       if (present(cache_nelems)) cache_nelems = nelems1
       if (present(cache_preemption)) cache_preemption = preemption1
    endif
  end function nf90_inquire_variable
  ! ----- 
  function nf90_rename_var(ncid, varid, newname)
    integer,             intent( in) :: ncid, varid
    character (len = *), intent( in) :: newname
    integer                          :: nf90_rename_var
    
    nf90_rename_var = nf_rename_var(ncid, varid, newname)
  end function nf90_rename_var
  ! -----