NCEPLIBS-bufr  11.7.0
 All Data Structures Files Functions Variables Pages
bufr_interface.f90
Go to the documentation of this file.
1 
7 
17 
18  use iso_c_binding
19 
20  implicit none
21 
22  private
23  public :: open_c, close_c
24  public :: openbf_c, closbf_c
25  public :: exitbufr_c
26  public :: ireadmg_c
27  public :: ireadsb_c
28  public :: ufbint_c
29  public :: ufbrep_c
30  public :: mtinfo_c
31 
32  integer, allocatable, target, save :: isc_f(:)
33  integer, allocatable, target, save :: link_f(:)
34  integer, allocatable, target, save :: itp_f(:)
35  integer, allocatable, target, save :: jmpb_f(:)
36  character(len=10), allocatable, target, save :: tag_f(:)
37  character(len=3), allocatable, target, save :: typ_f(:)
38 
39 contains
40 
41 !Private
42 
51 function c_f_string(c_str) result(f_str)
52  character(kind=c_char,len=1), intent(in) :: c_str(*)
53  character(len=:), allocatable :: f_str
54  integer :: nchars
55 
56  nchars = 1
57  do while (c_str(nchars) /= c_null_char)
58  nchars = nchars + 1
59  end do
60  nchars = nchars - 1
61 
62  allocate(character(len=nchars) :: f_str)
63  f_str = transfer(c_str(1:nchars), f_str)
64 end function c_f_string
65 
66 
76 subroutine copy_f_c_str(f_str, c_str, c_str_len)
77  character(len=*), target, intent(in) :: f_str
78  character(kind=c_char, len=1), intent(inout) :: c_str(*)
79  integer, intent(in) :: c_str_len
80  integer :: max_str_len
81 
82  if (c_str_len /= 0) then
83  max_str_len = c_str_len
84  c_str(1)(1:max_str_len) = f_str(1:max_str_len)
85  c_str(1)(max_str_len:max_str_len) = c_null_char
86  end if
87 end subroutine copy_f_c_str
88 
89 !Public
90 
100 subroutine open_c(lunit, filepath) bind(C, name='open_f')
101  integer(c_int), value, intent(in) :: lunit
102  character(kind=c_char, len=1) :: filepath
103 
104  open(lunit, file=c_f_string(filepath))
105 end subroutine open_c
106 
107 
116 subroutine close_c(lunit) bind(C, name='close_f')
117  integer(c_int), value, intent(in) :: lunit
118 
119  close(unit=lunit)
120 end subroutine close_c
121 
122 
132 subroutine openbf_c(bufr_unit, cio, table_file_id) bind(C, name='openbf_f')
133  integer(c_int), value, intent(in) :: bufr_unit
134  character(kind=c_char, len=1), intent(in) :: cio
135  integer(c_int), value, intent(in) :: table_file_id
136 
137  call openbf(bufr_unit, c_f_string(cio), table_file_id)
138 end subroutine openbf_c
139 
140 
148 subroutine closbf_c(bufr_unit) bind(C, name='closbf_f')
149  integer(c_int), value, intent(in) :: bufr_unit
150 
151  call closbf(bufr_unit)
152 end subroutine closbf_c
153 
154 
161 subroutine exitbufr_c() bind(C, name='exitbufr_f')
162  call exitbufr()
163 end subroutine exitbufr_c
164 
165 
176 function ireadmg_c(bufr_unit, c_subset, iddate, subset_str_len) result(ires) bind(C, name='ireadmg_f')
177  integer(c_int), value, intent(in) :: bufr_unit
178  character(kind=c_char, len=1), intent(inout) :: c_subset(*)
179  integer(c_int), intent(out) :: iddate
180  integer(c_int), value, intent(in) :: subset_str_len
181  integer(c_int) :: ires
182  character(len=25) :: f_subset
183  integer :: ireadmg
184 
185  ires = ireadmg(bufr_unit, f_subset, iddate)
186 
187  if (ires == 0) then
188  call copy_f_c_str(f_subset, c_subset, int(subset_str_len))
189  end if
190 end function ireadmg_c
191 
192 
200 function ireadsb_c(bufr_unit) result(ires) bind(C, name='ireadsb_f')
201  integer(c_int), value, intent(in) :: bufr_unit
202  integer(c_int) :: ires
203  integer :: ireadsb
204 
205  ires = ireadsb(bufr_unit)
206 end function ireadsb_c
207 
208 
220 subroutine ufbint_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic) bind(C, name='ufbint_f')
221  integer(c_int), value, intent(in) :: bufr_unit
222  type(c_ptr), intent(inout) :: c_data
223  integer(c_int), value, intent(in) :: dim_1, dim_2
224  integer(c_int), intent(out) :: iret
225  character(kind=c_char, len=1), intent(in) :: table_b_mnemonic
226  real, pointer :: f_data
227 
228  call c_f_pointer(c_data, f_data)
229  call ufbint(bufr_unit, f_data, dim_1, dim_2, iret, c_f_string(table_b_mnemonic))
230 end subroutine ufbint_c
231 
232 
244 subroutine ufbrep_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic) bind(C, name='ufbrep_f')
245  integer(c_int), value, intent(in) :: bufr_unit
246  type(c_ptr), intent(inout) :: c_data
247  integer(c_int), value, intent(in) :: dim_1, dim_2
248  integer(c_int), intent(out) :: iret
249  character(kind=c_char, len=1), intent(in) :: table_b_mnemonic
250  real, pointer :: f_data
251 
252  call c_f_pointer(c_data, f_data)
253  call ufbrep(bufr_unit, f_data, dim_1, dim_2, iret, c_f_string(table_b_mnemonic))
254 end subroutine ufbrep_c
255 
256 
266 subroutine mtinfo_c(path, file_unit_1, file_unit_2) bind(C, name='mtinfo_f')
267  character(kind=c_char, len=1), intent(in) :: path
268  integer(c_int), value, intent(in) :: file_unit_1
269  integer(c_int), value, intent(in) :: file_unit_2
270 
271  call mtinfo(c_f_string(path), file_unit_1, file_unit_2)
272 end subroutine mtinfo_c
273 
274 
275 ! ----------------------------------------------------------------------
277 ! ----------------------------------------------------------------------
278 
289 subroutine status_c(file_unit, lun, il, im) bind(C, name='status_f')
290  integer(c_int), value, intent(in) :: file_unit
291  integer(c_int), intent(out) :: lun
292  integer(c_int), intent(out) :: il
293  integer(c_int), intent(out) :: im
294 
295  call status(file_unit, lun, il, im)
296 end subroutine status_c
297 
298 
307 subroutine get_isc_c(isc_ptr, isc_size) bind(C, name='get_isc_f')
308  use moda_tables
309  type(c_ptr), intent(inout) :: isc_ptr
310  integer(c_int), intent(out) :: isc_size
311 
312  allocate(isc_f(ntab))
313  isc_f(1:ntab) = isc(1:ntab)
314  isc_size = size(isc_f)
315  isc_ptr = c_loc(isc_f(1))
316 end subroutine get_isc_c
317 
318 
327 subroutine get_link_c(link_ptr, link_size) bind(C, name='get_link_f')
328  use moda_tables
329  type(c_ptr), intent(inout) :: link_ptr
330  integer(c_int), intent(out) :: link_size
331 
332  allocate(link_f(ntab))
333  link_f(1:ntab) = link(1:ntab)
334  link_size = size(link_f)
335  link_ptr = c_loc(link_f(1))
336 end subroutine get_link_c
337 
338 
347 subroutine get_itp_c(itp_ptr, itp_size) bind(C, name='get_itp_f')
348  use moda_tables
349  type(c_ptr), intent(inout) :: itp_ptr
350  integer(c_int), intent(out) :: itp_size
351 
352  allocate(itp_f(ntab))
353  itp_f(1:ntab) = itp(1:ntab)
354  itp_size = size(itp_f)
355  itp_ptr = c_loc(itp_f(1))
356 end subroutine get_itp_c
357 
358 
368 subroutine get_typ_c(typ_ptr, typ_len, mem_size) bind(C, name='get_typ_f')
369  use moda_tables
370  type(c_ptr), intent(inout) :: typ_ptr
371  integer(c_int), intent(out) :: typ_len
372  integer(c_int), intent(out) :: mem_size
373 
374  allocate(typ_f(ntab))
375  typ_f(1:ntab) = typ(1:ntab)
376  typ_len = len(typ(1))
377  mem_size = size(typ_f)
378  typ_ptr = c_loc(typ_f(1))
379 end subroutine get_typ_c
380 
381 
391 subroutine get_tag_c(tag_ptr, tag_len, mem_size) bind(C, name='get_tag_f')
392  use moda_tables
393  type(c_ptr), intent(inout) :: tag_ptr
394  integer(c_int), intent(out) :: tag_len
395  integer(c_int), intent(out) :: mem_size
396 
397  allocate(tag_f(ntab))
398  tag_f(1:ntab) = tag(1:ntab)
399  tag_len = len(tag(1))
400  mem_size = size(tag_f)
401  tag_ptr = c_loc(tag_f(1))
402 end subroutine get_tag_c
403 
404 
413 subroutine get_jmpb_c(jmpb_ptr, jmpb_size) bind(C, name='get_jmpb_f')
414  use moda_tables
415  type(c_ptr), intent(inout) :: jmpb_ptr
416  integer(c_int), intent(out) :: jmpb_size
417 
418  allocate(jmpb_f(ntab))
419  jmpb_f(1:ntab) = jmpb(1:ntab)
420  jmpb_size = size(jmpb_f)
421  jmpb_ptr = c_loc(jmpb_f(1))
422 end subroutine get_jmpb_c
423 
424 
433 subroutine get_inode_c(lun, start_node) bind(C, name='get_inode_f')
434  use moda_msgcwd
435  integer(c_int), value, intent(in) :: lun
436  integer(c_int), intent(out) :: start_node
437 
438  start_node = inode(lun)
439 end subroutine get_inode_c
440 
441 
450 subroutine get_nval_c(lun, numNodes) bind(C, name='get_nval_f')
451  use moda_usrint
452  integer(c_int), value, intent(in) :: lun
453  integer(c_int), intent(out) :: numnodes
454 
455  numnodes = nval(lun)
456 end subroutine get_nval_c
457 
458 
468 subroutine get_val_c(lun, val_ptr, val_size) bind(C, name='get_val_f')
469  use moda_usrint
470  integer(c_int), value, intent(in) :: lun
471  type(c_ptr), intent(inout) :: val_ptr
472  integer(c_int), intent(out) :: val_size
473 
474  val_size = size(val(:, lun))
475  val_ptr = c_loc(val(1, lun))
476 end subroutine get_val_c
477 
478 
488 subroutine get_inv_c(lun, inv_ptr, inv_size) bind(C, name='get_inv_f')
489  use moda_usrint
490  integer(c_int), value, intent(in) :: lun
491  type(c_ptr), intent(inout) :: inv_ptr
492  integer(c_int), intent(out) :: inv_size
493 
494  inv_size = size(inv(:, lun))
495  inv_ptr = c_loc(inv(1, lun))
496 end subroutine get_inv_c
497 
498 
504 subroutine delete_table_data_c() bind(C, name='delete_table_data_f')
505  if (allocated(isc_f)) deallocate(isc_f)
506  if (allocated(link_f)) deallocate(link_f)
507  if (allocated(itp_f)) deallocate(itp_f)
508  if (allocated(typ_f)) deallocate(typ_f)
509  if (allocated(tag_f)) deallocate(tag_f)
510  if (allocated(jmpb_f)) deallocate(jmpb_f)
511 end subroutine delete_table_data_c
512 
513 end module bufr_c_interface_mod
subroutine, public exitbufr_c()
Wraps BUFRLIB "exitbufr" subroutine. Closes all open file units used by BUFRLIB.
function ireadmg(LUNIT, SUBSET, IDATE)
This function calls BUFRLIB subroutine readmg() and passes back its return code as the function value...
Definition: ireadmg.f:39
subroutine get_val_c(lun, val_ptr, val_size)
Get pointer to the moda_usrint VAL array.
subroutine, public open_c(lunit, filepath)
Wraps fortran "open" statement so we can open a Fortran file from a C program.
function ireadsb(LUNIT)
This function calls BUFRLIB subroutine readsb() and passes back its return code as the function value...
Definition: ireadsb.f:30
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:144
subroutine, public openbf_c(bufr_unit, cio, table_file_id)
Wraps BUFRLIB "openbf" subroutine.
subroutine, public ufbint_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic)
Wraps BUFRLIB "ufbint" function.
subroutine mtinfo(CMTDIR, LUNMT1, LUNMT2)
This subroutine allows the specification of the directory location and Fortran logical unit numbers t...
Definition: mtinfo.f:46
subroutine closbf(LUNIT)
This subroutine closes the connection between logical unit LUNIT and the BUFRLIB software.
Definition: closbf.f:34
subroutine 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 ufbrep_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic)
Wraps BUFRLIB "ufbrep" function.
This module contains array and variable declarations used to store the internal jump/link table...
Definition: moda_tables.F:13
subroutine status(LUNIT, LUN, IL, IM)
This subroutine checks whether a specified Fortran logical unit number is currently connected to the ...
Definition: status.f:55
subroutine openbf(LUNIT, IO, LUNDX)
This subroutine connects a new file to the BUFRLIB software for input or output operations.
Definition: openbf.f:138
subroutine get_link_c(link_ptr, link_size)
Get copy of the moda_tables LINK array.
subroutine get_itp_c(itp_ptr, itp_size)
Get copy of the moda_tables ITP array.
subroutine get_inode_c(lun, start_node)
Get the bufr node idx for the start node of the subset.
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:152
subroutine, public closbf_c(bufr_unit)
Wraps BUFRLIB "closbf" subroutine.
subroutine exitbufr
This subroutine frees all dynamically-allocated memory, closes all logical units that are open within...
Definition: exitbufr.f:38
subroutine 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 get_nval_c(lun, numNodes)
Get the number of values in the current subset.
This module contains functions which wrap certain Fortran BUFRLIB functions so they can be called fro...
subroutine get_typ_c(typ_ptr, typ_len, mem_size)
Get copy of the moda_tables TYP array.
subroutine get_tag_c(tag_ptr, tag_len, mem_size)
Get copy of the moda_tables TAG array.
integer(c_int) function, public ireadmg_c(bufr_unit, c_subset, iddate, subset_str_len)
Wraps BUFRLIB "ireadmg" subroutine.
subroutine get_inv_c(lun, inv_ptr, inv_size)
Get pointer to the moda_usrint INV array.
subroutine, public close_c(lunit)
Wraps fortran "close" statement so we can close a Fortran file from a C program.