!
!    #    #    #     #     #####    ##    #       #
!    #    ##   #     #       #     #  #   #       #
!    #    # #  #     #       #    #    #  #       #
!    #    #  # #     #       #    ######  #       #
!    #    #   ##     #       #    #    #  #       #
!    #    #    #     #       #    #    #  ######  ######
!
!
!  $Author: pkubota $
!  $Date: 2008/09/23 17:51:54 $
!  $Revision: 1.9 $
!
MODULE InitAll
  USE Constants, Only: &
       r8,             &
       i8
  USE Options       , Only : InitOptions,dtt,path_inp,idate,idatec,idatep,nfsibd,nfprt,nfsibt,&
                             fNameSibVeg,fNameSibmsk,ifday,tod,fNameIBISMask,fNameSSTAOI,&
			     fNameIBISDeltaTemp,fNameSandMask,fNameClayMask,fNameClimaTemp,&
			     RESTART,DELTAOUT,DELTAIN,UNDIMENSION,sand_site,clay_site,clmt_site,InitOptionsB ! LGG dt
  USE Sfc_Ibis_Fiels, Only : InitFieldsIbis,nband,nsoilay,depth, InitFieldsIbisB, texdat
  USE Sizes         , Only : InitSizes, ibMaxPerJB,iMax, jMax, kMax, ibMax, jbMax
  USE FieldsPhysics , Only : initFieldsPhysics
  USE Utils         , Only : InitUtils
  USE IbisOutput    , ONLY : Init_IbisOutput
  USE AtmosModel    , ONLY : RunAtmosModel,InitAtmosModel,RunAtmosModel1D

  IMPLICIT NONE
  PRIVATE
  PUBLIC :: InitA,InitB
CONTAINS

  SUBROUTINE InitA()  
    IMPLICIT NONE
    INTEGER :: nVars=345
    REAL(KIND=r8) :: xres
    REAL(KIND=r8) :: yres
        print*,'InitOptions'

    CALL InitOptions(iMax,jMax,kMax)    
    xres= 360.0_r8/REAL(iMax,kind=r8)
    yres= 180.0_r8/REAL(jMax,kind=r8)
         print*,'InitOptions'
   
    CALL InitSizes(iMax,jMax)
    print*,'InitUtils'
    CALL InitUtils(iMax,jMax)
    CALL initFieldsPhysics  (iMax,jMax,kMax,ibMax,jbMax)   
    CALL InitAtmosModel(ibMax,jbMax,nband)
    IF(.NOT.UNDIMENSION)THEN
       CALL RunAtmosModel  (1,dtt,nband,dtt,idate,idatec,idatep,DELTAIN) ! LGG dt
    ELSE
       CALL RunAtmosModel1D(1,dtt,nband,dtt,idate,idatec,idatep,DELTAIN,.TRUE.) ! LGG dt
    END IF
    CALL InitFieldsIbis (iMax,jMax,kMax,ibMax,jbMax,dtt,xres,&
                         yres,idate,idatec,path_inp,nfsibd,nfprt,nfsibt,fNameSibVeg,&
                         fNameSibmsk,ifday ,ibMaxPerJB,tod ,fNameIBISMask,fNameSSTAOI,&
                         fNameIBISDeltaTemp,fNameSandMask,fNameClayMask,fNameClimaTemp,RESTART   ) ! LGG dt

    CALL Init_IbisOutput(nVars,ibMax,jbMax,nsoilay,DELTAOUT,dtt,depth) ! LGG dt

  END SUBROUTINE InitA
  SUBROUTINE InitB(i)  
    IMPLICIT NONE
    INTEGER :: nVars=345
    integer :: i,j
    REAL(KIND=r8) :: xres
    REAL(KIND=r8) :: yres
!        print*,'LGG in initAll - InitB before InitFieldsIbisB'

    !CALL InitOptionsB(iMax,jMax,kMax)    
    xres= 360.0_r8/REAL(iMax,kind=r8)
    yres= 180.0_r8/REAL(jMax,kind=r8)
    !     print*,'InitOptions'
   
    !CALL InitSizes(iMax,jMax)
    !print*,'InitUtils'
    !CALL InitUtils(iMax,jMax)
    !CALL initFieldsPhysics  (iMax,jMax,kMax,ibMax,jbMax)   
    !CALL InitAtmosModel(ibMax,jbMax,nband)
    !IF(.NOT.UNDIMENSION)THEN
    !   CALL RunAtmosModel  (1,dtt,nband,dtt,idate,idatec,idatep,DELTAIN) ! LGG dt
    !ELSE
    !   CALL RunAtmosModel1D(1,dtt,nband,dtt,idate,idatec,idatep,DELTAIN,.TRUE.) ! LGG dt
    !END IF
    j=i
   if ( i .eq. 4  ) j = 5
   if ( i .eq. 6  ) j = 4
   if ( i .eq. 7  ) j = 6
   if ( i .eq. 9  ) j = 7
   if ( i .eq. 10 ) j = 9
   if ( i .eq. 11 ) j = 10
   if ( i .eq. 12 ) j = 11
   if ( i .eq. 13 ) j = 8
   if ( i .eq. 14 ) j = 6
   if ( i .eq. 15 ) j = 8
   if ( i .eq. 16 ) j = 11
   if ( i .eq. 17 ) j = 3
   if ( i .eq. 18 ) j = 9
   if ( i .eq. 19 ) j = 1
   
   sand_site = texdat(1,j)*100
   clmt_site = texdat(2,j)*100
   clay_site = texdat(3,j)*100
!   sand = texdat(1,j)
!   clmt = texdat(2,j)
!   clay = texdat(3,j)

!        write(0,*) 'LGG in initAll - InitB before InitFieldsIbisB', sand_site
    CALL InitFieldsIbisB (iMax,jMax,kMax,ibMax,jbMax,dtt,xres,&
                         yres,idate,idatec,path_inp,nfsibd,nfprt,nfsibt,fNameSibVeg,&
                         fNameSibmsk,ifday ,ibMaxPerJB,tod ,fNameIBISMask,fNameSSTAOI,&
                         fNameIBISDeltaTemp,fNameSandMask,fNameClayMask,fNameClimaTemp,RESTART   ) ! LGG dt
   sand_site = texdat(1,j)*100
   clmt_site = texdat(2,j)*100
   clay_site = texdat(3,j)*100
!   sand = texdat(1,j)
!   clmt = texdat(2,j)
!   clay = texdat(3,j)

!        write(0,*) 'LGG in initAll - InitB after InitFieldsIbisB', sand_site



    !CALL Init_IbisOutput(nVars,ibMax,jbMax,nsoilay,DELTAOUT,dtt,depth) ! LGG dt

  END SUBROUTINE InitB

END MODULE InitAll
