NCEPLIBS-ncio  1.0.0
All Namespaces Files Functions
write_vardata_code.f90
1 
5  type(Dataset), intent(inout) :: dset
6  character(len=*), intent(in) :: varname
7  integer, intent(in), optional :: nslice
8  integer, intent(in), optional :: slicedim
9  integer, intent(out), optional :: errcode
10  integer ncerr, nvar, ncount, ndim, nd, n
11  integer, allocatable, dimension(:) :: start, count, dimlens
12  logical is_slice
13  logical return_errcode
14  ! check if use the errcode
15  if(present(errcode)) then
16  return_errcode=.true.
17  errcode = 0
18  else
19  return_errcode=.false.
20  endif
21  ! check if count/dims of data are avail
22  if (present(nslice)) then
23  ncount = nslice
24  is_slice = .true.
25  else
26  ncount = 1
27  is_slice = .false.
28  endif
29  ! define variable name and allocate variable
30  nvar = get_nvar(dset,varname)
31  allocate(start(dset%variables(nvar)%ndims),count(dset%variables(nvar)%ndims))
32  allocate(dimlens(dset%variables(nvar)%ndims))
33  start(:) = 1
34  count(:) = 1
35  dimlens(:) = 1
36  if (present(slicedim)) then
37  nd = slicedim
38  else
39  nd = dset%variables(nvar)%ndims
40  end if
41  ndim = 1
42  do n=1,dset%variables(nvar)%ndims
43  if (n == nd) then
44  start(n) = ncount
45  count(n) = 1
46  else
47  start(n) = 1
48  count(n) = dset%variables(nvar)%dimlens(n)
49  dimlens(ndim) = dset%variables(nvar)%dimlens(n)
50  ndim = ndim + 1
51  end if
52  end do
53  ! write operations on a parallel file system are performed collectively
54  ncerr = nf90_var_par_access(dset%ncid, dset%variables(nvar)%varid, nf90_collective)
55  if (is_slice) then
56  if (dset%variables(nvar)%ndims > 1) then
57  ncerr = nf90_put_var(dset%ncid, dset%variables(nvar)%varid,values, &
58  start=start,count=count)
59  else if (dset%variables(nvar)%ndims == 1) then
60  if (return_errcode) then
61  errcode = -1
62  return
63  else
64  print *,'cannot write a slice to a 1d variable'
65  stop 99
66  endif
67  endif
68  else if (present(ncstart) .and. present(nccount)) then
69  ncerr = nf90_put_var(dset%ncid, dset%variables(nvar)%varid,values, &
70  start=ncstart, count=nccount)
71  else
72  ncerr = nf90_put_var(dset%ncid, dset%variables(nvar)%varid, values)
73  endif
74  if (return_errcode) then
75  call nccheck(ncerr,halt=.false.)
76  errcode=ncerr
77  if (ncerr /= 0) return
78  else
79  call nccheck(ncerr)
80  endif
81  ! reset unlim dim size for all variables
82  if (dset%variables(nvar)%hasunlim) then
83  if (return_errcode) then
84  call set_varunlimdimlens_(dset,errcode)
85  else
86  call set_varunlimdimlens_(dset)
87  endif
88  endif