! Copyright 2008-2021 Earth Sciences Department, BSC-CNS
!
! This file is part of MONARCH.
!
! MONARCH is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! MONARCH 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
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with MONARCH. If not, see <http://www.gnu.org/licenses/>.
                        module module_bsc_chemistry_initialize
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!-----------------------------------------------------------------------
      use module_kinds
      use module_dm_parallel,only : dstrb,  &
                              mype_share,npes &
                             ,mpi_comm_comp
      use module_exchange,only : halo_exch 
!
!-----------------------------------------------------------------------
!
      implicit none

      integer(kind=kint),private :: &
       mype
!
!-----------------------------------------------------------------------
!
!
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!-----------------------------------------------------------------------
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
       contains
!
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!-----------------------------------------------------------------------
  
!---------------------------------------------------------------------
!
      SUBROUTINE CHEMISTRY_INITIALIZE(GLOBAL            &
                                  ,COUPLE_DUST          &
                                  ,COUPLE_NETCDF        &
                                  ,CHEM                 &
                                  ,CAT_AERO             &
                                  ,NUM_AERO             &
                                  ,NUM_GAS              &
                                  ,NUM_GAS_TOTAL        &
                                  ,NUM_TRACERS_TOTAL    &
                                  ,NUM_TRACERS_CHEM     &
                                  ,NUM_WATER            &
                                  ,NUM_TRACERS_MET      &
                                  ,IDS,IDE,JDS,JDE,LM   &
                                  ,IMS,IME,JMS,JME      &
                                  ,ITS,ITE,JTS,JTE)
!
!-----------------------------------------------------------------------
!
      use netcdf
      use module_bsc_chem_data,ONLY :     &  !ALL CHEMISTRY INDEXES
                       P_NO2       &
                      ,P_NO        &
                      ,P_O3        &
                      ,P_NO3       &
                      ,P_N2O5      &
                      ,P_HNO3      &
                      ,P_HONO      &
                      ,P_PNA       &
                      ,P_H2O2      &
                      ,P_NTR       &
                      ,P_ROOH      &
                      ,P_FORM      &
                      ,P_ALD2      &
                      ,P_ALDX      &
                      ,P_PAR       &
                      ,P_CO        &
                      ,P_MEPX      &
                      ,P_MEOH      &
                      ,P_FACD      &
                      ,P_PAN       &
                      ,P_PACD      &
                      ,P_AACD      &
                      ,P_PANX      &
                      ,P_OLE       &
                      ,P_ETH       &
                      ,P_IOLE      &
                      ,P_TOL       &
                      ,P_CRES      &
                      ,P_OPEN      &
                      ,P_MGLY      &
                      ,P_XYL       &
                      ,P_ISOP      &
                      ,P_ISPD      &
                      ,P_TERP      &
                      ,P_SO2       &
                      ,P_SULF      &
                      ,P_ETOH      &
                      ,P_ETHA      &
                      ,P_NH3       &
                      ,P_BENZENE   &
                      ,P_NO2E      &
!NON-ADVECTED SPECIES
                      ,P_O         &
                      ,P_O1D       &
                      ,P_OH        &
                      ,P_HO2       &
                      ,P_XO2       &
                      ,P_XO2N      &
                      ,P_MEO2      &
                      ,P_HCO3      &
                      ,P_C2O3      &
                      ,P_CXO3      &
                      ,P_ROR       &
                      ,P_TO2       &
                      ,P_CRO       &
                      ,P_CH4       &
                      ,P_HCHO      &
                      ,MECH        &
                      ,INIT

!
!-----------------------------------------------------------------------
!                     
      LOGICAL, INTENT(IN) :: GLOBAL,COUPLE_DUST,COUPLE_NETCDF
!                     
      INTEGER, INTENT(IN) :: CAT_AERO,NUM_GAS,NUM_AERO,NUM_GAS_TOTAL,NUM_TRACERS_TOTAL,NUM_TRACERS_CHEM
!
      INTEGER, INTENT(IN) :: NUM_WATER,NUM_TRACERS_MET
!                     
      REAL,DIMENSION(IMS:IME,JMS:JME,1:LM,1:NUM_GAS_TOTAL),INTENT(INOUT) ::  &
                                                   CHEM
!                     
      INTEGER,INTENT(IN) :: IDS,IDE,JDS,JDE,LM                         &
                           ,IMS,IME,JMS,JME                            &
                           ,ITS,ITE,JTS,JTE
!                     
!-----------------------------------------------------------------------
!***  LOCAL VARIABLES 
!-----------------------------------------------------------------------
!
      INTEGER              :: I,j,l,K,IRTN,N,NCHEM,SKIP2,NSOIL
      INTEGER              :: IDS2,IDE2,JDS2,JDE2
      REAL,DIMENSION(:,:),ALLOCATABLE :: TEMP1,TEMP4
      REAL,DIMENSION(:,:,:),ALLOCATABLE :: TEMP2
      logical(kind=klog) :: opened
      integer :: varid, input_id,stat
      character(30) :: couplefile
      character(3) :: num_var
      character(11) :: name_variable
!
!*** FROM CONFIGURE_FILE
!      INTEGER :: INIT !0  = INITIALIZE FROM IDEAL CONDITIONS
!                      !1  = INITIALIZE FROM A PREVIOUS RUN
!                      !2  = INITIALIZE FROM INCA CHEM DATA
!                      !3  = if .not. global INITIALIZE FROM NMMB-CHEM global data
!                      !4  = INITIALIZE FROM GLOBAL PREVIOUS RUN DIFF. RESOLUTION
!                     
!-----------------------------------------------------------------------
!                     
                      
      ALLOCATE(TEMP1(IDS:IDE,JDS:JDE),STAT=I)
                      
      MYPE=MYPE_SHARE 

!-----------------------------------------------------------------------
      if(mype==0)then
        print *,'START CHEMISTRY INITIALIZE'
      endif
!
      netcdf_if: if(COUPLE_NETCDF)then
!
        if(COUPLE_DUST)then
          couplefile='history_INIT.hhh.nc'
          if(mype==0)then
             write(0,*) 'Open history_INIT.hhh.nc in netcdf'
             stat = nf90_open(couplefile,nf90_nowrite,input_id)
          endif
!
          do n=1,num_gas_total
            do l=1,lm
              if(mype==0)then
                write(num_var,'(I3.3)')n
                name_variable='TRACERS_'//num_var
                stat = nf90_inq_varid(input_id,name_variable,varid)
                stat = nf90_get_var(input_id,varid,temp1,start=(/1,1,l/),count=(/IDE,JDE,1/))
               ! read(31) temp1
              endif
              call dstrb(temp1,chem(:,:,:,n),1,1,1,lm,l,mype,mpi_comm_comp)
            enddo
            call halo_exch(chem(ims:ime,jms:jme,1:lm,n),lm,2,2)
          enddo
!
          if(mype==0) stat=nf90_close(input_id)
        else
          do n=1,NUM_GAS_TOTAL
            do l=1,lm
              do j=jts,jte
                do i=its,ite
                  CHEM(i,j,l,n)=1.0e-30
                enddo
              enddo
            enddo
          enddo
          if(MECH==37)then
            do l=1,lm
              do j=jts,jte
                do i=its,ite
CHEM(i,j,l,P_NO2)=1.6e-4
CHEM(i,j,l,P_NO)=8.3e-5
CHEM(i,j,l,P_O3)=0.35e-1
CHEM(i,j,l,P_FORM)=0.25e-3
CHEM(i,j,l,P_CO)=0.08
CHEM(i,j,l,P_BENZENE)=4.45e-5
CHEM(i,j,l,P_C2O3)=1e-9
CHEM(i,j,l,P_CRES)=1e-9
CHEM(i,j,l,P_ETH)=5e-6
CHEM(i,j,l,P_ETHA)=0.0001
CHEM(i,j,l,P_FACD)=1e-6
CHEM(i,j,l,P_H2O2)=0.001
CHEM(i,j,l,P_HNO3)=5e-5
CHEM(i,j,l,P_HO2)=0.
CHEM(i,j,l,P_HONO)=1e-9
CHEM(i,j,l,P_IOLE)=1e-7
CHEM(i,j,l,P_ISOP)=0.00015
CHEM(i,j,l,P_MEPX)=2.5e-7
CHEM(i,j,l,P_MGLY)=2.5e-7
CHEM(i,j,l,P_N2O5)=0.
CHEM(i,j,l,P_NH3)=0.0001
CHEM(i,j,l,P_NO3)=0.
CHEM(i,j,l,P_NTR)=2e-5
CHEM(i,j,l,P_OH)=0.
CHEM(i,j,l,P_OLE)=2e-7
CHEM(i,j,l,P_OPEN)=2.5e-7
CHEM(i,j,l,P_PACD)=3e-5
CHEM(i,j,l,P_PAN)=2e-5
CHEM(i,j,l,P_PANX)=1e-8
CHEM(i,j,l,P_PAR)=2.4e-3
CHEM(i,j,l,P_PNA)=2e-9
CHEM(i,j,l,P_ROOH)=3e-8
CHEM(i,j,l,P_SO2)=3e-4
CHEM(i,j,l,P_SULF)=1e-8
CHEM(i,j,l,P_TOL)=1e-6
CHEM(i,j,l,P_XYL)=2e-7
!                  CHEM(i,j,l,P_NO2)=0.2e-4
!                  CHEM(i,j,l,P_NO)=0.3e-4
!                  CHEM(i,j,l,P_O3)=0.5e-1
!                  CHEM(i,j,l,P_FORM)=0.25e-3
!                  CHEM(i,j,l,P_CO)=0.1
                enddo
              enddo
            enddo
          endif
        endif
!
      else
      select_unit: do n=51,59
        inquire(n,opened=opened)
        if(.not.opened)then
          nchem=n
          exit select_unit
        endif
      enddo select_unit
!                      
mechanism: IF(MECH==1)THEN !CBM-IV 
                      
      if(mype==0)then
        WRITE(0,*) '*****INITIALIZE CBM-IV*******'
      endif

      do l=1,lm
        do j=jts,jte
          do i=its,ite
CHEM(i,j,l,P_O1D)=1.0e-30
CHEM(i,j,l,P_H2O2)=0.1e-2
CHEM(i,j,l,P_PAN)=0.1e-4
CHEM(i,j,l,P_CRO)=1.0e-30
CHEM(i,j,l,P_TOL)=0.1e-5
CHEM(i,j,l,P_N2O5)=1.0e-30
CHEM(i,j,l,P_XYL)=0.1e-6
CHEM(i,j,l,P_XO2N)=1.0e-30
CHEM(i,j,l,P_HONO)=0.1e-8
CHEM(i,j,l,P_PNA)=0.1e-4
CHEM(i,j,l,P_TO2)=1.0e-30
CHEM(i,j,l,P_HNO3)=0.5e-4
CHEM(i,j,l,P_ROR)=1.0e-30
CHEM(i,j,l,P_CRES)=1.0e-30
CHEM(i,j,l,P_MGLY)=0.2e-6
CHEM(i,j,l,P_CO)=0.8e-1
CHEM(i,j,l,P_ETH)=1.0e-30
CHEM(i,j,l,P_XO2)=1.0e-30
CHEM(i,j,l,P_OPEN)=1.0e-30
CHEM(i,j,l,P_PAR)=0.3e-4
CHEM(i,j,l,P_HCHO)=0.2e-3
CHEM(i,j,l,P_ISOP)=0.25e-5
CHEM(i,j,l,P_OLE)=0.1e-6
CHEM(i,j,l,P_ALD2)=0.3e-4
CHEM(i,j,l,P_O3)=0.5e-1
CHEM(i,j,l,P_NO2)=0.1e-4
CHEM(i,j,l,P_OH)=1.0e-30
CHEM(i,j,l,P_HO2)=1.0e-30
CHEM(i,j,l,P_O)=1.0e-30
CHEM(i,j,l,P_NO3)=1.0e-30
CHEM(i,j,l,P_NO)=0.1e-4
CHEM(i,j,l,P_C2O3)=1.0e-30
          enddo
        enddo
      enddo

      IF (INIT.eq.1) THEN !INITIALIZE WITH A PREVIOUS RUN

      ALLOCATE(TEMP2(IDS:IDE,JDS:JDE,1:LM),STAT=I)

        if(mype==0)then
        print *,'open nmm_b_history file from a previous run'
        OPEN(nchem,FILE='nmm_b_history.048_spin-up',STATUS='OLD',FORM='UNFORMATTED' &
              ,ACCESS='sequential')

      read(nchem)!iyear
      read(nchem)!imonth
      read(nchem)!iday
      read(nchem)!ihour
      read(nchem)!iminute
      read(nchem)!second
      read(nchem)!nf_hours
      read(nchem)!nf_minutes
      read(nchem)!nf_seconds
      read(nchem) !im
      read(nchem) !jm
      read(nchem) !lm
      read(nchem)!ihrst
      read(nchem)!idat
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)!pdtop
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)

      read(nchem)!sg1
      read(nchem)!sg2
      read(nchem)!dsg1
      read(nchem)!dsg2
      read(nchem)!sgml1
      read(nchem)!sgml2

      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)!global
      read(nchem)!run
      
      read(nchem)
      
      read(nchem)!insoil
      read(nchem)!inveg
      
      read(nchem)
      read(nchem)
      
      read(nchem)!fis
      
      read(nchem)         !!********* GLAT is not read from input file but computed later *****!!
      read(nchem)         !!********* GLON is not read from input file but computed later *****!!
      
      read(nchem)!pd
      
      read(nchem)
      read(nchem)
!     
      do l=1,lm
        read(nchem)((TEMP2  (i,j,l),i=ids,ide),j=jds,jde)
      enddo
      do l=1,lm
        read(nchem)((TEMP2(i,j,l),i=ids,ide),j=jds,jde)
      enddo
      do l=1,lm+1
        read(nchem)((TEMP2(i,j,l),i=ids,ide),j=jds,jde)
      enddo
      
      do l=1,lm
        read(nchem)  ! omgalf
      enddo

      do l=1,lm
        read(nchem)((TEMP2 (i,j,l),i=ids,ide),j=jds,jde) !rrw
      enddo

!read 2D fields 24+24+21+14=83
      do l=1,83
         read(nchem)
      enddo
!read 3D fields 7+7+3
      do k=1,17
      do l=1,lm
        read(nchem)  ! omgalf
      enddo
      enddo
!4D nwets smc, stc
      do k=1,2
      do l=1,4
        read(nchem)
      enddo 
      enddo
!3D dust num_dust_bins
      do k=1,3
      do l=1,0 !num_dust_bins=0
        read(nchem)
      enddo
      enddo

      do l=1,lm
        read(nchem)  ! dust_ext
      enddo
!3D dust num_dust_bins
      do k=1,3
      do l=1,0 !num_dust_bins=0
        read(nchem)
      enddo
      enddo

      do l=1,lm
        read(nchem)  ! ph_no2
      enddo
      endif
      CALL MPI_BARRIER(MPI_COMM_COMP,IRTN)
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_O1D),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !H2O2
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_H2O2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !PAN
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PAN),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !CRO
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CRO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !TOL
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_TOL),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !N2O5
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_N2O5),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !XYL
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_XYL),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !XO2N
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_XO2N),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !HONO
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HONO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PNA),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_TO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HNO3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ROR),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CRES),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_MGLY),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ETH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_XO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_OPEN),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PAR),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HCHO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ISOP),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_OLE),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ALD2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_O3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_OH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_O),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_C2O3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
!
      DEALLOCATE(TEMP2)
    ELSEIF(INIT.EQ.2)THEN !INITIALIZE FROM INCA DATA
!     
      if(mype==0)then
        WRITE(0,*) 'OPEN CHEMINIT-INCA FILE '
        open(nchem,file='chemic',status='old',form='unformatted')
      endif
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! O3
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_O3),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! NO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_NO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! NO2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_NO2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! N2O5
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_N2O5),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! HNO2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_HONO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! HNO3
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_HNO3),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! H2O2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_H2O2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CH2O
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_HCHO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_CO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CH3CHO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_ALD2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CH3COCHO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_MGLY),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! C2H4
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_ETH),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! ISOP
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_ISOP),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! PAN
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_PAN),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
!       
    ENDIF
      
      call halo_exch(CHEM(:,:,:,P_O1D),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_H2O2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PAN),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CRO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_TOL),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_N2O5),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_XYL),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_XO2N),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HONO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PNA),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_TO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HNO3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ROR),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CRES),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_MGLY),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ETH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_XO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_OPEN),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PAR),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HCHO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ISOP),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_OLE),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ALD2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_O3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_OH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_O),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NO3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_C2O3),lm,2,2)

      ELSEIF(MECH==2 .OR. MECH==22)THEN !CBM05

      if(mype==0)then
        WRITE(0,*) '*****INITIALIZE CBM-05*******'
        WRITE(0,*) '*****INIT   =  ',INIT
      endif

      do l=1,lm
        do j=jts,jte
          do i=its,ite
CHEM(i,j,l,P_NO2)=0.2e-4 !1.0e-30
CHEM(i,j,l,P_NO)=0.3e-4 !1.0e-30
CHEM(i,j,l,P_O3)=0.5e-1 ! 1.0e-30
CHEM(i,j,l,P_NO3)=1.0e-30
CHEM(i,j,l,P_N2O5)=1.0e-30
CHEM(i,j,l,P_HNO3)=1.0e-30
CHEM(i,j,l,P_HONO)=1.0e-30
CHEM(i,j,l,P_PNA)=1.0e-30
CHEM(i,j,l,P_H2O2)=1.0e-30
CHEM(i,j,l,P_NTR)=1.0e-30
CHEM(i,j,l,P_ROOH)=1.0e-30
CHEM(i,j,l,P_FORM)=0.25e-3 !1.0e-30
CHEM(i,j,l,P_ALD2)=1.0e-30
CHEM(i,j,l,P_ALDX)=1.0e-30
CHEM(i,j,l,P_PAR)=1.0e-30
CHEM(i,j,l,P_CO)=0.1    !1.0e-30
CHEM(i,j,l,P_MEPX)=1.0e-30
CHEM(i,j,l,P_MEOH)=1.0e-30
CHEM(i,j,l,P_FACD)=1.0e-30
CHEM(i,j,l,P_PAN)=1.0e-30
CHEM(i,j,l,P_PACD)=1.0e-30
CHEM(i,j,l,P_AACD)=1.0e-30
CHEM(i,j,l,P_PANX)=1.0e-30
CHEM(i,j,l,P_OLE)=1.0e-30
CHEM(i,j,l,P_ETH)=1.0e-30
CHEM(i,j,l,P_IOLE)=1.0e-30
CHEM(i,j,l,P_TOL)=1.0e-30
CHEM(i,j,l,P_CRES)=1.0e-30
CHEM(i,j,l,P_OPEN)=1.0e-30
CHEM(i,j,l,P_MGLY)=1.0e-30
CHEM(i,j,l,P_XYL)=1.0e-30 
CHEM(i,j,l,P_ISOP)=1.0e-30
CHEM(i,j,l,P_ISPD)=1.0e-30
CHEM(i,j,l,P_TERP)=1.0e-30
CHEM(i,j,l,P_SO2)=1.0e-30
CHEM(i,j,l,P_SULF)=1.0e-30
CHEM(i,j,l,P_ETOH)=1.0e-30
CHEM(i,j,l,P_ETHA)=1.0e-30
!NON-ADVECTED SPECIES
CHEM(i,j,l,P_O)=1.0e-30  
CHEM(i,j,l,P_O1D)=1.0e-30
CHEM(i,j,l,P_OH)=1.0e-30
CHEM(i,j,l,P_HO2)=1.0e-30
CHEM(i,j,l,P_XO2)=1.0e-30
CHEM(i,j,l,P_XO2N)=1.0e-30
CHEM(i,j,l,P_MEO2)=1.0e-30
CHEM(i,j,l,P_HCO3)=1.0e-30
CHEM(i,j,l,P_C2O3)=1.0e-30
CHEM(i,j,l,P_CXO3)=1.0e-30
CHEM(i,j,l,P_ROR)=1.0e-30
CHEM(i,j,l,P_TO2)=1.0e-30
CHEM(i,j,l,P_CRO)=1.0e-30
CHEM(i,j,l,P_CH4)=1.0e-30
       enddo
      enddo
     enddo

!     do n=1,NUM_GAS_TOTAL
!      do l=1,lm
!        do j=jts,jte
!          do i=its,ite
!CHEM(i,j,l,n)=1.0e-30
!          enddo
!        enddo
!      enddo
!     enddo
      IF (INIT.eq.1) THEN !INITIALIZE FROM A PREVIOUS RUN
        IF(MYPE==0) PRINT *,'INITIALIZE FROM A PREVIOUS RUN'

      ALLOCATE(TEMP2(IDS:IDE,JDS:JDE,1:LM),STAT=I)

        if(mype==0)then
        print *,'open nmm_b_history file from a previous run'
        OPEN(nchem,FILE='history_INIT.hhh',STATUS='OLD',FORM='UNFORMATTED' &
              ,ACCESS='sequential')

      nsoil=4
      skip2=174+lm*11+lm+1+lm*10+nsoil*3
      do i=1,skip2 
        read(nchem)!iyear
      enddo

          if(num_aero>0)then
        do l=1,num_aero
            read(nchem)                                     ! AERO_ACPREC
        enddo
!      
        do l=1,num_aero
            read(nchem)                                     ! AERO_CUPREC
        enddo
!
        do l=1,num_aero
            read(nchem)                                     ! AERO_DEPDRY
        enddo
          else
            read(nchem)
            read(nchem)
            read(nchem)
          endif
!
!AOD CALCULATED IN RRTM. TOTAL OF 6 AEROSOLS: DU,SS,OM,BC,SU,TOTAL NEEDS TO BE FIXED TO 6
!IF NOT BSC THIS 6 DIMENSION COMES FROM GOCART
        do l=1,6
            read(nchem)                                     ! AERO_AOD_R
        enddo
!
          if(num_aero>0)then
        do l=1,num_aero
            read(nchem)                                     ! DLOAD
        enddo

        do l=1,num_aero
            read(nchem)                                     ! EMISS_AERO
        enddo

        do l=1,num_aero
            read(nchem)                                     ! AERO02
        enddo
          else
            read(nchem)
            read(nchem)
            read(nchem)
          endif

      do l=1,num_tracers_chem
!        print *,'adrydep ',l
        read(nchem) !ADRYDEP_GAS
      enddo

      do l=1,num_tracers_chem
        read(nchem) !WETDEP_GAS
      enddo

      do l=1,lm
        read(nchem)  ! PHOT
      enddo
!
      endif !mype==0
      CALL MPI_BARRIER(MPI_COMM_COMP,IRTN)

!SKIP METEOROLOGICAL TRACERS - TRACERS ARRAY IS SAVED FROM 5 TO END IN OUTPUT
      if(mype==0)then
      do l=5,num_tracers_total-NUM_GAS_TOTAL !num_tracers_chem
!        print *,'skip meteo tracers ',l
      do k=1,lm
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      enddo
      enddo
      endif
!
      do n=1,NUM_GAS_TOTAL
         do k=1,lm
            if(mype==0)then
              read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
            endif
            call dstrb(TEMP1,CHEM(:,:,:,n),1,1,1,lm,k,mype,mpi_comm_comp)
         enddo   
      enddo

      DEALLOCATE(TEMP2)

      if(mype==0) close(nchem)

      ELSEIF(INIT.eq.2) THEN !INITIALIZE WITH LMDZ-INCA
!
      if(mype==0)then
        WRITE(0,*) 'OPEN CHEMINIT-INCA FILE '
        open(nchem,file='chemic',status='old',form='unformatted')
      endif
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! O3
       print*,'TEMPORAL DELETE DEBUG IC O3 INCA=',K,TEMP1(10,10)
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_O3),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! NO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_NO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! NO2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_NO2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! N2O5
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_N2O5),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! HNO2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_HONO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! HNO3
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_HNO3),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! H2O2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_H2O2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CH2O
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_FORM),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_CO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CH3CHO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_ALD2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CH3COCHO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_MGLY),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! C2H4
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_ETH),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! ISOP
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_ISOP),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! PAN
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_PAN),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO

      if(mype==0) close(nchem)

      ELSEIF(INIT.eq.3 .and. .not.GLOBAL) THEN !INITIALIZE REGIONAL DOMAIN WITH GLOBAL NMMB-CHEM
!-----------------------------------------------------------------------
      if(mype==0)then
        WRITE(0,*) 'OPEN CHEMINIT-REG  FILE '
        open(nchem,file='chemic-reg',status='old',form='unformatted')
      endif
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! NO2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_NO2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! NO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_NO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! O3
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_O3),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! HNO3
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_HNO3),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! H2O2
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_H2O2),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! FORM
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_FORM),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! CO
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_CO),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------
      DO K=1,LM
        IF(MYPE==0)THEN
          READ(nchem)TEMP1   ! PAN
        ENDIF
        CALL DSTRB(TEMP1,CHEM(:,:,:,P_PAN),1,1,1,lm,k,mype,mpi_comm_comp)
      ENDDO
!-----------------------------------------------------------------------

      if(mype==0) close(nchem)

      ELSEIF(INIT.eq.4) THEN !INITIALIZE FROM PREVIOUS RUN DIFF. RESOLUTION   

        IF(MYPE==0) PRINT *,'INITIALIZE FROM A PREVIOUS RUN DIFF RESOLUTION'

      ids2=1     !FROM 2x2.8 resolution
      ide2=131 
      jds2=1
      jde2=93 

      ALLOCATE(TEMP4(IDS2:IDE2,JDS2:JDE2),STAT=I)

        if(mype==0)then
        print *,'open nmm_b_history file from a previous run different resolution'
        OPEN(nchem,FILE='history_INIT.hhh',STATUS='OLD',FORM='UNFORMATTED' &
              ,ACCESS='sequential')

      read(nchem)!iyear
      read(nchem)!imonth
      read(nchem)!iday
      read(nchem)!ihour
      read(nchem)!iminute
      read(nchem)!second
      read(nchem)!nf_hours
      read(nchem)!nf_minutes
      read(nchem)!nf_seconds
      read(nchem) !im
      read(nchem) !jm
      read(nchem) !lm
      read(nchem)!ihrst
      read(nchem)!idat
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)!pdtop
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      
      read(nchem)!sg1
      read(nchem)!sg2
      read(nchem)!dsg1
      read(nchem)!dsg2
      read(nchem)!sgml1
      read(nchem)!sgml2

      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)
      read(nchem)!global
      read(nchem)!run

      read(nchem)

      read(nchem)!insoil
      read(nchem)!inveg

      read(nchem)
      read(nchem)

      read(nchem) !fis

      read(nchem)         !!********* GLAT is not read from input file but computed later *****!!
      read(nchem)         !!********* GLON is not read from input file but computed later *****!!

      read(nchem)!pd

      read(nchem)
      read(nchem)

!      
      do l=1,lm
        read(nchem) !((w   (i,j,l),i=1,im),j=1,jm) !w
      enddo
      do l=1,lm
        read(nchem) !((dwdt(i,j,l),i=1,im),j=1,jm) !dwdt
      enddo
      do l=1,lm+1
        read(nchem) !((pint(i,j,l),i=1,im),j=1,jm) !pint
      enddo

      do l=1,lm
        read(nchem)  ! omgalf
      enddo

      do l=1,lm
        read(nchem) !((rrw (i,j,l),i=1,im),j=1,jm) !rrw
      enddo

!read 2D fields 24+24+21+15=84
      do l=1,84
         read(nchem)
      enddo
!read 3D fields 7+7+2
      do k=1,16
      do l=1,lm
        read(nchem)  ! omgalf
      enddo
      enddo
!4D nwets sh2o, smc, stc
      do k=1,3
      do l=1,4
        read(nchem)
      enddo
      enddo
!3D dust num_dust_bins
      do k=1,3
      do l=1,0 !num_dust_bins=0
        read(nchem)
      enddo
      enddo

      do l=1,lm
        read(nchem)  ! dust_ext
      enddo
!3D dust num_dust_bins
      do k=1,3
      do l=1,0 !num_dust_bins=0
        read(nchem)
      enddo
      enddo

!      do l=1,lm
!        read(nchem)((TEMP2  (i,j,l),i=ids,ide),j=jds,jde)
!      enddo
!      do l=1,lm
!        read(nchem)((TEMP2(i,j,l),i=ids,ide),j=jds,jde)
!      enddo
!      do l=1,lm+1
!        read(nchem)((TEMP2(i,j,l),i=ids,ide),j=jds,jde)
!      enddo

      do l=1,lm
        read(nchem)  ! PHOT
      enddo

!      do l=1,lm
!        read(nchem)((TEMP2 (i,j,l),i=ids,ide),j=jds,jde)
!      enddo
      endif
      CALL MPI_BARRIER(MPI_COMM_COMP,IRTN)
!SKIP METEOROLOGICAL TRACERS
      do l=1,num_tracers_total-num_tracers_chem-1
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      endif
      enddo
      enddo

      do k=1,lm+6   !debug for restart input
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      endif
      enddo

      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_O3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_N2O5),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HNO3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HONO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PNA),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_H2O2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NTR),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ROOH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_FORM),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ALD2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ALDX),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PAR),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_MEPX),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_MEOH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_FACD),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PAN),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PACD),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_AACD),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_PANX),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_OLE),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ETH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_IOLE),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_TOL),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CRES),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_OPEN),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_MGLY),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_XYL),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ISOP),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ISPD),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_TERP),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_SO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_SULF),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ETOH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ETHA),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_O),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_O1D),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_OH),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_XO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_XO2N),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_MEO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_HCO3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_C2O3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CXO3),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_ROR),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_TO2),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CRO),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_CH4),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo
      do k=1,lm
      if(mype==0)then
        read(nchem) ((TEMP4 (i,j),i=ids2,ide2),j=jds2,jde2) !O1D
      call trans_res(TEMP1,TEMP4,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
      endif
      call dstrb(TEMP1,CHEM(:,:,:,P_NO2E),1,1,1,lm,k,mype,mpi_comm_comp)
      enddo

      DEALLOCATE(TEMP4)

      CALL MPI_BARRIER(MPI_COMM_COMP,IRTN)

      if(mype==0)then
        close(nchem)
      endif



      ENDIF

      call halo_exch(CHEM(:,:,:,P_NO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_O3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NO3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_N2O5),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HNO3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HONO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PNA),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_H2O2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NTR),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ROOH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_FORM),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ALD2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ALDX),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PAR),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_MEPX),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_MEOH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_FACD),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PAN),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PACD),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_AACD),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_PANX),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_OLE),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ETH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_IOLE),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_TOL),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CRES),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_OPEN),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_MGLY),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_XYL),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ISOP),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ISPD),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_TERP),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_SO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_SULF),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ETOH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ETHA),lm,2,2)
!NON-ADVECTED SPECIES
      call halo_exch(CHEM(:,:,:,P_O),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_O1D),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_OH),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_XO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_XO2N),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_MEO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_HCO3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_C2O3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CXO3),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_ROR),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_TO2),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CRO),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_CH4),lm,2,2)
      call halo_exch(CHEM(:,:,:,P_NO2E),lm,2,2)

      ELSEIF(MECH==3.OR.MECH==37.OR.MECH==4)THEN !CBM05EXT AND GENERIC

      if(mype==0)then
        WRITE(0,*) '*****INITIALIZE CBM-05EXT OR TRACER*******'
        WRITE(0,*) '*****MECH   =  ',MECH
        WRITE(0,*) '*****INIT   =  ',INIT
        WRITE(0,*) '*****ONLY WORKS WITH INIT==0 or 1'
      endif

     do n=1,NUM_GAS_TOTAL
      do l=1,lm
        do j=jts,jte
          do i=its,ite
CHEM(i,j,l,n)=1.0e-30
CHEM(i,j,l,P_NO2)=1.6e-4 
CHEM(i,j,l,P_NO)=8.3e-5 
CHEM(i,j,l,P_O3)=0.35e-1 
CHEM(i,j,l,P_FORM)=0.25e-3 
CHEM(i,j,l,P_CO)=0.08    
CHEM(i,j,l,P_BENZENE)=4.45e-5
CHEM(i,j,l,P_C2O3)=1e-9
CHEM(i,j,l,P_CRES)=1e-9
CHEM(i,j,l,P_ETH)=5e-6
CHEM(i,j,l,P_ETHA)=0.0001
CHEM(i,j,l,P_FACD)=1e-6
CHEM(i,j,l,P_H2O2)=0.001
CHEM(i,j,l,P_HNO3)=5e-5
CHEM(i,j,l,P_HO2)=0.
CHEM(i,j,l,P_HONO)=1e-9
CHEM(i,j,l,P_IOLE)=1e-7
CHEM(i,j,l,P_ISOP)=0.00015
CHEM(i,j,l,P_MEPX)=2.5e-7
CHEM(i,j,l,P_MGLY)=2.5e-7
CHEM(i,j,l,P_N2O5)=0.
CHEM(i,j,l,P_NH3)=0.0001
CHEM(i,j,l,P_NO3)=0.
CHEM(i,j,l,P_NTR)=2e-5
CHEM(i,j,l,P_OH)=0.
CHEM(i,j,l,P_OLE)=2e-7
CHEM(i,j,l,P_OPEN)=2.5e-7
CHEM(i,j,l,P_PACD)=3e-5
CHEM(i,j,l,P_PAN)=2e-5
CHEM(i,j,l,P_PANX)=1e-8
CHEM(i,j,l,P_PAR)=2.4e-3
CHEM(i,j,l,P_PNA)=2e-9
CHEM(i,j,l,P_ROOH)=3e-8
CHEM(i,j,l,P_SO2)=3e-4
CHEM(i,j,l,P_SULF)=1e-8
CHEM(i,j,l,P_TOL)=1e-6
CHEM(i,j,l,P_XYL)=2e-7
          enddo
        enddo
      enddo
     enddo

      IF (INIT.eq.1) THEN !INITIALIZE FROM A PREVIOUS RUN
        IF(MYPE==0) PRINT *,'INITIALIZE FROM A PREVIOUS RUN'

      ALLOCATE(TEMP2(IDS:IDE,JDS:JDE,1:LM),STAT=I)

        if(mype==0)then
          print *,'open nmm_b_history file from a previous run'
          OPEN(nchem,FILE='history_INIT.hhh',STATUS='OLD',FORM='UNFORMATTED' &
              ,ACCESS='sequential')

          nsoil=4

          skip2=171+11*lm+lm+1+10*lm+3*nsoil

          print*,'COUPLE_SPINUP_GAS skip: ',skip2
          do n=1,skip2
            read(nchem)
          enddo

          if(num_aero>0)then
        do l=1,num_aero
            read(nchem)                                     ! AERO_ACPREC
        enddo
!      
        do l=1,num_aero
            read(nchem)                                     ! AERO_CUPREC
        enddo
!
        do l=1,num_aero
            read(nchem)                                     ! AERO_DEPDRY
        enddo
          else
            read(nchem)
            read(nchem)
            read(nchem)
          endif
!
!AOD CALCULATED IN RRTM. TOTAL OF 6 AEROSOLS: DU,SS,OM,BC,SU,TOTAL NEEDS TO BE FIXED TO 6
        do l=1,6
            read(nchem)                                     ! AERO_AOD_R
        enddo
!
          if(num_aero>0)then
        do l=1,num_aero
            read(nchem)                                     ! DLOAD
        enddo

        do l=1,num_aero
            read(nchem)                                     ! EMISS_AERO
        enddo

!        do l=1,num_aero
!            read(nchem)                                     ! AERO02
!        enddo
          else
            read(nchem)
            read(nchem)
!            read(nchem)
          endif

      do l=1,num_gas_total !num_tracers_chem
!        print *,'adrydep ',l
        read(nchem) !ADRYDEP_GAS
      enddo

      do l=1,num_gas_total !num_tracers_chem
        read(nchem) !WETDEP_GAS
      enddo

      do l=1,lm
        read(nchem)  ! PHOT
      enddo
!
      endif !mype==0
      CALL MPI_BARRIER(MPI_COMM_COMP,IRTN)

!SKIP METEOROLOGICAL TRACERS - TRACERS ARRAY IS SAVED FROM 5 TO END IN OUTPUT
!SKIP ALSO AEROSOLS TRACERS
      if(mype==0)then
!      do l=5,num_tracers_total-num_tracers_chem !NUM_GAS_TOTAL !num_tracers_chem
      do l=num_tracers_met+1,num_tracers_met+num_water+num_aero
!        print *,'skip meteo tracers ',l
      do k=1,lm
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      enddo
      enddo
      endif
!
      do n=1,NUM_GAS_TOTAL
         do k=1,lm
            if(mype==0)then
              read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
            endif
            call dstrb(TEMP1,CHEM(:,:,:,n),1,1,1,lm,k,mype,mpi_comm_comp)
         enddo
      enddo

      DEALLOCATE(TEMP2)

      if(mype==0) close(nchem)

      ENDIF !INIT

      do n=1,NUM_GAS_TOTAL
         call halo_exch(CHEM(:,:,:,n),lm,2,2)
      enddo
      ELSEIF(MECH==6.OR.MECH==7)THEN !GLOBAL SULFUR MECHANISM

      if(mype==0)then
        WRITE(0,*) '*****INITIALIZE GLOBAL SULFUR MECH OR TRACER*******'
        WRITE(0,*) '*****MECH   =  ',MECH
        WRITE(0,*) '*****INIT   =  ',INIT
        WRITE(0,*) '*****ONLY WORKS WITH INIT==0 or 1'
      endif

     do n=1,NUM_GAS_TOTAL
      do l=1,lm
        do j=jts,jte
          do i=its,ite
CHEM(i,j,l,n)=1.0e-30
          enddo
        enddo
      enddo
     enddo

      IF (INIT.eq.1) THEN !INITIALIZE FROM A PREVIOUS RUN
        IF(MYPE==0) PRINT *,'INITIALIZE FROM A PREVIOUS RUN'

      ALLOCATE(TEMP2(IDS:IDE,JDS:JDE,1:LM),STAT=I)

        if(mype==0)then
          print *,'open nmm_b_history file from a previous run'
          OPEN(nchem,FILE='history_INIT.hhh',STATUS='OLD',FORM='UNFORMATTED' &
              ,ACCESS='sequential')

          nsoil=4

          skip2=171+11*lm+lm+1+10*lm+3*nsoil

          print*,'COUPLE_SPINUP_GAS skip: ',skip2
          do n=1,skip2
            read(nchem)
          enddo

          if(num_aero>0)then
        do l=1,num_aero
            read(nchem)                                     ! AERO_ACPREC
        enddo
!      
        do l=1,num_aero
            read(nchem)                                     ! AERO_CUPREC
        enddo
!
        do l=1,num_aero
            read(nchem)                                     ! AERO_DEPDRY
        enddo
          else
            read(nchem)
            read(nchem)
            read(nchem)
          endif
!
!AOD CALCULATED IN RRTM. TOTAL OF 6 AEROSOLS: DU,SS,OM,BC,SU,TOTAL NEEDS TO BE FIXED TO 6
       do l=1,6
          read(nchem)                                       !AERO_AOD_R 
       enddo
!
          if(num_aero>0)then
        do l=1,num_aero
            read(nchem)                                     ! DLOAD
        enddo

        do l=1,num_aero
            read(nchem)                                     ! EMISS_AERO
        enddo

!        do l=1,num_aero
!            read(nchem)                                     ! AERO02
!        enddo
          else
            read(nchem)
            read(nchem)
!            read(nchem)
          endif

      do l=1,num_gas_total

        read(nchem) !ADRYDEP_GAS
      enddo

      do l=1,num_gas_total
        read(nchem) !WETDEP_GAS
      enddo

      do l=1,lm
        read(nchem)  ! PHOT
      enddo
!
      endif !mype==0
      CALL MPI_BARRIER(MPI_COMM_COMP,IRTN)

!SKIP METEOROLOGICAL TRACERS - TRACERS ARRAY IS SAVED FROM 5 TO END IN OUTPUT
      if(mype==0)then
      do l=4,num_tracers_total-NUM_GAS_TOTAL !num_tracers_chem
!        print *,'skip meteo tracers ',l
      do k=1,lm
        read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
      enddo
      enddo
      endif
!
      do n=1,NUM_GAS_TOTAL
         do k=1,lm
            if(mype==0)then
              read(nchem) ((TEMP1 (i,j),i=ids,ide),j=jds,jde) !O1D
            endif
            call dstrb(TEMP1,CHEM(:,:,:,n),1,1,1,lm,k,mype,mpi_comm_comp)
         enddo
      enddo

      DEALLOCATE(TEMP2)

      if(mype==0) close(nchem)

      ENDIF !INIT

      do n=1,NUM_GAS_TOTAL
         call halo_exch(CHEM(:,:,:,n),lm,2,2)
      enddo
      ENDIF mechanism
!
      endif netcdf_if
!
      DEALLOCATE(TEMP1)
!
!----------------------------------------------------------------------
!
      END SUBROUTINE CHEMISTRY_INITIALIZE
!
!----------------------------------------------------------------------
      SUBROUTINE trans_res(A,B,ids,ide,jds,jde,ids2,ide2,jds2,jde2)
!
      integer,intent(in) :: ids,ide,jds,jde,ids2,ide2,jds2,jde2
      real,intent(inout) :: A(ids:ide,jds:jde),B(ids2:ide2,jds2:jde2)

!

      integer :: i,j

!

      do i=ids2,ide2-1
        do j=jds2,jde2-1
          if(i.eq.(ide2-1).and.j.lt.(jde2-1))then
            A((2*i-1),(2*j-1))=B(i,j)
            A((2*i-1),(2*j))=B(i,j)
          elseif(i.lt.(ide2-1).and.j.eq.(jde2-1))then
            A((2*i-1),(2*j-1))=B(i,j)
            A((2*i),(2*j-1))=B(i,j)
          elseif(i.eq.(ide2-1).and.j.eq.(jde2-1))then
            A((2*i-1),(2*j-1))=B(i,j)
          else
            A((2*i-1),(2*j-1))=B(i,j)
            A((2*i),(2*j))=B(i,j)
            A((2*i-1),(2*j))=B(i,j)
            A((2*i),(2*j-1))=B(i,j)
          endif
        enddo
      enddo

      return
!
!----------------------------------------------------------------------
!
      END SUBROUTINE trans_res            
!
!----------------------------------------------------------------------
!
!---------------------------------------------------------------------
!
      subroutine chem_ic(tracers                        &
                        ,indx_gas_start                 &
                        ,indx_gas_end                   &
                        ,num_tracers_total              &
                        ,ids,ide,jds,jde,lm             &
                        ,ims,ime,jms,jme                &
                        ,its,ite,jts,jte)
!
!-----------------------------------------------------------------------
!
! abstract:
!     set up initial conditions of the chemistry species for a cold start
!     
      use netcdf
      use module_bsc_chem_data,only :                                            &
                       p_no2       &
                      ,p_no        &
                      ,p_o3        &
                      ,p_no3       &
                      ,p_n2o5      &
                      ,p_hno3      &
                      ,p_hono      &
                      ,p_pna       &
                      ,p_h2o2      &
                      ,p_ntr       &
                      ,p_rooh      &
                      ,p_form      &
                      ,p_ald2      &
                      ,p_aldx      &
                      ,p_par       &
                      ,p_co        &
                      ,p_mepx      &
                      ,p_meoh      &
                      ,p_facd      &
                      ,p_pan       &
                      ,p_pacd      &
                      ,p_aacd      &
                      ,p_panx      &
                      ,p_ole       &
                      ,p_eth       &
                      ,p_iole      &
                      ,p_tol       &
                      ,p_cres      &
                      ,p_open      &
                      ,p_mgly      &
                      ,p_xyl       &
                      ,p_isop      &
                      ,p_ispd      &
                      ,p_terp      &
                      ,p_so2       &
                      ,p_sulf      &
                      ,p_etoh      &
                      ,p_etha      &
                      ,p_nh3       &
                      ,p_benzene   &
                      ,p_no2e      &
                      ,p_o         &
                      ,p_o1d       &
                      ,p_oh        &
                      ,p_ho2       &
                      ,p_xo2       &
                      ,p_xo2n      &
                      ,p_meo2      &
                      ,p_hco3      &
                      ,p_c2o3      &
                      ,p_cxo3      &
                      ,p_ror       &
                      ,p_to2       &
                      ,p_cro       &
                      ,p_ch4       &
                      ,p_hcho      &
                      ,p_dms       &
                      ,mech
!
!-----------------------------------------------------------------------
!
      integer,intent(in) :: indx_gas_start,indx_gas_end,num_tracers_total
!
      real,dimension(ims:ime,jms:jme,1:lm,1:num_tracers_total),intent(inout) ::  &
                     tracers
!
      integer,intent(in) :: ids,ide,jds,jde,lm                                   &
                           ,ims,ime,jms,jme                                      &
                           ,its,ite,jts,jte
!                     
!-----------------------------------------------------------------------
!***  local variables 
!-----------------------------------------------------------------------
!
      integer            :: i,j,l,n
!                     
!-----------------------------------------------------------------------
!
      mype=mype_share

      do n=indx_gas_start,indx_gas_end
        do l=1,lm
          do j=jts,jte
            do i=its,ite
              tracers(i,j,l,n)=1.0e-30
            enddo
          enddo
        enddo
      enddo
!
      if(mech==6.or.mech==7)then
        if(mype==0)then
          write(0,*) '*****initialize global aerosol mechanism*******'
          write(0,*) '*****mech   =  ',mech
        endif
        do l=1,lm
          do j=jts,jte
            do i=its,ite
              tracers(i,j,l,indx_gas_start-1+p_so2)=5.0e-3
              tracers(i,j,l,indx_gas_start-1+p_dms)=4.0e-4
            enddo
          enddo
        enddo
      elseif(mech==37)then
        if(mype==0)then
          write(0,*) '*****initialize full chemistry mechanism*******'
          write(0,*) '*****mech   =  ',mech
        endif
        do l=1,lm
          do j=jts,jte
            do i=its,ite
              tracers(i,j,l,indx_gas_start-1+p_no2)=1.6e-4
              tracers(i,j,l,indx_gas_start-1+p_no)=8.3e-5
              tracers(i,j,l,indx_gas_start-1+p_o3)=0.35e-1
              tracers(i,j,l,indx_gas_start-1+p_form)=0.25e-3
              tracers(i,j,l,indx_gas_start-1+p_co)=0.08
              tracers(i,j,l,indx_gas_start-1+p_benzene)=4.45e-5
              tracers(i,j,l,indx_gas_start-1+p_c2o3)=1e-9
              tracers(i,j,l,indx_gas_start-1+p_cres)=1e-9
              tracers(i,j,l,indx_gas_start-1+p_eth)=5e-6
              tracers(i,j,l,indx_gas_start-1+p_etha)=0.0001
              tracers(i,j,l,indx_gas_start-1+p_facd)=1e-6
              tracers(i,j,l,indx_gas_start-1+p_h2o2)=0.001
              tracers(i,j,l,indx_gas_start-1+p_hno3)=5e-5
              tracers(i,j,l,indx_gas_start-1+p_ho2)=0.
              tracers(i,j,l,indx_gas_start-1+p_hono)=1e-9
              tracers(i,j,l,indx_gas_start-1+p_iole)=1e-7
              tracers(i,j,l,indx_gas_start-1+p_isop)=0.00015
              tracers(i,j,l,indx_gas_start-1+p_mepx)=2.5e-7
              tracers(i,j,l,indx_gas_start-1+p_mgly)=2.5e-7
              tracers(i,j,l,indx_gas_start-1+p_n2o5)=0.
              tracers(i,j,l,indx_gas_start-1+p_nh3)=0.0001
              tracers(i,j,l,indx_gas_start-1+p_no3)=0.
              tracers(i,j,l,indx_gas_start-1+p_ntr)=2e-5
              tracers(i,j,l,indx_gas_start-1+p_oh)=0.
              tracers(i,j,l,indx_gas_start-1+p_ole)=2e-7
              tracers(i,j,l,indx_gas_start-1+p_open)=2.5e-7
              tracers(i,j,l,indx_gas_start-1+p_pacd)=3e-5
              tracers(i,j,l,indx_gas_start-1+p_pan)=2e-5
              tracers(i,j,l,indx_gas_start-1+p_panx)=1e-8
              tracers(i,j,l,indx_gas_start-1+p_par)=2.4e-3
              tracers(i,j,l,indx_gas_start-1+p_pna)=2e-9
              tracers(i,j,l,indx_gas_start-1+p_rooh)=3e-8
              tracers(i,j,l,indx_gas_start-1+p_so2)=3e-4
              tracers(i,j,l,indx_gas_start-1+p_sulf)=1e-8
              tracers(i,j,l,indx_gas_start-1+p_tol)=1e-6
              tracers(i,j,l,indx_gas_start-1+p_xyl)=2e-7
!              tracers(i,j,l,indx_gas_start-1+p_no2)=0.2e-4
!              tracers(i,j,l,indx_gas_start-1+p_no)=0.3e-4
!              tracers(i,j,l,indx_gas_start-1+p_o3)=0.5e-1
!              tracers(i,j,l,indx_gas_start-1+p_form)=0.25e-3
!              tracers(i,j,l,indx_gas_start-1+p_co)=0.1
            enddo
          enddo
        enddo
      endif
!
      do n=indx_gas_start,indx_gas_end
        call halo_exch(tracers(ims:ime,jms:jme,1:lm,n),lm,2,2)
      enddo
!
!----------------------------------------------------------------------
!
      end subroutine chem_ic  
!
!----------------------------------------------------------------------
!
                       end module module_bsc_chemistry_initialize
!
!-----------------------------------------------------------------------
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!-----------------------------------------------------------------------
