Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combined subroutines into files #604

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ gb_info.F90 getdim.F90 getfield.F90 getgb2.F90 getgb2l.F90 getgb2p.F90
getgb2r.F90 getgb2rp.F90 g2index.F90 getlocal.F90 getpoly.F90
gettemplates.F90 gf_free.F90 gf_getfld.F90 g2unpack.F90 gribcreate.F90
gribend.F90 gribinfo.F90 ${CMAKE_CURRENT_BINARY_DIR}/gribmod.F90
gridtemplates.F90 intmath.F90 jpcpack.F90 jpcunpack.F90 misspack.F90
mkieee.F90 pack_gp.f params_ecmwf.F90 params.F90 pdstemplates.F90
pngpack.F90 pngunpack.F90 putgb2.F90 rdieee.F90 realloc.F90 reduce.f
simpack.F90 simunpack.F90 skgb.F90 specpack.F90 specunpack.F90)
gridtemplates.F90 intmath.F90 g2jpc.F90 misspack.F90 mkieee.F90
pack_gp.f params_ecmwf.F90 params.F90 pdstemplates.F90 g2png.F90
putgb2.F90 rdieee.F90 realloc.F90 reduce.f g2sim.F90 skgb.F90
g2spec.F90)

# This function calls NCEPLIBS-w3emc.
if (BUILD_WITH_W3EMC)
Expand Down
66 changes: 65 additions & 1 deletion src/jpcpack.F90 → src/g2jpc.F90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!> @file
!> @brief Pack a data field into a JPEG2000 code stream as defined in
!> @brief Pack/unpack a data field into a JPEG2000 code stream as defined in
!> [Data Representation Template
!> 5.40](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-40.shtml).
!> @author Stephen Gilbert @date 2002-12-17
Expand Down Expand Up @@ -156,3 +156,67 @@ end function enc_jpeg2000
if (idrstmpl(6) .eq. 0) idrstmpl(7) = 255 ! lossy not used

end subroutine

!> Unpack a data field from a JPEG2000 code stream as defined in
!> [Data Representation Template
!> 5.40](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-40.shtml).
!>
!> This subroutine unpacks a data field that was packed into a
!> JPEG2000 code stream using info from the GRIB2 Data Representation
!> Template 5.40 or 5.40000.
!>
!> @param[in] cpack The packed data field (character*1 array).
!> @param[in] len length of packed field cpack().
!> @param[in] idrstmpl Array of values for Data Representation
!> Template 5.40 or 5.40000.
!> @param[in] ndpts The number of data values to unpack.
!> @param[out] fld Contains the unpacked data values.
!>
!> @author Stephen Gilbert @date 2002-12-17
subroutine jpcunpack(cpack,len,idrstmpl,ndpts,fld)
implicit none

character(len=1),intent(in) :: cpack(len)
integer,intent(in) :: ndpts,len
integer,intent(in) :: idrstmpl(*)
real,intent(out) :: fld(ndpts)

integer :: ifld(ndpts)
integer(4) :: ieee
integer(8) :: len8
real :: ref,bscale,dscale
integer :: nbits, j, iret

interface
function dec_jpeg2000(cin, len, ifld) &
bind(c, name="g2c_dec_jpeg2000")
use iso_c_binding
character(kind = c_char), intent(in) :: cin(*)
integer(c_size_t), value, intent(in) :: len
integer(c_int), intent(inout) :: ifld(*)
integer(c_int) :: dec_jpeg2000
end function dec_jpeg2000
end interface

ieee = idrstmpl(1)
call rdieee(ieee,ref,1)
bscale = 2.0**real(idrstmpl(2))
dscale = 10.0**real(-idrstmpl(3))
nbits = idrstmpl(4)

! if nbits equals 0, we have a constant field where the reference value
! is the data value at each gridpoint
if (nbits.ne.0) then
! call g2_gbytesc(cpack,ifld,0,nbits,0,ndpts)
len8 = len
iret=dec_jpeg2000(cpack,len8,ifld)
do j=1,ndpts
fld(j)=((real(ifld(j))*bscale)+ref)*dscale
enddo
else
do j=1,ndpts
fld(j)=ref
enddo
endif

end subroutine jpcunpack
70 changes: 68 additions & 2 deletions src/pngpack.F90 → src/g2png.F90
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
!> @file
!> @brief Pack a data field into PNG image format, defined in [Data Representation
!> Template 5.40](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-40.shtml).
!> @brief Pack/unpack a data field into PNG image format, defined in
!> [Data Representation Template
!> 5.40](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-40.shtml).
!> @author Stephen Gilbert @date 2002-12-21

!> Pack a data field into PNG image format, defined in [Data
Expand Down Expand Up @@ -146,3 +147,68 @@ end function enc_png
idrstmpl(5) = 0 ! original data were reals

end subroutine pngpack

!> Unpack a data field with PNG, defined in [Data Representation
!> Template
!> 5.40](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-40.shtml).
!>
!> This subroutine unpacks a data field that was packed into a
!> PNG image format using info from the GRIB2 Data Representation
!> Template 5.40 or 5.40000.
!>
!> @param[in] cpack The packed data field (character*1 array).
!> @param[in] len length of packed field cpack().
!> @param[in] idrstmpl Contains the array of values for Data
!> Representation Template 5.40 or 5.40000.
!> @param[in] ndpts The number of data values to unpack.
!> @param[out] fld Contains the unpacked data values.
!>
!> @author Stephen Gilbert @date 2000-06-21
subroutine pngunpack(cpack, len, idrstmpl, ndpts, fld)
implicit none

character(len = 1), intent(in) :: cpack(len)
integer, intent(in) :: ndpts, len
integer, intent(in) :: idrstmpl(*)
real, intent(out) :: fld(ndpts)

integer :: ifld(ndpts)
character(len = 1), allocatable :: ctemp(:)
integer(4) :: ieee
real :: ref, bscale, dscale
integer :: width, height
integer :: iret, itype, j, nbits

interface
function dec_png(pngbuf, width, height, cout) bind(c, name="dec_png")
use iso_c_binding
character(kind = c_char), intent(in) :: pngbuf(*)
integer(c_int), intent(in) :: width, height
character(kind = c_char), intent(out) :: cout(*)
integer(c_int) :: dec_png
end function dec_png
end interface

ieee = idrstmpl(1)
call rdieee(ieee, ref, 1)
bscale = 2.0**real(idrstmpl(2))
dscale = 10.0**real(-idrstmpl(3))
nbits = idrstmpl(4)
itype = idrstmpl(5)

! If nbits equals 0, we have a constant field where the reference value
! is the data value at each gridpoint.
if (nbits .ne. 0) then
allocate(ctemp(ndpts * 4))
iret = dec_png(cpack, width, height, ctemp)
call g2_gbytesc(ctemp, ifld, 0, nbits, 0, ndpts)
deallocate(ctemp)
do j = 1, ndpts
fld(j) = ((real(ifld(j)) * bscale) + ref) * dscale
enddo
else
do j = 1, ndpts
fld(j) = ref
enddo
endif
end subroutine pngunpack
50 changes: 49 additions & 1 deletion src/simpack.F90 → src/g2sim.F90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!> @file
!> @brief Pack a data field using simple packing algorithm.
!> @brief Pack/unpack a data field using simple packing algorithm.
!> @author Stephen Gilbert @date 2000-06-21

!> Pack a data field using a simple packing algorithm.
Expand Down Expand Up @@ -152,3 +152,51 @@ subroutine simpack(fld,ndpts,idrstmpl,cpack,lcpack)
idrstmpl(5)=0 ! original data were reals

end subroutine simpack

!> Unpack a data field that was packed using a simple packing, [Data
!> Representation Template
!> 5.0](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-0.shtml).
!>
!> @param[in] cpack The packed data field (character*1 array).
!> @param[in] len length of packed field cpack.
!> @param[in] idrstmpl Contains the array of values for Data
!> Representation Template 5.0.
!> @param[in] ndpts The number of data values to unpack.
!> @param[out] fld Contains the unpacked data values.
!>
!> @author Stephen Gilbert @date 2000-06-21
subroutine simunpack(cpack, len, idrstmpl, ndpts, fld)
implicit none

character(len=1), intent(in) :: cpack(len)
integer, intent(in) :: ndpts, len
integer, intent(in) :: idrstmpl(*)
real, intent(out) :: fld(ndpts)

integer :: ifld(ndpts)
integer(4) :: ieee
real :: ref, bscale, dscale
integer :: itype, j, nbits

ieee = idrstmpl(1)
call rdieee(ieee, ref, 1)
bscale = 2.0**real(idrstmpl(2))
dscale = 10.0**real(-idrstmpl(3))
nbits = idrstmpl(4)
itype = idrstmpl(5)

! If nbits equals 0, we have a constant field where the reference value
! is the data value at each gridpoint.
if (nbits .ne. 0) then
call g2_gbytesc(cpack, ifld, 0, nbits, 0, ndpts)
do j=1, ndpts
fld(j) = ((real(ifld(j)) * bscale) + ref) * dscale
enddo
else
!print *, 'unpack ref ', ref
!print *, 'unpack ndpts ', ndpts
do j=1, ndpts
fld(j) = ref
enddo
endif
end subroutine simunpack
81 changes: 80 additions & 1 deletion src/specpack.F90 → src/g2spec.F90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!> @file
!> @brief Pack a spectral data field using the complex
!> @brief Pack/unpack a spectral data field using the complex
!> packing algorithm for spherical harmonic data as defined in
!> [Data Representation Template
!> 5.51](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-51.shtml).
Expand Down Expand Up @@ -104,3 +104,82 @@ subroutine specpack(fld,ndpts,JJ,KK,MM,idrstmpl,cpack,lcpack)
idrstmpl(10)=1 ! Unpacked spectral data is 32-bit IEEE

end subroutine specpack

!> Unpack a spectral data field using the complex packing algorithm
!> for spherical harmonic data, [Data Representation Template
!> 5.51](https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_temp5-51.shtml).
!>
!> @param[in] cpack The packed data field (character*1 array).
!> @param[in] len length of packed field cpack.
!> @param[in] idrstmpl Contains the array of values for Data
!> Representation Template 5.51.
!> @param[in] ndpts The number of data values in array fld.
!> @param[in] JJ J pentagonal resolution parameter.
!> @param[in] KK K pentagonal resolution parameter.
!> @param[in] MM M pentagonal resolution parameter.
!> @param[out] fld Contains the unpacked data values.
!>
!> @author Stephen Gilbert @date 2002-12-19
subroutine specunpack(cpack,len,idrstmpl,ndpts,JJ,KK,MM,fld)

character(len=1),intent(in) :: cpack(len)
integer,intent(in) :: ndpts,len,JJ,KK,MM
integer,intent(in) :: idrstmpl(*)
real,intent(out) :: fld(ndpts)

integer :: ifld(ndpts),Ts
integer(4) :: ieee
real :: ref,bscale,dscale,unpk(ndpts)
real,allocatable :: pscale(:)

ieee = idrstmpl(1)
call rdieee(ieee,ref,1)
bscale = 2.0**real(idrstmpl(2))
dscale = 10.0**real(-idrstmpl(3))
nbits = idrstmpl(4)
Js=idrstmpl(6)
Ks=idrstmpl(7)
Ms=idrstmpl(8)
Ts=idrstmpl(9)

if (idrstmpl(10).eq.1) then ! unpacked floats are 32-bit IEEE
call rdieee(cpack,unpk,Ts) ! read IEEE unpacked floats
iofst=32*Ts
call g2_gbytesc(cpack,ifld,iofst,nbits,0,ndpts-Ts) ! unpack scaled data

! Calculate Laplacian scaling factors for each possible wave number.
allocate(pscale(JJ+MM))
tscale=real(idrstmpl(5))*1E-6
do n=Js,JJ+MM
pscale(n)=real(n*(n+1))**(-tscale)
enddo

! Assemble spectral coeffs back to original order.
inc=1
incu=1
incp=1
do m=0,MM
Nm=JJ ! triangular or trapezoidal
if (KK .eq. JJ+MM) Nm=JJ+m ! rhombodial
Ns=Js ! triangular or trapezoidal
if (Ks .eq. Js+Ms) Ns=Js+m ! rhombodial
do n=m,Nm
if (n.le.Ns .AND. m.le.Ms) then ! grab unpacked value
fld(inc)=unpk(incu) ! real part
fld(inc+1)=unpk(incu+1) ! imaginary part
inc=inc+2
incu=incu+2
else ! Calc coeff from packed value
fld(inc)=((real(ifld(incp))*bscale)+ref)*dscale*pscale(n) ! real part
fld(inc+1)=((real(ifld(incp+1))*bscale)+ref)*dscale*pscale(n) ! imaginary part
inc=inc+2
incp=incp+2
endif
enddo
enddo
deallocate(pscale)
else
print *,'specunpack: Cannot handle 64 or 128-bit floats.'
fld=0.0
endif
end subroutine specunpack
69 changes: 0 additions & 69 deletions src/jpcunpack.F90

This file was deleted.

Loading
Loading