Skip to content

Commit

Permalink
fix: check for unlimited dimension on variable before adjusting counts
Browse files Browse the repository at this point in the history
A netcdf file may have an unlimited dimension, but a particular variable may not have the unlimited
dimension.
See #519 for CESM2.3 CLM which has the unlimited cohort dimension.  In the example restart and history files
no state variables (or any variables) have the cohort dimension.
Massive assumption in direct_netcdf_mod.f90 that we want to read the one slice (the latest slice) of the unlimited dimension. This may not be true for all models.
  • Loading branch information
hkershaw-brown committed Jul 20, 2023
1 parent 1b76f3a commit c4a9562
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions assimilation_code/modules/io/direct_netcdf_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module direct_netcdf_mod
get_index_start, get_index_end , get_num_dims, &
create_diagnostic_structure, &
end_diagnostic_structure, &
has_unlimited_dim
has_unlimited_dim, get_io_dim_ids

use io_filenames_mod, only : get_restart_filename, inherit_copy_units, &
stage_metadata_type, get_file_description, &
Expand Down Expand Up @@ -859,15 +859,16 @@ subroutine read_variables(ncfile_in, var_block, start_var, end_var, domain)

slice_start(:) = 1 ! default to read all dimensions start at 1

if (has_unlimited_dim(domain)) then
ret = nf90_inquire(ncfile_in, unlimitedDimID=unlim_dimID)
call nc_check(ret, 'read_variables: nf90_inquire', 'unlimitedDimID')

if (has_unlimited_dim(domain) .and. any(get_io_dim_ids(domain, i) == unlim_dimID )) then

counts(num_dims) = 1 ! one slice of unlimited dimesion
counts(1:num_dims-1) = get_dim_lengths(domain, i) ! the state
counts(1:get_num_dims(domain, i)) = get_dim_lengths(domain, i) ! the state

! read latest time slice - hack to get started with tiegcm
! not sure if it will always be the last time slice
ret = nf90_inquire(ncfile_in, unlimitedDimID=unlim_dimID)
call nc_check(ret, 'read_variables: nf90_inquire', 'unlimitedDimID')

if (unlim_dimID /= -1) then ! unlimited dimension exists
ret = nf90_inquire_dimension(ncfile_in, unlim_dimID, len=slice_start(num_dims))
Expand Down Expand Up @@ -1588,15 +1589,16 @@ subroutine write_variables(ncid, var_block, start_var, end_var, domain, &
slice_start(:) = 1 ! default to read all dimensions starting at 1
counts(:) = 1

if (has_unlimited_dim(domain)) then
ret = nf90_inquire(ncid, unlimitedDimID=unlim_dimID)
call nc_check(ret, 'read_variables: nf90_inquire', 'unlimitedDimID')

if (has_unlimited_dim(domain) .and. any(get_io_dim_ids(domain, i) == unlim_dimID )) then

counts(num_dims) = 1 ! one slice of unlimited dimesion
counts(1:get_num_dims(domain, i)) = get_dim_lengths(domain, i)

! write the latest time slice - HK hack to get started with tiegcm
! not sure if it will always be the last time slice
ret = nf90_inquire(ncid, unlimitedDimID=unlim_dimID)
call nc_check(ret, 'write_variables: nf90_inquire', 'unlimitedDimID')
if (unlim_dimID /= -1) then ! unlimited dimension exists
ret = nf90_inquire_dimension(ncid, unlim_dimID, len=slice_start(num_dims))
call nc_check(ret, 'write_variables: nf90_inquire dimension', 'unlimitedDim length')
Expand Down

0 comments on commit c4a9562

Please sign in to comment.