Skip to content

Commit

Permalink
adding open_index
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhartnett committed Jun 14, 2024
1 parent d173f5b commit fb5b796
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/g2c_interface.F90
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ function g2c_open(path, mode, g2idp) bind(c)
integer(c_int) :: g2c_open
end function g2c_open

function g2c_open_index(data_file, index_file, mode, g2cid) bind(c)
use iso_c_binding
character(kind=c_char), intent(in) :: data_file(*), index_file(*)
integer(c_int), value :: mode
integer(c_int), intent(out) :: g2cid
integer(c_int) :: g2c_open_index
end function g2c_open_index

function g2c_close(g2id) result(status)
use iso_c_binding
integer(c_int), intent(in) :: g2id
integer(c_int) :: status
end function g2c_close

end interface
end module g2c_interface
42 changes: 42 additions & 0 deletions src/g2cf.F90
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,48 @@ function g2cf_open(path, mode, g2id) result (status)

end function g2cf_open

!> Open a GRIB2 file using an exsiting index file (generated by the
!> grb2index utility).
!>
!> @param data_file Path to data file.
!> @param index_file Path to index file.
!> @param mode Open mode, may be NC_CLOBBER (0) or NC_NOCLOBBER.
!> @param g2cid File ID.
!>
!> @return
!> - 0 No error
!>
!> @author Ed Hartnett @date 2022-11-21
function g2cf_open_index(data_file, index_file, mode, g2cid) result (status)
use iso_c_binding
implicit none

character(len=*), intent(in) :: data_file, index_file
integer, intent(in) :: mode
integer, intent(inout) :: g2cid
integer :: status

integer(c_int) :: cmode, cg2cid, cstatus
character(len = (len(data_file) + 1)) :: cdata_file
character(len = (len(index_file) + 1)) :: cindex_file
integer :: ie1, ie2

cmode = mode
cg2cid = 0

! check for c null character on path and add one if not present.
cdata_file = addcnullchar(data_file, ie1)
cindex_file = addcnullchar(index_file, ie2)

! call nc_create to create file
cstatus = g2c_open_index(cdata_file(1:ie1), cindex_file(1:ie2), cmode, cg2cid)

if (cstatus == 0) then
g2cid = cg2cid
endif
status = cstatus
end function g2cf_open_index

!> Close a GRIB2 file.
!>
!> @param g2id the ID of the open file
Expand Down

0 comments on commit fb5b796

Please sign in to comment.