C********
      subroutine write_cbc
C
C revised to conform to new cbc file format

      INCLUDE 'CASCO.DIM'
      include 'deltabcs.h'
      include 'mesh.h' 
      include 'misc.h'

C local variables
      character*132 filename 
      integer i, k
      integer lun
      integer istart,iend
      integer iday, imonth, iyear
C
      integer nelev_bcs

      nr = nelev_bcs()
      write(*,*)'write_bcs: write final bcs to a file '
      write(*,*)'nr, ntbc ', nr, ntbc

C                                   write the file
	  write(*,*)'Enter file name for final perturbation bcs'
          read(*,'(a)')filename
	  lun = 51
	  open(lun,file=filename,status='unknown')

C calculate the start time in DD MM YY format
	  call DMY(iday,imonth,iyear,kdbc(1))
          call cstring(meshname,istart,iend)
	  write(lun,'(a)')meshname(istart:iend)
          write(lun,'(a)')'perturbation bcs: dbc_best(nr,ntbc)'
c	  write(lun,*)kdbc(1),secbc(1)
 	  write(lun,*)iday, imonth,iyear, secbc(1)
	  write(lun,*)ntbc, delttbc 
	  write(lun,*)nr
          do k = 1, ntbc
	     do i = 1, nr
		write(lun,*)k,i,lrvs(i),dbc_best(i,k)
c               write(lun,*)i,k,DBC(i,k)
	     enddo
	  enddo

	  close(lun)

      RETURN 
      END


