!-------------------------------------------------------!
! liubo 2015-07-27
!-------------------------------------------------------!
      module restoring_mod
       
      use kinds_mod
      use domain
      use constants
      use broadcast 
      use io
      use forcing_tools
      use time_management
      use prognostic
      use grid
      use exit_mod
      
      implicit none
      private
      save
      
      public :: init_restoring
      
      real (r8), dimension(nx_block, ny_block, max_blocks_clinic) &
               , public ::  res_sst, res_spc
      
      contains

      subroutine init_restoring
 
      integer (int_kind) ::       &
           i,j, k, n, iblock   
      character(char_len) ::      &
          forcing_infile_sst,     &
          forcing_infile_spc,     &
          forcing_filename_sst,   &
          forcing_filename_spc
      type (datafile) ::          &
          res_data_file_sst,      &
          res_data_file_spc 
      type(io_field_desc) ::      &
          res_datain_sst,         &
          res_datain_spc
      type(io_dim) ::             &
          i_dim, j_dim, t_dim


!     print*,my_task,'rest-check1'
      forcing_infile_sst='restoring_sst/c82_mg4.sst'
      forcing_infile_spc='restoring_spc/c82_mg4.spc'

!     print*,"ID=",my_task,'rest-check5',iyear,iday_of_year,frac_day
      write( forcing_filename_sst, 5132) trim(forcing_infile_sst), &
                                          iyear,iday_of_year
      write( forcing_filename_spc, 5132) trim(forcing_infile_spc), &
                                         iyear,iday_of_year
 5132 FORMAT ( A, '.', I4.4, '.', I3.3, '.12')

      if (my_task == master_task) then    
     print*,"forcing_filename_sst:"//trim(forcing_filename_sst)//'.nc'
     print*,"forcing_filename_spc:"//trim(forcing_filename_spc)//'.nc'
      endif
      

      res_data_file_sst = construct_file('nc',                      &
              full_name=trim(forcing_filename_sst)//'.nc',          &
              recl_words=nx_global*ny_global)
              !record_length = rec_type_real,                        &

!TS     print*,my_task,'rest-check6:'
      call data_set(res_data_file_sst, 'open_read')
!TS     print*,my_task,'rest-check7'
      i_dim = construct_io_dim('i', nx_global)
      j_dim = construct_io_dim('j', ny_global)

      res_datain_sst = construct_io_field('sst',                    &
                                dim1=i_dim, dim2=j_dim,             &
                                field_loc = field_loc_center,       &
                                field_type = field_type_scalar,     &
                                d2d_array = res_sst)
!TS     print*,my_task,'rest-check9'
      call data_set(res_data_file_sst, 'define', res_datain_sst)
!TS     print*,my_task,'rest-check10'
!TS if(my_task.eq.75)WRITE(*,*)'rest-check12a',res_sst(4,4,:)
      call data_set(res_data_file_sst, 'read',   res_datain_sst)
!TS     print*,my_task,'rest-check11'
!TS if(my_task.eq.75)WRITE(*,*)'rest-check12b',res_sst(4,4,:)
      call destroy_io_field(res_datain_sst)
      call data_set(res_data_file_sst, 'close')
      call destroy_file(res_data_file_sst)
!TS if(my_task.eq.75)WRITE(*,*)'rest-check12c',res_sst(4,4,:)

      res_data_file_spc = construct_file('nc',                      &
              full_name=trim(forcing_filename_spc)//'.nc',          &
              recl_words=nx_global*ny_global)
              !record_length = rec_type_real,                        &
      call data_set(res_data_file_spc, 'open_read')

      i_dim = construct_io_dim('i', nx_global)
      j_dim = construct_io_dim('j', ny_global)

      res_datain_spc = construct_io_field('rtau',                   &
                                dim1=i_dim, dim2=j_dim,             &
                                field_loc = field_loc_center,       &
                                field_type = field_type_scalar,     &
                                d2d_array = res_spc)

      call data_set(res_data_file_spc, 'define', res_datain_spc)
      call data_set(res_data_file_spc, 'read',   res_datain_spc)
      call destroy_io_field(res_datain_spc)
      call data_set(res_data_file_spc, 'close')
      call destroy_file(res_data_file_spc)
!     print*,my_task,'rest-exiting'
      end subroutine init_restoring

      end module restoring_mod

