Skip to content

Commit

Permalink
netCDF: Partial implementation of get_file_fields
Browse files Browse the repository at this point in the history
Not done yet, but I need to leave soon...
  • Loading branch information
marshallward committed Jan 3, 2023
1 parent 11b5461 commit 53a3396
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/framework/MOM_io_file.F90
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module MOM_io_file
use MOM_netcdf, only : write_netcdf_axis
use MOM_netcdf, only : write_netcdf_attribute
use MOM_netcdf, only : get_netcdf_size
use MOM_netcdf, only : get_netcdf_fields

implicit none ; private

Expand Down Expand Up @@ -1501,12 +1502,25 @@ subroutine get_file_info_nc(handle, ndim, nvar, ntime)
end subroutine get_file_info_nc


!> Return the field metadata associated with a MOM framework file
!> Return the field metadata associated with a MOM netCDF file
subroutine get_file_fields_nc(handle, fields)
class(MOM_netcdf_file), intent(inout) :: handle
type(MOM_field), intent(inout) :: fields(:)

! TODO
type(netcdf_field), allocatable :: fields_nc(:)
integer :: i
character(len=64) :: label

allocate(fields_nc(size(fields)))
call get_netcdf_fields(handle%handle_nc, fields_nc)

do i = 1, size(fields)
! TODO
!call get_field_atts(fields_nc(i), name=label)
!call handle%fields%append(fields_nc(i), trim(label))
!fields(i)%label = trim(label)
enddo

!call get_file_fields(handle%handle_nc, fields)
end subroutine get_file_fields_nc

Expand Down
47 changes: 47 additions & 0 deletions src/framework/MOM_netcdf.F90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module MOM_netcdf
use netcdf, only : nf90_strerror, NF90_NOERR
use netcdf, only : NF90_GLOBAL
use netcdf, only : nf90_inquire, nf90_inquire_dimension
use netcdf, only : nf90_inq_dimids, nf90_inq_varids

use MOM_error_handler, only : MOM_error, FATAL
use MOM_io_infra, only : READONLY_FILE, WRITEONLY_FILE
Expand All @@ -35,6 +36,7 @@ module MOM_netcdf
public :: write_netcdf_axis
public :: write_netcdf_attribute
public :: get_netcdf_size
public :: get_netcdf_fields

! TODO: How much of these types can be made private?

Expand Down Expand Up @@ -532,6 +534,51 @@ subroutine get_netcdf_size(handle, ndims, nvars, nsteps)
end subroutine get_netcdf_size


! TODO: Can this be a function?
subroutine get_netcdf_fields(handle, fields)
type(netcdf_file_type), intent(in) :: handle
type(netcdf_field), intent(inout), allocatable :: fields(:)

integer :: ndims
! Number of netCDF dimensions
integer :: nvars
! Number of netCDF variables (including axes)
integer :: nfields
! Number of fields (i.e. non-axis netCDF variables)
integer :: rc
! netCDF return code
integer, allocatable :: dim_ids(:)
! netCDF dimension IDs
integer, allocatable :: field_ids(:)
! netCDF field variable IDs
integer :: grp_ndims, grp_nvars
! Group-based counts for nf90_inq_* (unused)
integer :: i

integer, save :: no_parent_groups = 0
! Flag indicating exclusion of parent groups in netCDF file
! NOTE: This must be passed as a variable, and cannot be declared as a
! parameter, so must be declared as a variable.

rc = nf90_inquire(handle%ncid, nDimensions=ndims, nVariables=nvars)
call check_netcdf_call(rc, 'get_netcdf_fields', &
'File "' // handle%filename // '"')

! Gather the axes, to be removed from the field list
rc = nf90_inq_dimids(handle%ncid, grp_ndims, dim_ids, no_parent_groups)
call check_netcdf_call(rc, 'get_netcdf_fields', &
'File "' // handle%filename // '"')

allocate(field_ids(nvars))
rc = nf90_inq_varids(handle%ncid, grp_nvars, field_ids)
call check_netcdf_call(rc, 'get_netcdf_fields', &
'File "' // handle%filename // '"')

nfields = nvars - ndims
allocate(fields(nfields))
end subroutine get_netcdf_fields


!> Check netCDF function return codes, report the error log, and abort the run.
subroutine check_netcdf_call(ncerr, header, message)
integer, intent(in) :: ncerr
Expand Down

0 comments on commit 53a3396

Please sign in to comment.