NCEPLIBS-bufr  11.6.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 contains
33 
34 !Private
35 
44 function c_f_string(c_str) result(f_str)
45  character(kind=c_char,len=1), intent(in) :: c_str(*)
46  character(len=:), allocatable :: f_str
47  integer :: nchars
48 
49  nchars = 1
50  do while (c_str(nchars) /= c_null_char)
51  nchars = nchars + 1
52  end do
53  nchars = nchars - 1
54 
55  allocate(character(len=nchars) :: f_str)
56  f_str = transfer(c_str(1:nchars), f_str)
57 end function c_f_string
58 
59 
69 subroutine copy_f_c_str(f_str, c_str, c_str_len)
70  character(len=*), target, intent(in) :: f_str
71  character(kind=c_char, len=1), intent(inout) :: c_str(*)
72  integer, intent(in) :: c_str_len
73  integer :: max_str_len
74 
75  if (c_str_len /= 0) then
76  max_str_len = min(c_str_len, len_trim(f_str) + 1)
77  c_str(1)(1:max_str_len) = f_str(1:max_str_len)
78  c_str(1)(max_str_len:max_str_len) = c_null_char
79  end if
80 end subroutine copy_f_c_str
81 
82 !Public
83 
93 subroutine open_c(lunit, filepath) bind(C, name='open_f')
94  integer(c_int), value, intent(in) :: lunit
95  character(kind=c_char, len=1) :: filepath
96 
97  open(lunit, file=c_f_string(filepath))
98 end subroutine open_c
99 
100 
109 subroutine close_c(lunit) bind(C, name='close_f')
110  integer(c_int), value, intent(in) :: lunit
111 
112  close(unit=lunit)
113 end subroutine close_c
114 
115 
125 subroutine openbf_c(bufr_unit, cio, table_file_id) bind(C, name='openbf_f')
126  integer(c_int), value, intent(in) :: bufr_unit
127  character(kind=c_char, len=1), intent(in) :: cio
128  integer(c_int), value, intent(in) :: table_file_id
129 
130  call openbf(bufr_unit, c_f_string(cio), table_file_id)
131 end subroutine openbf_c
132 
133 
141 subroutine closbf_c(bufr_unit) bind(C, name='closbf_f')
142  integer(c_int), value, intent(in) :: bufr_unit
143 
144  call closbf(bufr_unit)
145 end subroutine closbf_c
146 
147 
154 subroutine exitbufr_c() bind(C, name='exitbufr_f')
155  call exitbufr()
156 end subroutine exitbufr_c
157 
158 
169 function ireadmg_c(bufr_unit, c_subset, iddate, subset_str_len) result(ires) bind(C, name='ireadmg_f')
170  integer(c_int), value, intent(in) :: bufr_unit
171  character(kind=c_char, len=1), intent(inout) :: c_subset(*)
172  integer(c_int), intent(out) :: iddate
173  integer(c_int), value, intent(in) :: subset_str_len
174  integer(c_int) :: ires
175  character(len=25) :: f_subset
176  integer :: ireadmg
177 
178  ires = ireadmg(bufr_unit, f_subset, iddate)
179  call copy_f_c_str(f_subset, c_subset, int(subset_str_len))
180 end function ireadmg_c
181 
182 
190 function ireadsb_c(bufr_unit) result(ires) bind(C, name='ireadsb_f')
191  integer(c_int), value, intent(in) :: bufr_unit
192  integer(c_int) :: ires
193  integer :: ireadsb
194 
195  ires = ireadsb(bufr_unit)
196 end function ireadsb_c
197 
198 
210 subroutine ufbint_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic) bind(C, name='ufbint_f')
211  integer(c_int), value, intent(in) :: bufr_unit
212  type(c_ptr), intent(inout) :: c_data
213  integer(c_int), value, intent(in) :: dim_1, dim_2
214  integer(c_int), intent(out) :: iret
215  character(kind=c_char, len=1), intent(in) :: table_b_mnemonic
216  real, pointer :: f_data
217 
218  call c_f_pointer(c_data, f_data)
219  call ufbint(bufr_unit, f_data, dim_1, dim_2, iret, c_f_string(table_b_mnemonic))
220 end subroutine ufbint_c
221 
222 
234 subroutine ufbrep_c(bufr_unit, c_data, dim_1, dim_2, iret, table_b_mnemonic) bind(C, name='ufbrep_f')
235  integer(c_int), value, intent(in) :: bufr_unit
236  type(c_ptr), intent(inout) :: c_data
237  integer(c_int), value, intent(in) :: dim_1, dim_2
238  integer(c_int), intent(out) :: iret
239  character(kind=c_char, len=1), intent(in) :: table_b_mnemonic
240  real, pointer :: f_data
241 
242  call c_f_pointer(c_data, f_data)
243  call ufbrep(bufr_unit, f_data, dim_1, dim_2, iret, c_f_string(table_b_mnemonic))
244 end subroutine ufbrep_c
245 
246 
256 subroutine mtinfo_c(path, file_unit_1, file_unit_2) bind(C, name='mtinfo_f')
257  character(kind=c_char, len=1), intent(in) :: path
258  integer(c_int), value, intent(in) :: file_unit_1
259  integer(c_int), value, intent(in) :: file_unit_2
260 
261  call mtinfo(c_f_string(path), file_unit_1, file_unit_2)
262 end subroutine mtinfo_c
263 
264 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, 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
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.
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 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, public mtinfo_c(path, file_unit_1, file_unit_2)
Wraps BUFRLIB "mtinfo" function.
This module contains functions which wrap certain Fortran BUFRLIB functions so they can be called fro...
integer(c_int) function, public ireadmg_c(bufr_unit, c_subset, iddate, subset_str_len)
Wraps BUFRLIB "ireadmg" subroutine.
subroutine, public close_c(lunit)
Wraps fortran "close" statement so we can close a Fortran file from a C program.