program view_fcst
  use netcdf
  implicit none 
!how to compile
! xlf90 -q32 -o view_fcst-high44.x view_fcst_new.f90 -I/gpfs/apps/NETCDF/32/include -L/gpfs/apps/NETCDF/32/lib -lnetcdf
!
! ifort -free -convert big_endian -o view_fcst.x view_fcst_NEMSCOR.f90 -I$NETCDF/include -L$NETCDF/lib -lnetcdf -lnetcdff
! gfortran -m64 view_fcst_NEMSCOR.f90 -L/$NETCDF/lib -lnetcdf -lnetcdff -lm -I/$NETCDF/include -fconvert=big-endian -o view_fcst.x

  include 'netcdf.inc'
  include 'modelgrid.inc'
!  include 'lmimjm.inc'
!-----------------------------------------------------------------------
!  integer, parameter :: im=311, jm=251, lm=24, nwets=4
!  integer, parameter :: imi=311, jmi=251, lnsh=1,lnsv=1 
! Parameters to ERAInt. MGoncalves.25.02.2013
! BOCO representation - NEMS v 5 /only atm. module
  integer, parameter :: nfields=24
!
  logical :: run
  integer :: i, l, fcst_ncid, stat
  integer :: lx_id, ly_id, lm_id, lnsh_id
  integer, dimension(3) :: idat
  integer :: ihrst,ihrend,ntsd,tboco2

  integer, dimension(nfields) ::  field_id
  real,dimension(imi,jmi,nfields) :: field
  integer, dimension(imi,jmi,nfields) :: ifield
  character(10), dimension(nfields) :: field_name
!! DECLARE BOUNDARY VARIABLES
!-----------------------------------------------------------------------
  real,dimension(imi,lnsh,2) :: pdbs,pdbn
  real,dimension(lnsh,jmi,2) :: pdbw,pdbe
!
  real,dimension(imi,lnsh,lm,2) :: tbs,tbn,qbs,qbn,wbs,wbn,ubs,ubn,vbs,vbn
  real,dimension(lnsv,jmi,lm,2) :: tbw,tbe,qbw,qbe,wbw,wbe,ubw,ube,vbe,vbw
!-----------------------------------------------------------------------

 field_name=(/'pdbs      ', 'pdbn      ', 'tbs       ', 'tbn       ', 'qbs       ', &
              'qbn       ', 'wbs       ', 'wbn       ', 'ubs       ', 'ubn       ', &
              'vbs       ', 'vbn       ', 'pdbe      ', 'pdbw      ', 'tbe       ', &
              'tbw       ', 'qbw       ', 'qbe       ', 'wbw       ', 'wbe       ', &
              'ube       ', 'ubw       ', 'vbe       ', 'vbw       '/)


      stat=nf90_create('boco_chem.24.nc', nf90_write,fcst_ncid)
      call check_stat(stat,'Opening output/boco.nc ')
      stat=nf90_def_dim(fcst_ncid, 'i_dim',imi,lx_id)
      stat=nf90_def_dim(fcst_ncid, 'j_dim',jmi,ly_id)
      stat=nf90_def_dim(fcst_ncid, 'l_dim',lm,lm_id)
      stat=nf90_def_dim(fcst_ncid, 'lnsh_dim',lnsh,lnsh_id)
      call check_stat(stat,'Defining dimensions ')

      print*, 'DIMENSIONS ',imi,jmi,lm,lnsh
      do i=1, 12
         print*, 'FIELD ', field_name(i)
         if(field_name(i).eq.'pdbs      ' .or. field_name(i).eq.'pdbn      ') then
            stat=nf90_def_var(fcst_ncid,field_name(i),NF90_FLOAT,(/lx_id,lnsh_id/),field_id(i))
            call check_stat(stat,'Defining 2d fields')
         else
            stat=nf90_def_var(fcst_ncid,field_name(i),NF90_FLOAT,(/lx_id,lnsh_id,lm_id/),field_id(i))
            call check_stat(stat,'Defining 3d fields')
         endif
      enddo
      do i=13, 24
         print*, 'FIELD ', field_name(i)
         if(field_name(i).eq.'pdbe      ' .or. field_name(i).eq.'pdbw      ') then
            stat=nf90_def_var(fcst_ncid,field_name(i),NF90_FLOAT,(/lnsh_id,ly_id/),field_id(i))
            call check_stat(stat,'Defining 2d fields')
         else
            stat=nf90_def_var(fcst_ncid,field_name(i),NF90_FLOAT,(/lnsh_id,ly_id,lm_id/),field_id(i))
            call check_stat(stat,'Defining 3d fields')
         endif
      enddo
      stat=nf90_enddef(fcst_ncid)
!***********************************************************************
! READ BOCO BINARY FILE AND WRITE THE NETCDF VARS
!write(fname,'(a,i5.5)')'../output/boco.'
!     &,ihr-ihboco
!      print*,'fname ',fname,'ihr ',ihr,'ihboco ',ihboco
!     open(unit=nbc,file=fname
!    &    ,status='unknown',form='unformatted')


      open(unit=21,file='boco.0000',status='old',form='unformatted')
      read(21) run,idat,ihrst,tboco2
      read(21) pdbs,pdbn,pdbw,pdbe
      read(21) tbs,tbn,tbw,tbe  
      read(21) qbs,qbn,qbw,qbe
      read(21) wbs,wbn,wbw,wbe
      read(21) ubs,ubn,ubw,ube
      read(21) vbs,vbn,vbw,vbe
      print*, run,idat,ihrst
      print*, 'PDBS', pdbs(1:10,1:3,:)
      print*, 'SIZE PDBS', size(pdbs,dim=1),size(pdbs,dim=2)
      print*, 'SIZE VBS', size(vbs,dim=1), size(vbs,dim=2), size(vbs,dim=3)
      print*, 'SIZE VBN', size(vbn,dim=1), size(vbn,dim=2), size(vbn,dim=3)

      print*, 'SIZE PDBE', size(pdbe,dim=1),size(pdbe,dim=2)
      print*, 'SIZE VBE', size(vbe,dim=1), size(vbe,dim=2), size(vbe,dim=3)
      print*, 'SIZE VBW', size(vbw,dim=1), size(vbw,dim=2), size(vbw,dim=3)

      print*, 'DEF DIMS: imi ',imi,'jmi ',jmi,'lnsh ',lnsh,'lm ',lm
      print*,'Regional' ! MGON1:fis,stdh,sm,pd    
!-------------------------North south boundaries ------------------------------------------------
         stat=nf90_put_var(fcst_ncid,field_id(1),pdbs,start=(/1,1/),count=(/imi,lnsh/))
         call check_stat(stat,'Writting field pdbs')
         stat=nf90_put_var(fcst_ncid,field_id(2),pdbn,start=(/1,1/),count=(/imi,lnsh/))
         call check_stat(stat,'Writting field pdbn')
         stat=nf90_put_var(fcst_ncid,field_id(3),tbs,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field tbs')
         stat=nf90_put_var(fcst_ncid,field_id(4),tbn,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field tbn')
         stat=nf90_put_var(fcst_ncid,field_id(5),qbs,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field qbs')
         stat=nf90_put_var(fcst_ncid,field_id(6),qbn,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field qbn')
         stat=nf90_put_var(fcst_ncid,field_id(7),wbs,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field wbs')
         stat=nf90_put_var(fcst_ncid,field_id(8),wbn,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field wbn')
         stat=nf90_put_var(fcst_ncid,field_id(9),ubs,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field ubs')
         stat=nf90_put_var(fcst_ncid,field_id(10),ubn,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field ubn')
         stat=nf90_put_var(fcst_ncid,field_id(11),vbs,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field ubs')
         stat=nf90_put_var(fcst_ncid,field_id(12),vbn,start=(/1,1,1/),count=(/imi,lnsh,lm/))
         call check_stat(stat,'Writting field vbn')
!-------------------------West east boundaries ------------------------------------------------
         stat=nf90_put_var(fcst_ncid,field_id(13),pdbe,start=(/1,1/),count=(/lnsh,jmi/))
         call check_stat(stat,'Writting field pdbe')
         stat=nf90_put_var(fcst_ncid,field_id(14),pdbw,start=(/1,1/),count=(/lnsh,jmi/))
         call check_stat(stat,'Writting field pdbw')
         stat=nf90_put_var(fcst_ncid,field_id(15),tbe,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field tbs')
         stat=nf90_put_var(fcst_ncid,field_id(16),tbw,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field tbn')
         stat=nf90_put_var(fcst_ncid,field_id(17),qbw,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field qbs')
         stat=nf90_put_var(fcst_ncid,field_id(18),qbe,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field qbn')
         stat=nf90_put_var(fcst_ncid,field_id(19),wbw,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field wbs')
         stat=nf90_put_var(fcst_ncid,field_id(20),wbe,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field wbn')
         stat=nf90_put_var(fcst_ncid,field_id(21),ube,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field ubs')
         stat=nf90_put_var(fcst_ncid,field_id(22),ubw,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field ubn')
         stat=nf90_put_var(fcst_ncid,field_id(23),vbe,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field ubs')
         stat=nf90_put_var(fcst_ncid,field_id(24),vbw,start=(/1,1,1/),count=(/lnsh,jmi,lm/))
         call check_stat(stat,'Writting field vbn')
      stat=nf90_close(fcst_ncid)
!-----------------------------------------------------------------------
      end program
subroutine check_stat(stat,message)
     use netcdf
     implicit none
     include 'netcdf.inc'
     integer, intent(in)        :: stat
     character*100, intent(in)   :: message

     if ( stat /= nf90_NoErr ) then
        print*, 'Error in netcdf while...'
        print*, TRIM(message)
       STOP 
     endif
end subroutine check_stat
