Skip to content

Commit

Permalink
squash+merge commit for Ron McLaren: added c wrappers for nemspecs, n…
Browse files Browse the repository at this point in the history
…emdefs, nemtab, and nemtbb
  • Loading branch information
rmclaren authored Aug 22, 2022
1 parent 0f7dd46 commit 8519a73
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 6 deletions.
162 changes: 156 additions & 6 deletions src/bufr_interface.f90
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
!> @file
!> @brief Enable a number of BUFRLIB subprograms to be called
!> via wrapper functions from C and C++ application programs.
!> @brief Enables a number of BUFRLIB functions and variables to be accessed
!> via wrapper functions from C and C++ based client programs.
!>
!> @author Ronald Mclaren
!> @date 2020-07-29

!> This module contains functions which wrap certain Fortran BUFRLIB
!> functions so they can be called from C and C++. The signatures of
!> the public functions match their Fortran equivalents, as shown within
!> the documentation for each of the individual functions.
!> This module contains functions which wrap Fortran BUFRLIB functions and
!> variables so they can be used from within C and C++ based apps. The
!> signatures of the public functions match their Fortran equivalents, as
!> shown within the documentation for each of the individual functions.
!> Local copies of some Fortran variables are stored as allocatable objects
!> especially isc, link, jmpb, tag and typ. Its the clients responsibility
!> to call delete_table_data_f in order to properly delete these variables.
!>
!>
!> @author Ronald Mclaren
!> @date 2020-07-29
Expand All @@ -28,6 +32,22 @@ module bufr_c_interface_mod
public :: ufbint_c
public :: ufbrep_c
public :: mtinfo_c
public :: status_c
public :: nemdefs_c
public :: nemspecs_c
public :: nemtab_c
public :: nemtbb_c
public :: get_isc_c
public :: get_link_c
public :: get_itp_c
public :: get_typ_c
public :: get_tag_c
public :: get_jmpb_c
public :: get_inode_c
public :: get_nval_c
public :: get_val_c
public :: get_inv_c
public :: delete_table_data_c

integer, allocatable, target, save :: isc_f(:)
integer, allocatable, target, save :: link_f(:)
Expand Down Expand Up @@ -296,6 +316,136 @@ subroutine status_c(file_unit, lun, il, im) bind(C, name='status_f')
end subroutine status_c


!> @author Ronald McLaren
!> @date 2022-08-08
!>
!> @brief Gets Table B Unit and Description strings for a mnemonic. Wraps BUFRLIB "nemdefs".
!>
!> @param[in] file_unit - c_int: Fortran file unit for the open file
!> @param[in] mnemonic - c_char: c str for mnemonic
!> @param[inout] unit_c - c_char: unit str
!> @param[in] unit_str_len - c_int: unit str length
!> @param[inout] desc_c - c_char: description string
!> @param[in] desc_str_len - c_int: description str length
!> @param[out] iret - c_int: return value. 0 indicates success -1 indicates failure.
!>
subroutine nemdefs_c(file_unit, mnemonic, unit_c, unit_str_len, desc_c, desc_str_len, iret) &
bind(C, name='nemdefs_f')
integer(c_int), value, intent(in) :: file_unit
character(kind=c_char,len=1), intent(in) :: mnemonic(*)
character(kind=c_char, len=1), intent(inout) :: unit_c(*)
integer(c_int), value, intent(in) :: unit_str_len
character(kind=c_char, len=1), intent(inout) :: desc_c(*)
integer(c_int), value, intent(in) :: desc_str_len
integer(c_int), intent(out) :: iret

character(len=24) :: unit_f
character(len=55) :: desc_f

! Get the unit and description strings
call nemdefs ( file_unit, c_f_string(mnemonic), desc_f, unit_f, iret)

if (iret == 0) then
! Copy the Unit fortran string into the resulting C style string.
call copy_f_c_str(unit_f, unit_c, min(len(unit_f) + 1, unit_str_len))
! Copy the Unit fortran string into the resulting C style string.
call copy_f_c_str(desc_f, desc_c, min(len(desc_f) + 1, desc_str_len))
end if
end subroutine nemdefs_c


!> @author Ronald McLaren
!> @date 2022-08-08
!>
!> @brief Gets Table B scale, reference, and bits values. Wraps BUFRLIB "nemspecs".
!>
!> @param[in] file_unit - c_int: Fortran file unit for the open file
!> @param[in] mnemonic - c_char: c str for mnemonic
!> @param[in] mnemonic_idx - c_int: indicates specific mnemonic element (if repeated)
!> @param[out] scale - c_int: scale of element
!> @param[out] reference - c_int: reference of element
!> @param[out] bits - c_int: number of bits representing the element
!> @param[out] iret - c_int: return value. 0 indicates success -1 indicates failure.
!>
subroutine nemspecs_c(file_unit, mnemonic, mnemonic_idx, scale, reference, bits, iret) &
bind(C, name='nemspecs_f')
integer(c_int), value, intent(in) :: file_unit
character(kind=c_char,len=1), intent(in) :: mnemonic(*)
integer(c_int), value, intent(in) ::mnemonic_idx
integer(c_int), intent(out) :: scale
integer(c_int), intent(out) :: reference
integer(c_int), intent(out) :: bits
integer(c_int), intent(out) :: iret

! Get the scale, reference and bits
call nemspecs(file_unit, c_f_string(mnemonic), mnemonic_idx, scale, reference, bits, iret)

end subroutine nemspecs_c


!> @author Ronald McLaren
!> @date 2022-08-16
!>
!> @brief This subroutine returns information about a descriptor from the internal DX BUFR tables,
!> based on the mnemonic associated with that descriptor.
!>
!> @param[in] bufr_unit - c_int: : the bufr file pointer
!> @param[in] mnemonic - c_char: c str for mnemonic
!> @param[out] descriptor - c_int: the binary descriptor for the mnemonic
!> @param[out] table type - c_char: 'A', 'B', 'C', or 'D' depeninding on table type
!> @param[out] table_idx - c_int: the table index, or 0 if not found
!>
subroutine nemtab_c(bufr_unit, mnemonic, descriptor, table_type, table_idx) &
bind(C, name='nemtab_f')
integer(c_int), value, intent(in) :: bufr_unit
character(kind=c_char,len=1), intent(in) :: mnemonic(*)
integer(c_int), intent(out) ::descriptor
character(kind=c_char,len=1), intent(out) :: table_type(*)
integer(c_int), intent(out) :: table_idx

character(len=1) :: table_type_f

! Get the scale, reference and bits
call nemtab(bufr_unit, c_f_string(mnemonic), descriptor, table_type_f, table_idx)

table_type(1)(1:1) = table_type_f(1:1)

! call copy_f_c_str(table_type_f, table_type, 1)
end subroutine nemtab_c


!> @author Ronald McLaren
!> @date 2022-08-16
!>
!> @brief Get information about a Table B descriptor.
!>
!> @param[in] bufr_unit - c_int: : the bufr file pointer
!> @param[in] table_idx - c_int : c str for mnemonic
!> @param[inout] unit_str - c_char: unit str
!> @param[in] unit_str_len - c_int: unit str length
!> @param[out] scale - c_int: scale of element
!> @param[out] reference - c_int: reference of elemen
!> @param[out] bits - c_int: bits of element
!>
subroutine nemtbb_c(bufr_unit, table_idx, unit_str, unit_str_len, scale, reference, bits) &
bind(C, name='nemtbb_f')
integer(c_int), intent(in), value :: bufr_unit
integer(c_int), intent(in), value :: table_idx
character(kind=c_char,len=1), intent(inout) :: unit_str(*)
integer(c_int), intent(in), value :: unit_str_len
integer(c_int), intent(out) :: scale
integer(c_int), intent(out) :: reference
integer(c_int), intent(out) :: bits

character(len=24) :: unit_str_f

! Get the scale, reference and bits
call nemtbb( bufr_unit, table_idx, unit_str_f, scale, reference, bits)
call copy_f_c_str(unit_str_f, unit_str, min(len(unit_str_f) + 1, unit_str_len))

end subroutine nemtbb_c


!> @author Ronald McLaren
!> @date 2022-03-23
!>
Expand Down
85 changes: 85 additions & 0 deletions src/bufr_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,91 @@ extern "C" {
void status_f(int file_unit, int* lun, int* il, int* im);


/** @author Ronald McLaren
@date 2022-08-08
@brief Gets Table B Unit and Description strings for a mnemonic. Wraps BUFRLIB "nemdefs".
@param[in] file_unit - int: Fortran file unit for the open file
@param[in] mnemonic - char*: c str for mnemonic
@param[inout] unit_c - char*: unit str
@param[in] unit_str_len - int: unit str length
@param[inout] desc_c - char*: description string
@param[in] desc_str_len - int: description str length
@param[out] iret - int*: return value. 0 indicates success -1 indicates failure.
*/
void nemdefs_f(int file_unit,
const char* mnemonic,
char* unit_c,
int unit_str_len,
char* desc_c,
int desc_str_len,
int* iret);


/** @author Ronald McLaren
@date 2022-08-08
@brief Gets Table B scale, reference, and bits values. Wraps BUFRLIB "nemspecs".
@param[in] file_unit - c_int: Fortran file unit for the open file
@param[in] mnemonic - const char*: c str for mnemonic
@param[in] mnemonic_idx - int: indicates specific mnemonic element (if repeated)
@param[out] scale - int*: scale of element
@param[out] reference - int*: reference of element
@param[out] bits - int*: number of bits representing the element
@param[out] iret - int*: return value. 0 indicates success -1 indicates failure.
*/
void nemspecs_f(int file_unit,
const char* mnemonic,
int mnemonic_idx,
int* scale,
int* reference,
int* bits,
int* iret);


/** @author Ronald McLaren
@date 2022-08-16
@brief This subroutine returns information about a descriptor from the internal DX BUFR tables,
based on the mnemonic associated with that descriptor.
@param[in] bufr_unit - int: : the bufr file pointer
@param[in] mnemonic - char*: c str for mnemonic
@param[out] descriptor - int*: the binary descriptor for the mnemonic
@param[out] table_type char* - c_char: 'A', 'B', 'C', or 'D' depeninding on table type
@param[out] table_idx - int*: the table index, or 0 if not found
*/
void nemtab_f(int bufr_unit,
const char* mnemonic,
int* descriptor,
char* table_type,
int* table_idx);


/** @author Ronald McLaren
@date 2022-08-16
@brief Get information about a Table B descriptor.
@param[in] bufr_unit - c_int: : the bufr file pointer
@param[in] table_idx - c_int : c str for mnemonic
@param[inout] unit_str - c_char: unit str
@param[in] unit_str_len - c_int: unit str length
@param[out] scale - c_int: scale of element
@param[out] reference - c_int: reference of elemen
@param[out] bits - c_int: bits of element
*/
void nemtbb_f(int bufr_unit,
int table_idx,
char* unit_str,
int unit_str_len,
int* scale,
int* reference,
int* bits);


/** @author Ronald McLaren
@date 2022-03-23
Expand Down
Loading

0 comments on commit 8519a73

Please sign in to comment.