SUBROUTINE wrvarbc(cdfname,ydvarbc,kngroup,knparam,kmpredxhst)
!-----------------------------------------------------------------------
!   WRVARBC - Write file with variational bias parameter information

!   Purpose.
!   --------
!   Cycling of variational bias parameter information

!   Interface.
!   ----------
!   CALL wrvarbc(cdfname,ydvarbc,kngroup,knparam)

!   Input arguments:
!      cdfname   - file name
!      ydvarbc   - observation bias parameter data structure
!      kngroup   - number of parameter groups
!      knparam   - number of bias parameters
!      kmpredxhst- histograms

!   Implicit output:
!      MPREDXCNT  - predictor sample counts
!      APREDMEAN  - predictor means
!      APREDXCOV  - predictor covariances

!   Author.
!   -------
!   Dick Dee (ECMWF)

!   Modifications.
!   --------------
!   Original    2004/03/19
!        P.Burton :  29-09-04 LVARBC cleanup
!        D. Dee      22-10-04 Write expid, date, time to formatted file
!        V. Guidard  10-03-08 hists are input args rather than implicit

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

USE parkind1, ONLY : JPIM, JPRB
USE yomhook , ONLY : lhook, dr_hook
USE yomct0  , ONLY : cnmexp
USE yomana  , ONLY : nandat, nantim
USE yomvarbc, ONLY : TYPE_VARBC, MPREDXCNT, APREDMEAN, APREDXCOV, NBINS
USE yomlun  , ONLY : nulout, nultmp
USE yommp   , ONLY : myproc

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN) :: kngroup 
CHARACTER(*)      ,INTENT(IN) :: cdfname 
TYPE(TYPE_VARBC)  ,INTENT(IN) :: ydvarbc(kngroup) 
INTEGER(KIND=JPIM),INTENT(IN) :: knparam 
INTEGER(KIND=JPIM),INTENT(IN) :: kmpredxhst(kngroup,NBINS)

INTEGER(KIND=JPIM) :: ioproc, ios, jg
REAL(KIND=JPRB)    :: zhook_handle

#include "abor1.intfb.h"

IF (lhook) CALL dr_hook('WRVARBC',0,zhook_handle)
WRITE(nulout,'(/,a)') &
 & 'WRVARBC: Writing bias information to file '//trim(cdfname)  
WRITE(nulout,'(/,a,a4,a,i8,a,i6)') &
 & 'WRVARBC: expid = ', cnmexp(1:4), &
 &          ' date = ', nandat, &
 &          ' time = ', nantim
 
!  Write the table to file
!  -----------------------
ioproc = 1
IF (myproc==ioproc) THEN

  OPEN(nultmp,file=trim(cdfname),form='formatted',status='replace',iostat=ios)
  IF (ios/=0) CALL ABOR1('WRVARBC: Cannot open file '//trim(cdfname))
  
  WRITE(nultmp,'(A)') 'VARBC_cycle.version002'
  WRITE(nultmp,*) cnmexp(1:4), nandat, nantim
  WRITE(nultmp,*) kngroup, knparam
  DO jg = 1, kngroup
    WRITE(nultmp,*) ydvarbc(jg)%nbufrid, &
                  & ydvarbc(jg)%nsensor, &
                  & ydvarbc(jg)%ncodetype, &
                  & ydvarbc(jg)%nchannl, &
                  & ydvarbc(jg)%nparam, &
                  & ydvarbc(jg)%ncount  
    WRITE(nultmp,*) ydvarbc(jg)%npredcs, &
                  & ydvarbc(jg)%aparams, &
                  & ydvarbc(jg)%aparam0, &
                  & ydvarbc(jg)%aparam5
  ENDDO

! Predictor statistics
  WRITE(nultmp,*) SIZE(MPREDXCNT,1) - 1
  WRITE(nultmp,*) MPREDXCNT, APREDMEAN, APREDXCOV
  
! FG departures histograms

  DO jg = 1, kngroup
    WRITE(nultmp,*) ydvarbc(jg)%nbufrid, &
                  & ydvarbc(jg)%nsensor, &
                  & ydvarbc(jg)%ncodetype, &
                  & ydvarbc(jg)%nchannl, &
                  & ydvarbc(jg)%nparam, &
                  & ydvarbc(jg)%ncount  
    WRITE(nultmp,*) kmpredxhst(jg,:)
  ENDDO
  
  CLOSE(nultmp)

ENDIF

WRITE(nulout,'(a,i4,a,i5,a)') &
 & 'WRVARBC: Wrote ', kngroup, ' groups with ', knparam, ' parameters'  
WRITE(nulout,'(a)') &
 & 'WRVARBC: Wrote predictor statistics'  

IF (lhook) CALL dr_hook('WRVARBC',1,zhook_handle)

END SUBROUTINE wrvarbc
