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'
!-----------------------------------------------------------------------
!  integer, parameter :: im=1021, jm=701, lm=40, nwets=4
!  integer, parameter :: im=311, jm=251, lm=24, nwets=4
  integer, parameter :: nfields=41, ndyn=6,km2=2, km3=3, 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, km3_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,3) :: field3
  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
!
  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 ', 'erodfrac3 ', & 
               'bedrck    ', 'vegfrac   ', 'vegfrcPv  ', '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 binary file: '
      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/soildust netcdf')

      stat=nf90_def_dim(fcst_ncid, 'i_dim',im,lx_id)
      stat=nf90_def_dim(fcst_ncid, 'j_dim',jm,ly_id)
      stat=nf90_def_dim(fcst_ncid, 'l_dim',lm,lm_id)
      stat=nf90_def_dim(fcst_ncid, 'k2_dim',km2,km2_id)
      stat=nf90_def_dim(fcst_ncid, 'k3_dim',km3,km3_id)
      stat=nf90_def_dim(fcst_ncid, 'k4_dim',km4,km4_id)
      stat=nf90_def_dim(fcst_ncid, 'k5_dim',km5,km5_id)
      stat=nf90_def_dim(fcst_ncid, 'nwets',nwets,wets_id)
      call check_stat(stat,'Defining dimensions ')

      stat=nf90_def_var(fcst_ncid,'landusenew',NF90_INT,(/lx_id,ly_id/),field_id(1))
      call check_stat(stat,'Defining integer landusenew')
      
      stat=nf90_def_var(fcst_ncid,'bare_soil_fr',NF90_FLOAT,(/lx_id,ly_id/),field_id(2))
      call check_stat(stat,'Defining real bare_soil_fr')

      stat=nf90_def_var(fcst_ncid,'pref',NF90_FLOAT,(/lx_id,ly_id,km4_id/),field_id(3))
      call check_stat(stat,'Defining real pref')

      stat=nf90_def_var(fcst_ncid,'landusefrac',NF90_FLOAT,(/lx_id,ly_id/),field_id(4))
      call check_stat(stat,'Defining real landusefrac')
      
      stat=nf90_def_var(fcst_ncid,'erodfrac',NF90_FLOAT,(/lx_id,ly_id,km3_id/),field_id(5))
      call check_stat(stat,'Defining real erodfrac')
     
      stat=nf90_def_var(fcst_ncid,'bedrock_fr',NF90_FLOAT,(/lx_id,ly_id/),field_id(6))
      call check_stat(stat,'Defining real bedrock_fr')

      stat=nf90_def_var(fcst_ncid,'vegfrac',NF90_FLOAT,(/lx_id,ly_id/),field_id(7))
      call check_stat(stat,'Defining real vegfrac')

      stat=nf90_def_var(fcst_ncid,'vegfrcPv',NF90_FLOAT,(/lx_id,ly_id/),field_id(8))
      call check_stat(stat,'Defining real vegfrcPv')
 
      stat=nf90_def_var(fcst_ncid,'soil_mf',NF90_FLOAT,(/lx_id,ly_id,km4_id/),field_id(9))
      call check_stat(stat,'Defining real soil_mf')

      stat=nf90_def_var(fcst_ncid,'soil_mf_kok',NF90_FLOAT,(/lx_id,ly_id,km4_id/),field_id(10))
      call check_stat(stat,'Defining real soil_mf_kok')

      stat=nf90_def_var(fcst_ncid,'soil_sa',NF90_FLOAT,(/lx_id,ly_id,km4_id/),field_id(11))
      call check_stat(stat,'Defining real soil_sa')

      stat=nf90_def_var(fcst_ncid,'soil_sa_kok',NF90_FLOAT,(/lx_id,ly_id,km4_id/),field_id(12))
      call check_stat(stat,'Defining real soil_sa_kok')

      stat=nf90_def_var(fcst_ncid,'alpha',NF90_FLOAT,(/lx_id,ly_id,km2_id/),field_id(13))
      call check_stat(stat,'Defining real alpha')

      stat=nf90_def_var(fcst_ncid,'wprim',NF90_FLOAT,(/lx_id,ly_id/),field_id(14))
      call check_stat(stat,'Defining real wprim')

      stat=nf90_def_var(fcst_ncid,'ustrtr',NF90_FLOAT,(/lx_id,ly_id,km5_id/),field_id(15))
      call check_stat(stat,'Defining real ustrtr')

      stat=nf90_def_var(fcst_ncid,'z0new',NF90_FLOAT,(/lx_id,ly_id/),field_id(16))
      call check_stat(stat,'Defining real z0new')

      stat=nf90_def_var(fcst_ncid,'z0corr',NF90_FLOAT,(/lx_id,ly_id/),field_id(17))
      call check_stat(stat,'Defining real z0corr')

      stat=nf90_def_var(fcst_ncid,'roughness',NF90_FLOAT,(/lx_id,ly_id/),field_id(18))
      call check_stat(stat,'Defining real roughness')

      stat=nf90_def_var(fcst_ncid,'z0_prig',NF90_FLOAT,(/lx_id,ly_id/),field_id(19))
      call check_stat(stat,'Defining real z0_prig')


      stat=nf90_enddef(fcst_ncid)

      open(unit=21,file=file_name_bin,status='old',form='unformatted')

      read(21) run,idat,ihrst

      print*, run,idat,ihrst

      read(21) ifield(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(1),ifield(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field landusenew ')

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(2),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field bare_soil_fr ')

      read(21) field4(:,:,1:4)
      do n=1,4 
         stat=nf90_put_var(fcst_ncid,field_id(3),field4(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field pref')
      enddo

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(4),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field landusefrac ')

      read(21) field3(:,:,1:3)
      do n=1,3
         stat=nf90_put_var(fcst_ncid,field_id(5),field3(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field erodfrac')
      enddo

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(6),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field bedrock_fr')

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(7),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field vegfrac')

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(8),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field vegfrcPv')

      read(21) field4(:,:,1:4)
      do n=1,4 
         stat=nf90_put_var(fcst_ncid,field_id(9),field4(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field soil_mf')
      enddo

      read(21) field4(:,:,1:4)
      do n=1,4
         stat=nf90_put_var(fcst_ncid,field_id(10),field4(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field soil_mf_kok')
      enddo

      read(21) field4(:,:,1:4)
      do n=1,4
         stat=nf90_put_var(fcst_ncid,field_id(11),field4(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field soil_sa')
      enddo

      read(21) field4(:,:,1:4)
      do n=1,4
         stat=nf90_put_var(fcst_ncid,field_id(12),field4(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field soil_sa_kok')
      enddo

      read(21) field2(:,:,1:2)
      do n=1,2
         stat=nf90_put_var(fcst_ncid,field_id(13),field2(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field alpha')
      enddo

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(14),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field wprim')

      read(21) field5(:,:,1:5)
      do n=1,5
         stat=nf90_put_var(fcst_ncid,field_id(15),field5(:,:,n),start=(/1,1,n/),count=(/im,jm,1/))
         call check_stat(stat,'Writting field ustrtr')
      enddo

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(16),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field z0new ')

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(17),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field z0corr ')

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(18),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field roughness ')

      read(21) field(:,:)
         stat=nf90_put_var(fcst_ncid,field_id(19),field(:,:),start=(/1,1/),count=(/im,jm/))
         call check_stat(stat,'Writting field z0_prig')

!   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
