0.6.2-dev0
FORCES
FORtran lib for Comp. Env. Sys.
Loading...
Searching...
No Matches
mo_ncread.f90
Go to the documentation of this file.
1!> \file mo_ncread.f90
2!> \brief \copybrief mo_ncread
3!> \details \copydetails mo_ncread
4
5!> \brief Reading netcdf files
6!> \details Subroutines for reading arrays from nc file using the netcdf4 library.
7!> \author Stephan Thober, Matthias Cuntz
8!> \date Nov 2011
9!> \copyright Copyright 2005-\today, the CHS Developers, Sabine Attinger: All rights reserved.
10!! FORCES is released under the LGPLv3+ license \license_note
12
13 use mo_kind, only : i4, i8, sp, dp
14
15 ! functions and constants of netcdf4 library
16 use netcdf, only : nf90_open, nf90_get_var, nf90_close, nf90_max_name, &
17 nf90_get_att, nf90_inq_varid, nf90_inquire_variable, &
18 nf90_inquire_dimension, nf90_nowrite, &
19 nf90_noerr, nf90_strerror, nf90_inquire_attribute
20
21 implicit none
22
23 public :: get_ncdim ! get the dimensions of a Variable
24 public :: get_ncdimatt ! get the attributes of the dimensions
25 public :: get_ncvaratt ! get attributes of a variable
26 public :: get_ncvar ! get the data of a Variable in a nc file
27 public :: ncopen ! Open a file and get a handle back
28 public :: ncclose ! Close a file
29
30 ! ------------------------------------------------------------------------------
31
32 !> \brief Read array from NC file
33
34 !> \details
35 !! Reads a 2 - 5 dimensional array from a nc file given
36 !! the variable name EXACTLY as specified in the file. If the
37 !! array is not allocated when calling, Get_NcVar will
38 !! allocate it internally. If the dimension of the actual data
39 !! is less than the ones of the array, then the dimension
40 !! lengths of the array will be filled with ones.
41 !!
42 !! \b Example
43 !!
44 !! See test program in directory test_mo_NcRead.
45 !!
46 !! \b Literature
47 !!
48 !! 1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
49 !!
50 !> \param[in] "character(len=*) :: Filename" Name of the nc file.
51 !> \param[in] "character(len=*) :: VarName" Name of the Variable in the nc file.
52 !> \param[in] "real(sp/dp), dimension(:,:[,:[,:[,:]]]), allocatable :: array" Array where data will be read.
53 !> \param[in] "integer(i4), dimension(:) :: jdate" Starting indeces of first value to read.
54 !! `len` is the number of dimensions of
55 !! array, default is 1, see example
56 !> \param[in] "integer(i4), dimension(:) :: a_count" Same size as jdate, specifies how
57 !! many values in each dimension
58 !! is going to be read
59 !> \param[in] "integer(i4) :: fid" File handle of opened netcdf file
60
61 !> \note Output array is a floating point of 2-5 dimensions.\n
62 !! NOT yet tested for different compilers than intel11.1.075
63 !! CANNOT read packed data.\n
64 !! i1 indicates, that 1 byte integer is read [type is integer(1)].
65
66 !> \author Stephan Thober
67 !> \date Nov 2011
68 !! - added comments
69 !> \date Mar 2012
70 !! - corrected dynamical read of data
71 !> \date May 2012
72 !! - fid
73 !> \date Nov 2012
74 !! - write out Varname, when vartype is incorrect
75 !> \date Feb 2013
76 !! - added 1 byte integer version
77 !> \date Mar 2014
78 !! - added subroutines for allocatable arrays
79 !> \author Matthias Cuntz
80 !> \date Jan 2012
81 !! - unified routines for different dimensions and data types
82
83 ! ------------------------------------------------------------------------------
84
85 interface get_ncvar
86 module procedure get_ncvar_0d_sp, get_ncvar_0d_dp, get_ncvar_1d_sp, &
87 get_ncvar_1d_dp, get_ncvar_2d_sp, get_ncvar_2d_dp, &
88 get_ncvar_3d_sp, get_ncvar_3d_dp, get_ncvar_4d_sp, &
89 get_ncvar_4d_dp, get_ncvar_5d_sp, get_ncvar_5d_dp, &
90 get_ncvar_0d_i4, get_ncvar_1d_i4, get_ncvar_2d_i4, &
91 get_ncvar_3d_i4, get_ncvar_4d_i4, get_ncvar_5d_i4, &
92 get_ncvar_0d_i1, get_ncvar_1d_i1, get_ncvar_2d_i1, &
93 get_ncvar_3d_i1, get_ncvar_4d_i1, get_ncvar_5d_i1
94 end interface get_ncvar
95
96 ! ------------------------------------------------------------------------------
97
98 private
99
100 ! ------------------------------------------------------------------------------
101
102contains
103
104 ! ------------------------------------------------------------------------------
105
106 !> \brief Dimension of netcdf variable.
107
108 !> \details
109 !! Gets the dimensions of variable in a netcdf file.
110 !!
111 !! \b Literature
112 !!
113 !! 1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
114 !!
115 !> \param[in] "character(len=*) :: Filename" Filename of netcdf file
116 !> \param[in] "character(len=*) :: Variable" Variable name exactly as specified in the file
117 !> \param[in] "logical, optional :: PrintInfo" If given and true, information about dimension
118 !! and their lengths will be printed to standard output.
119 !> \retval "integer(i4), dimension(5) :: Get_NcDim" Dimension length, -1 if dimension does not exist
120 !> \param[out] "integer(i4), optional :: ndims" # of dimensions
121
122 !> \author Stephan Thober
123 !> \date Dec 2011
124 !> \author Matthias Cuntz
125 !> \date Jan 2012
126 !! - ndims
127 function get_ncdim(Filename, Variable, PrintInfo, ndims)
128 !
129 implicit none
130 !
131 character(len = *), intent(in) :: filename
132 character(len = *), intent(in) :: variable
133 logical, optional, intent(in) :: printinfo
134 integer(i4), optional, intent(out) :: ndims
135 integer(i4), dimension(5) :: get_ncdim
136 !
137 logical :: printflag
138 integer(i4) :: ncid ! id of input stream
139 integer(i4) :: varid ! id of variable to be read
140 integer(i4) :: vartype ! type of variable
141 integer(i4) :: numdims ! # of dimensions
142 !
143 ! Open NetCDF filename
144 call check(nf90_open(filename, nf90_nowrite, ncid))
145 !
146 printflag = .false.
147 if (present(printinfo)) printflag = printinfo
148 !
149 ! Inquire file and check if VarName exists in the dataset,
150 ! get number of dimensions and
151 ! get the length of the dimensions
152 call get_info(variable, ncid, varid, vartype, get_ncdim, info = printflag, ndims = numdims)
153 if (present(ndims)) ndims = numdims
154 !
155 ! close File
156 call check(nf90_close(ncid))
157 !
158 end function get_ncdim
159
160 ! ------------------------------------------------------------------
161
162 !> \brief Name and size of variable in netcdf.
163
164 !> \details
165 !! Gets the name and size of the dimensions of a variable in a netcdf file
166 !!
167 !! \b Example
168 !!
169 !! Filename = 'test.nc'
170 !! Varname = 'data'
171 !! call Get_NcDimAtt(Filename, Varname, DimNames, DimLen)
172 !!
173 !! See example in test directory.
174 !!
175 !! \b Literature
176 !!
177 !! 1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
178 !!
179 !> \param[in] "character(len=*), intent(in) :: Filename" Filename of netcdf file.
180 !> \param[in] "character(len=*), intent(in) :: Variable" Variable name exactly as specified in the file.
181 !> \param[out] "integer(i4), dimension(:), allocatable, optional, intent(out) :: DimLen" Allocatable array with the size
182 !! of the dimensions
183 !> \retval "character(len=*), dimension(:), allocatable, intent(out) :: DimName" Allocatable array with the
184 !! names of the dimensions.
185
186 !> \note DimName and DimLen are both allocated within the subroutine, so please just provide an 1 dimensional
187 !! allocatable array to the subroutine.
188
189 !> \author Matthias Zink
190 !> \date Oct 2012
191
192 subroutine get_ncdimatt(Filename, Variable, DimName, DimLen)
193 !
194 implicit none
195 !
196 character(len = *), intent(in) :: filename
197 character(len = *), intent(in) :: variable
198 character(len = *), dimension(:), allocatable, &
199 intent(out) :: dimname
200 integer(i4), dimension(:), allocatable, &
201 optional, intent(out) :: dimlen
202 !
203 integer(i4), dimension(5) :: get_ncdim
204 !
205 integer(i4) :: ncid ! id of input stream
206 integer(i4) :: varid ! id of variable to be read
207 integer(i4) :: vartype ! type of variable
208 integer(i4) :: numdims ! # of dimensions
209 integer(i4) :: dimid
210 integer(i4) :: len
211 !
212 ! Open NetCDF filename
213 call check(nf90_open(filename, nf90_nowrite, ncid))
214 !
215 ! Inquire file and check if VarName exists in the dataset,
216 ! get number of dimensions and
217 ! get the length of the dimensions
218 call get_info(variable, ncid, varid, vartype, get_ncdim, info = .false., ndims = numdims)
219 !
220 allocate(dimname(numdims))
221 if (present(dimlen)) allocate(dimlen(numdims))
222 !
223 do dimid = 1, numdims
224 call check(nf90_inquire_dimension(ncid, dimid, dimname(dimid), len))
225 if (present(dimlen)) dimlen(dimid) = len
226 end do
227 ! close File
228 call check(nf90_close(ncid))
229 !
230 end subroutine get_ncdimatt
231
232 ! ------------------------------------------------------------------
233
234 !> \brief Get attribute of netcdf variable.
235
236 !> \details
237 !! Gets the values of an particular attribute of an variable.
238 !!
239 !! \b Example
240 !!
241 !! Filename = 'test.nc'
242 !! VarName = 'data'
243 !! AttName = 'units'
244 !! call Get_NcDimAtt(Filename, Varname, AttName, AttValues)
245 !!
246 !! See example in test directory.
247 !!
248 !! \b Literature
249 !!
250 !! 1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
251
252 !> \param[in] "character(len=*), intent(in) :: FileName" Filename of netcdf file.
253 !> \param[in] "character(len=*), intent(in) :: VarName" Variable name exactly as specified in the file.
254 !> \param[in] "character(len=*), intent(in) :: AttName" Attribute name exactly as specified for the Variable.
255 !> \param[in] "integer(i4), optional, intent(in) :: fid" File handle of opened netcdf file
256 !> \param[in] "integer(i4), optional, intent(in) :: dtype" Datatype (ineteger,float) see NetCDF convention
257 !! (unidata.ucar)
258 !> \retval "character(len=*), intent(out) :: AttValues" Values of the Attribute.
259
260 !> \note AttValues are restricted to be of character type. \n
261 !! The name of the variable (VarName) has to be known beforehand. \n
262 !! The name of the attribute (AttName) has to be known beforehand.
263
264 !> \author Matthias Zink
265 !> \date Oct 2012
266 !> \author Matthias Cuntz & Juliane Mai
267 !> \date Nov 2014
268 !! - correct data type detection.
269
270 subroutine get_ncvaratt(FileName, VarName, AttName, AttValues, fid, dtype)
271 !
272 implicit none
273 !
274 character(len = *), intent(in) :: filename
275 character(len = *), intent(in) :: varname
276 character(len = *), intent(in) :: attname
277 character(len = *), intent(out) :: attvalues
278 integer(i4), optional, intent(in) :: fid
279 integer(i4), optional, intent(out) :: dtype
280 !
281 integer(i4) :: ncid
282 integer(i4) :: varid
283 !
284 integer(i4) :: type
285 integer(i8) :: avint
286 real(dp) :: avfloat
287 character(256) :: avchar
288 !
289 if (present(fid)) then
290 ncid = fid
291 else
292 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
293 end if
294 !
295 ! Inquire file, check if VarName exists and get the id
296 call check(nf90_inq_varid(ncid, trim(varname), varid))
297 ! get type of the attribute
298 call check(nf90_inquire_attribute(ncid, varid, trim(attname), type))
299 !
300 ! read attribute by type
301 select case (type)
302 case (1) ! 1 = NF90_BYTE
303 call check(nf90_get_att(ncid, varid, trim(attname), avint))
304 write(attvalues, '(i4)') avint
305 attvalues = adjustl(trim(attvalues))
306 if (present(dtype)) dtype = type
307 case (2) ! NF90_CHAR
308 call check(nf90_get_att(ncid, varid, trim(attname), avchar))
309 attvalues = adjustl(trim(avchar))
310 if (present(dtype)) dtype = type
311 case (3) ! NF90_SHORT
312 call check(nf90_get_att(ncid, varid, trim(attname), avint))
313 write(attvalues, '(i6)') avint
314 attvalues = adjustl(trim(attvalues))
315 if (present(dtype)) dtype = type
316 case (4) ! NF90_INT
317 call check(nf90_get_att(ncid, varid, trim(attname), avint))
318 write(attvalues, '(i11)') avint
319 attvalues = adjustl(trim(attvalues))
320 if (present(dtype)) dtype = type
321 case (5) ! NF90_FLOAT
322 call check(nf90_get_att(ncid, varid, trim(attname), avfloat))
323 write(attvalues, '(E15.7)') avfloat
324 attvalues = adjustl(trim(attvalues))
325 if (present(dtype)) dtype = type
326 case (6) ! NF90_DOUBLE
327 call check(nf90_get_att(ncid, varid, trim(attname), avfloat))
328 write(attvalues, '(E24.15)') avfloat
329 attvalues = adjustl(trim(attvalues))
330 if (present(dtype)) dtype = type
331 case DEFAULT
332 print*, '***ERROR: mo_ncread: Mismatch in attribute datatype!'
333 end select
334 !
335 if (.not. present(fid)) call check(nf90_close(ncid))
336 !
337 end subroutine get_ncvaratt
338
339 ! ------------------------------------------------------------------------------
340
341 subroutine get_ncvar_0d_sp(Filename, VarName, Dat, fid)
342 !
343 implicit none
344 !
345 integer, parameter :: itype = 5 ! 5 = float, 6 = double
346 !
347 character(len = *), intent(in) :: filename
348 character(len = *), intent(in) :: varname ! Variable name
349 real(sp), intent(inout) :: dat ! array where values should be stored
350 integer(i4), optional, intent(in) :: fid
351 !
352 integer(i4) :: ncid ! id of input stream
353 integer(i4) :: varid ! id of variable to be read
354 integer(i4) :: vartype ! type of variable
355 !
356 ! Open NetCDF filename
357 if (present(fid)) then
358 ncid = fid
359 else
360 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
361 end if
362 !
363 ! Inquire file, check if VarName exists and get the id
364 call get_info(varname, ncid, varid, vartype)
365 ! check variable type ( 5 equals float type, 6 equals double )
366 if (vartype /= itype) then
367 print *, 'Variable name: ', trim(varname)
368 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
369 stop
370 end if
371 !
372 ! get values by varid
373 call check(nf90_get_var(ncid, varid, dat))
374 !
375 ! close File
376 if (.not. present(fid)) call check(nf90_close(ncid))
377 !
378 end subroutine get_ncvar_0d_sp
379
380 subroutine get_ncvar_0d_dp(Filename, VarName, Dat, fid)
381 !
382 implicit none
383 !
384 integer, parameter :: itype = 6 ! 5 = float, 6 = double
385 !
386 character(len = *), intent(in) :: filename
387 character(len = *), intent(in) :: varname ! Variable name
388 real(dp), intent(inout) :: dat ! array where values should be stored
389 integer(i4), optional, intent(in) :: fid
390 !
391 integer(i4) :: ncid ! id of input stream
392 integer(i4) :: varid ! id of variable to be read
393 integer(i4) :: vartype ! type of variable
394 !
395 ! Open NetCDF filename
396 if (present(fid)) then
397 ncid = fid
398 else
399 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
400 end if
401 !
402 ! Inquire file, check if VarName exists and get the id
403 call get_info(varname, ncid, varid, vartype)
404 ! check variable type ( 5 equals float type, 6 equals double )
405 if (vartype /= itype) then
406 print *, 'Variable name: ', trim(varname)
407 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
408 stop
409 end if
410 !
411 ! get values by varid
412 call check(nf90_get_var(ncid, varid, dat))
413 !
414 ! close File
415 if (.not. present(fid)) call check(nf90_close(ncid))
416 !
417 end subroutine get_ncvar_0d_dp
418
419 subroutine get_ncvar_1d_sp(Filename, VarName, Dat, start, a_count, fid)
420 !
421 implicit none
422 !
423 integer, parameter :: idims = 1
424 integer, parameter :: itype = 5 ! 5 = float, 6 = double
425 !
426 character(len = *), intent(in) :: filename
427 character(len = *), intent(in) :: varname ! Variable name
428 real(sp), dimension(:), allocatable, intent(inout) :: dat ! array where values should be stored
429 integer(i4), dimension(:), optional, intent(in) :: start
430 integer(i4), dimension(:), optional, intent(in) :: a_count
431 integer(i4), optional, intent(in) :: fid
432 !
433 integer(i4), dimension(5) :: rstart
434 integer(i4), dimension(5) :: rcount
435 integer(i4) :: ncid ! id of input stream
436 integer(i4) :: varid ! id of variable to be read
437 integer(i4) :: vartype ! type of variable
438 integer(i4) :: i
439 !
440 ! Defaults for Options Start and Count
441 rstart = 1
442 rcount = 1
443 !
444 ! allocate Dat
445 if (.not. allocated(dat)) then
446 if (.not. present(a_count)) then
447 rcount = get_ncdim(filename, varname)
448 else
449 rcount(1 : idims) = a_count(1 : idims)
450 end if
451 allocate(dat(rcount(1)))
452 else
453 rcount(1 : idims) = shape(dat)
454 end if
455 !
456 ! Assign options Start and Count if present
457 if (present(start)) then
458 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
459 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
460 rstart(1 : size(start)) = start
461 end if
462 !
463 if (present(a_count)) then
464 if (size(a_count) < size(shape(dat))) stop 'ERROR*** count has less values than data has dimensions. GetNcVar'
465 if (size(a_count) > 5) stop 'ERROR*** count has dimension greater than 5. GetNcVar'
466 rcount(1 : size(a_count)) = a_count
467 do i = 1, idims
468 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
469 end do
470 end if
471 !
472 ! Open NetCDF filename
473 if (present(fid)) then
474 ncid = fid
475 else
476 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
477 end if
478 !
479 ! Inquire file, check if VarName exists and get the id
480 call get_info(varname, ncid, varid, vartype)
481 ! check variable type ( 5 equals float type, 6 equals double )
482 if (vartype /= itype) then
483 print *, 'Variable name: ', trim(varname)
484 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
485 stop
486 end if
487 !
488 ! get values by varid
489 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
490 !
491 ! close File
492 if (.not. present(fid)) call check(nf90_close(ncid))
493 !
494 end subroutine get_ncvar_1d_sp
495
496
497 subroutine get_ncvar_1d_dp(Filename, VarName, Dat, start, a_count, fid)
498 !
499 implicit none
500 !
501 integer, parameter :: idims = 1
502 integer, parameter :: itype = 6 ! 5 = float, 6 = double
503 !
504 character(len = *), intent(in) :: filename
505 character(len = *), intent(in) :: varname ! Variable name
506 real(dp), dimension(:), allocatable, intent(inout) :: dat ! array where values should be stored
507 integer(i4), dimension(:), optional, intent(in) :: start
508 integer(i4), dimension(:), optional, intent(in) :: a_count
509 integer(i4), optional, intent(in) :: fid
510 !
511 integer(i4), dimension(5) :: rstart
512 integer(i4), dimension(5) :: rcount
513 integer(i4) :: ncid ! id of input stream
514 integer(i4) :: varid ! id of variable to be read
515 integer(i4) :: vartype ! type of variable
516 integer(i4) :: i
517 !
518 ! Defaults for Options Start and Count
519 rstart = 1
520 rcount = 1
521 !
522 ! allocate Dat
523 if (.not. allocated(dat)) then
524 if (.not. present(a_count)) then
525 rcount = get_ncdim(filename, varname)
526 else
527 rcount(1 : idims) = a_count(1 : idims)
528 end if
529 allocate(dat(rcount(1)))
530 else
531 rcount(1 : idims) = shape(dat)
532 end if
533 !
534 ! Assign options Start and Count if present
535 if (present(start)) then
536 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
537 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
538 rstart(1 : size(start)) = start
539 end if
540 !
541 if (present(a_count)) then
542 if (size(a_count) < size(shape(dat))) stop 'ERROR*** count has less values than data has dimensions. GetNcVar'
543 if (size(a_count) > 5) stop 'ERROR*** count has dimension greater than 5. GetNcVar'
544 rcount(1 : size(a_count)) = a_count
545 do i = 1, idims
546 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
547 end do
548 end if
549 !
550 ! Open NetCDF filename
551 if (present(fid)) then
552 ncid = fid
553 else
554 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
555 end if
556 !
557 ! Inquire file, check if VarName exists and get the id
558 call get_info(varname, ncid, varid, vartype)
559 ! check variable type ( 5 equals float type, 6 equals double )
560 if (vartype /= itype) then
561 print *, 'Variable name: ', trim(varname)
562 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
563 stop
564 end if
565 !
566 ! get values by varid
567 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
568 !
569 ! close File
570 if (.not. present(fid)) call check(nf90_close(ncid))
571 !
572 end subroutine get_ncvar_1d_dp
573
574
575 subroutine get_ncvar_2d_sp(Filename, VarName, Dat, start, a_count, fid)
576 !
577 implicit none
578 !
579 integer, parameter :: idims = 2
580 integer, parameter :: itype = 5 ! 5 = float, 6 = double
581 !
582 character(len = *), intent(in) :: filename
583 character(len = *), intent(in) :: varname ! Variable name
584 !real(sp), dimension(:,:), allocatable, intent(inout) :: Dat ! array where values should be stored
585 real(sp), dimension(:, :), allocatable, intent(inout) :: dat ! array where values should be stored
586 integer(i4), dimension(:), optional, intent(in) :: start
587 integer(i4), dimension(:), optional, intent(in) :: a_count
588 integer(i4), optional, intent(in) :: fid
589 !
590 integer(i4), dimension(5) :: rstart
591 integer(i4), dimension(5) :: rcount
592 integer(i4) :: ncid ! id of input stream
593 integer(i4) :: varid ! id of variable to be read
594 integer(i4) :: vartype ! type of variable
595 integer(i4) :: i
596 !
597 ! Defaults for Options Start and Count
598 rstart = 1
599 rcount = 1
600 !
601 ! allocate Dat
602 if (.not. allocated(dat)) then
603 if (.not. present(a_count)) then
604 rcount = get_ncdim(filename, varname)
605 else
606 rcount(1 : idims) = a_count(1 : idims)
607 end if
608 allocate(dat(rcount(1), rcount(2)))
609 else
610 rcount(1 : idims) = shape(dat)
611 end if
612 !
613 ! Assign options Start and Count if present
614 if (present(start)) then
615 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
616 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
617 rstart(1 : size(start)) = start
618 end if
619 !
620 if (present(a_count)) then
621 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
622 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
623 rcount(1 : size(a_count)) = a_count
624 do i = 1, idims
625 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
626 end do
627 end if
628 !
629 ! Open NetCDF filename
630 if (present(fid)) then
631 ncid = fid
632 else
633 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
634 end if
635 !
636 ! Inquire file, check if VarName exists and get the id
637 call get_info(varname, ncid, varid, vartype)
638 ! check variable type ( 5 equals float type, 6 equals double )
639 if (vartype /= itype) then
640 print *, 'Variable name: ', trim(varname)
641 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
642 stop
643 end if
644 !
645 ! get values by varid
646 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
647 !
648 ! close File
649 if (.not. present(fid)) call check(nf90_close(ncid))
650 !
651 end subroutine get_ncvar_2d_sp
652
653
654 subroutine get_ncvar_2d_dp(Filename, VarName, Dat, start, a_count, fid)
655 !
656 implicit none
657 !
658 integer, parameter :: idims = 2
659 integer, parameter :: itype = 6 ! 5 = float, 6 = double
660 !
661 character(len = *), intent(in) :: filename
662 character(len = *), intent(in) :: varname ! Variable name
663 real(dp), dimension(:, :), allocatable, intent(inout) :: dat ! array where values should be stored
664 integer(i4), dimension(:), optional, intent(in) :: start
665 integer(i4), dimension(:), optional, intent(in) :: a_count
666 integer(i4), optional, intent(in) :: fid
667 !
668 integer(i4), dimension(5) :: rstart
669 integer(i4), dimension(5) :: rcount
670 integer(i4) :: ncid ! id of input stream
671 integer(i4) :: varid ! id of variable to be read
672 integer(i4) :: vartype ! type of variable
673 integer(i4) :: i
674 !
675 ! Defaults for Options Start and Count
676 rstart = 1
677 rcount = 1
678 !
679 ! allocate Dat
680 if (.not. allocated(dat)) then
681 if (.not. present(a_count)) then
682 rcount = get_ncdim(filename, varname)
683 else
684 rcount(1 : idims) = a_count(1 : idims)
685 end if
686 allocate(dat(rcount(1), rcount(2)))
687 else
688 rcount(1 : idims) = shape(dat)
689 end if
690 !
691 ! Assign options Start and Count if present
692 if (present(start)) then
693 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
694 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
695 rstart(1 : size(start)) = start
696 end if
697 !
698 if (present(a_count)) then
699 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
700 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
701 rcount(1 : size(a_count)) = a_count
702 do i = 1, idims
703 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
704 end do
705 end if
706 !
707 ! Open NetCDF filename
708 if (present(fid)) then
709 ncid = fid
710 else
711 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
712 end if
713 !
714 ! Inquire file, check if VarName exists and get the id
715 call get_info(varname, ncid, varid, vartype)
716 ! check variable type ( 5 equals float type, 6 equals double )
717 if (vartype /= itype) then
718 print *, 'Variable name: ', trim(varname)
719 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
720 stop
721 end if
722 !
723 ! get values by varid
724 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
725 !
726 ! close File
727 if (.not. present(fid)) call check(nf90_close(ncid))
728 !
729 end subroutine get_ncvar_2d_dp
730
731
732 subroutine get_ncvar_3d_sp(Filename, VarName, Dat, start, a_count, fid)
733 !
734 implicit none
735 !
736 integer, parameter :: idims = 3
737 integer, parameter :: itype = 5 ! 5 = float, 6 = double
738 !
739 character(len = *), intent(in) :: filename
740 character(len = *), intent(in) :: varname ! Variable name
741 real(sp), dimension(:, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
742 integer(i4), dimension(:), optional, intent(in) :: start
743 integer(i4), dimension(:), optional, intent(in) :: a_count
744 integer(i4), optional, intent(in) :: fid
745 !
746 integer(i4), dimension(5) :: rstart
747 integer(i4), dimension(5) :: rcount
748 integer(i4) :: ncid ! id of input stream
749 integer(i4) :: varid ! id of variable to be read
750 integer(i4) :: vartype ! type of variable
751 integer(i4) :: i
752
753 !
754 ! Defaults for Options Start and Count
755 rstart = 1
756 rcount = 1
757 !
758 ! allocate Dat
759 if (.not. allocated(dat)) then
760 if (.not. present(a_count)) then
761 rcount = get_ncdim(filename, varname)
762 else
763 rcount(1 : idims) = a_count(1 : idims)
764 end if
765 allocate(dat(rcount(1), rcount(2), rcount(3)))
766 else
767 rcount(1 : idims) = shape(dat)
768 end if
769 !
770 ! Assign options Start and Count if present
771 if (present(start)) then
772 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
773 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
774 rstart(1 : size(start)) = start
775 end if
776 !
777 if (present(a_count)) then
778 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
779 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
780 rcount(1 : size(a_count)) = a_count
781 do i = 1, idims
782 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
783 end do
784 end if
785
786 !
787 ! Open NetCDF filename
788 if (present(fid)) then
789 ncid = fid
790 else
791 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
792 end if
793 !
794 ! Inquire file, check if VarName exists and get the id
795 call get_info(varname, ncid, varid, vartype)
796 ! check variable type ( 5 equals float type, 6 equals double )
797 if (vartype /= itype) then
798 print *, 'Variable name: ', trim(varname)
799 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
800 stop
801 end if
802 !
803 ! get values by varid
804 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
805 !
806 ! close File
807 if (.not. present(fid)) call check(nf90_close(ncid))
808 !
809 end subroutine get_ncvar_3d_sp
810
811
812 subroutine get_ncvar_3d_dp(Filename, VarName, Dat, start, a_count, fid)
813 !
814 implicit none
815 !
816 integer, parameter :: idims = 3
817 integer, parameter :: itype = 6 ! 5 = float, 6 = double
818 !
819 character(len = *), intent(in) :: filename
820 character(len = *), intent(in) :: varname ! Variable name
821 real(dp), dimension(:, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
822 integer(i4), dimension(:), optional, intent(in) :: start
823 integer(i4), dimension(:), optional, intent(in) :: a_count
824 integer(i4), optional, intent(in) :: fid
825 !
826 integer(i4), dimension(5) :: rstart
827 integer(i4), dimension(5) :: rcount
828 integer(i4) :: ncid ! id of input stream
829 integer(i4) :: varid ! id of variable to be read
830 integer(i4) :: vartype ! type of variable
831 integer(i4) :: i
832 !
833 ! Defaults for Options Start and Count
834 rstart = 1
835 rcount = 1
836 !
837 ! allocate Dat
838 if (.not. allocated(dat)) then
839 if (.not. present(a_count)) then
840 rcount = get_ncdim(filename, varname)
841 else
842 rcount(1 : idims) = a_count(1 : idims)
843 end if
844 allocate(dat(rcount(1), rcount(2), rcount(3)))
845 else
846 rcount(1 : idims) = shape(dat)
847 end if
848 !
849 ! Assign options Start and Count if present
850 if (present(start)) then
851 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
852 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
853 rstart(1 : size(start)) = start
854 end if
855 !
856 if (present(a_count)) then
857 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
858 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
859 rcount(1 : size(a_count)) = a_count
860 do i = 1, idims
861 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
862 end do
863 end if
864 !
865 ! Open NetCDF filename
866 if (present(fid)) then
867 ncid = fid
868 else
869 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
870 end if
871 !
872 ! Inquire file, check if VarName exists and get the id
873 call get_info(varname, ncid, varid, vartype)
874 ! check variable type ( 5 equals float type, 6 equals double )
875 if (vartype /= itype) then
876 print *, 'Variable name: ', trim(varname)
877 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
878 stop
879 end if
880 !
881 ! get values by varid
882 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
883 !
884 ! close File
885 if (.not. present(fid)) call check(nf90_close(ncid))
886 !
887 end subroutine get_ncvar_3d_dp
888
889
890 subroutine get_ncvar_4d_sp(Filename, VarName, Dat, start, a_count, fid)
891 !
892 implicit none
893 !
894 integer, parameter :: idims = 4
895 integer, parameter :: itype = 5 ! 5 = float, 6 = double
896 !
897 character(len = *), intent(in) :: filename
898 character(len = *), intent(in) :: varname ! Variable name
899 real(sp), dimension(:, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
900 integer(i4), dimension(:), optional, intent(in) :: start
901 integer(i4), dimension(:), optional, intent(in) :: a_count
902 integer(i4), optional, intent(in) :: fid
903 !
904 integer(i4), dimension(5) :: rstart
905 integer(i4), dimension(5) :: rcount
906 integer(i4) :: ncid ! id of input stream
907 integer(i4) :: varid ! id of variable to be read
908 integer(i4) :: vartype ! type of variable
909 integer(i4) :: i
910 !
911 ! Defaults for Options Start and Count
912 rstart = 1
913 rcount = 1
914 !
915 ! allocate Dat
916 if (.not. allocated(dat)) then
917 if (.not. present(a_count)) then
918 rcount = get_ncdim(filename, varname)
919 else
920 rcount(1 : idims) = a_count(1 : idims)
921 end if
922 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4)))
923 else
924 rcount(1 : idims) = shape(dat)
925 end if
926 !
927 ! Assign options Start and Count if present
928 if (present(start)) then
929 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
930 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
931 rstart(1 : size(start)) = start
932 end if
933 !
934 if (present(a_count)) then
935 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
936 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
937 rcount(1 : size(a_count)) = a_count
938 do i = 1, idims
939 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
940 end do
941 end if
942 !
943 ! Open NetCDF filename
944 if (present(fid)) then
945 ncid = fid
946 else
947 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
948 end if
949 !
950 ! Inquire file, check if VarName exists and get the id
951 call get_info(varname, ncid, varid, vartype)
952 ! check variable type ( 5 equals float type, 6 equals double )
953 if (vartype /= itype) then
954 print *, 'Variable name: ', trim(varname)
955 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
956 stop
957 end if
958 !
959 ! get values by varid
960 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
961 !
962 ! close File
963 if (.not. present(fid)) call check(nf90_close(ncid))
964 !
965 end subroutine get_ncvar_4d_sp
966
967
968 subroutine get_ncvar_4d_dp(Filename, VarName, Dat, start, a_count, fid)
969 !
970 implicit none
971 !
972 integer, parameter :: idims = 4
973 integer, parameter :: itype = 6 ! 5 = float, 6 = double
974 !
975 character(len = *), intent(in) :: filename
976 character(len = *), intent(in) :: varname ! Variable name
977 real(dp), dimension(:, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
978 integer(i4), dimension(:), optional, intent(in) :: start
979 integer(i4), dimension(:), optional, intent(in) :: a_count
980 integer(i4), optional, intent(in) :: fid
981 !
982 integer(i4), dimension(5) :: rstart
983 integer(i4), dimension(5) :: rcount
984 integer(i4) :: ncid ! id of input stream
985 integer(i4) :: varid ! id of variable to be read
986 integer(i4) :: vartype ! type of variable
987 integer(i4) :: i
988 !
989 ! Defaults for Options Start and Count
990 rstart = 1
991 rcount = 1
992 !
993 ! allocate Dat
994 if (.not. allocated(dat)) then
995 if (.not. present(a_count)) then
996 rcount = get_ncdim(filename, varname)
997 else
998 rcount(1 : idims) = a_count(1 : idims)
999 end if
1000 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4)))
1001 else
1002 rcount(1 : idims) = shape(dat)
1003 end if
1004 !
1005 ! Assign options Start and Count if present
1006 if (present(start)) then
1007 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
1008 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
1009 rstart(1 : size(start)) = start
1010 end if
1011 !
1012 if (present(a_count)) then
1013 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
1014 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
1015 rcount(1 : size(a_count)) = a_count
1016 do i = 1, idims
1017 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
1018 end do
1019 end if
1020 !
1021 ! Open NetCDF filename
1022 if (present(fid)) then
1023 ncid = fid
1024 else
1025 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1026 end if
1027 !
1028 ! Inquire file, check if VarName exists and get the id
1029 call get_info(varname, ncid, varid, vartype)
1030 ! check variable type ( 5 equals float type, 6 equals double )
1031 if (vartype /= itype) then
1032 print *, 'Variable name: ', trim(varname)
1033 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1034 stop
1035 end if
1036 !
1037 ! get values by varid
1038 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1039 !
1040 ! close File
1041 if (.not. present(fid)) call check(nf90_close(ncid))
1042 !
1043 end subroutine get_ncvar_4d_dp
1044
1045
1046 subroutine get_ncvar_5d_sp(Filename, VarName, Dat, start, a_count, fid)
1047 !
1048 implicit none
1049 !
1050 integer, parameter :: idims = 5
1051 integer, parameter :: itype = 5 ! 5 = float, 6 = double
1052 !
1053 character(len = *), intent(in) :: filename
1054 character(len = *), intent(in) :: varname ! Variable name
1055 real(sp), dimension(:, :, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1056 integer(i4), dimension(:), optional, intent(in) :: start
1057 integer(i4), dimension(:), optional, intent(in) :: a_count
1058 integer(i4), optional, intent(in) :: fid
1059 !
1060 integer(i4), dimension(5) :: rstart
1061 integer(i4), dimension(5) :: rcount
1062 integer(i4) :: ncid ! id of input stream
1063 integer(i4) :: varid ! id of variable to be read
1064 integer(i4) :: vartype ! type of variable
1065 integer(i4) :: i
1066 !
1067 ! Defaults for Options Start and Count
1068 rstart = 1
1069 rcount = 1
1070 !
1071 ! allocate Dat
1072 if (.not. allocated(dat)) then
1073 if (.not. present(a_count)) then
1074 rcount = get_ncdim(filename, varname)
1075 else
1076 rcount(1 : idims) = a_count(1 : idims)
1077 end if
1078 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4), rcount(5)))
1079 else
1080 rcount(1 : idims) = shape(dat)
1081 end if
1082 !
1083 ! Assign options Start and Count if present
1084 if (present(start)) then
1085 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
1086 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
1087 rstart(1 : size(start)) = start
1088 end if
1089 !
1090 if (present(a_count)) then
1091 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
1092 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
1093 rcount(1 : size(a_count)) = a_count
1094 do i = 1, idims
1095 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
1096 end do
1097 end if
1098 !
1099 ! Open NetCDF filename
1100 if (present(fid)) then
1101 ncid = fid
1102 else
1103 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1104 end if
1105 !
1106 ! Inquire file, check if VarName exists and get the id
1107 call get_info(varname, ncid, varid, vartype)
1108 ! check variable type ( 5 equals float type, 6 equals double )
1109 if (vartype /= itype) then
1110 print *, 'Variable name: ', trim(varname)
1111 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1112 stop
1113 end if
1114 !
1115 ! get values by varid
1116 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1117 !
1118 ! close File
1119 if (.not. present(fid)) call check(nf90_close(ncid))
1120 !
1121 end subroutine get_ncvar_5d_sp
1122
1123
1124 subroutine get_ncvar_5d_dp(Filename, VarName, Dat, start, a_count, fid)
1125 !
1126 implicit none
1127 !
1128 integer, parameter :: idims = 5
1129 integer, parameter :: itype = 6 ! 5 = float, 6 = double
1130 !
1131 character(len = *), intent(in) :: filename
1132 character(len = *), intent(in) :: varname ! Variable name
1133 real(dp), dimension(:, :, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1134 integer(i4), dimension(:), optional, intent(in) :: start
1135 integer(i4), dimension(:), optional, intent(in) :: a_count
1136 integer(i4), optional, intent(in) :: fid
1137 !
1138 integer(i4), dimension(5) :: rstart
1139 integer(i4), dimension(5) :: rcount
1140 integer(i4) :: ncid ! id of input stream
1141 integer(i4) :: varid ! id of variable to be read
1142 integer(i4) :: vartype ! type of variable
1143 integer(i4) :: i
1144 !
1145 ! Defaults for Options Start and Count
1146 rstart = 1
1147 rcount = 1
1148 !
1149 ! allocate Dat
1150 if (.not. allocated(dat)) then
1151 if (.not. present(a_count)) then
1152 rcount = get_ncdim(filename, varname)
1153 else
1154 rcount(1 : idims) = a_count(1 : idims)
1155 end if
1156 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4), rcount(5)))
1157 else
1158 rcount(1 : idims) = shape(dat)
1159 end if
1160 !
1161 ! Assign options Start and Count if present
1162 if (present(start)) then
1163 if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
1164 if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
1165 rstart(1 : size(start)) = start
1166 end if
1167 !
1168 if (present(a_count)) then
1169 if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
1170 if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
1171 rcount(1 : size(a_count)) = a_count
1172 do i = 1, idims
1173 if (size(dat, i) < rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
1174 end do
1175 end if
1176 !
1177 ! Open NetCDF filename
1178 if (present(fid)) then
1179 ncid = fid
1180 else
1181 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1182 end if
1183 !
1184 ! Inquire file, check if VarName exists and get the id
1185 call get_info(varname, ncid, varid, vartype)
1186 ! check variable type ( 5 equals float type, 6 equals double )
1187 if (vartype /= itype) then
1188 print *, 'Variable name: ', trim(varname)
1189 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1190 stop
1191 end if
1192 !
1193 ! get values by varid
1194 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1195 !
1196 ! close File
1197 if (.not. present(fid)) call check(nf90_close(ncid))
1198 !
1199 end subroutine get_ncvar_5d_dp
1200
1201
1202 subroutine get_ncvar_0d_i4(Filename, VarName, Dat, fid)
1203 !
1204 implicit none
1205 !
1206 integer, parameter :: itype = 4 ! 5 = float, 6 = double
1207 !
1208 character(len = *), intent(in) :: filename
1209 character(len = *), intent(in) :: varname ! Variable name
1210 integer(i4), intent(inout) :: dat ! array where values should be stored
1211 integer(i4), optional, intent(in) :: fid
1212 !
1213 integer(i4) :: ncid ! id of input stream
1214 integer(i4) :: varid ! id of variable to be read
1215 integer(i4) :: vartype ! type of variable
1216 !
1217 ! Open NetCDF filename
1218 if (present(fid)) then
1219 ncid = fid
1220 else
1221 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1222 end if
1223 !
1224 ! Inquire file, check if VarName exists and get the id
1225 call get_info(varname, ncid, varid, vartype)
1226 ! check variable type ( 5 equals float type, 6 equals double )
1227 if (vartype /= itype) then
1228 print *, 'Variable name: ', trim(varname)
1229 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1230 stop
1231 end if
1232 !
1233 ! get values by varid
1234 call check(nf90_get_var(ncid, varid, dat))
1235 !
1236 ! close File
1237 if (.not. present(fid)) call check(nf90_close(ncid))
1238 !
1239 end subroutine get_ncvar_0d_i4
1240
1241
1242 subroutine get_ncvar_1d_i4(Filename, VarName, Dat, start, a_count, fid)
1243 !
1244 implicit none
1245 !
1246 integer, parameter :: idims = 1
1247 integer, parameter :: itype = 4 ! 3 = single 5 = float, 6 = double
1248 !
1249 character(len = *), intent(in) :: filename
1250 character(len = *), intent(in) :: varname ! Variable name
1251 integer(i4), dimension(:), allocatable, intent(inout) :: dat ! array where values should be stored
1252 integer(i4), dimension(:), optional, intent(in) :: start
1253 integer(i4), dimension(:), optional, intent(in) :: a_count
1254 integer(i4), optional, intent(in) :: fid
1255 !
1256 integer(i4), dimension(5) :: rstart
1257 integer(i4), dimension(5) :: rcount
1258 integer(i4) :: ncid ! id of input stream
1259 integer(i4) :: varid ! id of variable to be read
1260 integer(i4) :: vartype ! type of variable
1261 integer(i4) :: i
1262 !
1263 ! Defaults for Options Start and Count
1264 rstart = 1
1265 rcount = 1
1266 !
1267 ! allocate Dat
1268 if (.not. allocated(dat)) then
1269 if (.not. present(a_count)) then
1270 rcount = get_ncdim(filename, varname)
1271 else
1272 rcount(1 : idims) = a_count(1 : idims)
1273 end if
1274 allocate(dat(rcount(1)))
1275 else
1276 rcount(1 : idims) = shape(dat)
1277 end if
1278 !
1279 ! Assign options Start and Count if present
1280 if (present(start)) then
1281 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1282 rstart = start
1283 end if
1284 !
1285 if (present(a_count)) then
1286 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1287 rcount = a_count
1288 do i = 1, idims
1289 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1290 end do
1291 end if
1292 !
1293 ! Open NetCDF filename
1294 if (present(fid)) then
1295 ncid = fid
1296 else
1297 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1298 end if
1299 !
1300 ! Inquire file, check if VarName exists and get the id
1301 call get_info(varname, ncid, varid, vartype)
1302 ! check variable type ( 5 equals float type, 6 equals double )
1303 if (vartype /= itype) then
1304 print *, 'Variable name: ', trim(varname)
1305 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1306 stop
1307 end if
1308 !
1309 ! get values by varid
1310 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1311 !
1312 ! close File
1313 if (.not. present(fid)) call check(nf90_close(ncid))
1314 !
1315 end subroutine get_ncvar_1d_i4
1316
1317
1318 subroutine get_ncvar_2d_i4(Filename, VarName, Dat, start, a_count, fid)
1319 !
1320 implicit none
1321 !
1322 integer, parameter :: idims = 2
1323 integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
1324 !
1325 character(len = *), intent(in) :: filename
1326 character(len = *), intent(in) :: varname ! Variable name
1327 integer(i4), dimension(:, :), allocatable, intent(inout) :: dat ! array where values should be stored
1328 integer(i4), dimension(:), optional, intent(in) :: start
1329 integer(i4), dimension(:), optional, intent(in) :: a_count
1330 integer(i4), optional, intent(in) :: fid
1331 !
1332 integer(i4), dimension(5) :: rstart
1333 integer(i4), dimension(5) :: rcount
1334 integer(i4) :: ncid ! id of input stream
1335 integer(i4) :: varid ! id of variable to be read
1336 integer(i4) :: vartype ! type of variable
1337 integer(i4) :: i
1338 !
1339 ! Defaults for Options Start and Count
1340 rstart = 1
1341 rcount = 1
1342 !
1343 ! allocate Dat
1344 if (.not. allocated(dat)) then
1345 if (.not. present(a_count)) then
1346 rcount = get_ncdim(filename, varname)
1347 else
1348 rcount(1 : idims) = a_count(1 : idims)
1349 end if
1350 allocate(dat(rcount(1), rcount(2)))
1351 else
1352 rcount(1 : idims) = shape(dat)
1353 end if
1354 !
1355 ! Assign options Start and Count if present
1356 if (present(start)) then
1357 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1358 rstart = start
1359 end if
1360 !
1361 if (present(a_count)) then
1362 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1363 rcount = a_count
1364 do i = 1, idims
1365 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1366 end do
1367 end if
1368 !
1369 ! Open NetCDF filename
1370 if (present(fid)) then
1371 ncid = fid
1372 else
1373 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1374 end if
1375 !
1376 ! Inquire file, check if VarName exists and get the id
1377 call get_info(varname, ncid, varid, vartype)
1378 ! check variable type ( 5 equals float type, 6 equals double )
1379 if (vartype /= itype) then
1380 print *, 'Variable name: ', trim(varname)
1381 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1382 stop
1383 end if
1384 !
1385 ! get values by varid
1386 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1387 !
1388 ! close File
1389 if (.not. present(fid)) call check(nf90_close(ncid))
1390 !
1391 end subroutine get_ncvar_2d_i4
1392
1393
1394 subroutine get_ncvar_3d_i4(Filename, VarName, Dat, start, a_count, fid)
1395 !
1396 implicit none
1397 !
1398 integer, parameter :: idims = 3
1399 integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
1400 !
1401 character(len = *), intent(in) :: filename
1402 character(len = *), intent(in) :: varname ! Variable name
1403 integer(i4), dimension(:, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1404 integer(i4), dimension(:), optional, intent(in) :: start
1405 integer(i4), dimension(:), optional, intent(in) :: a_count
1406 integer(i4), optional, intent(in) :: fid
1407 !
1408 integer(i4), dimension(5) :: rstart
1409 integer(i4), dimension(5) :: rcount
1410 integer(i4) :: ncid ! id of input stream
1411 integer(i4) :: varid ! id of variable to be read
1412 integer(i4) :: vartype ! type of variable
1413 integer(i4) :: i
1414 !
1415 ! Defaults for Options Start and Count
1416 rstart = 1
1417 rcount = 1
1418 !
1419 ! allocate Dat
1420 if (.not. allocated(dat)) then
1421 if (.not. present(a_count)) then
1422 rcount = get_ncdim(filename, varname)
1423 else
1424 rcount(1 : idims) = a_count(1 : idims)
1425 end if
1426 allocate(dat(rcount(1), rcount(2), rcount(3)))
1427 else
1428 rcount(1 : idims) = shape(dat)
1429 end if
1430 !
1431 ! Assign options Start and Count if present
1432 if (present(start)) then
1433 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1434 rstart = start
1435 end if
1436 !
1437 if (present(a_count)) then
1438 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1439 rcount = a_count
1440 do i = 1, idims
1441 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1442 end do
1443 end if
1444 !
1445 ! Open NetCDF filename
1446 if (present(fid)) then
1447 ncid = fid
1448 else
1449 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1450 end if
1451 !
1452 ! Inquire file, check if VarName exists and get the id
1453 call get_info(varname, ncid, varid, vartype)
1454 ! check variable type ( 5 equals float type, 6 equals double )
1455 if (vartype /= itype) then
1456 print *, 'Variable name: ', trim(varname)
1457 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1458 stop
1459 end if
1460 !
1461 ! get values by varid
1462 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1463 !
1464 ! close File
1465 if (.not. present(fid)) call check(nf90_close(ncid))
1466 !
1467 end subroutine get_ncvar_3d_i4
1468
1469
1470 subroutine get_ncvar_4d_i4(Filename, VarName, Dat, start, a_count, fid)
1471 !
1472 implicit none
1473 !
1474 integer, parameter :: idims = 4
1475 integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
1476 !
1477 character(len = *), intent(in) :: filename
1478 character(len = *), intent(in) :: varname ! Variable name
1479 integer(i4), dimension(:, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1480 integer(i4), dimension(:), optional, intent(in) :: start
1481 integer(i4), dimension(:), optional, intent(in) :: a_count
1482 integer(i4), optional, intent(in) :: fid
1483 !
1484 integer(i4), dimension(5) :: rstart
1485 integer(i4), dimension(5) :: rcount
1486 integer(i4) :: ncid ! id of input stream
1487 integer(i4) :: varid ! id of variable to be read
1488 integer(i4) :: vartype ! type of variable
1489 integer(i4) :: i
1490 !
1491 ! Defaults for Options Start and Count
1492 rstart = 1
1493 rcount = 1
1494 !
1495 ! allocate Dat
1496 if (.not. allocated(dat)) then
1497 if (.not. present(a_count)) then
1498 rcount = get_ncdim(filename, varname)
1499 else
1500 rcount(1 : idims) = a_count(1 : idims)
1501 end if
1502 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4)))
1503 else
1504 rcount(1 : idims) = shape(dat)
1505 end if
1506 !
1507 ! Assign options Start and Count if present
1508 if (present(start)) then
1509 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1510 rstart = start
1511 end if
1512 !
1513 if (present(a_count)) then
1514 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1515 rcount = a_count
1516 do i = 1, idims
1517 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1518 end do
1519 end if
1520 !
1521 ! Open NetCDF filename
1522 if (present(fid)) then
1523 ncid = fid
1524 else
1525 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1526 end if
1527 !
1528 ! Inquire file, check if VarName exists and get the id
1529 call get_info(varname, ncid, varid, vartype)
1530 ! check variable type ( 5 equals float type, 6 equals double )
1531 if (vartype /= itype) then
1532 print *, 'Variable name: ', trim(varname)
1533 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1534 stop
1535 end if
1536 !
1537 ! get values by varid
1538 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1539 !
1540 ! close File
1541 if (.not. present(fid)) call check(nf90_close(ncid))
1542 !
1543 end subroutine get_ncvar_4d_i4
1544
1545
1546 subroutine get_ncvar_5d_i4(Filename, VarName, Dat, start, a_count, fid)
1547 !
1548 implicit none
1549 !
1550 integer, parameter :: idims = 5
1551 integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
1552 !
1553 character(len = *), intent(in) :: filename
1554 character(len = *), intent(in) :: varname ! Variable name
1555 integer(i4), dimension(:, :, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1556 integer(i4), dimension(:), optional, intent(in) :: start
1557 integer(i4), dimension(:), optional, intent(in) :: a_count
1558 integer(i4), optional, intent(in) :: fid
1559 !
1560 integer(i4), dimension(5) :: rstart
1561 integer(i4), dimension(5) :: rcount
1562 integer(i4) :: ncid ! id of input stream
1563 integer(i4) :: varid ! id of variable to be read
1564 integer(i4) :: vartype ! type of variable
1565 integer(i4) :: i
1566 !
1567 ! Defaults for Options Start and Count
1568 rstart = 1
1569 rcount = 1
1570 !
1571 ! allocate Dat
1572 if (.not. allocated(dat)) then
1573 if (.not. present(a_count)) then
1574 rcount = get_ncdim(filename, varname)
1575 else
1576 rcount(1 : idims) = a_count(1 : idims)
1577 end if
1578 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4), rcount(5)))
1579 else
1580 rcount(1 : idims) = shape(dat)
1581 end if
1582 !
1583 ! Assign options Start and Count if present
1584 if (present(start)) then
1585 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1586 rstart = start
1587 end if
1588 !
1589 if (present(a_count)) then
1590 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1591 rcount = a_count
1592 do i = 1, idims
1593 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1594 end do
1595 end if
1596 !
1597 ! Open NetCDF filename
1598 if (present(fid)) then
1599 ncid = fid
1600 else
1601 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1602 end if
1603 !
1604 ! Inquire file, check if VarName exists and get the id
1605 call get_info(varname, ncid, varid, vartype)
1606 ! check variable type ( 5 equals float type, 6 equals double )
1607 if (vartype /= itype) then
1608 print *, 'Variable name: ', trim(varname)
1609 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1610 stop
1611 end if
1612 !
1613 ! get values by varid
1614 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1615 !
1616 ! close File
1617 if (.not. present(fid)) call check(nf90_close(ncid))
1618 !
1619 end subroutine get_ncvar_5d_i4
1620
1621 subroutine get_ncvar_0d_i1(Filename, VarName, Dat, fid)
1622 !
1623 implicit none
1624 !
1625 integer, parameter :: itype = 1 ! 5 = float, 6 = double
1626 !
1627 character(len = *), intent(in) :: filename
1628 character(len = *), intent(in) :: varname ! Variable name
1629 integer(1), intent(inout) :: dat ! array where values should be stored
1630 integer(i4), optional, intent(in) :: fid
1631 !
1632 integer(i4) :: ncid ! id of input stream
1633 integer(i4) :: varid ! id of variable to be read
1634 integer(i4) :: vartype ! type of variable
1635 !
1636 ! Open NetCDF filename
1637 if (present(fid)) then
1638 ncid = fid
1639 else
1640 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1641 end if
1642 !
1643 ! Inquire file, check if VarName exists and get the id
1644 call get_info(varname, ncid, varid, vartype)
1645 ! check variable type ( 5 equals float type, 6 equals double )
1646 if (vartype /= itype) then
1647 print *, 'Variable name: ', trim(varname)
1648 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1649 stop
1650 end if
1651 !
1652 ! get values by varid
1653 call check(nf90_get_var(ncid, varid, dat))
1654 !
1655 ! close File
1656 if (.not. present(fid)) call check(nf90_close(ncid))
1657 !
1658 end subroutine get_ncvar_0d_i1
1659
1660
1661 subroutine get_ncvar_1d_i1(Filename, VarName, Dat, start, a_count, fid)
1662 !
1663 implicit none
1664 !
1665 integer, parameter :: idims = 1
1666 integer, parameter :: itype = 1 ! 3 = single 5 = float, 6 = double
1667 !
1668 character(len = *), intent(in) :: filename
1669 character(len = *), intent(in) :: varname ! Variable name
1670 integer(1), dimension(:), allocatable, intent(inout) :: dat ! array where values should be stored
1671 integer(i4), dimension(:), optional, intent(in) :: start
1672 integer(i4), dimension(:), optional, intent(in) :: a_count
1673 integer(i4), optional, intent(in) :: fid
1674 !
1675 integer(i4), dimension(5) :: rstart
1676 integer(i4), dimension(5) :: rcount
1677 integer(i4) :: ncid ! id of input stream
1678 integer(i4) :: varid ! id of variable to be read
1679 integer(i4) :: vartype ! type of variable
1680 integer(i4) :: i
1681 !
1682 ! Defaults for Options Start and Count
1683 rstart = 1
1684 rcount = 1
1685 !
1686 ! allocate Dat
1687 if (.not. allocated(dat)) then
1688 if (.not. present(a_count)) then
1689 rcount = get_ncdim(filename, varname)
1690 else
1691 rcount(1 : idims) = a_count(1 : idims)
1692 end if
1693 allocate(dat(rcount(1)))
1694 else
1695 rcount(1 : idims) = shape(dat)
1696 end if
1697 !
1698 ! Assign options Start and Count if present
1699 if (present(start)) then
1700 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1701 rstart = start
1702 end if
1703 !
1704 if (present(a_count)) then
1705 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1706 rcount = a_count
1707 do i = 1, idims
1708 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1709 end do
1710 end if
1711 !
1712 ! Open NetCDF filename
1713 if (present(fid)) then
1714 ncid = fid
1715 else
1716 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1717 end if
1718 !
1719 ! Inquire file, check if VarName exists and get the id
1720 call get_info(varname, ncid, varid, vartype)
1721 ! check variable type ( 5 equals float type, 6 equals double )
1722 if (vartype /= itype) then
1723 print *, 'Variable name: ', trim(varname)
1724 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1725 stop
1726 end if
1727 !
1728 ! get values by varid
1729 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1730 !
1731 ! close File
1732 if (.not. present(fid)) call check(nf90_close(ncid))
1733 !
1734 end subroutine get_ncvar_1d_i1
1735
1736
1737 subroutine get_ncvar_2d_i1(Filename, VarName, Dat, start, a_count, fid)
1738 !
1739 implicit none
1740 !
1741 integer, parameter :: idims = 2
1742 integer, parameter :: itype = 1 ! 1 = 1 byte, 3 = single, 5 = float, 6 = double
1743 !
1744 character(len = *), intent(in) :: filename
1745 character(len = *), intent(in) :: varname ! Variable name
1746 integer(1), dimension(:, :), allocatable, intent(inout) :: dat ! array where values should be stored
1747 integer(i4), dimension(:), optional, intent(in) :: start
1748 integer(i4), dimension(:), optional, intent(in) :: a_count
1749 integer(i4), optional, intent(in) :: fid
1750 !
1751 integer(i4), dimension(5) :: rstart
1752 integer(i4), dimension(5) :: rcount
1753 integer(i4) :: ncid ! id of input stream
1754 integer(i4) :: varid ! id of variable to be read
1755 integer(i4) :: vartype ! type of variable
1756 integer(i4) :: i
1757 !
1758 ! Defaults for Options Start and Count
1759 rstart = 1
1760 rcount = 1
1761 !
1762 ! allocate Dat
1763 if (.not. allocated(dat)) then
1764 if (.not. present(a_count)) then
1765 rcount = get_ncdim(filename, varname)
1766 else
1767 rcount(1 : idims) = a_count(1 : idims)
1768 end if
1769 allocate(dat(rcount(1), rcount(2)))
1770 else
1771 rcount(1 : idims) = shape(dat)
1772 end if
1773 !
1774 ! Assign options Start and Count if present
1775 if (present(start)) then
1776 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1777 rstart = start
1778 end if
1779 !
1780 if (present(a_count)) then
1781 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1782 rcount = a_count
1783 do i = 1, idims
1784 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1785 end do
1786 end if
1787 !
1788 ! Open NetCDF filename
1789 if (present(fid)) then
1790 ncid = fid
1791 else
1792 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1793 end if
1794 !
1795 ! Inquire file, check if VarName exists and get the id
1796 call get_info(varname, ncid, varid, vartype)
1797 ! check variable type ( 5 equals float type, 6 equals double )
1798 if (vartype /= itype) then
1799 print *, 'Variable name: ', trim(varname)
1800 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1801 stop
1802 end if
1803 !
1804 ! get values by varid
1805 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1806 !
1807 ! close File
1808 if (.not. present(fid)) call check(nf90_close(ncid))
1809 !
1810 end subroutine get_ncvar_2d_i1
1811
1812
1813 subroutine get_ncvar_3d_i1(Filename, VarName, Dat, start, a_count, fid)
1814 !
1815 implicit none
1816 !
1817 integer, parameter :: idims = 3
1818 integer, parameter :: itype = 1 ! 3 = single, 5 = float, 6 = double
1819 !
1820 character(len = *), intent(in) :: filename
1821 character(len = *), intent(in) :: varname ! Variable name
1822 integer(1), dimension(:, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1823 integer(i4), dimension(:), optional, intent(in) :: start
1824 integer(i4), dimension(:), optional, intent(in) :: a_count
1825 integer(i4), optional, intent(in) :: fid
1826 !
1827 integer(i4), dimension(5) :: rstart
1828 integer(i4), dimension(5) :: rcount
1829 integer(i4) :: ncid ! id of input stream
1830 integer(i4) :: varid ! id of variable to be read
1831 integer(i4) :: vartype ! type of variable
1832 integer(i4) :: i
1833 !
1834 ! Defaults for Options Start and Count
1835 rstart = 1
1836 rcount = 1
1837 !
1838 ! allocate Dat
1839 if (.not. allocated(dat)) then
1840 if (.not. present(a_count)) then
1841 rcount = get_ncdim(filename, varname)
1842 else
1843 rcount(1 : idims) = a_count(1 : idims)
1844 end if
1845 allocate(dat(rcount(1), rcount(2), rcount(3)))
1846 else
1847 rcount(1 : idims) = shape(dat)
1848 end if
1849 !
1850 ! Assign options Start and Count if present
1851 if (present(start)) then
1852 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1853 rstart = start
1854 end if
1855 !
1856 if (present(a_count)) then
1857 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1858 rcount = a_count
1859 do i = 1, idims
1860 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1861 end do
1862 end if
1863 !
1864 ! Open NetCDF filename
1865 if (present(fid)) then
1866 ncid = fid
1867 else
1868 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1869 end if
1870 !
1871 ! Inquire file, check if VarName exists and get the id
1872 call get_info(varname, ncid, varid, vartype)
1873 ! check variable type ( 5 equals float type, 6 equals double )
1874 if (vartype /= itype) then
1875 print *, 'Variable name: ', trim(varname)
1876 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1877 stop
1878 end if
1879 !
1880 ! get values by varid
1881 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1882 !
1883 ! close File
1884 if (.not. present(fid)) call check(nf90_close(ncid))
1885 !
1886 end subroutine get_ncvar_3d_i1
1887
1888
1889 subroutine get_ncvar_4d_i1(Filename, VarName, Dat, start, a_count, fid)
1890 !
1891 implicit none
1892 !
1893 integer, parameter :: idims = 4
1894 integer, parameter :: itype = 1 ! 1 = 1 byte, 3 = single, 5 = float, 6 = double
1895 !
1896 character(len = *), intent(in) :: filename
1897 character(len = *), intent(in) :: varname ! Variable name
1898 integer(1), dimension(:, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1899 integer(i4), dimension(:), optional, intent(in) :: start
1900 integer(i4), dimension(:), optional, intent(in) :: a_count
1901 integer(i4), optional, intent(in) :: fid
1902 !
1903 integer(i4), dimension(5) :: rstart
1904 integer(i4), dimension(5) :: rcount
1905 integer(i4) :: ncid ! id of input stream
1906 integer(i4) :: varid ! id of variable to be read
1907 integer(i4) :: vartype ! type of variable
1908 integer(i4) :: i
1909 !
1910 ! Defaults for Options Start and Count
1911 rstart = 1
1912 rcount = 1
1913 !
1914 ! allocate Dat
1915 if (.not. allocated(dat)) then
1916 if (.not. present(a_count)) then
1917 rcount = get_ncdim(filename, varname)
1918 else
1919 rcount(1 : idims) = a_count(1 : idims)
1920 end if
1921 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4)))
1922 else
1923 rcount(1 : idims) = shape(dat)
1924 end if
1925 !
1926 ! Assign options Start and Count if present
1927 if (present(start)) then
1928 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
1929 rstart = start
1930 end if
1931 !
1932 if (present(a_count)) then
1933 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
1934 rcount = a_count
1935 do i = 1, idims
1936 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
1937 end do
1938 end if
1939 !
1940 ! Open NetCDF filename
1941 if (present(fid)) then
1942 ncid = fid
1943 else
1944 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
1945 end if
1946 !
1947 ! Inquire file, check if VarName exists and get the id
1948 call get_info(varname, ncid, varid, vartype)
1949 ! check variable type ( 5 equals float type, 6 equals double )
1950 if (vartype /= itype) then
1951 print *, 'Variable name: ', trim(varname)
1952 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
1953 stop
1954 end if
1955 !
1956 ! get values by varid
1957 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
1958 !
1959 ! close File
1960 if (.not. present(fid)) call check(nf90_close(ncid))
1961 !
1962 end subroutine get_ncvar_4d_i1
1963
1964
1965 subroutine get_ncvar_5d_i1(Filename, VarName, Dat, start, a_count, fid)
1966 !
1967 implicit none
1968 !
1969 integer, parameter :: idims = 5
1970 integer, parameter :: itype = 1 ! 1 = 1 byte, 3 = single, 5 = float, 6 = double
1971 !
1972 character(len = *), intent(in) :: filename
1973 character(len = *), intent(in) :: varname ! Variable name
1974 integer(1), dimension(:, :, :, :, :), allocatable, intent(inout) :: dat ! array where values should be stored
1975 integer(i4), dimension(:), optional, intent(in) :: start
1976 integer(i4), dimension(:), optional, intent(in) :: a_count
1977 integer(i4), optional, intent(in) :: fid
1978 !
1979 integer(i4), dimension(5) :: rstart
1980 integer(i4), dimension(5) :: rcount
1981 integer(i4) :: ncid ! id of input stream
1982 integer(i4) :: varid ! id of variable to be read
1983 integer(i4) :: vartype ! type of variable
1984 integer(i4) :: i
1985 !
1986 ! Defaults for Options Start and Count
1987 rstart = 1
1988 rcount = 1
1989 !
1990 ! allocate Dat
1991 if (.not. allocated(dat)) then
1992 if (.not. present(a_count)) then
1993 rcount = get_ncdim(filename, varname)
1994 else
1995 rcount(1 : idims) = a_count(1 : idims)
1996 end if
1997 allocate(dat(rcount(1), rcount(2), rcount(3), rcount(4), rcount(5)))
1998 else
1999 rcount(1 : idims) = shape(dat)
2000 end if
2001 !
2002 ! Assign options Start and Count if present
2003 if (present(start)) then
2004 if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
2005 rstart = start
2006 end if
2007 !
2008 if (present(a_count)) then
2009 if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
2010 rcount = a_count
2011 do i = 1, idims
2012 if (size(dat, i) /= rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
2013 end do
2014 end if
2015 !
2016 ! Open NetCDF filename
2017 if (present(fid)) then
2018 ncid = fid
2019 else
2020 call check(nf90_open(trim(filename), nf90_nowrite, ncid))
2021 end if
2022 !
2023 ! Inquire file, check if VarName exists and get the id
2024 call get_info(varname, ncid, varid, vartype)
2025 ! check variable type ( 5 equals float type, 6 equals double )
2026 if (vartype /= itype) then
2027 print *, 'Variable name: ', trim(varname)
2028 print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
2029 stop
2030 end if
2031 !
2032 ! get values by varid
2033 call check(nf90_get_var(ncid, varid, dat, rstart, rcount))
2034 !
2035 ! close File
2036 if (.not. present(fid)) call check(nf90_close(ncid))
2037 !
2038 end subroutine get_ncvar_5d_i1
2039 ! ------------------------------------------------------------------------------
2040
2041 !> \brief Open netcdf file
2042
2043 !> \details Opens a netcdf file and returns file handle
2044 !!
2045 !! \b Example
2046 !!
2047 !! \code{.f90}
2048 !! id = NcOpen(Fname)
2049 !! \endcode
2050 !!
2051 !! \b Literature
2052 !!
2053 !! 1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
2054
2055 !> \param[in] "character(len=*) :: Fname" Filename of file to open
2056 !> \param[out] "integer(i4) :: NcOpen" id of opened stream
2057
2058 !> \author Stephan Thober,
2059 !> \date May 2012
2060
2061 function ncopen(Fname)
2062 !
2063 implicit none
2064 !
2065 character(len = *), intent(in) :: fname
2066 integer(i4) :: ncopen
2067 !
2068 call check(nf90_open(trim(fname), nf90_nowrite, ncopen))
2069 !
2070 end function ncopen
2071
2072 ! ------------------------------------------------------------------------------
2073
2074 !> \brief Closes netcdf file
2075
2076 !> \details Closes a netcdf file by file handle
2077 !!
2078 !! \b Example
2079 !!
2080 !! \code{.f90}
2081 !! call NcClose(ncid)
2082 !! \endcode
2083 !!
2084 !! \b Literature
2085 !!
2086 !! 1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
2087
2088 !> \param[in] "ncid" - file handle of open netcdf file
2089
2090 !> \author Stephan Thober
2091 !> \date May 2012
2092
2093 subroutine ncclose(ncid)
2094 !
2095 implicit none
2096 !
2097 integer(i4), intent(in) :: ncid
2098 !
2099 call check(nf90_close(ncid))
2100 !
2101 end subroutine ncclose
2102
2103 ! ------------------------------------------------------------------------------
2104 !> \brief Get variable info from netcdf files
2105
2106 !> \details This subroutine is PRIVATE and therefore does not exist outside of this module.
2107 !!
2108 !! This subroutine inquires the nc file. Given the Variable name and the stream
2109 !! of the nc file, this subroutine determines the variable id, the kind of the
2110 !! variable and the length of the dimensions in the file.
2111 !!
2112 !! See: http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90/ -> NF90-INQUIRE
2113 !! for detailed information.
2114
2115 !> \author Matthias Cuntz,
2116 !> \date Nov 2012
2117 !! - default dimension length -1
2118
2119 subroutine get_info(Varname, ncid, varid, xtype, dl, Info, ndims)
2120 !
2121 implicit none
2122 !
2123 character(len = *), intent(in) :: Varname
2124 integer(i4), intent(in) :: ncid
2125 integer(i4), intent(out) :: varid ! variable id of data to be read
2126 integer(i4), intent(out) :: xtype ! type of the variable
2127 integer(i4), dimension(:), optional, intent(inout) :: dl
2128 logical, optional, intent(in) :: Info
2129 integer(i4), optional, intent(out) :: ndims ! Number of Dimensions for specific variable
2130 !
2131 integer(i4), dimension(:), allocatable :: DimID ! Id of dimension
2132 character(NF90_MAX_NAME) :: name ! name of Variables in the file
2133 integer(i4) :: NumDims ! Number of Dimensions for specific variable
2134 integer(i4) :: n ! loop index
2135 character(256) :: form ! output format
2136 integer(i4) :: itmp
2137 !
2138 call check(nf90_inq_varid(ncid, varname, varid))
2139 call check(nf90_inquire_variable(ncid, varid, ndims = numdims))
2140 if (present(ndims)) ndims = numdims
2141 !
2142 ! get the dimension Ids and the data type of the variable
2143 allocate(dimid(numdims))
2144 call check(nf90_inquire_variable(ncid, varid, xtype = xtype, dimids = dimid))
2145 !
2146 if (present(dl)) then
2147 ! check consistency of dimensions
2148 if (numdims > size(dl)) &
2149 stop 'ERROR*** Dimension size of Variable is greater than dims of array. subroutine Get_Info'
2150 ! go through dimension ids and get its length
2151 dl(:) = -1 ! initialise
2152 dimloop : do n = 1, numdims
2153 call check(nf90_inquire_dimension(ncid, dimid(n), name, itmp))
2154 dl(n) = itmp
2155 if (present(info)) then
2156 if (info) then
2157 write(form, '(a12,I03.3,a1)') "(a10,i1,a4,a", len(trim(name)), ")"
2158 write(*, form) 'Dimension ', n, ' is ', trim(name)
2159 write(*, '(a14,i5.5)') 'The Length is ', dl(n)
2160 end if
2161 end if
2162 end do dimloop
2163 !
2164 end if
2165 !
2166 end subroutine get_info
2167
2168 ! -----------------------------------------------------------------------------
2169 ! private error checking routine
2170 subroutine check(status)
2171 !
2172 implicit none
2173 !
2174 integer(i4), intent(in) :: status
2175 !
2176 if (status /= nf90_noerr) then
2177 write(*, *) trim(nf90_strerror(status))
2178 stop
2179 end if
2180 !
2181 end subroutine check
2182
2183end module mo_ncread
Read array from NC file.
Definition mo_ncread.f90:85
Define number representations.
Definition mo_kind.F90:17
integer, parameter sp
Single Precision Real Kind.
Definition mo_kind.F90:44
integer, parameter i4
4 Byte Integer Kind
Definition mo_kind.F90:40
integer, parameter i8
8 Byte Integer Kind
Definition mo_kind.F90:42
integer, parameter dp
Double Precision Real Kind.
Definition mo_kind.F90:46
Reading netcdf files.
Definition mo_ncread.f90:11
subroutine, public get_ncvaratt(filename, varname, attname, attvalues, fid, dtype)
Get attribute of netcdf variable.
subroutine, public get_ncdimatt(filename, variable, dimname, dimlen)
Name and size of variable in netcdf.
integer(i4) function, public ncopen(fname)
Open netcdf file.
subroutine get_info(varname, ncid, varid, xtype, dl, info, ndims)
Get variable info from netcdf files.
subroutine, public ncclose(ncid)
Closes netcdf file.
integer(i4) function, dimension(5), public get_ncdim(filename, variable, printinfo, ndims)
Dimension of netcdf variable.