!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
***s/r e_wrgeop
*

      subroutine e_wrgeop  1,2
      implicit none
*
*author 
*     M. Desgagne - January 2001
*
*revision
* v2_20 - Pellerin P.       - Adapted from MC2 to writeout geobus
* v2_21 - Desgagne M.       - Include global statistics
* v2_21 - Lee V.            - Correction on IF statement for LAM
* v3_30 - Desgagne M.       - new physics interface
*
#include "e_grids.cdk"
* comdecks used also in GEMPP
#include "model_macros_f.h"
#include "itf_phy_buses.cdk"
#include "lctl.cdk"
#include "lun.cdk"
#include "path.cdk"
#include <clib_interface.cdk>
*
*MODULES
*
**
      integer i, j, nim, njm, ict, ivar, im, osgeo, err
      character*1024 rootfn
*
*----------------------------------------------------------------------
*
      Lctl_r8stat_L=.true.
      nim=nifi
      njm=njfi
*
      if (.not.lam) nim=nifi-1
*
*     contraction of the geophysical bus (to remove the last column)
*
         ict=0
         do ivar = 1, p_bgeo_top
         do im   = 1, geopar(ivar,3)
            do j=1,njm
            do i=1,nim
               ict=ict+1
               geobus(ict)=
     $         geobus(geopar(ivar,1)+ (im-1)*nifi*njfi+ (j-1)*nifi+ i-1)
            end do
            end do
         end do
         end do
*
*     re-defining geobus structure in terms of nim,njm
*
         p_bgeo_top=0
         p_bgeo_siz=0
         do i=1,p_bgeo_top
            geonm(i,1)=' '
            geonm(i,2)=' '
            geonm(i,5)=' '
         enddo
         call e_geopini (nim,njm,-1)
*
      lun_out = 6
      do i=1,p_bgeo_top
      do j=1,geopar(i,3)
         call statfld (geobus(geopar(i,1)+(j-1)*nim*njm),geonm(i,1),
     $                 j,"geopfld",1,nim,1,njm,1,1,1,1,nim,njm,1)
      end do
      end do
*
      rootfn = trim(Path_output_S)//'/INIT_SFC/'
      err    = clib_mkdir (trim(rootfn))
      osgeo  = 53
      open(osgeo,file=trim(rootfn)//'/geophy.bin',
     $                     access='SEQUENTIAL',form='UNFORMATTED')
*
      write(osgeo) p_bgeo_top, p_bgeo_siz
      write(osgeo) (geonm (i,1),i=1,p_bgeo_top),
     $             (geonm (i,2),i=1,p_bgeo_top),
     $             (geonm (i,5),i=1,p_bgeo_top)
      write(osgeo) (geopar(i,1),i=1,p_bgeo_top),
     $             (geopar(i,2),i=1,p_bgeo_top),
     $             (geopar(i,3),i=1,p_bgeo_top)
      write(osgeo) (geobus(i),i=1,p_bgeo_siz)
*
      close (osgeo)
*
*----------------------------------------------------------------------
      return
      end