FV3 Bundle
ncdr_global_attrs.f90
Go to the documentation of this file.
2  use ncd_kinds, only: i_long
4  use ncdr_climsg, only: ncdr_error
7  use netcdf, only: nf90_inquire_attribute, nf90_inquire, &
8  nf90_inq_attname, nf90_global, nf90_max_name, nf90_enotatt, &
9  nf90_noerr
10 
11  implicit none
12 
14  module procedure nc_diag_read_id_check_global_attr, &
16  end interface nc_diag_read_check_global_attr
17 
19  module procedure nc_diag_read_id_get_global_attr_type, &
22 
24  module procedure nc_diag_read_id_ret_global_attr_len, &
27 
29  module procedure nc_diag_read_id_get_global_attr_len, &
32 
36 
40 
41  contains
42  function nc_diag_read_id_check_global_attr(file_ncdr_id, attr_name) result(attr_exists)
43  integer(i_long), intent(in) :: file_ncdr_id
44  character(len=*), intent(in) :: attr_name
45 
46  integer(i_long) :: nc_err
47 
48  logical :: attr_exists
49 
50  call ncdr_check_ncdr_id(file_ncdr_id)
51 
52  nc_err = nf90_inquire_attribute(ncdr_files(file_ncdr_id)%ncid, &
53  nf90_global, attr_name)
54 
55  ! If attribute doesn't exist, return false.
56  if (nc_err == nf90_enotatt) then
57  attr_exists = .false.
58  return
59  end if
60 
61  ! Sanity check - could be another error!
62  if (nc_err /= nf90_noerr) then
63  call ncdr_nc_check(nc_err)
64  end if
65 
66  attr_exists = .true.
68 
69  function nc_diag_read_noid_check_global_attr(attr_name) result(attr_exists)
70  character(len=*), intent(in) :: attr_name
71 
72  logical :: attr_exists
73 
75 
76  attr_exists = nc_diag_read_id_check_global_attr(current_ncdr_id, attr_name)
78 
79  function nc_diag_read_id_get_global_attr_type(file_ncdr_id, attr_name) result(attr_type)
80  integer(i_long), intent(in) :: file_ncdr_id
81  character(len=*), intent(in) :: attr_name
82 
83  integer(i_long) :: attr_type
84 
85  call ncdr_check_ncdr_id(file_ncdr_id)
86 
87  call ncdr_nc_check(nf90_inquire_attribute(ncdr_files(file_ncdr_id)%ncid, &
88  nf90_global, attr_name, attr_type))
90 
91  function nc_diag_read_noid_get_global_attr_type(attr_name) result(attr_type)
92  character(len=*), intent(in) :: attr_name
93 
94  integer(i_long) :: attr_type
95 
97 
100 
101  function nc_diag_read_id_ret_global_attr_len(file_ncdr_id, attr_name) result(attr_len)
102  integer(i_long), intent(in) :: file_ncdr_id
103  character(len=*), intent(in) :: attr_name
104 
105  integer(i_long) :: attr_len
106 
107  call ncdr_check_ncdr_id(file_ncdr_id)
108 
109  call ncdr_nc_check(nf90_inquire_attribute(ncdr_files(file_ncdr_id)%ncid, &
110  nf90_global, attr_name, len = attr_len))
112 
113  function nc_diag_read_noid_ret_global_attr_len(attr_name) result(attr_len)
114  character(len=*), intent(in) :: attr_name
115  integer(i_long) :: attr_len
116 
118 
121 
122  subroutine nc_diag_read_id_get_global_attr_len(file_ncdr_id, attr_name, attr_len)
123  integer(i_long), intent(in) :: file_ncdr_id
124  character(len=*), intent(in) :: attr_name
125  integer(i_long), intent(out) :: attr_len
126 
127  call ncdr_check_ncdr_id(file_ncdr_id)
128 
129  call ncdr_nc_check(nf90_inquire_attribute(ncdr_files(file_ncdr_id)%ncid, &
130  nf90_global, attr_name, len = attr_len))
132 
133  subroutine nc_diag_read_noid_get_global_attr_len(attr_name, attr_len)
134  character(len=*), intent(in) :: attr_name
135  integer(i_long), intent(out) :: attr_len
136 
138 
139  call nc_diag_read_id_get_global_attr_len(current_ncdr_id, attr_name, attr_len)
141 
142  subroutine nc_diag_read_id_get_global_attr_names(file_ncdr_id, num_global_attrs, attr_name_mlen, attr_names)
143  integer(i_long), intent(in) :: file_ncdr_id
144  integer(i_long), intent(out), optional :: num_global_attrs
145  integer(i_long), intent(out), optional :: attr_name_mlen
146  character(len=:), intent(inout), dimension(:), allocatable, optional:: attr_names
147 
148  integer(i_long) :: nattrs, attr_index, max_global_attr_name_len
149 
150  character(len=NF90_MAX_NAME) :: attr_name
151 
152  max_global_attr_name_len = 0
153 
154  call ncdr_check_ncdr_id(file_ncdr_id)
155 
156  call ncdr_nc_check(nf90_inquire(ncdr_files(file_ncdr_id)%ncid, nattributes = nattrs))
157 
158  if (present(num_global_attrs)) &
159  num_global_attrs = nattrs
160 
161  ! Figure out character max length
162  do attr_index = 1, nattrs
163  call ncdr_nc_check(nf90_inq_attname(ncdr_files(file_ncdr_id)%ncid, &
164  nf90_global, &
165  attr_index, &
166  attr_name))
167 
168  if (len_trim(attr_name) > max_global_attr_name_len) &
169  max_global_attr_name_len = len_trim(attr_name)
170  end do
171 
172  if (present(attr_name_mlen)) &
173  attr_name_mlen = max_global_attr_name_len
174 
175  if (present(attr_names)) then
176  if (.NOT. allocated(attr_names)) then
177  allocate(character(max_global_attr_name_len) :: attr_names(nattrs))
178  else
179  if (size(attr_names) /= nattrs) &
180  call ncdr_error("Invalid allocated array size for attribute names storage!")
181  if (len(attr_names) < max_global_attr_name_len) &
182  call ncdr_error("Invalid allocated array size for attribute names storage! (String length does not match!)")
183  end if
184 
185  do attr_index = 1, nattrs
186  call ncdr_nc_check(nf90_inq_attname(ncdr_files(file_ncdr_id)%ncid, &
187  nf90_global, &
188  attr_index, &
189  attr_names(attr_index)))
190  end do
191  end if
193 
194  subroutine nc_diag_read_noid_get_global_attr_names(num_global_attrs, attr_name_mlen, attr_names)
195  integer(i_long), intent(out), optional :: num_global_attrs
196  integer(i_long), intent(out), optional :: attr_name_mlen
197  character(len=:), intent(inout), dimension(:), allocatable, optional:: attr_names
198 
200 
201  call nc_diag_read_id_get_global_attr_names(current_ncdr_id, num_global_attrs, attr_name_mlen, attr_names)
203 end module ncdr_global_attrs
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
integer(i_long) function nc_diag_read_id_ret_global_attr_len(file_ncdr_id, attr_name)
subroutine ncdr_nc_check(status)
Definition: ncdr_check.f90:85
integer(i_long) current_ncdr_id
Definition: ncdr_state.f90:7
type(ncdr_file), dimension(:), allocatable ncdr_files
Definition: ncdr_state.f90:14
subroutine ncdr_check_current_ncdr_id
Definition: ncdr_check.f90:22
integer(i_long) function nc_diag_read_noid_get_global_attr_type(attr_name)
logical function nc_diag_read_id_check_global_attr(file_ncdr_id, attr_name)
subroutine nc_diag_read_id_get_global_attr_len(file_ncdr_id, attr_name, attr_len)
subroutine ncdr_check_ncdr_id(file_ncdr_id)
Definition: ncdr_check.f90:12
integer(i_long) function nc_diag_read_noid_ret_global_attr_len(attr_name)
integer(i_long) function nc_diag_read_id_get_global_attr_type(file_ncdr_id, attr_name)
subroutine ncdr_error(err)
Definition: ncdr_climsg.F90:13
subroutine nc_diag_read_id_get_global_attr_names(file_ncdr_id, num_global_attrs, attr_name_mlen, attr_names)
logical function nc_diag_read_noid_check_global_attr(attr_name)
subroutine nc_diag_read_noid_get_global_attr_len(attr_name, attr_len)