Debian Codegraph @

src/atlas_f/util/atlas_Metadata_module.F90

raw
1 ! (C) Copyright 2013 ECMWF.
2 !
3 ! This software is licensed under the terms of the Apache Licence Version 2.0
4 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 ! In applying this licence, ECMWF does not waive the privileges and immunities
6 ! granted to it by virtue of its status as an intergovernmental organisation nor
7 ! does it submit to any jurisdiction.
8
9 #include "atlas/atlas_f.h"
10
11 module atlas_metadata_module
12
13 use fckit_object_module, only : fckit_object
14
15 implicit none
16
17 private :: fckit_object
18
19 public :: atlas_Metadata
20
21 private
22
23 integer, parameter :: MAX_STR_LEN=255
24
25 !------------------------------------------------------------------------------
26 TYPE, extends(fckit_object) :: atlas_Metadata
27
28 ! Purpose :
29 ! -------
30 ! *Metadata* : Container of Metadata, parameters or attributes
31 ! The Metadata are seted as key, value pairs
32
33 ! Methods :
34 ! -------
35 ! set : set a new property with given key and value
36 ! set : Modify a property with given key and value
37 ! get : Return a property value for given key
38
39 ! Author :
40 ! ------
41 ! 20-Nov-2013 Willem Deconinck *ECMWF*
42
43 !------------------------------------------------------------------------------
44 contains
45 procedure, private :: set_logical => Metadata__set_logical
46 procedure, private :: set_int32 => Metadata__set_int32
47 procedure, private :: set_real32 => Metadata__set_real32
48 procedure, private :: set_real64 => Metadata__set_real64
49 procedure, private :: set_string => Metadata__set_string
50 procedure, private :: set_array_int32 => Metadata__set_array_int32
51 procedure, private :: set_array_int64 => Metadata__set_array_int64
52 procedure, private :: set_array_real32 => Metadata__set_array_real32
53 procedure, private :: set_array_real64 => Metadata__set_array_real64
54 procedure :: has => Metadata__has
55 generic :: set => set_logical, set_int32, set_real32, set_real64, set_string, set_array_int32, &
56 set_array_int64, set_array_real32, set_array_real64
57 procedure :: get_int32 => Metadata__get_int32
58 procedure :: get_logical => Metadata__get_logical
59 procedure :: get_real32 => Metadata__get_real32
60 procedure :: get_real64 => Metadata__get_real64
61 procedure :: get_string => Metadata__get_string
62 procedure :: get_array_int32 => Metadata__get_array_int32
63 procedure :: get_array_int64 => Metadata__get_array_int64
64 procedure :: get_array_real32 => Metadata__get_array_real32
65 procedure :: get_array_real64 => Metadata__get_array_real64
66 generic :: get => get_int32, get_logical, get_real32, get_real64, get_string, get_array_int32, &
67 get_array_int64, get_array_real32, get_array_real64
68
69 procedure :: print => Metadata__print
70 procedure :: json => Metadata__json
71
72 procedure, public :: delete => atlas_Metadata__delete
73
74 #if FCKIT_FINAL_NOT_INHERITING
75 final :: atlas_Metadata__final_auto
76 #endif
77
78 END TYPE atlas_Metadata
79
80 !------------------------------------------------------------------------------
81
82 interface atlas_Metadata
83 module procedure atlas_Metadata__ctor
84 end interface
85
86 !------------------------------------------------------------------------------
87
88
89 !========================================================
90 contains
91 !========================================================
92
93
94 ! -----------------------------------------------------------------------------
95 ! Metadata routines
96
97 function atlas_Metadata__ctor() result(this)
98 use atlas_metadata_c_binding
99 use fckit_c_interop_module
100 type(atlas_Metadata) :: this
101 call this%reset_c_ptr( atlas__Metadata__new() )
102 end function atlas_Metadata__ctor
103
104 subroutine atlas_Metadata__delete(this)
105 use atlas_metadata_c_binding
106 class(atlas_Metadata), intent(inout) :: this
107 if ( .not. this%is_null() ) then
108 call atlas__Metadata__delete(this%CPTR_PGIBUG_A)
109 end if
110 call this%reset_c_ptr()
111 end subroutine atlas_Metadata__delete
112
113 function Metadata__has(this, name) result(value)
114 use atlas_metadata_c_binding
115 use fckit_c_interop_module, only : c_str
116 class(atlas_Metadata), intent(inout) :: this
117 character(len=*), intent(in) :: name
118 logical :: value
119 integer :: value_int
120 value_int = atlas__Metadata__has(this%CPTR_PGIBUG_A, c_str(name) )
121 if( value_int == 1 ) then
122 value = .True.
123 else
124 value = .False.
125 end if
126 end function Metadata__has
127
128 subroutine Metadata__set_logical(this, name, value)
129 use atlas_metadata_c_binding
130 use fckit_c_interop_module, only : c_str
131 class(atlas_Metadata), intent(inout) :: this
132 character(len=*), intent(in) :: name
133 logical, intent(in) :: value
134 integer :: value_int
135 if( value ) then
136 value_int = 1
137 else
138 value_int = 0
139 end if
140 call atlas__Metadata__set_int(this%CPTR_PGIBUG_A, c_str(name), value_int )
141 end subroutine Metadata__set_logical
142
143 subroutine Metadata__set_int32(this, name, value)
144 use atlas_metadata_c_binding
145 use fckit_c_interop_module, only : c_str
146 class(atlas_Metadata), intent(inout) :: this
147 character(len=*), intent(in) :: name
148 integer, intent(in) :: value
149 call atlas__Metadata__set_int(this%CPTR_PGIBUG_A, c_str(name), value)
150 end subroutine Metadata__set_int32
151
152 subroutine Metadata__set_real32(this, name, value)
153 use atlas_metadata_c_binding
154 use fckit_c_interop_module, only : c_str
155 use, intrinsic :: iso_c_binding
156 class(atlas_Metadata), intent(inout) :: this
157 character(len=*), intent(in) :: name
158 real(c_float), intent(in) :: value
159 call atlas__Metadata__set_float(this%CPTR_PGIBUG_A, c_str(name) ,value)
160 end subroutine Metadata__set_real32
161
162 subroutine Metadata__set_real64(this, name, value)
163 use atlas_metadata_c_binding
164 use, intrinsic :: iso_c_binding
165 use fckit_c_interop_module, only : c_str
166 class(atlas_Metadata), intent(inout) :: this
167 character(len=*), intent(in) :: name
168 real(c_double), intent(in) :: value
169 call atlas__Metadata__set_double(this%CPTR_PGIBUG_A, c_str(name) ,value)
170 end subroutine Metadata__set_real64
171
172 subroutine Metadata__set_string(this, name, value)
173 use atlas_metadata_c_binding
174 use fckit_c_interop_module, only : c_str
175 class(atlas_Metadata), intent(inout) :: this
176 character(len=*), intent(in) :: name
177 character(len=*), intent(in) :: value
178 call atlas__Metadata__set_string(this%CPTR_PGIBUG_A, c_str(name) , c_str(value) )
179 end subroutine Metadata__set_string
180
181 subroutine Metadata__get_logical(this, name, value)
182 use atlas_metadata_c_binding
183 use fckit_c_interop_module, only : c_str
184 class(atlas_Metadata), intent(in) :: this
185 character(len=*), intent(in) :: name
186 logical, intent(out) :: value
187 integer :: value_int
188 value_int = atlas__Metadata__get_int(this%CPTR_PGIBUG_A,c_str(name) )
189 if (value_int > 0) then
190 value = .True.
191 else
192 value = .False.
193 end if
194 end subroutine Metadata__get_logical
195
196 subroutine Metadata__get_int32(this, name, value)
197 use atlas_metadata_c_binding
198 use fckit_c_interop_module, only : c_str
199 class(atlas_Metadata), intent(in) :: this
200 character(len=*), intent(in) :: name
201 integer, intent(out) :: value
202 value = atlas__Metadata__get_int(this%CPTR_PGIBUG_A, c_str(name) )
203 end subroutine Metadata__get_int32
204
205 subroutine Metadata__get_real32(this, name, value)
206 use atlas_metadata_c_binding
207 use, intrinsic :: iso_c_binding
208 use fckit_c_interop_module, only : c_str
209 class(atlas_Metadata), intent(in) :: this
210 character(len=*), intent(in) :: name
211 real(c_float), intent(out) :: value
212 value = atlas__Metadata__get_float(this%CPTR_PGIBUG_A, c_str(name) )
213 end subroutine Metadata__get_real32
214
215 subroutine Metadata__get_real64(this, name, value)
216 use atlas_metadata_c_binding
217 use, intrinsic :: iso_c_binding
218 use fckit_c_interop_module, only : c_str
219 class(atlas_Metadata), intent(in) :: this
220 character(len=*), intent(in) :: name
221 real(c_double), intent(out) :: value
222 value = atlas__Metadata__get_double(this%CPTR_PGIBUG_A, c_str(name) )
223 end subroutine Metadata__get_real64
224
225 subroutine Metadata__get_string(this, name, value)
226 use atlas_metadata_c_binding
227 use fckit_c_interop_module, only : c_str, c_str_to_string
228 class(atlas_Metadata), intent(in) :: this
229 character(len=*), intent(in) :: name
230 character(len=:), allocatable, intent(out) :: value
231 character(len=MAX_STR_LEN) :: value_cstr
232 call atlas__Metadata__get_string(this%CPTR_PGIBUG_A, c_str(name), value_cstr, MAX_STR_LEN )
233 value = c_str_to_string(value_cstr)
234 end subroutine Metadata__get_string
235
236 subroutine Metadata__set_array_int32(this, name, value)
237 use atlas_metadata_c_binding
238 use fckit_c_interop_module, only : c_str
239 use, intrinsic :: iso_c_binding
240 class(atlas_Metadata), intent(in) :: this
241 character(len=*), intent(in) :: name
242 integer(c_int), intent(in) :: value(:)
243 call atlas__Metadata__set_array_int(this%CPTR_PGIBUG_A, c_str(name), &
244 & value, size(value) )
245 end subroutine Metadata__set_array_int32
246
247 subroutine Metadata__set_array_int64(this, name, value)
248 use atlas_metadata_c_binding
249 use fckit_c_interop_module, only : c_str
250 use, intrinsic :: iso_c_binding
251 class(atlas_Metadata), intent(in) :: this
252 character(len=*), intent(in) :: name
253 integer(c_long), intent(in) :: value(:)
254 call atlas__Metadata__set_array_long(this%CPTR_PGIBUG_A, c_str(name), &
255 & value, size(value) )
256 end subroutine Metadata__set_array_int64
257
258 subroutine Metadata__set_array_real32(this, name, value)
259 use atlas_metadata_c_binding
260 use fckit_c_interop_module, only : c_str
261 use, intrinsic :: iso_c_binding
262 class(atlas_Metadata), intent(in) :: this
263 character(len=*), intent(in) :: name
264 real(c_float), intent(in) :: value(:)
265 call atlas__Metadata__set_array_float(this%CPTR_PGIBUG_A, c_str(name), &
266 & value, size(value) )
267 end subroutine Metadata__set_array_real32
268
269 subroutine Metadata__set_array_real64(this, name, value)
270 use atlas_metadata_c_binding
271 use fckit_c_interop_module, only : c_str
272 use, intrinsic :: iso_c_binding
273 class(atlas_Metadata), intent(in) :: this
274 character(len=*), intent(in) :: name
275 real(c_double), intent(in) :: value(:)
276 call atlas__Metadata__set_array_double(this%CPTR_PGIBUG_A, c_str(name), &
277 & value, size(value) )
278 end subroutine Metadata__set_array_real64
279
280 subroutine Metadata__get_array_int32(this, name, value)
281 use atlas_metadata_c_binding
282 use fckit_c_interop_module, only : c_str, c_ptr_free
283 use, intrinsic :: iso_c_binding
284 class(atlas_Metadata), intent(in) :: this
285 character(len=*), intent(in) :: name
286 integer(c_int), allocatable, intent(out) :: value(:)
287 type(c_ptr) :: value_cptr
288 integer(c_int), pointer :: value_fptr(:)
289 integer :: value_size
290 integer :: value_allocated
291 call atlas__Metadata__get_array_int(this%CPTR_PGIBUG_A, c_str(name), &
292 & value_cptr, value_size, value_allocated )
293 call c_f_pointer(value_cptr,value_fptr,[value_size])
294 allocate(value(value_size))
295 value(:) = value_fptr(:)
296 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
297 end subroutine Metadata__get_array_int32
298
299 subroutine Metadata__get_array_int64(this, name, value)
300 use atlas_metadata_c_binding
301 use fckit_c_interop_module, only : c_str, c_ptr_free
302 use, intrinsic :: iso_c_binding
303 class(atlas_Metadata), intent(in) :: this
304 character(len=*), intent(in) :: name
305 integer(c_long), allocatable, intent(out) :: value(:)
306 type(c_ptr) :: value_cptr
307 integer(c_long), pointer :: value_fptr(:)
308 integer :: value_size
309 integer :: value_allocated
310 call atlas__Metadata__get_array_long(this%CPTR_PGIBUG_A, c_str(name), &
311 & value_cptr, value_size, value_allocated )
312 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
313 allocate(value(value_size))
314 value(:) = value_fptr(:)
315 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
316 end subroutine Metadata__get_array_int64
317
318 subroutine Metadata__get_array_real32(this, name, value)
319 use atlas_metadata_c_binding
320 use, intrinsic :: iso_c_binding
321 use fckit_c_interop_module, only : c_str, c_ptr_free
322 class(atlas_Metadata), intent(in) :: this
323 character(len=*), intent(in) :: name
324 real(c_float), allocatable, intent(out) :: value(:)
325 type(c_ptr) :: value_cptr
326 real(c_float), pointer :: value_fptr(:)
327 integer :: value_size
328 integer :: value_allocated
329 call atlas__Metadata__get_array_float(this%CPTR_PGIBUG_A, c_str(name), &
330 & value_cptr, value_size, value_allocated )
331 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
332 allocate(value(value_size))
333 value(:) = value_fptr(:)
334 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
335 end subroutine Metadata__get_array_real32
336
337 subroutine Metadata__get_array_real64(this, name, value)
338 use atlas_metadata_c_binding
339 use, intrinsic :: iso_c_binding
340 use fckit_c_interop_module, only : c_str, c_ptr_free
341 class(atlas_Metadata), intent(in) :: this
342 character(len=*), intent(in) :: name
343 real(c_double), allocatable, intent(out) :: value(:)
344 type(c_ptr) :: value_cptr
345 real(c_double), pointer :: value_fptr(:)
346 integer :: value_size
347 integer :: value_allocated
348 call atlas__Metadata__get_array_double(this%CPTR_PGIBUG_A, c_str(name), &
349 & value_cptr, value_size, value_allocated )
350 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
351 allocate(value(value_size))
352 value(:) = value_fptr(:)
353 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
354 end subroutine Metadata__get_array_real64
355
356 subroutine MetaData__print(this,channel)
357 use atlas_metadata_c_binding
358 use fckit_log_module, only : fckit_logchannel
359 class(atlas_Metadata), intent(in) :: this
360 type(fckit_logchannel), intent(in) :: channel
361 call atlas__Metadata__print(this%CPTR_PGIBUG_A,channel%CPTR_PGIBUG_A)
362 end subroutine Metadata__print
363
364 function Metadata__json(this) result(json)
365 use atlas_metadata_c_binding
366 use, intrinsic :: iso_c_binding
367 use fckit_c_interop_module, only : c_ptr_to_string, c_ptr_free
368 character(len=:), allocatable :: json
369 class(atlas_Metadata), intent(in) :: this
370 type(c_ptr) :: json_cptr
371 integer(c_int) :: json_size
372 integer(c_int) :: json_allocated
373 call atlas__Metadata__json(this%CPTR_PGIBUG_A,json_cptr,json_size,json_allocated)
374 allocate(character(len=json_size) :: json )
375 json = c_ptr_to_string(json_cptr)
376 if( json_allocated == 1 ) call c_ptr_free(json_cptr)
377 end function Metadata__json
378
379 !-------------------------------------------------------------------------------
380
381 #if FCKIT_FINAL_NOT_INHERITING
382 ATLAS_FINAL subroutine atlas_Metadata__final_auto(this)
383 type(atlas_Metadata), intent(inout) :: this
384 #if FCKIT_FINAL_DEBUGGING
385 write(0,*) "atlas_Metadata__final_auto"
386 #endif
387 #if FCKIT_FINAL_NOT_PROPAGATING
388 call this%final()
389 #endif
390 FCKIT_SUPPRESS_UNUSED( this )
391 end subroutine
392 #endif
393
394 end module atlas_metadata_module
395