SUBROUTINE FPNILPHY(KFIELDS,KST,KEND,KFPROMA,KFLDAUX,LDINTER,PAUX,PROW,LDNIL)

!**** *FPNILPHY*  - FULL-POS writer of the non-interpolatable fields

!     PURPOSE.
!     --------
!        To compute the fields that are not directly computable 
!        (ie : an auxilary computation has been necessary or a 
!        straight value is applied, unless abort is required).

!**   INTERFACE.
!     ----------
!       *CALL* *FPNILPHY*

!        EXPLICIT ARGUMENTS
!        --------------------

!        INPUT:
!         KFIELDS: number of fields in row.
!         KST    : first output point in row.
!         KEND   : last output point in row.
!         KFPROMA: length of the output row.
!         KFLDAUX: total number of fields in auxilary fields row
!         LDINTER: .TRUE. if horizontal interpolation active

!        INPUT/OUTPUT:
!         PAUX   : auxilary fields row
!         PROW   : output fields row

!        OUTPUT:
!         LDNIL : logical mask : .TRUE. if the non-interpolatable 
!                  field has been computed

!        IMPLICIT ARGUMENTS
!        --------------------
!          See module below

!     METHOD.
!     -------
!          Scan the request ; if it is a non-interpolatable field
!          then fill the row as possible or abort ; set the logical mask
!          to .TRUE. if the field row is filled.

!     EXTERNALS.
!     ----------
!      CVLANISO

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS
!        See documentation about FULL-POS.

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 98-09-03 (partially from FPINTPHY)
!      R. El Khatib 99-11-23 Enable extrapolations for ISBA clim. fields
!                            when there have been no interpolations before
!      R. El Khatib 00-04-11 Enable pseudo-interpolations for ISBA clim. fields
!                            (ie: option "nearest point")
!      Vanda Costa  00-10-20 Resistance to evapotranspiration and interception
!                            content removed
!        D. Giard   00-10-25 Two more fields for ISBA
!      R. El Khatib 01-03-26 Modified test on KIND to prevent abort in Aladin E-zone
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        K. Yessad: 31-01-2007 Cleanings + optional calcul. of PROW,PAUX,LDNIL
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE PTRFP4   , ONLY : YRQGFP
USE PTRFPB2  , ONLY : MAFPTR
USE YOMRFPDS , ONLY : MOFPTR
USE YOMAFN   , ONLY : GFP_SDOG ,GFP_PADOU,GFP_PADOV,GFP_ACOT ,GFP_ALS  , &
 & GFP_ALV ,GFP_DPAT ,GFP_RSMIN,GFP_ARG  ,GFP_SAB  ,GFP_D2   , &
 & GFP_IVEG,GFP_LAI  ,GFP_Z0H  ,GFP_BSR  ,GFP_BAAL ,GFP_PHYDS
USE YOMLUN   , ONLY : NULOUT

!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDAUX 
LOGICAL           ,INTENT(IN)    :: LDINTER 
REAL(KIND=JPRB)   ,INTENT(INOUT), OPTIONAL :: PAUX(KFPROMA,KFLDAUX) 
REAL(KIND=JPRB)   ,INTENT(INOUT), OPTIONAL :: PROW(KFPROMA,KFIELDS) 
LOGICAL           ,INTENT(OUT)  , OPTIONAL :: LDNIL(KFIELDS) 

!     ------------------------------------------------------------------

REAL(KIND=JPRB) :: ZDUMM(KFPROMA)

INTEGER(KIND=JPIM) :: IERR, JFLD, ICOD
INTEGER(KIND=JPIM) :: IASDO, IADOU, IADOV

LOGICAL :: LLALFA, LLCONV, LLGAMA, LLN, LLR
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

#include "abor1.intfb.h"
#include "cvlaniso.intfb.h"

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('FPNILPHY',0,ZHOOK_HANDLE)

!     ------------------------------------------------------------------

!*       1. Initializations
!           ---------------

! Compute LDNIL:
IF (PRESENT(LDNIL)) THEN
  LLN=.TRUE.
ELSE
  LLN=.FALSE.
ENDIF

! Use/modify PROW and PAUX:
IF (PRESENT(PAUX).AND.(.NOT.PRESENT(PROW))) THEN
  CALL ABOR1(' FPNILPHY: PAUX present => PROW should be present ')
ELSEIF (PRESENT(PROW).AND.(.NOT.PRESENT(PAUX))) THEN
  CALL ABOR1(' FPNILPHY: PROW present => PAUX should be present ')
ELSEIF (PRESENT(PROW).AND.PRESENT(PAUX)) THEN
  LLR=.TRUE.
ELSE
  LLR=.FALSE.
ENDIF

IASDO=MAFPTR(GFP_SDOG%ICOD)
IADOU=MAFPTR(GFP_PADOU%ICOD)
IADOV=MAFPTR(GFP_PADOV%ICOD)

!     ------------------------------------------------------------------

!*       2. LOOP ON PHYSICAL FIELDS
!           -----------------------

IERR=0

DO JFLD=1,KFIELDS

  ICOD=YRQGFP%ICOD(JFLD)
  IF (MOFPTR(ICOD) <= 0) THEN

    IF (ICOD == GFP_ACOT%ICOD) THEN    
      ! Anisotropy of orography
      !  => Deconvolution of vector (sigma**2)*(1-gamma)/(1+gamma)
      IF (IASDO > 0.AND.IADOU > 0.AND.IADOV > 0) THEN
        IF (LLR) THEN
          LLCONV=.FALSE.
          LLGAMA=.TRUE.
          LLALFA=.FALSE.
          CALL CVLANISO(KST,KEND,KFPROMA,LLCONV,LLGAMA,LLALFA,PROW(1,JFLD), &
           & ZDUMM,PAUX(1,IADOU),PAUX(1,IADOV),PAUX(1,IASDO))  
        ENDIF
        IF (LLN) LDNIL(JFLD)=.TRUE.
      ELSE
        CALL ABOR1('FPNILPHY : INTERNAL ERROR IASDO/IADOU/IADOV')
      ENDIF
    ELSEIF(ICOD == GFP_DPAT%ICOD) THEN
      ! Main axis of orography
      !  => Deconvolution of vector (sigma**2)*(1-gamma)/(1+gamma)
      IF (IADOU > 0.AND.IADOV > 0) THEN
        IF (LLR) THEN
          LLCONV=.FALSE.
          LLGAMA=.FALSE.
          LLALFA=.TRUE.
          CALL CVLANISO(KST,KEND,KFPROMA,LLCONV,LLGAMA,LLALFA,ZDUMM, &
           & PROW(1,JFLD),PAUX(1,IADOU),PAUX(1,IADOV),ZDUMM)  
        ENDIF
        IF (LLN) LDNIL(JFLD)=.TRUE.
      ELSE
        CALL ABOR1('FPNILPHY : INTERNAL ERROR IADOU/IADOV')
      ENDIF
    ELSEIF(LDINTER.AND.GFP_PHYDS(ICOD)%INTER > 0) THEN
      IF (ICOD == GFP_IVEG%ICOD  .OR. &
         & ICOD == GFP_RSMIN%ICOD .OR. &
         & ICOD == GFP_ARG%ICOD   .OR. &
         & ICOD == GFP_SAB%ICOD   .OR. &
         & ICOD == GFP_D2%ICOD    .OR. &
         & ICOD == GFP_LAI%ICOD   .OR. &
         & ICOD == GFP_BSR%ICOD   .OR. &
         & ICOD == GFP_BAAL%ICOD  .OR. &
         & ICOD == GFP_ALS%ICOD   .OR. &
         & ICOD == GFP_ALV%ICOD   .OR. &
         & ICOD == GFP_Z0H%ICOD) THEN  
        WRITE(NULOUT,'(''INTERPOLATION NOT ALLOWED ON FIELD '',A16)') &
         & GFP_PHYDS(ICOD)%CLNAME  
        IERR=IERR+1
      ENDIF 
    ENDIF
  ENDIF

ENDDO

IF (IERR > 0) THEN
  CALL ABOR1('FPNILPHY : ABOR1 CALLED (IERR>0)')
ENDIF

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('FPNILPHY',1,ZHOOK_HANDLE)
END SUBROUTINE FPNILPHY
