program rs_check
  use prm,          only: IMAX, JMAX, KMAX, DX
  use com_var,      only: x, zk  ! $B:BI8>pJs(B
  use file_switch,  only: rsrd_UID

  implicit none
  

  integer :: IDATE                     ! $B@QJ,3+;O;~9o(B
  integer :: KT                        ! $BA0$N@QJ,4|4V(B
  real(8) :: H(1:IMAX, 1:JMAX)             ! $BI89b(B
  real(8) :: B(1:IMAX, 1:JMAX)             ! $B4pHW9bEY(B
  real(8) :: T(1:IMAX, 1:JMAX, 1:KMAX)     ! $B29EY(B
   
  integer :: rIMAX, rJMAX, rKMAX                       ! $B%U%!%$%kFb$N>pJs(B
  real(8) :: rDX                                       ! $B%U%!%$%kFb$N>pJs(B

  integer :: i

  character(LEN=80)   :: rs_file_in
  character(LEN=80)   :: rs_file_out
  character(LEN=100)  :: HEAD
  character(LEN=3)    :: IMAXC
  character(LEN=3)    :: JMAXC
  character(LEN=3)    :: KMAXC
  character(LEN=6)    :: TIME

  namelist /NAM_FILE/ HEAD, TIME

    ! $B%G%U%)%k%H$O;~4V(B 0 $B$+$i(B
  HEAD = '2DICE-SHEET_pre'
  TIME = '040000'
    !    rewind(5)
  read(5, NAM_FILE)
  write(6, NAM_FILE)

  write(IMAXC, '(I3.3)')IMAX
  write(JMAXC, '(I3.3)')JMAX
  write(KMAXC, '(I3.3)')KMAX

    ! $BFI$_9~$_%j%9%?!<%H%U%!%$%kL>$N7hDj(B
  rs_file_in = TRIM(HEAD)//IMAXC//'_'//JMAXC//'_'//KMAXC//'_'//TIME
  write(6, *)'restart file is ', rs_file_in

  open(unit=rsrd_UID , file=TRIM(rs_file_in), access='sequential', form='unformatted', status='OLD')

  
!----------!
! $B%X%C%@!<(B !
!----------!
  write(6, *)'restart file read start'
  READ(rsrd_UID   )HEAD
  write(6, *)'HEADER: ', HEAD

!----------!
! $B2rA|EY(B   !
!----------!
  write(6, *)'IMAX, JMAX, KMAX, DX: ', IMAX, JMAX, KMAX, DX
  READ(rsrd_UID)   rIMAX, rJMAX, rKMAX, rDX
  
  if((rIMAX /= IMAX).or.(rJMAX /= JMAX).or.(rKMAX /= KMAX))then
     write(6, *)'restart file is error'
     write(6, *)'system parameter is IMAX, JMAX, KMAX, DX'
     write(6, *)'system parameter is', IMAX, JMAX, KMAX, DX
     write(6, *)'rs file          is',rIMAX, rJMAX, rKMAX, rDX
     stop
  elseif(rDX /= DX)then
     write(6, *)'warning. DX of rs file differ Dx of system '
  end if


  READ(rsrd_UID) x, zk
  write(6, *) x
  write(6, *)zk

!----------!
! $B;~4V>pJs(B !
!----------!
  READ(rsrd_UID)IDATE, KT
  write(6, *) IDATE, KT
!----------!
! $BM=JsJQ?t(B !
!----------!
  
  READ(rsrd_UID) H
  READ(rsrd_UID) B
  READ(rsrd_UID) T
  close(rsrd_UID)

  
  open(unit=rsrd_UID , file=TRIM(rs_file_in)//'.rewite', access='direct', form='unformatted', status='NEW', recl=8*imax*jmax)

  write(rsrd_UID, rec=1) H
  write(rsrd_UID, rec=2) B
  do i = 1, KMAX
     write(rsrd_UID, rec=2+i) T(:, :, i-1)
  end do
  close(rsrd_UID)

end program rs_check
