read_dims Subroutine

private subroutine read_dims(ncid, varid, grid, nz_src, needs_transpose, ierr)

Validate the T/S variable’s dims against the model grid and detect whether the file is C-ordered (z, y, x) => needs_transpose = .true. (by the first Fortran dim name). Returns source z-level count nz_src; error-stops on mismatch.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: ncid
integer, intent(in) :: varid
type(hgrid_t), intent(in) :: grid
integer, intent(out) :: nz_src
logical, intent(out) :: needs_transpose
integer, intent(out), optional :: ierr

Non-zero on a dimensionality/shape mismatch when present; absent behaves as today (error stop).


Calls

proc~~read_dims~~CallsGraph proc~read_dims read_dims nf90_inquire_dimension nf90_inquire_dimension proc~read_dims->nf90_inquire_dimension nf90_inquire_variable nf90_inquire_variable proc~read_dims->nf90_inquire_variable proc~fail fail proc~read_dims->proc~fail proc~nc_check nc_check proc~read_dims->proc~nc_check proc~nc_close nc_close proc~read_dims->proc~nc_close proc~zinit_io_ok zinit_io_ok proc~read_dims->proc~zinit_io_ok to_string to_string proc~read_dims->to_string error error proc~fail->error proc~error_ring_push error_ring_push proc~fail->proc~error_ring_push proc~nc_check->proc~fail nf90_strerror nf90_strerror proc~nc_check->nf90_strerror proc~nc_close->proc~nc_check nf90_close nf90_close proc~nc_close->nf90_close proc~zinit_io_ok->proc~nc_close

Called by

proc~~read_dims~~CalledByGraph proc~read_dims read_dims proc~seed_ts_from_zfile seed_ts_from_zfile proc~seed_ts_from_zfile->proc~read_dims proc~seed_zinit_overlay seed_zinit_overlay proc~seed_zinit_overlay->proc~seed_ts_from_zfile proc~ocean_state_seed_from_cfg ocean_state_seed_from_cfg proc~ocean_state_seed_from_cfg->proc~seed_zinit_overlay proc~engine_setup engine_setup proc~engine_setup->proc~ocean_state_seed_from_cfg proc~complete_ocean_create complete_ocean_create proc~complete_ocean_create->proc~engine_setup proc~driver_run_ocean driver_run_ocean proc~driver_run_ocean->proc~engine_setup proc~driver_validate driver_validate proc~driver_validate->proc~engine_setup

Variables

Type Visibility Attributes Name Initial
integer, private :: d1_len
character(len=64), private :: d1_name
integer, private :: d2_len
integer, private :: d3_len
integer, private :: local_ierr
logical, private :: ok
integer, private :: var_dimids(3)
integer, private :: var_ndims

Source Code

   subroutine read_dims(ncid, varid, grid, nz_src, needs_transpose, ierr)
      !! Validate the T/S variable's dims against the model grid and
      !! detect whether the file is C-ordered (z, y, x) =>
      !! `needs_transpose = .true.` (by the first Fortran dim name).
      !! Returns source z-level count `nz_src`; error-stops on mismatch.
      integer, intent(in) :: ncid, varid
      type(hgrid_t), intent(in) :: grid
      integer, intent(out) :: nz_src
      logical, intent(out) :: needs_transpose
      integer, intent(out), optional :: ierr
         !! Non-zero on a dimensionality/shape mismatch when present;
         !! absent behaves as today (`error stop`).

      integer :: var_ndims
      integer :: var_dimids(3)
      integer :: d1_len, d2_len, d3_len
      integer :: local_ierr
      character(len=64) :: d1_name
      logical :: ok

      call nc_check(nf90_inquire_variable(ncid, varid, ndims=var_ndims, dimids=var_dimids), &
                    "querying z-level T/S variable", local_ierr)
      if (.not. zinit_io_ok(local_ierr, ierr, ncid)) return
      if (var_ndims /= 3) then
         call nc_close(ncid)
         call fail("ocean_zinit: T/S variable must be 3D (x,y,z); got "// &
                   to_string(var_ndims)//" dims", ierr, OCEAN_STATUS_ERR_IO)
         return
      end if

      call nc_check(nf90_inquire_dimension(ncid, var_dimids(1), name=d1_name, len=d1_len), &
                    "querying T/S dim 1", local_ierr)
      if (.not. zinit_io_ok(local_ierr, ierr, ncid)) return
      call nc_check(nf90_inquire_dimension(ncid, var_dimids(2), len=d2_len), &
                    "querying T/S dim 2", local_ierr)
      if (.not. zinit_io_ok(local_ierr, ierr, ncid)) return
      call nc_check(nf90_inquire_dimension(ncid, var_dimids(3), len=d3_len), &
                    "querying T/S dim 3", local_ierr)
      if (.not. zinit_io_ok(local_ierr, ierr, ncid)) return

      ! C-ordered files store temp(x,y,z); Fortran's NetCDF lib reverses
      ! that to temp(z,y,x) so dim1 is "z".  Fortran-ordered files keep
      ! dim1 == "x".
      needs_transpose = (trim(d1_name) == "z" .or. trim(d1_name) == "depth" .or. &
                         trim(d1_name) == "lev" .or. trim(d1_name) == "z_src")

      if (needs_transpose) then
         ! Fortran storage (z, y, x): physical lengths are d3=x, d2=y, d1=z.
         ok = (d3_len == grid%nx_global .and. d2_len == grid%ny_global)
         nz_src = d1_len
      else
         ! Fortran storage (x, y, z): physical lengths are d1=x, d2=y, d3=z.
         ok = (d1_len == grid%nx_global .and. d2_len == grid%ny_global)
         nz_src = d3_len
      end if

      if (.not. ok) then
         call nc_close(ncid)
         call fail("ocean_zinit: T/S grid mismatch: file horizontal dims do not match "// &
                   "simulation "//to_string(grid%nx_global)//" x "//to_string(grid%ny_global), &
                   ierr, OCEAN_STATUS_ERR_IO)
         return
      end if
      if (present(ierr)) ierr = OCEAN_STATUS_OK
   end subroutine read_dims