NCEPLIBS-bufr 11.7.1
bufr_interface.f90
Go to the documentation of this file.
1
7
22
23 use iso_c_binding
24
25 implicit none
26
27 private
28 public :: open_c, close_c
29 public :: openbf_c, closbf_c
30 public :: exitbufr_c
31 public :: ireadmg_c
32 public :: ireadsb_c
33 public :: ufbint_c
34 public :: ufbrep_c
35 public :: mtinfo_c
36 public :: status_c
37 public :: nemdefs_c
38 public :: nemspecs_c
39 public :: nemtab_c
40 public :: nemtbb_c
41 public :: get_isc_c
42 public :: get_link_c
43 public :: get_itp_c
44 public :: get_typ_c
45 public :: get_tag_c
46 public :: get_jmpb_c
47 public :: get_inode_c
48 public :: get_nval_c
49 public :: get_val_c
50 public :: get_inv_c
51 public :: delete_table_data_c
52
53 integer, allocatable, target, save :: isc_f(:)
54 integer, allocatable, target, save :: link_f(:)
55 integer, allocatable, target, save :: itp_f(:)
56 integer, allocatable, target, save :: jmpb_f(:)
57 character(len=10), allocatable, target, save :: tag_f(:)
58 character(len=3), allocatable, target, save :: typ_f(:)
59
60contains
61
62!Private
63
72function c_f_string(c_str) result(f_str)
73 character(kind=c_char,len=1), intent(in) :: c_str(*)
74 character(len=:), allocatable :: f_str
75 integer :: nchars
76
77 nchars = 1
78 do while (c_str(nchars) /= c_null_char)
79 nchars = nchars + 1
80 end do
81 nchars = nchars - 1
82
83 allocate(character(len=nchars) :: f_str)
84 f_str = transfer(c_str(1:nchars), f_str)
85end function c_f_string
86
87
97subroutine copy_f_c_str(f_str, c_str, c_str_len)
98 character(len=*), target, intent(in) :: f_str
99 character(kind=c_char, len=1), intent(inout) :: c_str(*)
100 integer, intent(in) :: c_str_len
101 integer :: max_str_len
102
103 if (c_str_len /= 0) then
104 max_str_len = c_str_len
105 c_str(1)(1:max_str_len) = f_str(1:max_str_len)
106 c_str(1)(max_str_len:max_str_len) = c_null_char
107 end if
108end subroutine copy_f_c_str
109
110!Public
111
121subroutine open_c(lunit, filepath) bind(C, name='open_f')
122 integer(c_int), value, intent(in) :: lunit
123 character(kind=c_char, len=1) :: filepath
124
125 open(lunit, file=c_f_string(filepath))
126end subroutine open_c
127
128
137subroutine close_c(lunit) bind(C, name='close_f')
138 integer(c_int), value, intent(in) :: lunit
139
140 close(unit=lunit)
141end subroutine close_c
142
143
153subroutine openbf_c(bufr_unit, cio, table_file_id) bind(C, name='openbf_f')
154 integer(c_int), value, intent(in) :: bufr_unit
155 character(kind=c_char, len=1), intent(in) :: cio
156 integer(c_int), value, intent(in) :: table_file_id
157
158 call openbf(bufr_unit, c_f_string(cio), table_file_id)
159end subroutine openbf_c
160
161
169subroutine closbf_c(bufr_unit) bind(C, name='closbf_f')
170 integer(c_int), value, intent(in) :: bufr_unit
171
172 call closbf(bufr_unit)
173end subroutine closbf_c
174
175
182subroutine exitbufr_c() bind(C, name='exitbufr_f')
183 call exitbufr()
184end subroutine exitbufr_c
185
186
197function ireadmg_c(bufr_unit, c_subset, iddate, subset_str_len) result(ires) bind(C, name='ireadmg_f')
198 integer(c_int), value, intent(in) :: bufr_unit
199 character(kind=c_char, len=1), intent(out) :: c_subset(*)
200 integer(c_int), intent(out) :: iddate
201 integer(c_int), value, intent(in) :: subset_str_len
202 integer(c_int) :: ires
203 character(len=25) :: f_subset
204 integer :: ireadmg
205
206 ires = ireadmg(bufr_unit, f_subset, iddate)
207
208 if (ires == 0) then
209 call copy_f_c_str(f_subset, c_subset, int(subset_str_len))
210 end if
211end function ireadmg_c
212
213
221function ireadsb_c(bufr_unit) result(ires) bind(C, name='ireadsb_f')
222 integer(c_int), value, intent(in) :: bufr_unit
223 integer(c_int) :: ires
224 integer :: ireadsb
225
226 ires = ireadsb(bufr_unit)
227end function ireadsb_c
228
229
241subroutine ufbint_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic) bind(C, name='ufbint_f')
242 integer(c_int), value, intent(in) :: bufr_unit
243 type(c_ptr), intent(inout) :: c_data
244 integer(c_int), value, intent(in) :: dim_1, dim_2
245 integer(c_int), intent(out) :: iret
246 character(kind=c_char, len=1), intent(in) :: table_b_mnemonic
247 real, pointer :: f_data
248
249 call c_f_pointer(c_data, f_data)
250 call ufbint(bufr_unit, f_data, dim_1, dim_2, iret, c_f_string(table_b_mnemonic))
251end subroutine ufbint_c
252
253
265subroutine ufbrep_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic) bind(C, name='ufbrep_f')
266 integer(c_int), value, intent(in) :: bufr_unit
267 type(c_ptr), intent(inout) :: c_data
268 integer(c_int), value, intent(in) :: dim_1, dim_2
269 integer(c_int), intent(out) :: iret
270 character(kind=c_char, len=1), intent(in) :: table_b_mnemonic
271 real, pointer :: f_data
272
273 call c_f_pointer(c_data, f_data)
274 call ufbrep(bufr_unit, f_data, dim_1, dim_2, iret, c_f_string(table_b_mnemonic))
275end subroutine ufbrep_c
276
277
287subroutine mtinfo_c(path, file_unit_1, file_unit_2) bind(C, name='mtinfo_f')
288 character(kind=c_char, len=1), intent(in) :: path
289 integer(c_int), value, intent(in) :: file_unit_1
290 integer(c_int), value, intent(in) :: file_unit_2
291
292 call mtinfo(c_f_string(path), file_unit_1, file_unit_2)
293end subroutine mtinfo_c
294
295
296! ----------------------------------------------------------------------
298! ----------------------------------------------------------------------
299
310subroutine status_c(file_unit, lun, il, im) bind(C, name='status_f')
311 integer(c_int), value, intent(in) :: file_unit
312 integer(c_int), intent(out) :: lun
313 integer(c_int), intent(out) :: il
314 integer(c_int), intent(out) :: im
315
316 call status(file_unit, lun, il, im)
317end subroutine status_c
318
319
333subroutine nemdefs_c(file_unit, mnemonic, unit_c, unit_str_len, desc_c, desc_str_len, iret) &
334 bind(c, name='nemdefs_f')
335 integer(c_int), value, intent(in) :: file_unit
336 character(kind=c_char,len=1), intent(in) :: mnemonic(*)
337 character(kind=c_char, len=1), intent(out) :: unit_c(*)
338 integer(c_int), value, intent(in) :: unit_str_len
339 character(kind=c_char, len=1), intent(out) :: desc_c(*)
340 integer(c_int), value, intent(in) :: desc_str_len
341 integer(c_int), intent(out) :: iret
342
343 character(len=24) :: unit_f
344 character(len=55) :: desc_f
345
346 ! Get the unit and description strings
347 call nemdefs ( file_unit, c_f_string(mnemonic), desc_f, unit_f, iret)
348
349 if (iret == 0) then
350 ! Copy the Unit fortran string into the resulting C style string.
351 call copy_f_c_str(unit_f, unit_c, min(len(unit_f) + 1, unit_str_len))
352 ! Copy the Unit fortran string into the resulting C style string.
353 call copy_f_c_str(desc_f, desc_c, min(len(desc_f) + 1, desc_str_len))
354 end if
355end subroutine nemdefs_c
356
357
371subroutine nemspecs_c(file_unit, mnemonic, mnemonic_idx, scale, reference, bits, iret) &
372 bind(c, name='nemspecs_f')
373 integer(c_int), value, intent(in) :: file_unit
374 character(kind=c_char,len=1), intent(in) :: mnemonic(*)
375 integer(c_int), value, intent(in) ::mnemonic_idx
376 integer(c_int), intent(out) :: scale
377 integer(c_int), intent(out) :: reference
378 integer(c_int), intent(out) :: bits
379 integer(c_int), intent(out) :: iret
380
381 ! Get the scale, reference and bits
382 call nemspecs(file_unit, c_f_string(mnemonic), mnemonic_idx, scale, reference, bits, iret)
383
384end subroutine nemspecs_c
385
386
399subroutine nemtab_c(bufr_unit, mnemonic, descriptor, table_type, table_idx) &
400 bind(c, name='nemtab_f')
401 integer(c_int), value, intent(in) :: bufr_unit
402 character(kind=c_char,len=1), intent(in) :: mnemonic(*)
403 integer(c_int), intent(out) :: descriptor
404 character(kind=c_char,len=1), intent(out) :: table_type(*)
405 integer(c_int), intent(out) :: table_idx
406
407 character(len=1) :: table_type_f
408
409 call nemtab(bufr_unit, c_f_string(mnemonic), descriptor, table_type_f, table_idx)
410
411 table_type(1)(1:1) = table_type_f(1:1)
412
413end subroutine nemtab_c
414
415
429subroutine nemtbb_c(bufr_unit, table_idx, unit_str, unit_str_len, scale, reference, bits) &
430 bind(c, name='nemtbb_f')
431 integer(c_int), intent(in), value :: bufr_unit
432 integer(c_int), intent(in), value :: table_idx
433 character(kind=c_char,len=1), intent(out) :: unit_str(*)
434 integer(c_int), intent(in), value :: unit_str_len
435 integer(c_int), intent(out) :: scale
436 integer(c_int), intent(out) :: reference
437 integer(c_int), intent(out) :: bits
438
439 character(len=24) :: unit_str_f
440
441 ! Get the scale, reference and bits
442 call nemtbb( bufr_unit, table_idx, unit_str_f, scale, reference, bits)
443 call copy_f_c_str(unit_str_f, unit_str, min(len(unit_str_f) + 1, unit_str_len))
444
445end subroutine nemtbb_c
446
447
456subroutine get_isc_c(isc_ptr, isc_size) bind(C, name='get_isc_f')
457 use moda_tables
458 type(c_ptr), intent(inout) :: isc_ptr
459 integer(c_int), intent(out) :: isc_size
460
461 allocate(isc_f(ntab))
462 isc_f(1:ntab) = isc(1:ntab)
463 isc_size = size(isc_f)
464 isc_ptr = c_loc(isc_f(1))
465end subroutine get_isc_c
466
467
476subroutine get_link_c(link_ptr, link_size) bind(C, name='get_link_f')
477 use moda_tables
478 type(c_ptr), intent(inout) :: link_ptr
479 integer(c_int), intent(out) :: link_size
480
481 allocate(link_f(ntab))
482 link_f(1:ntab) = link(1:ntab)
483 link_size = size(link_f)
484 link_ptr = c_loc(link_f(1))
485end subroutine get_link_c
486
487
496subroutine get_itp_c(itp_ptr, itp_size) bind(C, name='get_itp_f')
497 use moda_tables
498 type(c_ptr), intent(inout) :: itp_ptr
499 integer(c_int), intent(out) :: itp_size
500
501 allocate(itp_f(ntab))
502 itp_f(1:ntab) = itp(1:ntab)
503 itp_size = size(itp_f)
504 itp_ptr = c_loc(itp_f(1))
505end subroutine get_itp_c
506
507
517subroutine get_typ_c(typ_ptr, typ_len, mem_size) bind(C, name='get_typ_f')
518 use moda_tables
519 type(c_ptr), intent(inout) :: typ_ptr
520 integer(c_int), intent(out) :: typ_len
521 integer(c_int), intent(out) :: mem_size
522
523 allocate(typ_f(ntab))
524 typ_f(1:ntab) = typ(1:ntab)
525 typ_len = len(typ(1))
526 mem_size = size(typ_f)
527 typ_ptr = c_loc(typ_f(1))
528end subroutine get_typ_c
529
530
540subroutine get_tag_c(tag_ptr, tag_len, mem_size) bind(C, name='get_tag_f')
541 use moda_tables
542 type(c_ptr), intent(inout) :: tag_ptr
543 integer(c_int), intent(out) :: tag_len
544 integer(c_int), intent(out) :: mem_size
545
546 allocate(tag_f(ntab))
547 tag_f(1:ntab) = tag(1:ntab)
548 tag_len = len(tag(1))
549 mem_size = size(tag_f)
550 tag_ptr = c_loc(tag_f(1))
551end subroutine get_tag_c
552
553
562subroutine get_jmpb_c(jmpb_ptr, jmpb_size) bind(C, name='get_jmpb_f')
563 use moda_tables
564 type(c_ptr), intent(inout) :: jmpb_ptr
565 integer(c_int), intent(out) :: jmpb_size
566
567 allocate(jmpb_f(ntab))
568 jmpb_f(1:ntab) = jmpb(1:ntab)
569 jmpb_size = size(jmpb_f)
570 jmpb_ptr = c_loc(jmpb_f(1))
571end subroutine get_jmpb_c
572
573
582subroutine get_inode_c(lun, start_node) bind(C, name='get_inode_f')
583 use moda_msgcwd
584 integer(c_int), value, intent(in) :: lun
585 integer(c_int), intent(out) :: start_node
586
587 start_node = inode(lun)
588end subroutine get_inode_c
589
590
599subroutine get_nval_c(lun, numNodes) bind(C, name='get_nval_f')
600 use moda_usrint
601 integer(c_int), value, intent(in) :: lun
602 integer(c_int), intent(out) :: numnodes
603
604 numnodes = nval(lun)
605end subroutine get_nval_c
606
607
617subroutine get_val_c(lun, val_ptr, val_size) bind(C, name='get_val_f')
618 use moda_usrint
619 integer(c_int), value, intent(in) :: lun
620 type(c_ptr), intent(inout) :: val_ptr
621 integer(c_int), intent(out) :: val_size
622
623 val_size = size(val(:, lun))
624 val_ptr = c_loc(val(1, lun))
625end subroutine get_val_c
626
627
637subroutine get_inv_c(lun, inv_ptr, inv_size) bind(C, name='get_inv_f')
638 use moda_usrint
639 integer(c_int), value, intent(in) :: lun
640 type(c_ptr), intent(inout) :: inv_ptr
641 integer(c_int), intent(out) :: inv_size
642
643 inv_size = size(inv(:, lun))
644 inv_ptr = c_loc(inv(1, lun))
645end subroutine get_inv_c
646
647
653subroutine delete_table_data_c() bind(C, name='delete_table_data_f')
654 if (allocated(isc_f)) deallocate(isc_f)
655 if (allocated(link_f)) deallocate(link_f)
656 if (allocated(itp_f)) deallocate(itp_f)
657 if (allocated(typ_f)) deallocate(typ_f)
658 if (allocated(tag_f)) deallocate(tag_f)
659 if (allocated(jmpb_f)) deallocate(jmpb_f)
660end subroutine delete_table_data_c
661
662end module bufr_c_interface_mod
subroutine closbf(LUNIT)
This subroutine closes the connection between logical unit LUNIT and the BUFRLIB software.
Definition: closbf.f:35
subroutine exitbufr
This subroutine frees all dynamically-allocated memory, closes all logical units that are open within...
Definition: exitbufr.f:39
function ireadmg(LUNIT, SUBSET, IDATE)
This function calls BUFRLIB subroutine readmg() and passes back its return code as the function value...
Definition: ireadmg.f:40
function ireadsb(LUNIT)
This function calls BUFRLIB subroutine readsb() and passes back its return code as the function value...
Definition: ireadsb.f:31
subroutine mtinfo(CMTDIR, LUNMT1, LUNMT2)
This subroutine allows the specification of the directory location and Fortran logical unit numbers t...
Definition: mtinfo.f:47
This module contains functions which wrap Fortran BUFRLIB functions and variables so they can be used...
subroutine, public openbf_c(bufr_unit, cio, table_file_id)
Wraps BUFRLIB "openbf" subroutine.
subroutine, public status_c(file_unit, lun, il, im)
Get Raw BUFR data functions.
subroutine, public open_c(lunit, filepath)
Wraps fortran "open" statement so we can open a Fortran file from a C program.
subroutine, public get_isc_c(isc_ptr, isc_size)
Get copy of the moda_tables ISC array.
subroutine, public close_c(lunit)
Wraps fortran "close" statement so we can close a Fortran file from a C program.
subroutine, public delete_table_data_c()
Deletes the copies of the moda_tables arrays.
subroutine, public mtinfo_c(path, file_unit_1, file_unit_2)
Wraps BUFRLIB "mtinfo" function.
subroutine, public ufbint_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic)
Wraps BUFRLIB "ufbint" function.
subroutine, public closbf_c(bufr_unit)
Wraps BUFRLIB "closbf" subroutine.
subroutine, public get_jmpb_c(jmpb_ptr, jmpb_size)
Get copy of the moda_tables JMPB array.
integer(c_int) function, public ireadsb_c(bufr_unit)
Wraps BUFRLIB "ireadsb" function.
subroutine, public nemspecs_c(file_unit, mnemonic, mnemonic_idx, scale, reference, bits, iret)
Gets Table B scale, reference, and bits values. Wraps BUFRLIB "nemspecs".
subroutine, public nemtbb_c(bufr_unit, table_idx, unit_str, unit_str_len, scale, reference, bits)
Get information about a Table B descriptor. Wraps BUFRLIB "nemtbb".
subroutine, public get_itp_c(itp_ptr, itp_size)
Get copy of the moda_tables ITP array.
subroutine, public get_inv_c(lun, inv_ptr, inv_size)
Get pointer to the moda_usrint INV array.
subroutine, public get_nval_c(lun, numNodes)
Get the number of values in the current subset.
subroutine, public get_val_c(lun, val_ptr, val_size)
Get pointer to the moda_usrint VAL array.
subroutine, public ufbrep_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic)
Wraps BUFRLIB "ufbrep" function.
subroutine, public get_typ_c(typ_ptr, typ_len, mem_size)
Get copy of the moda_tables TYP array.
subroutine, public get_link_c(link_ptr, link_size)
Get copy of the moda_tables LINK array.
subroutine, public nemdefs_c(file_unit, mnemonic, unit_c, unit_str_len, desc_c, desc_str_len, iret)
Gets Table B Unit and Description strings for a mnemonic. Wraps BUFRLIB "nemdefs".
integer(c_int) function, public ireadmg_c(bufr_unit, c_subset, iddate, subset_str_len)
Wraps BUFRLIB "ireadmg" subroutine.
subroutine, public exitbufr_c()
Wraps BUFRLIB "exitbufr" subroutine. Closes all open file units used by BUFRLIB.
subroutine, public nemtab_c(bufr_unit, mnemonic, descriptor, table_type, table_idx)
This subroutine returns information about a descriptor from the internal DX BUFR tables,...
subroutine, public get_tag_c(tag_ptr, tag_len, mem_size)
Get copy of the moda_tables TAG array.
subroutine, public get_inode_c(lun, start_node)
Get the bufr node idx for the start node of the subset.
This module contains array and variable declarations used to store the internal jump/link table.
Definition: moda_tables.F:13
integer, dimension(:), allocatable link
Link indices corresponding to tag, typ and jmpb:
Definition: moda_tables.F:136
integer, dimension(:), allocatable itp
Integer type values corresponding to typ:
Definition: moda_tables.F:141
integer, dimension(:), allocatable isc
Scale factors corresponding to tag and typ:
Definition: moda_tables.F:140
character *3, dimension(:), allocatable typ
Type indicators corresponding to tag:
Definition: moda_tables.F:133
character *10, dimension(:), allocatable tag
Mnemonics in the jump/link table.
Definition: moda_tables.F:132
integer ntab
Number of entries in the jump/link table.
Definition: moda_tables.F:131
integer, dimension(:), allocatable jmpb
Jump backward indices corresponding to tag and typ:
Definition: moda_tables.F:137
subroutine nemdefs(LUNIT, NEMO, CELEM, CUNIT, IRET)
Given a Table B mnemonic defined in the DX BUFR Tables associated with a BUFR file (or in the master ...
Definition: nemdefs.f:37
subroutine nemspecs(LUNIT, NEMO, NNEMO, NSCL, NREF, NBTS, IRET)
Given a Table B mnemonic defined within a data subset, this subroutine returns the scale factor,...
Definition: nemspecs.f:52
subroutine nemtab(LUN, NEMO, IDN, TAB, IRET)
This subroutine returns information about a descriptor from the internal DX BUFR tables,...
Definition: nemtab.f:45
subroutine nemtbb(LUN, ITAB, UNIT, ISCL, IREF, IBIT)
This subroutine returns information about a Table B descriptor from the internal DX BUFR tables.
Definition: nemtbb.f:32
subroutine openbf(LUNIT, IO, LUNDX)
This subroutine connects a new file to the BUFRLIB software for input or output operations.
Definition: openbf.f:139
subroutine status(LUNIT, LUN, IL, IM)
This subroutine checks whether a specified Fortran logical unit number is currently connected to the ...
Definition: status.f:56
subroutine ufbint(LUNIN, USR, I1, I2, IRET, STR)
This subroutine reads or writes one or more data values from or to the BUFR data subset that is curre...
Definition: ufbint.f:153
subroutine ufbrep(LUNIN, USR, I1, I2, IRET, STR)
This subroutine reads or writes one or more data values from or to the BUFR data subset that is curre...
Definition: ufbrep.f:145