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

Update netCDF code (in src/Shared/NcdfUtil) to use the netCDF-F90 interface #226

Merged
merged 2 commits into from
Jul 17, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- NetCDF routines in `src/Shared/NcdfUtil` now use the Fortran-90 API

## [Unreleased 3.7.1] - TBD
### Changed
- Updated version numbers to 3.7.1
Expand Down
2 changes: 1 addition & 1 deletion src/Core/hcoio_write_std_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ SUBROUTINE HCOIO_Write( HcoState, ForceWrite, &
VarUnit = 'Pa', &
DataType = dp, &
VarCt = VarCt, &
Compress = .TRUE. )
Compress = .FALSE. )
CALL NC_Var_Write( fId, 'P0', P0 )

! Deallocate arrays
Expand Down
73 changes: 23 additions & 50 deletions src/Shared/NcdfUtil/hco_m_netcdf_io_checks.F90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!------------------------------------------------------------------------------
! NcdfUtilities: by Harvard Atmospheric Chemistry Modeling Group !
! Ncdfutilities: by Harvard Atmospheric Chemistry Modeling Group !
! and NASA/GSFC, SIVO, Code 610.3 !
!------------------------------------------------------------------------------
!BOP
Expand Down Expand Up @@ -44,9 +44,7 @@ module HCO_m_netcdf_io_checks
!
function Ncdoes_Udim_Exist (ncid)
!
implicit none
!
include "netcdf.inc"
use netCDF
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id to check
Expand All @@ -69,19 +67,12 @@ function Ncdoes_Udim_Exist (ncid)
!BOC
!
! !LOCAL VARIABLES:
integer :: ierr
integer :: udimid
!
ierr = Nf_Inq_Unlimdim (ncid, udimid)

if (ierr == NF_NOERR) then
Ncdoes_Udim_Exist = .true.
else
Ncdoes_Udim_Exist = .false.
end if

return
integer :: ierr, udim_id

Ncdoes_Udim_Exist = .false.
ierr = NF90_Inquire(ncid, unlimitedDimId=udim_id)
IF ( ierr /= NF90_NOERR ) Ncdoes_Udim_Exist = .true.

end function Ncdoes_Udim_Exist
!EOC
!------------------------------------------------------------------------------
Expand All @@ -96,9 +87,7 @@ end function Ncdoes_Udim_Exist
!
function Ncdoes_Var_Exist (ncid, varname)
!
implicit none
!
include "netcdf.inc"
use netCDF
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id to check
Expand Down Expand Up @@ -126,15 +115,9 @@ function Ncdoes_Var_Exist (ncid, varname)
integer :: ierr
integer :: varid
!
ierr = Nf_Inq_Varid (ncid, varname, varid)

if (ierr == NF_NOERR) then
Ncdoes_Var_Exist = .true.
else
Ncdoes_Var_Exist = .false.
end if

return
ierr = NF90_Inq_Varid(ncid, varname, varid)
Ncdoes_Var_Exist = .false.
if (ierr == NF90_NOERR) Ncdoes_Var_Exist = .true.

end function Ncdoes_Var_Exist
!EOC
Expand All @@ -148,11 +131,9 @@ end function Ncdoes_Var_Exist
!
! !INTERFACE:
!
function Ncdoes_Attr_Exist (ncid, varname, attname, attType)
function Ncdoes_Attr_Exist(ncid, varname, attname, attType)
!
implicit none
!
include "netcdf.inc"
use netCDF
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id to check
Expand Down Expand Up @@ -185,27 +166,24 @@ function Ncdoes_Attr_Exist (ncid, varname, attname, attType)
!BOC
!
! !LOCAL VARIABLES:
integer :: ierr
integer :: varid
INTEGER :: attLen
INTEGER :: ierr, varId, attLen, attNum

! Init
Ncdoes_Attr_Exist = .false.
attType = -1

! First check the variable
ierr = Nf_Inq_Varid (ncid, varname, varid)
ierr = NF90_Inq_Varid (ncid, varname, varid)

! Check the attribute if variable was found
IF ( ierr == NF_NOERR ) THEN
ierr = Nf_Inq_Att( ncId, varId, attName, attType, attLen )
IF ( ierr == NF_NOERR ) THEN
IF ( ierr == NF90_NOERR ) THEN
ierr = NF90_Inquire_Attribute( ncId, varId, attName, &
attType, attLen, attNum )
IF ( ierr == NF90_NOERR ) THEN
NcDoes_Attr_Exist = .TRUE.
ENDIF
ENDIF

return

end function Ncdoes_Attr_Exist
!EOC
!------------------------------------------------------------------------------
Expand All @@ -220,9 +198,7 @@ end function Ncdoes_Attr_Exist
!
function Ncdoes_Dim_Exist (ncid, dimname )
!
implicit none
!
include "netcdf.inc"
use netCDF
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id to check
Expand Down Expand Up @@ -251,14 +227,11 @@ function Ncdoes_Dim_Exist (ncid, dimname )
integer :: dimid

! First check the variable
ierr = Nf_Inq_Dimid (ncid, dimname, dimid)
ierr = NF90_Inq_Dimid(ncid, dimname, dimid)

! Check the attribute if variable was found
if (ierr == NF_NOERR) then
Ncdoes_Dim_Exist = .true.
else
Ncdoes_Dim_Exist = .false.
end if
Ncdoes_Dim_Exist = .false.
if (ierr == NF90_NOERR) Ncdoes_Dim_Exist = .true.

return

Expand Down
17 changes: 6 additions & 11 deletions src/Shared/NcdfUtil/hco_m_netcdf_io_close.F90
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ subroutine Nccl (ncid)
!
! !USES:
!
use netCDF
use m_do_err_out
!
implicit none
!
include "netcdf.inc"
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id
Expand All @@ -70,10 +67,10 @@ subroutine Nccl (ncid)
character (len=512) :: err_msg
integer :: ierr
!
ierr = Nf_Close (ncid)
ierr = Nf90_Close (ncid)

if (ierr /= NF_NOERR) then
err_msg = 'In Nccl: ' // Nf_Strerror (ierr)
if (ierr /= NF90_NOERR) then
err_msg = 'In Nccl: ' // Nf90_Strerror (ierr)
call Do_Err_Out (err_msg, .true., 1, ncid, 0, 0, 0.0d0, 0.0d0)
end if

Expand All @@ -93,9 +90,7 @@ end subroutine Nccl
!
subroutine Nccl_Noerr (ncid)
!
implicit none
!
include "netcdf.inc"
use netCDF
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id
Expand All @@ -117,7 +112,7 @@ subroutine Nccl_Noerr (ncid)
! !LOCAL VARIABLES:
integer :: ierr
!
ierr = Nf_Close (ncid)
ierr = Nf90_Close (ncid)

return

Expand Down
44 changes: 18 additions & 26 deletions src/Shared/NcdfUtil/hco_m_netcdf_io_create.F90
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ subroutine Nccr_Wr (ncid, filname, WRITE_NC4)
!
! !USES:
!
use netCDF
use m_do_err_out
!
implicit none
!
include "netcdf.inc"
!
! !INPUT PARAMETERS:
! ncid : opened netCDF file id
! filname : name of netCDF file to open for writing
integer , intent(in) :: ncid
character (len=*), intent(in) :: filname
LOGICAL, OPTIONAL, INTENT(IN) :: WRITE_NC4
integer , intent(INOUT) :: ncid
character (len=*), intent(IN) :: filname
LOGICAL, OPTIONAL, INTENT(IN) :: WRITE_NC4
!
! !DESCRIPTION: Creates a netCDF file for writing and does some error checking.
!\\
Expand All @@ -64,10 +61,10 @@ subroutine Nccr_Wr (ncid, filname, WRITE_NC4)
! John Tannahill (LLNL) and Jules Kouatchou
!
! !REMARKS:
! If the netCDF4 library is used, then the NF_CLOBBER flag will write
! If the netCDF4 library is used, then the NF90_CLOBBER flag will write
! a classic (i.e. netCDF3) file. Use OR(NF_NETCDF4,NF_CLASSIC_MODEL) to
! create netCDF-4 file that supports compression and uses "classic" netcdf data model
! (no groups, no user-defined types)
! create netCDF-4 file that supports compression and uses "classic"
! netcdf data model (no groups, no user-defined types)
!
! !REVISION HISTORY:
! See https://github.com/geoschem/ncdfutil for complete history
Expand All @@ -91,17 +88,17 @@ subroutine Nccr_Wr (ncid, filname, WRITE_NC4)

IF ( TMP_NC4 ) THEN
#if defined( NC_HAS_COMPRESSION )
mode = IOR( NF_NETCDF4, NF_CLASSIC_MODEL ) ! netCDF4 file
ierr = Nf_Create (filname, mode, ncid) ! w/ compression
mode = IOR( NF90_NETCDF4, NF90_CLASSIC_MODEL ) ! netCDF4 file
ierr = NF90_Create(filname, mode, ncid) ! w/ compression
#else
ierr = Nf_Create (filname, NF_64BIT_OFFSET, ncid) ! netCDF4 file
! w/o compression
ierr = NF90_Create(filname, NF90_64BIT_OFFSET, ncid) ! netCDF4 file
! w/o compression
#endif
ELSE
ierr = Nf_Create (filname, NF_CLOBBER, ncid) ! netCDF3 file
ierr = NF90_Create(filname, NF90_CLOBBER, ncid) ! netCDF3 file
ENDIF

if (ierr /= NF_NOERR) then
if (ierr /= NF90_NOERR) then
err_msg = 'In Nccr_Wr, cannot create: ' // Trim (filname)
call Do_Err_Out (err_msg, .true., 0, 0, 0, 0 , 0.0d0, 0.0d0)
end if
Expand All @@ -120,15 +117,12 @@ end subroutine Nccr_Wr
!
! !INTERFACE:
!
subroutine Ncdo_Sync (ncid)
subroutine Ncdo_Sync(ncid)
!
! !USES:
!
use netCDF
use m_do_err_out
!
implicit none
!
include "netcdf.inc"
!
! !INPUT PARAMETERS:
!! ncid : netCDF file id
Expand All @@ -150,15 +144,13 @@ subroutine Ncdo_Sync (ncid)
character (len=128) :: err_msg
integer :: ierr
!
ierr = Nf_Sync (ncid)
ierr = Nf90_Sync (ncid)

if (ierr /= NF_NOERR) then
err_msg = 'In Ncdo_Sync: ' // Nf_Strerror (ierr)
if (ierr /= NF90_NOERR) then
err_msg = 'In Ncdo_Sync: ' // Nf90_Strerror (ierr)
call Do_Err_Out (err_msg, .true., 1, ncid, 0, 0, 0.0d0, 0.0d0)
end if

return

end subroutine Ncdo_Sync
!EOC
end module HCO_m_netcdf_io_create
Loading