find_var Subroutine

private subroutine find_var(ncid, override, fallbacks, label, varid, ierr)

Resolve a variable id: try the namelist override name first (when non-blank), then the documented fallbacks in order. Error-stops with a descriptive message when none is found.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: ncid
character(len=*), intent(in) :: override
character(len=*), intent(in) :: fallbacks(:)
character(len=*), intent(in) :: label
integer, intent(out) :: varid
integer, intent(out), optional :: ierr

Non-zero when no matching variable is found, when present; absent behaves as today (error stop).


Calls

proc~~find_var~~CallsGraph proc~find_var find_var nf90_inq_varid nf90_inq_varid proc~find_var->nf90_inq_varid proc~fail fail proc~find_var->proc~fail proc~nc_close nc_close proc~find_var->proc~nc_close error error proc~fail->error proc~error_ring_push error_ring_push proc~fail->proc~error_ring_push nf90_close nf90_close proc~nc_close->nf90_close proc~nc_check nc_check proc~nc_close->proc~nc_check proc~nc_check->proc~fail nf90_strerror nf90_strerror proc~nc_check->nf90_strerror

Called by

proc~~find_var~~CalledByGraph proc~find_var find_var proc~seed_ts_from_zfile seed_ts_from_zfile proc~seed_ts_from_zfile->proc~find_var 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 :: n
integer, private :: status
character(len=512), private :: tried

Source Code

   subroutine find_var(ncid, override, fallbacks, label, varid, ierr)
      !! Resolve a variable id: try the namelist `override` name first
      !! (when non-blank), then the documented `fallbacks` in order.
      !! Error-stops with a descriptive message when none is found.
      integer, intent(in) :: ncid
      character(len=*), intent(in) :: override
      character(len=*), intent(in) :: fallbacks(:)
      character(len=*), intent(in) :: label
      integer, intent(out) :: varid
      integer, intent(out), optional :: ierr
         !! Non-zero when no matching variable is found, when present;
         !! absent behaves as today (`error stop`).

      integer :: status, n
      character(len=512) :: tried

      if (len_trim(override) > 0) then
         status = nf90_inq_varid(ncid, trim(override), varid)
         if (status == nf90_noerr) then
            if (present(ierr)) ierr = OCEAN_STATUS_OK
            return
         end if
         call nc_close(ncid)
         call fail("ocean_zinit: "//trim(label)//" variable override '"// &
                   trim(override)//"' not found in file", ierr, OCEAN_STATUS_ERR_IO)
         return
      end if

      tried = ""
      do n = 1, size(fallbacks)
         status = nf90_inq_varid(ncid, trim(fallbacks(n)), varid)
         if (status == nf90_noerr) then
            if (present(ierr)) ierr = OCEAN_STATUS_OK
            return
         end if
         tried = trim(tried)//" "//trim(fallbacks(n))
      end do

      call nc_close(ncid)
      call fail("ocean_zinit: no "//trim(label)//" variable found (tried:"// &
                trim(tried)//")", ierr, OCEAN_STATUS_ERR_IO)
   end subroutine find_var