C***** RDARGRP
c
      subroutine ncf_rdargrp(igrid,ndate,ttime,nox,noy,nlay_ems,
     &                                          numcls,igroup,emscls)
      use filunit
      use grid
      use chmstry
      use bndary
      use camxcom
      use tracer
      implicit none
c
c      Copyright Ramboll
c
c----CAMx v7.40 260801
c
c-----------------------------------------------------------------------
c    LOG:
c-----------------------------------------------------------------------
c
c-----------------------------------------------------------------------
c    Include files:
c-----------------------------------------------------------------------
c
      include 'camx.prm'
      include 'flags.inc'
      include 'soap.inc'
      include 'soap3.inc'
      include 'netcdf.inc'
c
c-----------------------------------------------------------------------
c    Argument declarations:
c-----------------------------------------------------------------------
c
      integer igrid
      integer ndate
      real    ttime
      integer nox
      integer noy
      integer nlay_ems
      integer numcls
      integer igroup
      real    emscls(numcls,nox,noy,nlay_ems)
c
c-----------------------------------------------------------------------
c    External functions:
c-----------------------------------------------------------------------
c
      integer istrln
      integer ncf_get_tstep
c
c-----------------------------------------------------------------------
c    Local variables:
c-----------------------------------------------------------------------
c
      character*200 fname, action
      character*10  this_var
      character*4   iname(10)
      integer       idx_poa_xx(IDX_SOAP_POA_OP:IDX_SOAP_POA_BB), this_class
      integer       ispc, num_emsfiles, idxfile, this_dat
      integer       iounit, iseg, i, j, k, nlays_in, buffer_offset
      integer       data_start(4), data_count(4), itmp, jtmp
      integer       icls, ierr, this_date, this_varid, isoa
      integer       this_time_tflag, this_time_etflag, this_tstep
      integer       iemspec
      real          this_time
      logical       use_this_file
c
      real, allocatable, dimension(:,:,:) :: emsgrd
c
c-----------------------------------------------------------------------
c    Entry point:
c-----------------------------------------------------------------------
c
      if( .NOT. larsrc ) goto 9999
      idx_poa_xx = -9
      icls = ntrcls
      if( luse_soap3 ) then
         do isoa=IDX_SOAP_POA_OP,IDX_SOAP_POA_BB
            icls = icls + 1
            do iemspec = 1,nemspc
               if( emspcname(iemspec) .EQ. soap_emiss_names(isoa) )
     &                                       idx_poa_xx(isoa) = iemspec
            enddo
         enddo
      endif
c
c   --- skip if filename not supplied ---
c
      num_emsfiles = num_iortem(igrid,igroup)
      if( igroup .EQ. 0 ) num_emsfiles = nemiss_files(igrid)
c
c   --- loop over all files ---
c
      do 10 idxfile=1,num_emsfiles
        use_this_file = .TRUE.
c
c   --- set the unit number for surface emissions file ---
c
        if( igroup .EQ. 0 ) then
            iounit = iarem(igrid,idxfile)
            write(fname,'(A,I10)') 'EMISSIONS -- UNIT ',iarem(igrid,idxfile)
            if( .NOT. is_netcdf_iarem(igrid,idxfile) ) use_this_file = .FALSE.
            write(action,'(2A,I3,A,I3)') 'Reading the NetCDF ',
     &                              'gridded emissions file. Grid: ',
     &                                           igrid,' File: ',idxfile
            buffer_offset = buffer_offset_iarem(igrid,idxfile)
        else
            iounit = iortem(igrid,igroup,idxfile)
            fname = temfil(igrid,igroup,idxfile)
            if( .NOT. is_netcdf_iortem(igrid,igroup,idxfile) ) use_this_file = .FALSE.
            write(action,'(2A,I3,A,I3,A,I3)') 'Reading the SA NetCDF ',
     &                     'gridded emissions file. Grid: ',igrid,
     &                                    ' Group: ',igroup,' File: ',idxfile
            buffer_offset = buffer_offset_iortem(igrid,igroup,idxfile)
        endif
        if( .NOT. use_this_file ) cycle
c
c   ---- get the number of layers in this file ---
c
        this_var = 'NLAYS'
        ierr = nf_get_att_int(iounit, NF_GLOBAL, 'NLAYS', nlays_in)
        if( ierr .NE. NF_NOERR ) goto 7000
c
c   --- allocate the local array ---
c
        allocate( emsgrd(nox-2*buffer_offset,noy-2*buffer_offset,nlays_in) )
c
c   --- if number of layers is not 1 make sure it matches grid ---
c
        if( nlays_in .NE. 1 .AND. nlays_in .NE. nlay(igrid) ) goto 7001
c
c   --- set the indexes for what to read ---
c
        data_start(1) = 1
        data_count(1) = nox-2*buffer_offset
        data_start(2) = 1
        data_count(2) = noy-2*buffer_offset
        data_start(3) = 1
        data_count(3) = nlays_in
c
c  ---- get the index for timestep containing this time ----
c
        this_date = ndate
        this_time = ttime
        this_tstep = ncf_get_tstep(iounit,action,
     &              ndate,ttime,this_time_tflag,this_time_etflag,
     &                                                       le1day,.TRUE.)
        data_start(4) = this_tstep
        data_count(4) = 1
c
c  ---- loop over the list of emissions spcies ---
c
        do iemspec=1,nemspc
            if( lemmap(iemspec) .LE. 0 ) cycle
            emsgrd = 0.
            ispc = iemspec
            this_var = emspcname(iemspec)
c
c  --- load data into temporary arrray ---
c
            ierr = nf_inq_varid(iounit, this_var, this_varid)
            if( ierr .NE. NF_NOERR) cycle
            ierr = nf_get_vara_real(iounit,this_varid,data_start,
     &                                               data_count,emsgrd)
            if( ierr .NE. NF_NOERR) goto 7002
c
c ---- convert the units ---
c
            emsgrd = emsgrd / (60.*dtems)
c
c--- if this is a POA_XX species set the flag for it ---
c
            if( lsoa .AND. this_var(1:4) .EQ. 'POA ') ispc = idx_poa_xx(IDX_SOAP_POA_AV)
c
c   --- skip if the species is a not used for SA ---
c
            if( .NOT. lusespc(lemmap(iemspec)) ) cycle
c
c   --- load the species into the cells in tracer array ----
c
            if( this_var(1:4) .NE. 'POA ' .AND. this_var(1:4) .NE. 'POA_' ) then
               do icls=1,ntrcls
                  this_class = icls
                  do k=1,nlays_in
                     do j=1+buffer_offset,noy-buffer_offset
                        jtmp = j-buffer_offset
                        do i=1+buffer_offset,nox-buffer_offset
                          itmp = i-buffer_offset
c
c   --- load into the tracer emissions array ---
c
                          emscls(this_class,i,j,k) = emscls(this_class,i,j,k) +
     &                        emsgrd(itmp,jtmp,k) * trspmap(lemmap(ispc),this_class)
                        enddo
                     enddo
                  enddo
               enddo
            endif
c
c --- now handle POA ---
c
            if( emspcname(iemspec)(1:4) .EQ. 'POA ' .AND. lsoa ) then
               this_class = ntrcls + IDX_SOAP_POA_AV - IDX_SOAP_POA_OP + 1
               do k=1,nlays_in
                  do j=1+buffer_offset,noy-buffer_offset
                     jtmp = j-buffer_offset
                     do i=1+buffer_offset,nox-buffer_offset
                       itmp = i-buffer_offset
                       emscls(this_class,i,j,k) = emscls(this_class,i,j,k) +
     &                                                     emsgrd(itmp,jtmp,k)
                     enddo
                  enddo
               enddo
            endif
        enddo
c
c   --- if doing SOAP3 now look for SOAP3 species and process separately ---
c
        icls = ntrcls
        if( luse_soap3 .AND. lsoa ) then
           do ispc=IDX_SOAP_POA_OP,IDX_SOAP_POA_BB
             icls = icls + 1
c
c  --- load data into temporary arrray ---
c
             this_var = soap_emiss_names(ispc)
             ierr = nf_inq_varid(iounit, this_var, this_varid)
             if( ierr .NE. NF_NOERR) cycle
             emsgrd = 0.
             ierr = nf_get_vara_real(iounit,this_varid,data_start,
     &                                              data_count,emsgrd)
             if( ierr .NE. NF_NOERR) goto 7001
             if( idx_poa_xx(ispc) .LE. 0 ) cycle
c
c ---- convert the units ---
c
             emsgrd = emsgrd / (60.*dtems)

c   --- load the species into the cells in tracer array ----
c
             do k=1,nlays_in
              do j=1+buffer_offset,noy-buffer_offset
                 jtmp = j-buffer_offset
                 do i=1+buffer_offset,nox-buffer_offset
                   itmp = i-buffer_offset
c
c   --- load into the tracer emissions array ---
c
                    emscls(icls,i,j,k) = emscls(icls,i,j,k) +
     &                                           emsgrd(itmp,jtmp,k)
                 enddo
              enddo
             enddo
           enddo
        endif
c
c  --- next file in grid/group ---
c
        deallocate( emsgrd )
 10   continue
c
      goto 9999
c
c----------------------------------------------------------------------
c    Error messages:
c-----------------------------------------------------------------------
c
 7000 continue
      write(iout,'(//,a)') 'ERROR in NCF_RDARGRP:'
      write(iout,'(A)') action(:istrln(action))
      write(iout,'(2A)') 'Cannot find global attribute: ',
     &                                      this_var(:istrln(this_var))
      write(iout,'(2A)') 'File: ',fname(:istrln(fname))
      write(iout,'(A,I5)') 'NetCDF error code: ',ierr
      call camxerr()
c
 7001 continue
      write(iout,'(//,a)') 'ERROR in NCF_RDARGRP:'
      write(iout,'(A)') action(:istrln(action))
      write(iout,'(A)') 'Number of layers does not match: '
      write(iout,'(A,I4)') 'User supplied: ',nlays_in
      write(iout,'(A,I4)') 'Value in file: ',nlay(igrid)
      write(iout,'(2A)') 'File: ',fname(:istrln(fname))
      call camxerr()
c
 7002 continue
      write(iout,'(//,a)') 'ERROR in NCF_RDARGRP:'
      write(iout,'(A)') action(:istrln(action))
      write(iout,'(2A)') 'Cannot read data for variable: ',
     &                                      this_var(:istrln(this_var))
      write(iout,'(2A)') 'File: ',fname(:istrln(fname))
      write(iout,'(A,I5)') 'NetCDF error code: ',ierr
      call camxerr()
c
 9999 continue
c
      return
      end
