NCEPLIBS-ncio  1.0.0
All Namespaces Files Functions
read_vardata_code_3d.f90
1 
5  type(Dataset), intent(in) :: dset
6  character(len=*), intent(in) :: varname
7  integer, intent(in), optional :: nslice
8  integer, intent(in), optional :: slicedim
9  integer, intent(in), optional :: ncstart(3)
10  integer, intent(in), optional :: nccount(3)
11  integer, intent(out), optional :: errcode
12  integer ncerr, nvar, n, nd, ndim, ncount
13  integer, allocatable, dimension(:) :: start, count
14  integer :: dimlens(3)
15  logical return_errcode
16  ! check if use the errcode
17  if(present(errcode)) then
18  return_errcode=.true.
19  errcode = 0
20  else
21  return_errcode=.false.
22  endif
23  ! check if count/dims of data are avail
24  if (present(nslice)) then
25  ncount = nslice
26  else
27  ncount = 1
28  endif
29  nvar = get_nvar(dset,varname)
30  allocate(start(dset%variables(nvar)%ndims),count(dset%variables(nvar)%ndims))
31  start(:) = 1
32  count(:) = 1
33  dimlens(:) = 1
34  if (present(slicedim)) then
35  nd = slicedim
36  else
37  nd = dset%variables(nvar)%ndims
38  end if
39  ndim = 1
40  do n=1,dset%variables(nvar)%ndims
41  if (n == nd) then
42  start(n) = ncount
43  count(n) = 1
44  else
45  start(n) = 1
46  count(n) = dset%variables(nvar)%dimlens(n)
47  dimlens(ndim) = dset%variables(nvar)%dimlens(n)
48  ndim = ndim + 1
49  end if
50  end do
51 
52  if (dset%variables(nvar)%ndims /= 3 .and. dset%variables(nvar)%ndims /= 4) then
53  if (return_errcode) then
54  call nccheck(ncerr,halt=.false.)
55  errcode=nf90_ebaddim
56  return
57  else
58  print *,'rank of data array != variable ndims (or ndims-1)'
59  stop 99
60  endif
61  endif
62  ! allocate/deallocate values
63  if (allocated(values)) deallocate(values)
64  if (present(ncstart) .and. present(nccount)) then
65  allocate(values(nccount(1),nccount(2),nccount(3)))
66  start(1)=ncstart(1); count(1)=nccount(1)
67  start(2)=ncstart(2); count(2)=nccount(2)
68  start(3)=ncstart(3); count(3)=nccount(3)
69  if (dset%variables(nvar)%ndims == 4) then
70  start(4)=1; count(4)=1
71  end if
72  ncerr = nf90_get_var(dset%ncid, dset%variables(nvar)%varid, values,&
73  start=start, count=count)
74  else
75  if (dset%variables(nvar)%ndims == 4) then
76  allocate(values(dimlens(1),dimlens(2),dimlens(3)))
77  ncerr = nf90_get_var(dset%ncid, dset%variables(nvar)%varid, values,&
78  start=start, count=count)
79  else
80  allocate(values(dset%variables(nvar)%dimlens(1),&
81  dset%variables(nvar)%dimlens(2),&
82  dset%variables(nvar)%dimlens(3)))
83  ncerr = nf90_get_var(dset%ncid, dset%variables(nvar)%varid, values)
84  end if
85  end if
86  ! err check
87  if (return_errcode) then
88  call nccheck(ncerr,halt=.false.)
89  errcode=ncerr
90  else
91  call nccheck(ncerr)
92  endif