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.
| Type | Intent | Optional | 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 ( |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private | :: | n | ||||
| integer, | private | :: | status | ||||
| character(len=512), | private | :: | tried |
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