gtool5 Fortran 90/95 Library 1.0.0-rc5
日本語
Loading...
Searching...
No Matches
gtdata_internal_vartable.f90
Go to the documentation of this file.
1
21
60
62 use dc_types, only: string
63 implicit none
64 private
65
66 integer, parameter, public :: vid_invalid = -1
67
68 integer, parameter, public :: vtb_class_unused = 0
69 integer, parameter, public :: vtb_class_netcdf = 1
70 integer, parameter, public :: classes_max = 2
71
72 type var_table_entry
73 integer:: class
74 integer:: cid
75 integer:: refcount
76 end type var_table_entry
77
78 type(var_table_entry), save, allocatable:: table(:)
79 integer, parameter:: table_ini_size = 16
80
81 type(gd_nc_variable_search), public, save:: gdnc_search
82
84 public:: vartable_dump
86 private:: var_table_entry, table, table_ini_size
87 private:: entry_cleanup
88
89 interface dimrange
90 module procedure dimrange_direct
91 end interface
92
93contains
94
106 subroutine vartable_dump(vid)
107 use dc_trace, only: dbgmessage
110 integer, intent(in):: vid
111 character(10):: class
112 if (.not. allocated(table)) return
113 if (vid <= 0 .or. vid > size(table)) return
114 select case(table(vid)%class)
115 case(vtb_class_netcdf)
116 class = 'netcdf'
117 case default
118 write(class, fmt="(i10)") table(vid)%class
119 end select
120 call dbgmessage('[vartable %d: class=%c cid=%d ref=%d]', &
121 & i=(/vid, table(vid)%cid, table(vid)%refcount/), &
122 & c1=trim(class))
123 select case(table(vid)%class)
124 case(vtb_class_netcdf)
125 call dbgmessage('[%c]', c1=trim(tostring(gd_nc_variable(table(vid)%cid))))
126 end select
127 end subroutine vartable_dump
128
140 subroutine entry_cleanup(vtb_entry)
141 type(var_table_entry), intent(out):: vtb_entry(:)
142 vtb_entry(:)%class = vtb_class_unused
143 vtb_entry(:)%cid = -1
144 vtb_entry(:)%refcount = 0
145 end subroutine entry_cleanup
146
168 subroutine vartableadd(vid, class, cid)
169 use dc_trace, only: dbgmessage
170 integer, intent(out):: vid
171 integer, intent(in):: class, cid
172 type(var_table_entry), allocatable:: tmp_table(:)
173 integer:: n
174 continue
175 ! 必要ならば初期幅確保
176 if (.not. allocated(table)) then
177 allocate(table(table_ini_size))
178 call entry_cleanup(table(:))
179 endif
180 ! 該当があれば参照数増加
181 do, n = 1, size(table)
182 if (table(n)%class == class .and. table(n)%cid == cid) then
183 table(n)%refcount = table(n)%refcount + 1
184 call dbgmessage('gtdata_vartable.add(class=%d cid=%d) found (ref=%d)', &
185 & i=(/table(n)%class, table(n)%cid, table(n)%refcount/))
186 vid = n
187 return
188 endif
189 enddo
190 ! もし空きが無ければ表を拡張
191 if (all(table(:)%class /= vtb_class_unused)) then
192 n = size(table)
193 allocate(tmp_table(n))
194 tmp_table(:) = table(:)
195 deallocate(table)
196 allocate(table(n * 2))
197 table(1:n) = tmp_table(1:n)
198 deallocate(tmp_table)
199 table(n+1:n*2) = var_table_entry(vtb_class_unused, -1, 0)
200 endif
201 do, n = 1, size(table)
202 if (table(n)%class == vtb_class_unused) then
203 table(n)%class = class
204 table(n)%cid = cid
205 table(n)%refcount = 1
206 vid = n
207 return
208 endif
209 enddo
210 vid = vid_invalid
211 end subroutine vartableadd
212
234 subroutine vartabledelete(vid, action, err)
235 integer, intent(in):: vid
236 logical, intent(out):: action
237 logical, intent(out), optional:: err
238 if (.not. allocated(table)) goto 999
239 if (vid <= 0 .or. vid > size(table)) goto 999
240 if (table(vid)%class <= vtb_class_unused) goto 999
241 if (table(vid)%class > classes_max) goto 999
242 table(vid)%refcount = max(table(vid)%refcount - 1, 0)
243 action = (table(vid)%refcount == 0)
244 if (present(err)) err = .false.
245 return
246999 continue
247 action = .false.
248 if (present(err)) err = .true.
249 end subroutine vartabledelete
250
270 subroutine vartablelookup(vid, class, cid)
271 integer, intent(in):: vid
272 integer, intent(out), optional:: class, cid
273 if (.not. allocated(table)) goto 999
274 if (vid <= 0 .or. vid > size(table)) goto 999
275 if (table(vid)%class <= vtb_class_unused) goto 999
276 if (table(vid)%class > classes_max) goto 999
277 if (present(class)) class = table(vid)%class
278 if (present(cid)) cid = table(vid)%cid
279 return
280999 continue
281 if (present(class)) class = vtb_class_unused
282 end subroutine vartablelookup
283
297 subroutine vartablemore(vid, err)
298 integer, intent(in):: vid
299 logical, intent(out), optional:: err
300 if (.not. allocated(table)) goto 999
301 if (vid <= 0 .or. vid > size(table)) goto 999
302 if (table(vid)%class <= vtb_class_unused) goto 999
303 if (table(vid)%class > classes_max) goto 999
304 table(vid)%refcount = table(vid)%refcount + 1
305 if (present(err)) err = .false.
306 return
307999 continue
308 if (present(err)) err = .true.
309 end subroutine vartablemore
310
326 subroutine dimrange_direct(vid, dimlo, dimhi)
328 use gtdata_netcdf_generic, only: gdncinquire => inquire
329 use dc_error, only: storeerror, nf90_einval
330 integer, intent(in):: vid
331 integer, intent(out):: dimlo, dimhi
332 integer:: class, cid
333 call vartablelookup(vid, class, cid)
334 select case(class)
335 case(vtb_class_netcdf)
336 dimlo = 1
337 call gdncinquire(gd_nc_variable(cid), dimlen=dimhi)
338 case default
339 call storeerror(nf90_einval, 'gtdata::dimrange')
340 end select
341 end subroutine dimrange_direct
342
356 integer function ndims(vid) result(result)
358 use gtdata_netcdf_generic, only: gdncinquire => inquire
359 use dc_error, only: storeerror, nf90_einval
360 integer, intent(in):: vid
361 integer:: class, cid
362 call vartablelookup(vid, class, cid)
363 select case(class)
364 case(vtb_class_netcdf)
365 call gdncinquire(gd_nc_variable(cid), ndims=result)
366 case default
367 call storeerror(nf90_einval, 'gtdata::ndims')
368 end select
369 end function ndims
370
384 subroutine query_growable(vid, result)
387 use dc_error, only: storeerror, nf90_einval
388 integer, intent(in):: vid
389 logical, intent(out):: result
390 integer:: class, cid
391 call vartablelookup(vid, class, cid)
392 select case(class)
393 case(vtb_class_netcdf)
394 call inquire(gd_nc_variable(cid), growable=result)
395 case default
396 call storeerror(nf90_einval, 'gtdata::ndims')
397 end select
398 end subroutine query_growable
399
Error handling module.
Definition dc_error.f90:454
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:891
Debug tracing module.
Definition dc_trace.f90:150
subroutine, public dbgmessage(fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:661
Provides kind type parameter values.
Definition dc_types.f90:55
integer, parameter, public string
Character length for string
Definition dc_types.f90:137
subroutine, public vartabledelete(vid, action, err)
subroutine, public vartablelookup(vid, class, cid)
integer, parameter, public classes_max
integer, parameter, public vid_invalid
subroutine, public query_growable(vid, result)
subroutine, public vartablemore(vid, err)
subroutine, public vartableadd(vid, class, cid)
integer, parameter, public vtb_class_netcdf
type(gd_nc_variable_search), save, public gdnc_search
subroutine, public vartable_dump(vid)
integer function, public ndims(vid)
integer, parameter, public vtb_class_unused