SUBROUTINE PTENDCOR(LDSHIP,LDTEND,LDSHIPC,PLAT,PLON,POBMANT,&
 & PUSHIP,KUSHIPF,PVSHIP,KVSHIPF,&
 & PRESS,PTEND,KPTENFL,&
 & PUWIND,KUWINDF,PVWIND,KVWINDF          )  

!**** SUBROUTINE  PTENDCOR - PRESSURE TENDENCY CORRECTION

!        D. VASILJEVIC    ECMWF    04/11/1994

!     PURPOSE
!     -------

!        PRESSURE TENDENCY CORRECTION

!**   INTERFACE
!     ---------

!        CALL PTENDCOR(LDSHIP,LDTEND,LDSHIPC,PLAT,PLON,POBMANT,
!                      PUSHIP,KUSHIPF,PVSHIP,KVSHIPF,
!                      PRESS,PTEND,KPTENFL,
!                      PUWIND,KUWINDF,PVWIND,KVWINDF          )
!     WHERE:
!           LDSHIP = SHIP SWITCH
!           LDTEND = TENDENCY CORRECTION IND.
!           LDSHIPC= SHIP TENDENCY CORRECTION IND.
!           PLAT   = LATITUDE
!           PLON   = LONGITUDE
!           POBMANT= OBS. MINUS ANAL. TIME
!           PUSHIP = U COMPONENT OF SHIP MOVEMENT
!           KUSHIPF= U COMPONENT OF SHIP MOVEMENT FLAG
!           PVSHIP = V COMPONENT OF SHIP MOVEMENT
!           KVSHIPF= V COMPONENT OF SHIP MOVEMENT FLAG
!           PRESS  = PRESSURE
!           PTEND  = PRESSURE TENDENCY
!           KPTENFL= PRESSURE TENDENCY FLAG
!           PUWIND = WIND U COMPONENT
!           KUWINDF= WIND U COMPONENT FLAG
!           PVWIND = WIND V COMPONENT
!           KVWINDF= WIND V COMPONENT FLAG

!        PTENDCOR IS CALLED BY SHIPSIN AND LANSUIN

!     METHOD
!     ------

!        IN CASE OF SYNOP OFF-TIME REPORT, PRESSURE IS CORRECTED BY
!     REPORTED PRESSURE TENDENCY.  IF REPORT IS SYNOP SHIP THEN
!     PRESSURE TENDENCY IS CORRECTED BY MEANS OF REPORTED SHIP
!     MOVEMENT AND WIND.

!**   EXTERNALS
!     ---------

!**   HISTORY
!     -------

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

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

USE YOMANCS  , ONLY : RADIANS  ,RMDI     ,RMINPHOU ,RSHPCO   ,&
 & RSINAG   ,RCOSAG  
USE YOMLIM   , ONLY : NFLLIMOP

IMPLICIT NONE

LOGICAL           ,INTENT(IN)    :: LDSHIP 
LOGICAL           ,INTENT(OUT)   :: LDTEND 
LOGICAL           ,INTENT(OUT)   :: LDSHIPC 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAT 
REAL(KIND=JPRB)                  :: PLON ! Argument NOT used
REAL(KIND=JPRB)   ,INTENT(IN)    :: POBMANT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUSHIP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KUSHIPF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVSHIP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KVSHIPF 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRESS 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPTENFL 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUWIND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KUWINDF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVWIND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KVWINDF 
INTEGER(KIND=JPIM) :: IFLLIM

REAL(KIND=JPRB) :: ZCONST, ZDPODX, ZDPODY, ZSINAG, ZSINLT, ZTEND
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*

!        1.       PRESET
!                 ------

IF (LHOOK) CALL DR_HOOK('PTENDCOR',0,ZHOOK_HANDLE)
LDTEND = .FALSE.
LDSHIPC= .FALSE.
!*
!-----------------------------------------------------------------------

!        2.        CHECK INPUT PARAMETERS
!                  ----------------------

!*          2.1   CHECK IF PRESSURE/PRESSURE TENDENCY PRESENT

IFLLIM = NFLLIMOP(1)
IF(PRESS   == RMDI      )                       GO TO 5
IF(PTEND   == RMDI  .OR.KPTENFL >= IFLLIM    )  GO TO 5

!*          2.1.1 CHANGE P.T. PER MINUTE

ZTEND = PTEND / (3._JPRB*RMINPHOU)

!*          2.2   CHECK IF SHIP

IF(.NOT.LDSHIP)                                 GO TO 4

!*          2.3   FOR SHIP CHECK WIND

IFLLIM = NFLLIMOP(1)
IF(PUWIND  == RMDI  .OR.&
 & PVWIND  == RMDI  .OR.&
 & KUWINDF >= IFLLIM.OR.&
 & KVWINDF >= IFLLIM    )                     GO TO 4  

!*          2.4   CHECK SHIP MOVEMENT

IFLLIM = NFLLIMOP(1)
IF(PUSHIP  == RMDI  .OR.&
 & PVSHIP  == RMDI  .OR.&
 & KUSHIPF >= IFLLIM.OR.&
 & KVSHIPF >= IFLLIM    )                     GO TO 4  
!*
!-----------------------------------------------------------------------

!        3.       FIND OUT PRESS. TENDENCY FOR SHIP
!                 ---------------------------------

ZSINLT   =   SIN(PLAT*RADIANS)

!*          3.1   CORIOLIS TERM MULTIPLIED BY DRAG COEFFICIENT

ZCONST   =   RSHPCO * ZSINLT

!*          3.2   SIN OF CROSS-ISOBAR ANGLE

ZSINAG   =   SIGN(RSINAG,ZSINLT)

!*          3.3   PRESS. GRADIENT

ZDPODX  =   ZCONST * (RCOSAG*PVWIND - ZSINAG*PUWIND)
ZDPODY  =  -ZCONST * (RCOSAG*PUWIND + ZSINAG*PVWIND)

!*          3.4   PRESS. TENDENCY CORRECTION DUE TO SHIP MOVEMENT

ZTEND   =   ZTEND -(PUSHIP*ZDPODX+PVSHIP*ZDPODY)*RMINPHOU

!*          3.5   SET SHIP PRESSURE TENDENCY CORRECTION SWITCH

LDSHIPC = .TRUE.
!*
!-----------------------------------------------------------------------

!        4.       PRESSURE TENDENCY CORRECTION
!                 ----------------------------

4 CONTINUE

!*          4.1   CORRECT IT

PRESS  =    PRESS - ZTEND*POBMANT

!*          4.2   SET PRESSURE TENDENCY CORRECTION SWITCH

LDTEND = .TRUE.
!*
!-----------------------------------------------------------------------

!        5.       RETURN
!                 ------

5 CONTINUE
IF (LHOOK) CALL DR_HOOK('PTENDCOR',1,ZHOOK_HANDLE)
END SUBROUTINE PTENDCOR
