NCEPLIBS-ncio  1.0.0
All Namespaces Files Functions
read_vardata_code_4d.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(4)
10  integer, intent(in), optional :: nccount(4)
11  integer, intent(out), optional :: errcode
12  integer ncerr, nvar, n, nd, ndim, ncount
13  integer, allocatable, dimension(:) :: start, count
14  integer :: dimlens(4)
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  if (dset%variables(nvar)%ndims /= 4 .and. dset%variables(nvar)%ndims /= 5) then
52  if (return_errcode) then
53  call nccheck(ncerr,halt=.false.)
54  errcode=nf90_ebaddim
55  return
56  else
57  print *,'rank of data array != variable ndims (or ndims-1)'
58  stop 99
59  endif
60  endif
61  ! allocate/deallocate values
62  if (allocated(values)) deallocate(values)
63  if (present(ncstart) .and. present(nccount)) then
64  allocate(values(nccount(1),nccount(2),nccount(3),nccount(4)))
65  start(1)=ncstart(1); count(1)=nccount(1)
66  start(2)=ncstart(2); count(2)=nccount(2)
67  start(3)=ncstart(3); count(3)=nccount(3)
68  start(4)=ncstart(4); count(4)=nccount(4)
69  if (dset%variables(nvar)%ndims == 5) then
70  start(5)=1; count(5)=1
71  end if
72  ncerr = nf90_get_var(dset%ncid, dset%variables(nvar)%varid, values,&
73  start=start, count=count)
74  else
75  allocate(values(dimlens(1),dimlens(2),dimlens(3),dimlens(4)))
76  if (dset%variables(nvar)%ndims == 5) then
77  ncerr = nf90_get_var(dset%ncid, dset%variables(nvar)%varid, values,&
78  start=start, count=count)
79  else
80  ncerr = nf90_get_var(dset%ncid, dset%variables(nvar)%varid, values)
81  end if
82  end if
83  ! err check
84  if (return_errcode) then
85  call nccheck(ncerr,halt=.false.)
86  errcode=ncerr
87  else
88  call nccheck(ncerr)
89  endif