FV3 Bundle
tools_nc.F90
Go to the documentation of this file.
1 !----------------------------------------------------------------------
2 ! Module: tools_nc
3 ! Purpose: NetCDF routines
4 ! Author: Benjamin Menetrier
5 ! Licensing: this code is distributed under the CeCILL-C license
6 ! Copyright © 2015-... UCAR, CERFACS, METEO-FRANCE and IRIT
7 !----------------------------------------------------------------------
8 module tools_nc
9 
10 use netcdf
11 use tools_kinds, only: kind_real
12 use type_mpl, only: mpl_type
13 
14 implicit none
15 
16 interface put_att
17  module procedure put_att_integer
18  module procedure put_att_integer_array
19  module procedure put_att_real
20  module procedure put_att_real_array
21  module procedure put_att_logical
22  module procedure put_att_logical_array
23  module procedure put_att_string
24  module procedure put_att_string_array
25 end interface
26 
27 integer,parameter :: ncfloat = nf90_double ! NetCDF type for real
28 
29 private
30 public :: ncfloat
31 public :: put_att
32 
33 contains
34 
35 !----------------------------------------------------------------------
36 ! Subroutine: put_att_integer
37 ! Purpose: write namelist integer as NetCDF attribute
38 !----------------------------------------------------------------------
39 subroutine put_att_integer(mpl,ncid,varname,var)
40 
41 implicit none
42 
43 ! Passed variables
44 type(mpl_type),intent(in) :: mpl ! MPI data
45 integer,intent(in) :: ncid ! NetCDF file id
46 character(len=*),intent(in) :: varname ! Variable name
47 integer,intent(in) :: var ! Integer
48 
49 ! Local variables
50 character(len=1024) :: subr='put_att_integer'
51 
52 ! Write integer
53 call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),var))
54 
55 end subroutine put_att_integer
56 
57 !----------------------------------------------------------------------
58 ! Subroutine: put_att_integer_array
59 ! Purpose: write namelist integer array as NetCDF attribute
60 !----------------------------------------------------------------------
61 subroutine put_att_integer_array(mpl,ncid,varname,n,var)
62 
63 implicit none
64 
65 ! Passed variables
66 type(mpl_type),intent(in) :: mpl ! MPI data
67 integer,intent(in) :: ncid ! NetCDF file id
68 character(len=*),intent(in) :: varname ! Variable name
69 integer,intent(in) :: n ! Integer array size
70 integer,intent(in) :: var(n) ! Integer array
71 
72 ! Local variables
73 integer :: i
74 character(len=1024) :: str,fullstr
75 character(len=1024) :: subr='put_att_integer_array'
76 
77 ! Write integer array as a string
78 if (n>0) then
79  write(fullstr,'(i3.3)') var(1)
80  do i=2,n
81  write(str,'(i3.3)') var(i)
82  fullstr = trim(fullstr)//':'//trim(str)
83  end do
84  call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),trim(fullstr)))
85 end if
86 
87 end subroutine put_att_integer_array
88 
89 !----------------------------------------------------------------------
90 ! Subroutine: put_att_real
91 ! Purpose: write namelist real as NetCDF attribute
92 !----------------------------------------------------------------------
93 subroutine put_att_real(mpl,ncid,varname,var)
94 
95 implicit none
96 
97 ! Passed variables
98 type(mpl_type),intent(in) :: mpl ! MPI data
99 integer,intent(in) :: ncid ! NetCDF file id
100 character(len=*),intent(in) :: varname ! Variable name
101 real(kind_real),intent(in) :: var ! Real
102 
103 ! Local variables
104 character(len=1024) :: subr='put_att_real'
105 
106 ! Write real
107 call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),var))
108 
109 end subroutine put_att_real
110 
111 !----------------------------------------------------------------------
112 ! Subroutine: put_att_real_array
113 ! Purpose: write namelist real array as NetCDF attribute
114 !----------------------------------------------------------------------
115 subroutine put_att_real_array(mpl,ncid,varname,n,var)
117 implicit none
118 
119 ! Passed variables
120 type(mpl_type),intent(in) :: mpl ! MPI data
121 integer,intent(in) :: ncid ! NetCDF file id
122 character(len=*),intent(in) :: varname ! Variable name
123 integer,intent(in) :: n ! Real array size
124 real(kind_real),intent(in) :: var(n) ! Real array
125 
126 ! Local variables
127 integer :: i
128 character(len=1024) :: str,fullstr
129 character(len=1024) :: subr='put_att_real_array'
130 
131 ! Write real array as a string
132 if (n>0) then
133  write(fullstr,'(e10.3)') var(1)
134  do i=2,n
135  write(str,'(e10.3)') var(i)
136  fullstr = trim(fullstr)//':'//trim(str)
137  end do
138  call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),trim(fullstr)))
139 end if
140 
141 end subroutine put_att_real_array
142 
143 !----------------------------------------------------------------------
144 ! Subroutine: put_att_logical
145 ! Purpose: write namelist logical as NetCDF attribute
146 !----------------------------------------------------------------------
147 subroutine put_att_logical(mpl,ncid,varname,var)
149 implicit none
150 
151 ! Passed variables
152 type(mpl_type),intent(in) :: mpl ! MPI data
153 integer,intent(in) :: ncid ! NetCDF file id
154 character(len=*),intent(in) :: varname ! Variable name
155 logical,intent(in) :: var ! Logical
156 
157 ! Local variables
158 character(len=1024) :: subr='put_att_logical'
159 
160 ! Write logical as a string
161 if (var) then
162  call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),'.true.'))
163 else
164  call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),'.false.'))
165 end if
166 
167 end subroutine put_att_logical
168 
169 !----------------------------------------------------------------------
170 ! Subroutine: put_att_logical_array
171 ! Purpose: write namelist logical array as NetCDF attribute
172 !----------------------------------------------------------------------
173 subroutine put_att_logical_array(mpl,ncid,varname,n,var)
175 implicit none
176 
177 ! Passed variables
178 type(mpl_type),intent(in) :: mpl ! MPI data
179 integer,intent(in) :: ncid ! NetCDF file id
180 character(len=*),intent(in) :: varname ! Variable name
181 integer,intent(in) :: n ! Real array size
182 logical,intent(in) :: var(n) ! Logical array
183 
184 ! Local variables
185 integer :: i
186 character(len=1024) :: str,fullstr
187 character(len=1024) :: subr='put_att_logical_array'
188 
189 ! Write real array as a string
190 if (n>0) then
191  if (var(1)) then
192  write(fullstr,'(a6)') '.true.'
193  else
194  write(fullstr,'(a7)') '.false.'
195  end if
196  do i=2,n
197  if (var(i)) then
198  write(str,'(a6)') '.true.'
199  else
200  write(str,'(a7)') '.false.'
201  end if
202  fullstr = trim(fullstr)//':'//trim(str)
203  end do
204  call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),trim(fullstr)))
205 end if
206 
207 end subroutine put_att_logical_array
208 
209 !----------------------------------------------------------------------
210 ! Subroutine: put_att_string
211 ! Purpose: write namelist string as NetCDF attribute
212 !----------------------------------------------------------------------
213 subroutine put_att_string(mpl,ncid,varname,var)
215 implicit none
216 
217 ! Passed variables
218 type(mpl_type),intent(in) :: mpl ! MPI data
219 integer,intent(in) :: ncid ! NetCDF file id
220 character(len=*),intent(in) :: varname ! Variable name
221 character(len=*),intent(in) :: var ! String
222 
223 ! Local variables
224 character(len=1024) :: subr='put_att_string'
225 
226 ! Write string
227 call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),trim(var)))
228 
229 end subroutine put_att_string
230 
231 !----------------------------------------------------------------------
232 ! Subroutine: put_att_string_array
233 ! Purpose: write namelist string array as NetCDF attribute
234 !----------------------------------------------------------------------
235 subroutine put_att_string_array(mpl,ncid,varname,n,var)
237 implicit none
238 
239 ! Passed variables
240 type(mpl_type),intent(in) :: mpl ! MPI data
241 integer,intent(in) :: ncid ! NetCDF file id
242 character(len=*),intent(in) :: varname ! Variable name
243 integer,intent(in) :: n ! String array size
244 character(len=*),intent(in) :: var(n) ! String array
245 
246 ! Local variables
247 integer :: i
248 character(len=1024) :: fullstr
249 character(len=1024) :: subr='put_att_string_array'
250 
251 ! Write string array
252 if (n>0) then
253  fullstr = trim(var(1))
254  do i=2,n
255  fullstr = trim(fullstr)//':'//trim(var(i))
256  end do
257  call mpl%ncerr(subr,nf90_put_att(ncid,nf90_global,trim(varname),trim(fullstr)))
258 end if
259 
260 end subroutine put_att_string_array
261 
262 end module tools_nc
subroutine put_att_integer_array(mpl, ncid, varname, n, var)
Definition: tools_nc.F90:62
subroutine put_att_real_array(mpl, ncid, varname, n, var)
Definition: tools_nc.F90:116
subroutine put_att_real(mpl, ncid, varname, var)
Definition: tools_nc.F90:94
subroutine put_att_logical(mpl, ncid, varname, var)
Definition: tools_nc.F90:148
subroutine put_att_string_array(mpl, ncid, varname, n, var)
Definition: tools_nc.F90:236
subroutine put_att_integer(mpl, ncid, varname, var)
Definition: tools_nc.F90:40
integer, parameter, public kind_real
subroutine put_att_logical_array(mpl, ncid, varname, n, var)
Definition: tools_nc.F90:174
integer, parameter, public ncfloat
Definition: tools_nc.F90:27
subroutine put_att_string(mpl, ncid, varname, var)
Definition: tools_nc.F90:214