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
!
  include 'netcdf.inc'
!  include 'modelgrid.inc'
  include 'llgrid_f90.inc'
!-----------------------------------------------------------------------
!  integer, parameter :: im=1021, jm=701, lm=40, nwets=4
!  integer, parameter :: im=311, jm=251, lm=24, nwets=4
  integer, parameter :: nfields=39, ndyn=6,km2=2, km4=4, km5=5
!
!  logical :: run,global
  integer :: i, l, n, fcst_ncid, stat
  integer :: lx_id, ly_id, lm_id, wets_id, km_id, km2_id, km4_id, km5_id
!  integer, dimension(3) :: idat
!  integer :: ihrst,ihrend,ntsd
!  integer :: pdtop,lpt2,sgm,sg1,dsg1,sgml1,sg2,dsg2,sgml2
!  integer :: dzsoil_id
!  integer :: i_par_sta,j_par_sta,imm,jmm,lmm,llnsh
!  real    :: dlmd,dphd,wbd,sbd,tlm0d,tph0d
!  real    :: pt
!  real,dimension(nwets)    :: dzsoil
!
  integer, dimension(nfields) ::  field_id
!  real,dimension(im,jm) :: field
!  real,dimension(im,jm,4) :: field4
!  real,dimension(im,jm,2) :: field2
!  real,dimension(im,jm,5) :: field5
!  integer, dimension(im,jm) :: ifield
!  character(10), dimension(nfields) :: field_name
!
!  integer, dimension(ndyn) ::  dyn_id
!  real,dimension(im,jm,lm,nfields) :: dyn
!  real,dimension(im,jm,lm) :: dyn
!  character(10), dimension(ndyn) :: dyn_name
!
!  real,dimension(nwets,im,jm) :: stmp, smst, sh20
!  integer                     :: stmp_id, smst_id, sh20_id
!
  real,dimension(nx,ny) :: temp
!
  character(100) :: file_name_bin, file_name_nc
 
!  field_name=(/'landusenew', 'br_soil_fr', 'pr_src_1  ', 'pr_src_2  ', 'pr_src_3  ', &
!               'pr_src_4  ', 'landfrac  ', 'erodfrac1 ', 'erodfrac2 ', 'bedrck    ', &
!               'vegfrac   ', 'soil_mf1  ', 'soil_mf2  ', 'soil_mf3  ', 'soil_mf4  ', &
!               'soil_kok1 ', 'soil_kok2 ', 'soil_kok3 ', 'soil_kok4 ', 'soil_sa1  ', &
!               'soil_sa2  ', 'soil_sa3  ', 'soil_sa4  ', 'soil_sa_k1', 'soil_sa_k2', & 
!               'soil_sa_k3', 'soil_sa_k4', 'alpha1    ', 'alpha2    ', 'wprim     ', &
!               'ustrtr1   ', 'ustrtr2   ', 'ustrtr3   ', 'ustrtr4   ', 'ustrtr5   ', &
!               'z0new     ', 'z0corr    ', 'rough     ', 'z0_prig   '/)

!  dyn_name=(/'u         ', 'v         ', 't         ', 'q         ', 'w         ' ,  &
!             'o3        '/)

!      print*, 'im, jm, lm: ', im, jm, lm
      print*, 'name of input ungrib meteo file (p.e., 000_SOILT1.dump): '
      read(5,*) file_name_bin
      print*, 'name of netcdf output with .nc extension: '
      read(5,*) file_name_nc


      stat=nf90_create(file_name_nc, nf90_write,fcst_ncid)
      call check_stat(stat,'Creating output/ungrib meteo file netcdf')

      stat=nf90_def_dim(fcst_ncid, 'i_dim',nx,lx_id)
      stat=nf90_def_dim(fcst_ncid, 'j_dim',ny,ly_id)
      stat=nf90_def_dim(fcst_ncid, 'l_dim',nz,lm_id)
      stat=nf90_def_dim(fcst_ncid, 'nwets',4,wets_id)
      call check_stat(stat,'Defining dimensions ')

      stat=nf90_def_var(fcst_ncid,trim(file_name_bin),NF90_FLOAT,(/lx_id,ly_id/),field_id(1))
      call check_stat(stat,'Defining real field')
      
      stat=nf90_enddef(fcst_ncid)

      open(unit=21,file='../output/'//trim(file_name_bin),status='old',form='unformatted')

!      do l=1,ns-1
!      read(21) temp
!      enddo
      read(21) temp
      close(21)

      stat=nf90_put_var(fcst_ncid,field_id(1),temp,start=(/1,1/),count=(/nx,ny/))
      call check_stat(stat,'Writting field '//trim(file_name_bin))




!   e  do i=1,ndyn
!         do l=1,lm
!            read(21) dyn(:,:,l) !,i)
!!            stat=nf90_put_var(fcst_ncid,dyn_id(i),dyn(:,:,l,i),start=(/1,1,l/),count=(/im,jm,1/))
!            stat=nf90_put_var(fcst_ncid,dyn_id(i),dyn(:,:,l),start=(/1,1,l/),count=(/im,jm,1/))
!            call check_stat(stat,'Writting dynamic field ')
!         enddo
!         print *,dyn_name(i)
!      enddo

!      do i=5,nfields-3
!         read(21) field(:,:,i)
!         stat=nf90_put_var(fcst_ncid,field_id(i),field(:,:,i),start=(/1,1/),count=(/im,jm/))
!         call check_stat(stat,'Writting field 'º)
!         print *,field_name(i)
!      enddo

!      read(21) stmp
!      stat=nf90_put_var(fcst_ncid,stmp_id,stmp,start=(/1,1,1/),count=(/nwets,im,jm/))
!      call check_stat(stat,'Writting stmp ')

!      read(21) smst
!      stat=nf90_put_var(fcst_ncid,smst_id,smst,start=(/1,1,1/),count=(/nwets,im,jm/))
!      call check_stat(stat,'Writting smst ')

!      read(21) sh20
!      stat=nf90_put_var(fcst_ncid,sh20_id,sh20,start=(/1,1,1/),count=(/nwets,im,jm/))
!      call check_stat(stat,'Writting sh20 ')

!      do i=nfields-2,nfields 
!         read(21) ifield(:,:,i)
!         stat=nf90_put_var(fcst_ncid,field_id(i),ifield(:,:,i),start=(/1,1/),count=(/im,jm/))
!         call check_stat(stat,'Writting field ')
!         print *,field_name(i)
!      enddo

!     read(21) field(:,:,22)
!     stat=nf90_put_var(fcst_ncid,field_id(i),field(:,:,i),start=(/1,1/),count=(/im,jm/))
!     call check_stat(stat,'Writting field ')
!         print *,field_name(22)

!      read(21) dzsoil
!      stat=nf90_put_var(fcst_ncid,dzsoil_id,dzsoil,start=(/1/),count=(/nwets/))
!      call check_stat(stat,'Writting dzsoil ')
!               print *,"dzsoil"
      
      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
