 MODULE outtools
   !!======================================================================
   !!                   ***  MODULE  outtools  ***
   !! Outputs : tools to create files, save data or describe netcdf files
   !!
   !!   History :
   !!        !  2011-01 (R. Ramel Alyotech) Ionc library in fortran 90/95, netcdf4 and MPI
   !!
   !!======================================================================

#include "toolcpp.h"

   !! * Modules used
   USE ionc4, ONLY : ionc4_createvar,ionc4_vatt_fill,ionc4_write_xyt,ionc4_write_zxyt,ionc4_write_xy
   
   IMPLICIT NONE
   PRIVATE

   !! * Accessibility
   PUBLIC out_write_xytvar,      &    ! routine called by output_save.F90
          out_write_zxytvar,     &
          out_write_zytvar,      &
          out_write_ytvar,       &
          out_grid

   !! * Shared or public module variables

   !! * Private variables

 CONTAINS

  !!======================================================================
  
  SUBROUTINE out_grid(filename)
  
   !&E-----------------------------------------------------------------------
   !&E   
   !&E			*** ROUTINE out_grid  ***
   !&E   
   !&E** Purpose : save grid parameters (scale factor, longitude_u _v _f, latitude_u _v _f)
   !&E
   !&E** Called by : create_file.F90 (ionc4)
   !&E
   !&E** External calls :
   !&E
   !&E** Used ij-arrays :
   !&E
   !&E** Modified variables :
   !&E
   !&E** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-05  (V. Garnier)  Original code
   !&E
   !&E-----------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : limin,limax,ljmin,ljmax,riolg,dg_valmanq_io,lchain
   USE comvars2d,  ONLY : lon2d,lat2d,lon2du,lon2dv,lon2df,lat2du,lat2dv,lat2df,dx,dx_u,dx_v,dx_f,dy,dy_u,dy_v,dy_f, &
                          riog_valid_min_dx,riog_valid_min_dy,riog_valid_max_dx,riog_valid_max_dy,lon2d_g,lat2d_g,surf
   USE output,     ONLY : l_out_nc4par,l_out_pack

   !! * Arguments
   CHARACTER(LEN=lchain), INTENT( IN ) :: filename
  
   !! * Local declaration
   REAL(kind=riolg)  ,DIMENSION(limin:limax,ljmin:ljmax) :: tmp_rlg
  
   !!------------------------------------------------------------------------
   !! * Executable part

   PRINT_DBG*, 'enter OUT_GRID routine for fileres=',TRIM(filename)

   CALL ionc4_createvar(filename,'longitude_u',units='degrees_east',long_name='longitude at u location', &
                        standard_name='longitude_at_u_location',fill_value = -dg_valmanq_io,               &
                        valid_min=REAL(MINVAL(lon2d_g)-0.1_riolg,riolg), &
                        valid_max=REAL(MAXVAL(lon2d_g)+0.1_riolg,riolg),dims='xy',location_in_cell='u',    &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par,l_auxcoordinate=.false.)
   CALL ionc4_write_xy(filename,'longitude_u',lon2du(limin:limax,ljmin:ljmax),limin,limax,ljmin,ljmax,  &
                        fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'longitude_v',units='degrees_east',long_name='longitude at v location', &
                        standard_name='longitude_at_v_location',fill_value = -dg_valmanq_io,               &
                        valid_min=REAL(MINVAL(lon2d_g)-0.1_riolg,riolg), &
                        valid_max=REAL(MAXVAL(lon2d_g)+0.1_riolg,riolg),dims='xy',location_in_cell='v',    &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par,l_auxcoordinate=.false.)
   CALL ionc4_write_xy(filename,'longitude_v',lon2dv(limin:limax,ljmin:ljmax),limin,limax,ljmin,ljmax,  &
                        fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'latitude_u',units='degrees_north',long_name='latitude at u location',  &
                        standard_name='latitude_at_u_location',fill_value = -dg_valmanq_io,                &
                        valid_min=REAL(MINVAL(lat2d_g)-0.1_riolg,riolg), &
                        valid_max=REAL(MAXVAL(lat2d_g)+0.1_riolg,riolg),dims='xy',location_in_cell='u',      &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par,l_auxcoordinate=.false.)
   CALL ionc4_write_xy(filename,'latitude_u',lat2du(limin:limax,ljmin:ljmax),limin,limax,ljmin,ljmax,   &
                        fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'latitude_v',units='degrees_north',long_name='latitude at v location',  &
                        standard_name='latitude_at_v_location',fill_value = -dg_valmanq_io,                &
                        valid_min=REAL(MINVAL(lat2d_g)-0.1_riolg,riolg), &
                        valid_max=REAL(MAXVAL(lat2d_g)+0.1_riolg,riolg),dims='xy',location_in_cell='v',      &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par,l_auxcoordinate=.false.)
   CALL ionc4_write_xy(filename,'latitude_v',lat2dv(limin:limax,ljmin:ljmax),limin,limax,ljmin,ljmax,   &
                        fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

#if ! defined key_out_grid_smallsize

   CALL ionc4_createvar(filename,'longitude_f',units='degrees_east',long_name='longitude at f location', &
                        standard_name='longitude_at_f_location',fill_value = -dg_valmanq_io,               &
                        valid_min=REAL(MINVAL(lon2d_g)-0.1_riolg,riolg), &
                        valid_max=REAL(MAXVAL(lon2d_g)+0.1_riolg,riolg),dims='xy',location_in_cell='f',    &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par,l_auxcoordinate=.false.)

   CALL ionc4_write_xy(filename,'longitude_f',lon2df(limin:limax,ljmin:ljmax),limin,limax,ljmin,ljmax,  &
                        fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'latitude_f',units='degrees_north',long_name='latitude at f location',  &
                        standard_name='latitude_at_f_location',fill_value = -dg_valmanq_io,                &
                        valid_min=REAL(MINVAL(lat2d_g)-0.1_riolg,riolg), &
                        valid_max=REAL(MAXVAL(lat2d_g)+0.1_riolg,riolg),dims='xy',location_in_cell='f',      &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par,l_auxcoordinate=.false.)
   CALL ionc4_write_xy(filename,'latitude_f',lat2df(limin:limax,ljmin:ljmax),limin,limax,ljmin,ljmax,   &
                        fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dx_f',units='m',long_name='mesh size along x at f location',    &
                        standard_name='cell_x_size_at_f_location',fill_value = -dg_valmanq_io,       &
                        valid_min=riog_valid_min_dx,valid_max=riog_valid_max_dx,dims='xy',location_in_cell='f',      &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)    
   tmp_rlg(limin:limax,ljmin:ljmax)=dx_f(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dx_f',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dx_u',units='m',long_name='mesh size along x at u location',    &
                        standard_name='cell_x_size_at_u_location',fill_value = -dg_valmanq_io,   &
                        valid_min=riog_valid_min_dx,valid_max=riog_valid_max_dx,dims='xy',location_in_cell='u',  &
                        l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)  
   tmp_rlg(limin:limax,ljmin:ljmax)=dx_u(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dx_u',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dx_v',units='m',long_name='mesh size along x at v location',    &
                               standard_name='cell_x_size_at_v_location',fill_value = -dg_valmanq_io,  &
                               valid_min=riog_valid_min_dx,valid_max=riog_valid_max_dx,dims='xy',location_in_cell='v', &
                               l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=dx_v(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dx_v',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dx',units='m',long_name='mesh size along x', &
               standard_name='cell_x_size',fill_value = -dg_valmanq_io,       &
                           valid_min=riog_valid_min_dx,valid_max=riog_valid_max_dx,dims='xy',          &
                           l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=dx(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dx',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dy_v',units='m',long_name='mesh size along y at v location',       &
                               standard_name='cell_y_size_at_v_location',fill_value = -dg_valmanq_io,  &
                               valid_min=riog_valid_min_dy,valid_max=riog_valid_max_dy,dims='xy',location_in_cell='v', &
                               l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=dy_v(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dy_v',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dy_u',units='m',long_name='mesh size along y at u location',        &
                                standard_name='cell_y_size_at_u_location',fill_value = -dg_valmanq_io, &
                                valid_min=riog_valid_min_dy,valid_max=riog_valid_max_dy,dims='xy',location_in_cell='u', &
                                l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=dy_u(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dy_u',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dy_f',units='m',long_name='mesh size along y at f location',   &
                           standard_name='cell_y_size_at_f_location',fill_value = -dg_valmanq_io, &
                           valid_min=riog_valid_min_dy,valid_max=riog_valid_max_dy,dims='xy',location_in_cell='f', &
                           l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=dy_f(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dy_f',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'dy',units='m',long_name='mesh size along y',     &
                           standard_name='cell_y_size',fill_value = -dg_valmanq_io, &
                           valid_min=riog_valid_min_dy,valid_max=riog_valid_max_dy,dims='xy',                &
                           l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=dy(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'dy',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)

   CALL ionc4_createvar(filename,'surf',units='m',long_name='surface mesh size',     &
                           standard_name='surf_size',fill_value = -dg_valmanq_io, &
                           valid_min=riog_valid_min_dy,valid_max=riog_valid_max_dy,dims='xy',                &
                           l_pack = l_out_pack,l_out_nc4par = l_out_nc4par)
   tmp_rlg(limin:limax,ljmin:ljmax)=surf(limin:limax,ljmin:ljmax)
   CALL ionc4_write_xy(filename,'surf',tmp_rlg,limin,limax,ljmin,ljmax,fill_value=-dg_valmanq_io,l_out_nc4par=l_out_nc4par)
#endif

   PRINT_DBG*, 'exit OUT_GRID routine',TRIM(filename)

  END SUBROUTINE out_grid

  !!==========================================================================

  SUBROUTINE out_write_xytvar(filename,varname,maskvalue,maskvar,var,irecord,l_out_nc4par)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE out_write_xytvar  ***
   !&E
   !&E ** Purpose : record a 3d variables into the netcdf file
   !&E              var(time,longitude,latitude)
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : output_save.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-05  (V. Garnier)  Original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : lchain,rsh,rlg,riosh,riolg,limin,limax,ljmin,ljmax, &
                          ishift_obc_west,ishift_obc_east,jshift_obc_south,jshift_obc_north, &
                          imin,imax,jmin,jmax

   !! * Arguments
   CHARACTER(LEN=lchain), INTENT( IN ) :: filename,varname
   LOGICAL,        DIMENSION(limin:limax,ljmin:ljmax), INTENT( IN )   :: maskvar
   INTEGER              , INTENT( IN ) :: irecord
   REAL(KIND=riosh)     , INTENT( IN ) :: maskvalue
   REAL(KIND=rsh), DIMENSION(limin:limax,ljmin:ljmax), INTENT( IN )   :: var
   LOGICAL, OPTIONAL    , INTENT( IN ) :: l_out_nc4par    

   !! * Local declarations
   INTEGER                :: i,j
   REAL(KIND=riosh), DIMENSION(limin:limax,ljmin:ljmax)        :: tecrit

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

   DO j=ljmin,ljmax
     DO i=limin,limax
       IF ( maskvar(i,j) ) THEN
         tecrit(i,j)=maskvalue
       ELSE
         tecrit(i,j)=var(i,j)
       END IF
     END DO
   END DO
   IF ( present(l_out_nc4par)) THEN
     CALL ionc4_write_xyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tecrit,    &
                limin,limax,ljmin,ljmax,irecord,fill_value=maskvalue,l_out_nc4par=l_out_nc4par) 
   ELSE
     CALL ionc4_write_xyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tecrit,    &
                limin,limax,ljmin,ljmax,irecord,fill_value=maskvalue) 
   ENDIF 

  END SUBROUTINE out_write_xytvar

   !!======================================================================

  SUBROUTINE out_write_zxytvar(filename,varname,maskvalue,maskvar,var,irecord,kmin,l_out_nc4par)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE out_write_zxytvar  ***
   !&E
   !&E ** Purpose : record a 4d variables into the netcdf file
   !&E              var(time,level,longitude,latitude)
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : output_save.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-05  (V. Garnier)  Original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : lchain,rsh,rlg,riosh,riolg,limin,limax,ljmin,ljmax,kmax

   !! * Arguments
   CHARACTER(LEN=lchain), INTENT( IN ) :: filename,varname
   LOGICAL,        DIMENSION(limin:limax,ljmin:ljmax), INTENT( IN )   :: maskvar
   INTEGER              , INTENT( IN ) :: irecord,kmin
   REAL(KIND=riosh)     , INTENT( IN ) :: maskvalue
   REAL(KIND=rsh), DIMENSION(kmin:kmax,limin:limax,ljmin:ljmax), INTENT( IN )   :: var
   LOGICAL, OPTIONAL,     INTENT( IN ) :: l_out_nc4par 

   !! * Local declarations
   INTEGER                :: i,j
   REAL(KIND=riosh), DIMENSION(kmin:kmax,limin:limax,ljmin:ljmax)        :: tzecrit

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

   DO j=ljmin,ljmax
     DO i=limin,limax
       IF ( maskvar(i,j) ) THEN
         tzecrit(:,i,j)=maskvalue
       ELSE
         tzecrit(:,i,j)=var(:,i,j)
       END IF
     END DO
   END DO
   IF ( present(l_out_nc4par)) THEN
     CALL ionc4_write_zxyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tzecrit,    &
                limin,limax,ljmin,ljmax,kmin,kmax,irecord,fill_value=maskvalue,l_out_nc4par = l_out_nc4par)
   ELSE
     CALL ionc4_write_zxyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tzecrit,    &
                limin,limax,ljmin,ljmax,kmin,kmax,irecord,fill_value=maskvalue)
   ENDIF

  END SUBROUTINE out_write_zxytvar
 
   !!======================================================================

  SUBROUTINE out_write_zytvar(filename,varname,maskvalue,maskvar,var,irecord,kmin,l_out_nc4par)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE out_write_zytvar  ***
   !&E
   !&E ** Purpose : record a 2d (zy) variables into the netcdf file
   !&E              var(time,level,latitude)
   !&E
   !&E ** Description : on ne fait pas des moyennes zonales tous les jours
   !&E                  donc je n ai pas cree des ionc4_write_zy et ionc4_write_zyt
   !&E                  Par consequent : recopie dans un tableau 3d avant sauvegarde quand moyenne zonale .....
   !&E
   !&E ** Called by : output_save.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-05  (V. Garnier)  Original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : lchain,rsh,rlg,riosh,riolg,limin,limax,ljmin,ljmax,kmax,liminp1
   USE ionc4,      ONLY : ionc4_write_zxyt

   !! * Arguments
   CHARACTER(LEN=lchain), INTENT( IN ) :: filename,varname
   LOGICAL,        DIMENSION(ljmin:ljmax), INTENT( IN )   :: maskvar
   INTEGER              , INTENT( IN ) :: irecord,kmin
   REAL(KIND=riosh)     , INTENT( IN ) :: maskvalue
   REAL(KIND=rsh), DIMENSION(kmin:kmax,ljmin:ljmax), INTENT( IN )   :: var
   LOGICAL, OPTIONAL,     INTENT( IN ) :: l_out_nc4par

   !! * Local declarations
   INTEGER                :: i,j
   REAL(KIND=riosh), DIMENSION(kmin:kmax,limin:limax,ljmin:ljmax)        :: tzecrit

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

   PRINT_DBG*, 'enter OUT_WRITE_ZYTVAR routine'

   DO j=ljmin,ljmax
     DO i=limin,limax
       IF ( maskvar(j) ) THEN
         tzecrit(:,i,j)=maskvalue
       ELSE
         tzecrit(:,i,j)=var(:,j)
       END IF
     END DO
   END DO
   IF ( present(l_out_nc4par)) THEN
     CALL ionc4_write_zxyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tzecrit, &
                limin,limax,ljmin,ljmax,kmin,kmax,irecord,fill_value=maskvalue,l_out_nc4par = l_out_nc4par)
   ELSE
     CALL ionc4_write_zxyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tzecrit, &
                limin,limax,ljmin,ljmax,kmin,kmax,irecord,fill_value=maskvalue)
   ENDIF

   PRINT_DBG*, 'exit OUT_WRITE_ZYTVAR routine'

  END SUBROUTINE out_write_zytvar

  !!==========================================================================

  SUBROUTINE out_write_ytvar(filename,varname,maskvalue,maskvar,var,irecord,l_out_nc4par)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE out_write_ytvar  ***
   !&E
   !&E ** Purpose : record a 1d (y) variables into the netcdf file
   !&E              var(time,latitude)
   !&E
   !&E ** Description : on ne fait pas des moyennes zonales tous les jours
   !&E                  donc je n ai pas cree des ionc4_write_zy et ionc4_write_zyt
   !&E                  Par consequent : recopie dans un tableau 3d avant sauvegarde quand moyenne zonale .....
   !&E
   !&E ** Called by : output_save.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2009-05  (V. Garnier)  Original code
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : lchain,rsh,rlg,riosh,riolg,limin,limax,ljmin,ljmax
   USE ionc4,      ONLY : ionc4_write_xyt

   !! * Arguments
   CHARACTER(LEN=lchain), INTENT( IN ) :: filename,varname
   LOGICAL,        DIMENSION(ljmin:ljmax), INTENT( IN )   :: maskvar
   INTEGER              , INTENT( IN ) :: irecord
   REAL(KIND=riosh)     , INTENT( IN ) :: maskvalue
   REAL(KIND=rsh), DIMENSION(ljmin:ljmax), INTENT( IN )   :: var
   LOGICAL, OPTIONAL    , INTENT( IN ) :: l_out_nc4par

   !! * Local declarations
   INTEGER                :: i,j
   REAL(KIND=riosh), DIMENSION(limin:limax,ljmin:ljmax)        :: tecrit

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

   DO j=ljmin,ljmax
     DO i=limin,limax
       IF ( maskvar(j) ) THEN
         tecrit(i,j)=maskvalue
       ELSE
         tecrit(i,j)=var(j)
       END IF
     END DO
   END DO
   IF ( present(l_out_nc4par)) THEN
     CALL ionc4_write_xyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tecrit,    &
                limin,limax,ljmin,ljmax,irecord,fill_value=maskvalue,l_out_nc4par=l_out_nc4par)
   ELSE
     CALL ionc4_write_xyt(filename,TRIM(ADJUSTL(ADJUSTR(varname))),tecrit,    &
                limin,limax,ljmin,ljmax,irecord,fill_value=maskvalue)
   ENDIF

  END SUBROUTINE out_write_ytvar

   !!======================================================================
 
 END MODULE outtools
