Skip to content

Commit

Permalink
Update g2clib.py
Browse files Browse the repository at this point in the history
Refactored unpack1() and unpack7(). These funcitons no longer pass
grbpos and np.empty into the Cython functions.

We are able to pass np.empty() return directly into _toarray()
from unpack*() functions.
  • Loading branch information
EricEngle-NOAA committed Feb 17, 2025
1 parent 91cc1ae commit d9e700b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
32 changes: 21 additions & 11 deletions src/ext/g2clib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ cdef _toarray(void *items, object a):
# ----------------------------------------------------------------------------------------
# Routine for reading GRIB2 files.
# ----------------------------------------------------------------------------------------
def unpack1(gribmsg, ipos, object arr):
def unpack1(gribmsg):
""" . . . .
Unpacks Section 1 (Identification Section) as defined in GRIB Edition 2.
Expand Down Expand Up @@ -158,17 +158,28 @@ def unpack1(gribmsg, ipos, object arr):
6 = Memory allocation error
"""
cdef unsigned char *cgrib
cdef g2int i, iofst, iret, idslen
cdef g2int iret
cdef g2int iofst
cdef g2int idslen
cdef g2int *ids

iret = 0
iofst = 0
cgrib = <unsigned char *>PyBytes_AsString(gribmsg)
iofst = <g2int>PyInt_AsLong(ipos*8)
iret = g2_unpack1(cgrib, &iofst, &ids, &idslen)

iret = g2_unpack1(
cgrib,
&iofst,
&ids,
&idslen,
)
if iret != 0:
msg = "Error unpacking section 1 - error code = %i" % iret
msg = f"Error unpacking section 1, error code = {iret}"
raise RuntimeError(msg)

idsect = _toarray(ids, arr(idslen, "i8"))
return idsect,iofst//8
idsect = _toarray(ids, np.empty(idslen, "i8"))

return idsect, iofst//8


def unpack3(gribmsg, ipos, object arr):
Expand Down Expand Up @@ -374,8 +385,6 @@ def unpack7(gribmsg,
int drtnum,
cnp.ndarray[cnp.int64_t, ndim=1] drtmpl,
int ndpts,
int ipos,
object arr,
storageorder="C"):
"""
Unpacks Section 7 (Data Section) as defined in GRIB Edition 2.
Expand Down Expand Up @@ -424,8 +433,8 @@ def unpack7(gribmsg,
cdef g2int[:] idrstmpl_view = drtmpl

iret = 0
iofst = 0
cgrib = <unsigned char *>PyBytes_AsString(gribmsg)
iofst = <g2int>PyInt_AsLong(ipos*8)

iret = g2_unpack7(
cgrib,
Expand All @@ -441,7 +450,8 @@ def unpack7(gribmsg,
msg = f"Error in unpack7, error code = {iret}"
raise RuntimeError(msg)

data = _toarray(fld, arr(ndpts, "f4", order=storageorder))
data = _toarray(fld, np.empty(ndpts, "f4", order=storageorder))

return data

# ----------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/grib2io/_grib2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _build_index(self):
# Unpack section
if secnum == 1:
# Unpack Section 1
section1, grbpos = g2clib.unpack1(secmsg,grbpos,np.empty)
section1, grbpos = g2clib.unpack1(secmsg)
elif secnum == 2:
# Unpack Section 2
section2 = self._filehandle.read(secsize-5)
Expand Down Expand Up @@ -1589,8 +1589,8 @@ def _data(
ipos = 0
npvals = msg.numberOfPackedValues
ngrdpts = msg.numberOfDataPoints
fld1 = g2clib.unpack7(filehandle.read(data_size),msg.gdtn,gdt,msg.drtn,drt,npvals,ipos,
np.empty,storageorder=storageorder)
fld1 = g2clib.unpack7(filehandle.read(data_size),msg.gdtn,gdt,msg.drtn,drt,npvals,
storageorder=storageorder)

# Handle the missing values
if msg.bitMapFlag in {0,254}:
Expand Down

0 comments on commit d9e700b

Please sign in to comment.