!===========================================================================

SUBROUTINE LBRM_ArraySizes(ErrStatus, WHandle, BoundCond, DaysOfSim)

   USE LBRM_MAIN

   IMPLICIT NONE
   INTEGER, INTENT(INOUT)  :: ErrStatus     !  0=OK,  otherwise an error
   INTEGER, INTENT(INOUT)  :: WHandle
   INTEGER, INTENT(INOUT)  :: BoundCond, DaysOfSim

   DLL_EXPORT LBRM_ArraySizes

   WinHandle = WHandle
   ErrorLevel = 0

   UseAhpsCfg = .FALSE.
   AhpsCfgFile = ''

   CALL ReadLBRMCFGfile             
   CALL FindArraySizes(DaysOfSim, BoundCond)

   ErrStatus = ErrorLevel
   IF (ErrStatus .NE. 0) CALL PassMsg

END SUBROUTINE LBRM_ArraySizes


!===========================================================================

SUBROUTINE LBRM_Guts(ErrStatus, WHandle, cCons, BCnt, DCnt,              &
  &                  BCSeqs, BCU, BCL, BCG, BCS, BCN,                    &
  &                  Mx, Mn, MP, ORn, OUz, OLz, OGz, OSz, OSn,           &
  &                  LT1, LT2, LT3, LT4, LT5, LT6)

   USE LBRM_Main

   IMPLICIT  NONE
   INTEGER, INTENT(INOUT)  :: ErrStatus     !  0=OK,  otherwise an error
   INTEGER, INTENT(INOUT)  :: WHandle
   INTEGER, INTENT(INOUT)  :: BCnt, DCnt
   REAL, INTENT(INOUT)     :: cCons
   INTEGER (KIND=INTEGER2), DIMENSION(DCnt), INTENT(INOUT) :: Mx, Mn, MP
   INTEGER, DIMENSION(BCnt), INTENT(INOUT) :: BCSeqs
   REAL,    DIMENSION(BCnt), INTENT(INOUT) :: BCU, BCL, BCG, BCS, BCN
   REAL,    DIMENSION(DCnt), INTENT(INOUT) :: ORn, OUz, OLz, OGz, OSz, OSn
   REAL,    INTENT(INOUT) :: LT1, LT2, LT3, LT4, LT5, LT6

   DLL_EXPORT LBRM_Guts

   WinHandle = WHandle
   ErrorLevel = 0

   iNoDataPoints = DCnt
   iNoBC = BCnt

   UseAhpsCfg = .FALSE.
   AhpsCfgFile = ''

   CALL ReadLBRMCfgFile
   CALL ReadBoundaryConditionsFile(BCSeqs, BCU, BCL, BCG, BCS, BCN)
   CALL ReadParameterFile
   CALL LBRM_Engine(cCons, BCnt, DCnt,                             &
  &                 BCSeqs, BCU, BCL, BCG, BCS, BCN,               &
  &                 Mx, Mn, MP, ORn, OUz, OLz, OGz, OSz, OSn,      &
  &                 LT1, LT2, LT3, LT4, LT5, LT6)

   ErrStatus = ErrorLevel
   IF (ErrStatus .NE. 0) CALL PassMsg

END SUBROUTINE LBRM_Guts


!===========================================================================

SUBROUTINE LBRM_ReadData(ErrStatus, WHandle, BCnt, DCnt, BCSeqs,      &
  &                      BCU, BCL, BCG, BCS, BCN, Mx, Mn, MP)

   USE LBRM_Main

   IMPLICIT  NONE
   INTEGER, INTENT(INOUT)  :: ErrStatus     !  0=OK,  otherwise an error
   INTEGER, INTENT(INOUT)  :: WHandle
   INTEGER, INTENT(INOUT)  :: BCnt, DCnt
   INTEGER (KIND=INTEGER2), DIMENSION(DCnt), INTENT(INOUT) :: Mx, Mn, MP
   INTEGER, DIMENSION(BCnt), INTENT(INOUT) :: BCSeqs
   REAL,    DIMENSION(BCnt), INTENT(INOUT) :: BCU, BCL, BCG, BCS, BCN

   DLL_EXPORT LBRM_ReadData

   WinHandle = WHandle
   ErrorLevel = 0

   iNoDataPoints = DCnt
   iNoBC = BCnt

   UseAhpsCfg = .FALSE.
   AhpsCfgFile = ''

   CALL InputLBRMdata(BCnt, DCnt, BCSeqs, BCU, BCL, BCG, BCS, BCN,    &
  &                   Mx, Mn, MP)
 
   ErrStatus = ErrorLevel
   IF (ErrStatus .NE. 0) CALL PassMsg

END SUBROUTINE LBRM_ReadData


!===========================================================================

SUBROUTINE LBRM_WriteData(ErrStatus, WHandle, DCnt,                   &
  &                       ORun, OUsz, OLsz, OGzm, OSrf, OSnw)

   USE LBRM_Main

   IMPLICIT  NONE
   INTEGER, INTENT(INOUT)  :: ErrStatus     !  0=OK,  otherwise an error
   INTEGER, INTENT(INOUT)  :: WHandle
   INTEGER, INTENT(INOUT)  :: DCnt
   REAL, DIMENSION(DCnt), INTENT(INOUT) :: ORun, OUsz, OLsz
   REAL, DIMENSION(DCnt), INTENT(INOUT) :: OGzm, OSrf, OSnw

   DLL_EXPORT LBRM_WriteData

   WinHandle = WHandle
   ErrorLevel = 0

   iNoDataPoints = DCnt

   UseAhpsCfg = .FALSE.
   AhpsCfgFile = ''

   CALL WriteLBRMOutputFile(ORun, OUsz, OLsz, OGzm, OSrf, OSnw)

   ErrStatus = ErrorLevel
   IF (ErrStatus .NE. 0) CALL PassMsg

END SUBROUTINE LBRM_WriteData



