MODULE read_metric_mod

CONTAINS


  SUBROUTINE read_metric
  USE icosa
  USE xios_mod
  USE omp_para
  IMPLICIT NONE
    TYPE(t_field),POINTER,SAVE :: f_xcell(:),f_ycell(:),f_zcell(:)
    REAL(rstd), POINTER :: xcell(:), ycell(:), zcell(:)
    TYPE(t_domain),POINTER :: d
    INTEGER :: ind,i,j,n
    CHARACTER(len=255) :: etat0_type
    LOGICAL :: read_metric_

    read_metric_=.FALSE.    
    CALL getin('etat0',etat0_type)
    CALL getin('read_metric', read_metric_) 
    
    CALL allocate_field(f_xcell,field_t,type_real,name='xcell')
    CALL allocate_field(f_ycell,field_t,type_real,name='ycell')
    CALL allocate_field(f_zcell,field_t,type_real,name='zcell')

    CALL xios_set_context_input
    CALL xios_init_write_field_input 

    IF (TRIM(etat0_type)=='start_file' .AND. read_metric_) THEN
    
      CALL xios_read_field("xcell_start",f_xcell)
      CALL xios_read_field("ycell_start",f_ycell)
      CALL xios_read_field("zcell_start",f_zcell)

      DO ind=1,ndomain
        IF (.NOT. assigned_domain(ind)  .OR. .NOT. is_omp_level_master) CYCLE
        d=>domain(ind)
        CALL swap_dimensions(ind)
        xcell=f_xcell(ind) 
        ycell=f_ycell(ind) 
        zcell=f_zcell(ind) 
      
        DO j=jj_begin,jj_end
          DO i=ii_begin,ii_end
            n=(j-1)*iim+i
            d%xyz(1,i,j) = xcell(n) 
            d%xyz(2,i,j) = ycell(n) 
            d%xyz(3,i,j) = zcell(n) 
          ENDDO
        ENDDO
      ENDDO
    ENDIF

    CALL xios_write_field_finalize
    CALL xios_set_context

  END SUBROUTINE read_metric



END MODULE read_metric_mod
