MODULE etat0_start_file_interp_mod

CONTAINS

   SUBROUTINE init_etat0
      USE xios_mod
      USE omp_para
      USE time_mod
      USE restart_mod
      IMPLICIT NONE

      IF (is_omp_master) THEN
         IF (using_xios) THEN
            CALL xios_set_file_attr("start_interp", enabled=.TRUE., output_freq=(itaumax + 1)*xios_timestep)
         END IF
      END IF
      CALL set_write_start(.TRUE.)
      CALL set_read_start(.TRUE.)
      CALL set_read_interp(.TRUE.)

   END SUBROUTINE init_etat0

   SUBROUTINE etat0(f_ps, f_phis, f_theta_rhodz, f_u, f_q)
      USE icosa
      USE restart_mod
      USE wind_from_lonlat_mod
      USE write_field_mod
      USE time_mod
      USE transfert_mod
      USE omp_para
      USE pression_mod
      USE xios_mod
      USE checksum_mod
      IMPLICIT NONE
      TYPE(t_field), POINTER :: f_ps(:)
      TYPE(t_field), POINTER :: f_phis(:)
      TYPE(t_field), POINTER :: f_theta_rhodz(:)
      TYPE(t_field), POINTER :: f_u(:)
      TYPE(t_field), POINTER :: f_q(:)

      TYPE(t_field), POINTER, SAVE :: f_ulon(:)
      TYPE(t_field), POINTER, SAVE :: f_ulat(:)
      TYPE(t_field), POINTER, SAVE :: f_pmid(:)
      TYPE(t_field), POINTER, SAVE :: f_theta_rhodz_1d(:)
      REAL(rstd), POINTER :: theta_rhodz(:, :, :), theta_rhodz_1d(:, :)

      LOGICAL :: etat0_start_file_colocated = .FALSE.
      INTEGER :: ind
      LOGICAL :: raz

      CALL allocate_field(f_theta_rhodz_1d, field_t, type_real, llm, name='theta_rhodz')

!$OMP BARRIER
      DO ind = 1, ndomain
         IF (.NOT. assigned_domain(ind) .OR. .NOT. is_omp_level_master) CYCLE
         f_q(ind)%rval4d(:, :, :) = 0.
      END DO
!$OMP BARRIER

      CALL allocate_field(f_ulon, field_t, type_real, llm, name='ulon')
      CALL allocate_field(f_ulat, field_t, type_real, llm, name='ulat')
      CALL allocate_field(f_pmid, field_t, type_real, llm, name='p')

      CALL read_start(itau0, f_ps, f_phis)
      CALL transfert_request(f_ps, req_i1)
      CALL transfert_request(f_ps, req_i0)
      CALL pression_mid(f_ps, f_pmid)
      CALL xios_write_field("p_start_interp", f_pmid)
      CALL read_start(field0=f_theta_rhodz_1d, field1=f_ulon, field2=f_ulat, field3=f_q)

      CALL transfert_request(f_ulat, req_i1)
      CALL transfert_request(f_ulat, req_i0)
      CALL transfert_request(f_ulon, req_i1)
      CALL transfert_request(f_ulon, req_i0)

      CALL ulonlat2un(f_ulon, f_ulat, f_u)

      CALL transfert_request(f_ps, req_i1)
      CALL transfert_request(f_ps, req_i0)
      CALL transfert_request(f_phis, req_i1)
      CALL transfert_request(f_phis, req_i0)
      CALL transfert_request(f_theta_rhodz_1d, req_i1)
      CALL transfert_request(f_theta_rhodz_1d, req_i0)
      CALL transfert_request(f_q, req_i1)
      CALL transfert_request(f_q, req_i0)
      CALL transfert_request(f_u, req_e1_vect)
      CALL transfert_request(f_u, req_e0_vect)

      CALL deallocate_field(f_ulon)
      CALL deallocate_field(f_ulat)
      CALL deallocate_field(f_pmid)

      !$OMP BARRIER
      DO ind = 1, ndomain
         IF (.NOT. assigned_domain(ind) .OR. .NOT. is_omp_level_master) CYCLE
         theta_rhodz = f_theta_rhodz(ind); theta_rhodz_1d = f_theta_rhodz_1d(ind)
         theta_rhodz(:, :, 1) = theta_rhodz_1d(:, :)
      END DO
      !$OMP BARRIER

      CALL bcast(itau0)
      raz = .FALSE.
      CALL getin("etat0_start_iteration_reset", raz)
      IF (raz) itau0 = 0
      CALL getin("etat0_start_iteration", itau0)

   END SUBROUTINE etat0

END MODULE etat0_start_file_interp_mod
