!-------------------------------------- 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_open_files
*
#include "model_macros_f.h"
*

      subroutine e_open_files (init_cond) 1,7
      implicit none
*
      logical init_cond
*author 
*    Michel Desgagne - Summer 2008
*
*revision
* v3_31 - Desgagne M.       - Initial version
*
#include "e_anal.cdk"
#include "e_grids.cdk"
#include "e_topo.cdk"
#include "bmf.cdk"
#include "ptopo.cdk"
#include "e_cdate.cdk"
#include "e_schm.cdk"
#include "pilot.cdk"
#include "e_grdc.cdk"
#include "path.cdk"
#include <clib_interface.cdk>
*
      character*16 pdate
      character*1024 rootfn,filen
      integer is,js,jn,iw,ie,jw,njw,niw,nis,njs,nvar,err
* ---------------------------------------------------------------------
*
      nvar=5
      if (E_Schm_offline_L) nvar = 3
      call datf2p (pdate,datev)

      rootfn=trim(Path_output_S)//'/BCDS_3D/'
      if (init_cond) rootfn=trim(Path_output_S)//'/INIT_3D/'

      err = clib_mkdir (trim(rootfn))

      if (Pil_bmf_L) then
         call bmf_splitstart (Ptopo_npex,Ptopo_npey,trim(rootfn),'BM',
     $                                Bmf_time1,Bmf_hh,Bmf_mm,Bmf_ss)
         call e_bmfsplitxy2 (topo ,nifi,njfi,'ME  ',1,1,pni ,0,0,0)
         call e_bmfsplitxy2 (topou,niu ,nju ,'TOPU',1,1,pniu,0,0,0)
         call e_bmfsplitxy2 (topov,niv ,njv ,'TOPV',1,1,pni ,0,0,0)
         call bmf_splitwrall ('RNA ',LV,1,1,Bmf_time1,Bmf_time2, 
     $                                    0,0,bmf_dtyp,0,RNA)
         if (anal_hyb.or.anal_eta) then
             call bmf_splitwrall ('PTOP',1,1,1,Bmf_time1,Bmf_time2
     $                                  ,0,0,bmf_dtyp,0,ptopa)
             call bmf_splitwrall ('PREF',1,1,1,Bmf_time1,Bmf_time2
     $                                  ,0,0,bmf_dtyp,0,prefa)
             call bmf_splitwrall ('RCOF',1,1,1,Bmf_time1,Bmf_time2
     $                                 ,0,0,bmf_dtyp,0,rcoefa)
         endif
      else
         if (Pil_bcs_hollow_L) then
            call bcs_hollow(
     $                 e_grdc_gid ,e_grdc_gif ,e_grdc_gjd ,e_grdc_gjf,
     $                 e_grdc_gjdi,e_grdc_hbsn,e_grdc_hbwe,is,nis,
     $                 js,njs,jn,iw,niw,ie,jw,njw)
            call e_sfile_bcs (pdate,is,nis,js,jn,njs,iw,ie,niw,jw,njw,
     $                                         'DYNAMICS',nvar,rootfn)
         else
*
* Write out the 3df_filemap.txt
*
            filen=trim(rootfn)//'/3df_filemap.txt'
            open (9,file=filen,access='SEQUENTIAL',form='FORMATTED')
            write (9,'(2i8,4e15.7,2i10)') 1,1,
     $               xg_8(e_grdc_gid),xg_8(e_grdc_gif),
     $               yg_8(e_grdc_gjd),yg_8(e_grdc_gjf),
     $               e_grdc_gif-e_grdc_gid+1,e_grdc_gjf-e_grdc_gjd+1
            close (9)

            call e_sfile_3df (pdate,'DYNAMICS',
     $        e_grdc_gid,e_grdc_gif,e_grdc_gjd,e_grdc_gjf, nvar,rootfn)
         endif
      endif
*
* ---------------------------------------------------------------------
*
      return
      end