   !!==========================================================================================================
#ifdef key_obstructions
   SUBROUTINE obst_output(nb,tect,x,fileresu1)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE obst_output  ***
   !&E
   !&E ** Purpose :
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : output.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2012-05 (F. Ganthy) Original code addapted from sedimoutput
   !&E       !  2014-02 (F. Ganthy) Modified for new netCDF4 format
   !&E       !  2014-02 (F. Ganthy) Additional outputs (z0bstress,bstress
   !&E                              bstressc,bstressw)
   !&E       !  2014-10 (F. Ganthy) Additional outputs (theta - the bending angle)
   !&E       !  2014-12 (F. GANTHY) MPI 
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   !!   ------------
   USE parameters,   ONLY : lchain,imin,imax,jmin,jmax,rsh,rlg,riosh,                             &
                            limin,limax,ljmin,ljmax,liminp1,limaxm1,ljminp1,ljmaxm1,              &
                            liminm1,ljminm1,limaxp1,ljmaxp1,limaxp2,ljmaxp2,                      &
                            rg_valmanq_io,valmanq,kmax
   USE ionc4,        ONLY : ionc4_createfile,ionc4_gatt,ionc4_createvar,ionc4_write_sig,          &
                            ionc4_vatt_missing,ionc4_write_time,ionc4_write_xyt,                  &
                            ionc4_write_zxyt,ionc4_write_xy,ionc4_sync,ionc4_close
   USE comvars2d,    ONLY : lon2d_g,lat2d_g,lon2d,lat2d,date_ref,h0,ig,id,jb,jh,iscreenlog,       &
                            h0fond,out_dir,cdate,suffix,nbouc,name_out_h0,name_out_xe,            &
                            riog_valid_min_h0,riog_valid_max_h0,z0s,z0b
   USE comvars3d,    ONLY : sig
   USE_MPI toolmpi,  ONLY : ADD_TAG,IPROC,NPROCS,ADD_ALL_MPI_INT,MPI_COMM_MARS,MASTER
   USE outtools,     ONLY : out_write_xytvar,out_write_zxytvar,out_grid
   USE output,       ONLY : iextmin,iextmax,jextmin,jextmax,igextmin,igextmax,                    &
                            jgextmin,jgextmax,kextmin,kextmax,stepi,stepj,stepk,                  &
                            l_filebydate,tsort,tfinsor,l_out_nc4par,l_out_pack
   USE obstructions, ONLY : obst_fn_out,obst_nbvar,                                               &
                            l_obstout_pos,l_obstout_height,l_obstout_dens,l_obstout_dens3d,       &
                            l_obstout_oai,l_obstout_z0s,l_obstout_z0b,l_obstout_z0bstress,        &
                            l_obstout_bstress,l_obstout_bstressc,l_obstout_bstressw,              &
                            l_obstout_theta,                                                      &
                            obst_position,obst_height,obst_dens3d,obst_dens_inst,                 &
                            obst_area_index_inst,obst_z0bstress,obst_bstress,obst_bstressc,       &
                            obst_bstressw,obst_theta,                                             &
                            name_out_pos,riog_valid_min_pos,riog_valid_max_pos,                   &
                            name_out_height,riog_valid_min_height,riog_valid_max_height,          &
                            name_out_2ddens,riog_valid_min_2ddens,riog_valid_max_2ddens,          &
                            name_out_densz,riog_valid_min_densz,riog_valid_max_densz,             &
                            name_out_oai,riog_valid_min_oai,riog_valid_max_oai,                   &
                            name_out_z0b,riog_valid_min_z0b,riog_valid_max_z0b,                   &
                            name_out_z0s,riog_valid_min_z0s,riog_valid_max_z0s,                   &
                            name_out_zroot,riog_valid_min_zroot,riog_valid_max_zroot,             &
                            name_out_z0bstress,riog_valid_min_z0bstress,riog_valid_max_z0bstress, &
                            name_out_bstress,riog_valid_min_bstress,riog_valid_max_bstress,       &
                            name_out_bstressc,riog_valid_min_bstressc,riog_valid_max_bstressc,    &
                            name_out_bstressw,riog_valid_min_bstressw,riog_valid_max_bstressw,    &
                            name_out_theta,riog_valid_min_theta,riog_valid_max_theta,             &
                            
#ifdef key_sedim
                            l_obstout_dzlimroot,obst_dzlimroot,                                   &
#endif
                            obst_varname   

   IMPLICIT NONE

#ifdef key_MPI_2D
   INCLUDE 'mpif.h'
#endif

   !! * Arguments
   INTEGER       ,       INTENT(in) :: nb
   REAL(KIND=rlg),       INTENT(in) :: tect
   REAL(KIND=rsh),DIMENSION(     limin:limax,ljmin:ljmax),INTENT(in)     :: x
   CHARACTER(LEN=lchain),INTENT(in) :: fileresu1


   !! * Local declarations
   !!   ------------------
   INTEGER                                                   :: i,j,k,l,ifois,lnam,iv
   INTEGER                                                   :: grp_world,grp_zoom,comm_zoom,ncpu,ip,ierr_mpi
   INTEGER, DIMENSION(:), ALLOCATABLE                        :: numrank,listrank
   INTEGER, DIMENSION(1)                                     :: listrank_nb
   CHARACTER(LEN=lchain)                                     :: chain
   CHARACTER (len=19)    :: tool_sectodat,cdateout
   CHARACTER(LEN=lchain)                                     :: gridfile,name_out
   CHARACTER(len=4)                                          :: nomc
   CHARACTER(len=12)                                         :: chaine
   LOGICAL,          DIMENSION(limin:limax,ljmin:ljmax)      :: mask_h0
   REAL(KIND=riosh), DIMENSION(limin:limax,ljmin:ljmax)      :: h0_local
   REAL(KIND=rsh), DIMENSION(limin:limax,ljmin:ljmax)        :: var2D
   REAL(KIND=rsh),DIMENSION(kmax,limin:limax,ljmin:ljmax)    :: var3D
   REAL(KIND=riosh),DIMENSION(0:kmax+1)                      :: sigecrit

   !DATA ifois /-1/


   !! * Executable part
   !!   ---------------

   PRINT_DBG*, 'enter obst_output'

   ! Masks
   ! -----
   h0_local(:,:)=h0(limin:limax,ljmin:ljmax)
   mask_h0(:,:)=.TRUE.
   DO j=ljmin,ljmax
     DO i=MAX(limin,ig(j)),MIN(limax,id(j))
       IF (h0_local(i,j)+x(i,j) > h0fond)  mask_h0(i,j)=.FALSE.
     END DO
   END DO

   comm_zoom=0
#ifdef key_MPI_2D
   comm_zoom=MPI_COMM_MARS
   IF ( (nbouc == 0 .AND. .NOT.l_filebydate(nb)) .OR. (l_filebydate(nb) .AND. tect >= tsort(nb)) ) THEN
     ! what a code ! Not a clever code probably but not clear in my head at the moment
     ! Count the number of processors active over the area to be saved
     ncpu=0
     IF ( jextmin(nb) <= jextmax(nb) .AND. iextmin(nb) <= iextmax(nb) ) ncpu=ncpu+1
     CALL ADD_ALL_MPI_INT(ncpu)
     ! Save the number of active processors
     ALLOCATE (numrank(ncpu))
     ALLOCATE (listrank(nprocs))
     numrank(:)=0   ! must be initialized if the proc 0 is active on the saved zoom
     listrank(:)=0
     ! which cpu is active ?
     IF ( jextmin(nb) <= jextmax(nb) .AND. iextmin(nb) <= iextmax(nb) ) listrank(1)=iproc
     ! all cpus get the number of active cpus plus sleeping cpus (set to 0)
     listrank_nb(1) = listrank(1)
     CALL MPI_ALLGATHER(listrank_nb(1),1,MPI_INTEGER,listrank,1,MPI_INTEGER,MPI_COMM_MARS,ierr_mpi)
     ! all cpus keep the active cpus only
     IF (ncpu/=nprocs) THEN
       ip=0
       DO i=1,nprocs 
         IF_MPI ( listrank(i) /= 0 ) THEN
         ip=ip+1
         numrank(ip)=listrank(i)
         ENDIF
       END DO
       ! Group relative to the MPI_COMM_MARS
       CALL MPI_COMM_GROUP(MPI_COMM_MARS,grp_world,ierr_mpi)
       ! Create the group of active cpus
       CALL MPI_GROUP_INCL(grp_world,ncpu,numrank,grp_zoom,ierr_mpi)
       ! Create the group of active cpus
       CALL MPI_COMM_CREATE(MPI_COMM_MARS,grp_zoom,comm_zoom,ierr_mpi)
     ENDIF
   ENDIF
#endif
   
   IF ( jextmin(nb) <= jextmax(nb) .AND. iextmin(nb) <= iextmax(nb) ) THEN

     !ifois=ifois+1

     !IF ( (ifois == 0 .AND. .NOT.l_filebydate(nb)) .OR. (l_filebydate(nb) .AND. tect >= tsort(nb)) ) THEN
     IF ( (nbouc == 0 .AND. .NOT.l_filebydate(nb)) .OR. (l_filebydate(nb) .AND. tect >= tsort(nb)) ) THEN

       !-----------------------
       ! create the ouput files
       !-----------------------
       IF (l_filebydate(nb)) THEN
         obst_fn_out=TRIM(out_dir)//TRIM(fileresu1)//'_'//TRIM(suffix)//'_obst'
         obst_fn_out=TRIM(obst_fn_out)//'_'//cdate(7:10)//cdate(4:5)//cdate(1:2)//cdate(12:13)//cdate(15:16)//cdate(18:19)
         obst_fn_out=TRIM(obst_fn_out)//'.nc'
       ELSE
         lnam=index(fileresu1,'.nc')-1
         obst_fn_out=TRIM(fileresu1(1:lnam))//'_obst.nc'
       END IF
       IF_MPI( .NOT. l_out_nc4par) CALL ADD_TAG(obst_fn_out,LEN(obst_fn_out))
       IF (.NOT. l_out_nc4par) THEN
         CALL ionc4_createfile(obst_fn_out,                                                           &
                          lon2d(iextmin(nb):iextmax(nb):stepi(nb),jextmin(nb):jextmax(nb):stepj(nb)), &
                          lat2d(iextmin(nb):iextmax(nb):stepi(nb),jextmin(nb):jextmax(nb):stepj(nb)), &
                          iextmin(nb),iextmax(nb),stepi(nb),                                          &
                          jextmin(nb),jextmax(nb),stepj(nb),                                          &
                          1,kmax,1,kmax,0) 
       ELSE
         CALL ionc4_createfile(obst_fn_out,                                                                 &
                          lon2d_g(igextmin(nb):igextmax(nb):stepi(nb),jgextmin(nb):jgextmax(nb):stepj(nb)), &
                          lat2d_g(igextmin(nb):igextmax(nb):stepi(nb),jgextmin(nb):jgextmax(nb):stepj(nb)), &
                          igextmin(nb),igextmax(nb),stepi(nb),                                              &
                          jgextmin(nb),jgextmax(nb),stepj(nb),                                              &
                          1,kmax,1,kmax,0,l_out_nc4par = .TRUE.,comm_active=comm_zoom)
       ENDIF

       ! save attribute of the spatial extension of the domain
       ! -----------------------------------------------------
       CALL ionc4_gatt(obst_fn_out,'global_imin',igextmin(nb))
       CALL ionc4_gatt(obst_fn_out,'global_imax',igextmax(nb))
       CALL ionc4_gatt(obst_fn_out,'global_jmin',jgextmin(nb))
       CALL ionc4_gatt(obst_fn_out,'global_jmax',jgextmax(nb))

       ! Add attributes to follow OCO netCDF convention
       ! ----------------------------------------------
       CALL out_netcdf_conv(obst_fn_out,nb,comm_zoom,igextmin(nb),igextmax(nb),jgextmin(nb),jgextmax(nb))
       IF_MPI (l_out_nc4par .AND. comm_zoom/=MPI_COMM_MARS) CALL MPI_COMM_FREE(comm_zoom,ierr_mpi)

       ! Save sigma
       ! -----------
       sigecrit(:) = sig(:)
       CALL ionc4_createvar(obst_fn_out,'SIG',' ','sigma variable',    &
#if defined key_siggen || defined key_gencoord
                            standard_name= 'ocean_s_variable',         &
#else
                            standard_name= 'ocean_sigma_variable',     &
#endif                          
                            l_auxcoordinate=.false.,                   &
                            dims='z',valid_min= -1.0_riosh,valid_max= 0.0_riosh,fill_value= rg_valmanq_io)
       CALL ionc4_write_sig(obst_fn_out,'SIG',sigecrit,kmax)


       ! Save the obstruction position
       ! -----------------------------
       IF (l_obstout_pos) THEN
         chain='Obstruction_position_within_the_domain'
         CALL ionc4_createvar(obst_fn_out,name_out_pos,units='num',                 &
                              long_name='Obstruction position within the domain',   &
                              fill_value = -rg_valmanq_io,                          &
                              valid_min = riog_valid_min_pos,                       &
                              valid_max = riog_valid_max_pos,                       &
                              dims = 'xyt',                                         &
                              l_pack = .FALSE.,                                     &
                              l_out_nc4par = l_out_nc4par) 
       ENDIF


       ! Create time-depent variables
       !-----------------------------
       IF (l_obstout_height) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_height,units= 'm',     &
                              long_name = 'obstruction height',           &
                              fill_value = rg_valmanq_io,                 &
                              valid_min = riog_valid_min_height,          &
                              valid_max = riog_valid_max_height,          &
                              dims= 'xyt',                                &
                              l_pack = .FALSE.,                           &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_dens) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_2ddens,units= 'm-2',   &
                              long_name = '2d obstruction density',       &
                              fill_value = rg_valmanq_io,                 &
                              valid_min = riog_valid_min_2ddens,          &
                              valid_max = riog_valid_max_2ddens,          &
                              dims= 'xyt',                                &
                              l_pack = .FALSE.,                           &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_dens3d) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_densz,units= 'm-2',    &
                              long_name = '3d obstruction density',       &
                              fill_value = rg_valmanq_io,                 &
                              valid_min = riog_valid_min_densz,           &
                              valid_max = riog_valid_max_densz,           &
                              dims= 'zxyt',                               &
                              l_pack = .FALSE.,                           &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_theta) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_theta,units= 'degres', &
                              long_name = '3d obstruction bending angle', &
                              fill_value = rg_valmanq_io,                 &
                              valid_min = riog_valid_min_theta,           &
                              valid_max = riog_valid_max_theta,           &
                              dims= 'zxyt',&
                              l_pack = .FALSE.,&
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_oai) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_oai,units= 'm-2',      &
                              long_name = 'obstruction area index',       &
                              fill_value = rg_valmanq_io,                 &
                              valid_min = riog_valid_min_oai,             &
                              valid_max = riog_valid_max_oai,             &
                              dims= 'xyt',                                &
                              l_pack = .FALSE.,                           &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_z0s) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_z0s,units= 'm',           &
                              long_name = 'roughness length at sea surface', &
                              fill_value = rg_valmanq_io,                    &
                              valid_min = riog_valid_min_z0s,                &
                              valid_max = riog_valid_max_z0s,                &
                              dims= 'xyt',                                   &
                              l_pack = .FALSE.,                              &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_z0b) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_z0b,units= 'm',           &
                              long_name = 'roughness length at bottom',      &
                              fill_value = rg_valmanq_io,                    &
                              valid_min = riog_valid_min_z0b,                &
                              valid_max = riog_valid_max_z0b,                &
                              dims= 'xyt',                                   &
                              l_pack = .FALSE.,                              &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

#ifdef key_sedim
       IF (l_obstout_dzlimroot) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_zroot,units= 'm-2',                            &
                              long_name = 'depth of obstruction root system within the sediment', &
                              fill_value = rg_valmanq_io,                                         &
                              valid_min = riog_valid_min_zroot,                                   &
                              valid_max = riog_valid_max_zroot,                                   &
                              dims= 'xyt',                                                        &
                              l_pack = .FALSE.,                                                   &
                              l_out_nc4par = l_out_nc4par)
       ENDIF
#endif

       IF (l_obstout_z0bstress) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_z0bstress,units= 'm',        &
                              long_name = 'roughness length for bottom stress', &
                              fill_value = rg_valmanq_io,                       &
                              valid_min = riog_valid_min_z0bstress,             &
                              valid_max = riog_valid_max_z0bstress,             &
                              dims= 'xyt',                                      &
                              l_pack = .FALSE.,                                 &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_bstress) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_bstress,units= 'N.m-2',      &
                              long_name = 'total_bottom stress',                &
                              fill_value = rg_valmanq_io,                       &
                              valid_min = riog_valid_min_bstress,               &
                              valid_max = riog_valid_max_bstress,               &
                              dims= 'xyt',                                      &
                              l_pack = .FALSE.,                                 &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_bstressc) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_bstressc,units= 'N.m-2',     &
                              long_name = 'current_bottom stress',              &
                              fill_value = rg_valmanq_io,                       &
                              valid_min = riog_valid_min_bstressc,              &
                              valid_max = riog_valid_max_bstressc,              &
                              dims= 'xyt',                                      &
                              l_pack = .FALSE.,                                 &
                              l_out_nc4par = l_out_nc4par)
       ENDIF

#ifdef key_wave
       IF (l_obstout_bstressw) THEN
         CALL ionc4_createvar(obst_fn_out,name_out_bstressw,units= 'N.m-2',     &
                              long_name = 'wave_bottom stress',                 &
                              fill_value = rg_valmanq_io,                       &
                              valid_min = riog_valid_min_bstressw,              &
                              valid_max = riog_valid_max_bstressw,              &
                              dims= 'xyt',                                      &
                              l_pack = .FALSE.,                                 &
                              l_out_nc4par = l_out_nc4par)
       ENDIF
#endif

     ENDIF ! END TEST ON nbouc (ifois) 

     !IF ( (ifois > 0 .AND. .NOT.l_filebydate(nb)) .OR. (l_filebydate(nb) .AND. tect >= tsort(nb) .AND. tect <= tfinsor(nb)) ) THEN
     !IF ( (nbouc > 0 .AND. .NOT.l_filebydate(nb)) .OR. (l_filebydate(nb) .AND. tect >= tsort(nb) .AND. tect <= tfinsor(nb)) ) THEN
     IF (.NOT.l_filebydate(nb) .OR. (l_filebydate(nb) .AND. tect >= tsort(nb) .AND. tect <= tfinsor(nb)) ) THEN

       CALL ionc4_write_time(obst_fn_out,0,tect)

       IF (l_obstout_pos) THEN
           var2D(:,:) = 0.0_rsh
           DO j=ljmin,ljmax
             DO i=limin,limax
                var2D(i,j) = REAL(obst_position(i,j))
             ENDDO
           ENDDO
           CALL out_write_xytvar(obst_fn_out,name_out_pos,-rg_valmanq_io,mask_h0, &
                                 var2D(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_height) CALL out_write_xytvar(obst_fn_out,name_out_height,rg_valmanq_io,mask_h0, &
                                                   obst_height(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)

       IF (l_obstout_dens) THEN
         var2D(:,:) = 0.0_rsh
         DO iv=1,obst_nbvar
           DO j=ljmin,ljmax
             DO i=limin,limax
               IF(obst_position(i,j).EQ.iv) var2D(i,j) = obst_dens_inst(iv)
             ENDDO
           ENDDO
         ENDDO
        CALL out_write_xytvar(obst_fn_out,name_out_2ddens,rg_valmanq_io,mask_h0, &
                              var2D(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_dens3d) CALL out_write_zxytvar(obst_fn_out,name_out_densz,rg_valmanq_io,mask_h0, &
                                obst_dens3d(:,limin:limax,ljmin:ljmax),0,1,l_out_nc4par = l_out_nc4par)

       IF (l_obstout_theta) THEN
         var3D(:,:,:) = 0.0_rsh
         DO j=ljmin,ljmax
           DO i=limin,limax
             DO k=1,kmax
               var3D(k,i,j) = obst_theta(k,i,j)*180.0_rsh/3.1415_rsh
             ENDDO
           ENDDO
         ENDDO
         CALL out_write_zxytvar(obst_fn_out,name_out_theta,rg_valmanq_io,mask_h0, &
                                var3D(:,limin:limax,ljmin:ljmax),0,1,l_out_nc4par = l_out_nc4par)
       ENDIF
 

       IF (l_obstout_oai) THEN
         var2D(:,:) = 0.0_rsh
         DO iv=1,obst_nbvar
           DO j=ljmin,ljmax
             DO i=limin,limax
               IF(obst_position(i,j).EQ.iv) var2D(i,j) = obst_area_index_inst(iv)
             ENDDO
           ENDDO
         ENDDO
         CALL out_write_xytvar(obst_fn_out,name_out_oai,rg_valmanq_io,mask_h0, &
                               var2D(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)
       ENDIF

       IF (l_obstout_z0s) CALL out_write_xytvar(obst_fn_out,name_out_z0s,rg_valmanq_io,mask_h0, &
                                                z0s(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)

       IF (l_obstout_z0b) CALL out_write_xytvar(obst_fn_out,name_out_z0b,rg_valmanq_io,mask_h0, &
                                                z0b(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)

#ifdef key_sedim
       IF (l_obstout_dzlimroot) CALL out_write_xytvar(obst_fn_out,name_out_zroot,rg_valmanq_io,mask_h0, &
                                                obst_dzlimroot(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)
#endif

       IF (l_obstout_z0bstress) CALL out_write_xytvar(obst_fn_out,name_out_z0bstress,rg_valmanq_io,mask_h0, &
                                                      obst_z0bstress(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)

       IF (l_obstout_bstress) CALL out_write_xytvar(obst_fn_out,name_out_bstress,rg_valmanq_io,mask_h0, &
                                                      obst_bstress(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)

       IF (l_obstout_bstressc) CALL out_write_xytvar(obst_fn_out,name_out_bstressc,rg_valmanq_io,mask_h0, &
                                                      obst_bstressc(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)

#ifdef key_wave
       IF (l_obstout_bstressw) CALL out_write_xytvar(obst_fn_out,name_out_bstressw,rg_valmanq_io,mask_h0, &
                                                      obst_bstressw(limin:limax,ljmin:ljmax),0,l_out_nc4par = l_out_nc4par)
#endif

     ENDIF 

     IF (l_filebydate(nb)) THEN
       CALL ionc4_close(obst_fn_out)
     ELSE
       CALL ionc4_sync(obst_fn_out)
     ENDIF

   ENDIF ! END TEST jextmin/jextmax iextmin/iextmax

   PRINT_DBG*, 'exit obst_output'

#else
  SUBROUTINE obst_output
#endif
  END SUBROUTINE obst_output
   !!===========================================================================
