subroutine patch0(f,rmiss,mft) c This routine fills in missing values of f c using interpolation or extrapolation. c The version is for a 1D array that starts at index 0. c c Modified Jan 2023 (KM) for NCO specifications c dimension f(0:mft) c do k=0,mft if (f(k) .ge. rmiss) then c Search forward for good data ifwd = -99 if (k .lt. mft) then do kk=k+1,mft if (f(kk) .lt. rmiss) then ifwd=kk ffwd=f(kk) exit !inner do loop endif enddo endif c c Search backward for good data ibwd = -99 do kk=k-1,0,-1 if (f(kk) .lt. rmiss) then ibwd=kk fbwd=f(kk) exit !inner do loop endif enddo c if (ibwd .gt. -99 .and. ifwd .eq. -99) then c Extrapolate forward f(k) = fbwd elseif (ibwd .eq. -99 .and. ifwd .gt. -99) then c Extrapolate backward f(k) = ffwd elseif (ibwd .gt. -99 .and. ifwd .gt. -99) then c Interpolate tnow = float(k) tbwd = float(ibwd) tfwd = float(ifwd) c wtbwd = (tfwd-tnow)/(tfwd-tbwd) wtfwd = (tnow-tbwd)/(tfwd-tbwd) c f(k) = wtbwd*fbwd + wtfwd*ffwd endif endif enddo !outer loop c return end