c**** NCF_SET_EMISS_MAPPING
c
      subroutine ncf_set_emiss_mapping(iounit,action)
      use filunit
      use chmstry
      implicit none
c
c----CAMx v7.40 260801
c
c-----------------------------------------------------------------------
c    Description:
c-----------------------------------------------------------------------
c
c   This routine checks which species are in the file and adds them to
c   the master emissions species list. It also sets the array for
c   the index of species into this file.
c
c      Copyright Ramboll
c
c      Argument description:
c       Inputs:
c         iounit        I NCF file ID
c         action        C string that describes file being read
c       Outputs:
c
c-----------------------------------------------------------------------
c    LOG:
c-----------------------------------------------------------------------
c
c     02/20/17   --gwilson-- Original development
c     11/21/25   --gwilson-- Changed the way lemmap works - it now points the other direction
c
c-----------------------------------------------------------------------
c    Include files:
c-----------------------------------------------------------------------
c
      include 'camx.prm'
      include 'flags.inc'
      include 'vbs.inc'
      include 'soap.inc'
      include 'soap3.inc'
      include 'netcdf.inc'
c
c-----------------------------------------------------------------------
c    Argument declarations:
c-----------------------------------------------------------------------
c
      integer       iounit
      character*(*) action
c
c-----------------------------------------------------------------------
c    External functions:
c-----------------------------------------------------------------------
c
      integer istrln
c
c-----------------------------------------------------------------------
c    Local parameters:
c-----------------------------------------------------------------------
c
      integer NUMVBS
      parameter( NUMVBS = 11 )
c
c-----------------------------------------------------------------------
c    Local variables:
c-----------------------------------------------------------------------
c
      character*20  this_var(NUM_SOAP_SPECIES), this_mod_species
      integer ispc, ierr, this_varid, i
      logical lfound
c
      integer isoa
c
c-----------------------------------------------------------------------
c    Entry point:
c-----------------------------------------------------------------------
c
      write(idiag,'(/,A)') TRIM(action)
      call flush(idiag)
c
      this_var = soap3_spec_names
c
c   --- loop ober species in list ----
c
      do ispc=1,nspec
         this_mod_species = spname(ispc)
c
c   --- seek this species ---
c
         if( luse_soap3 ) then
             do isoa=1,NUM_SOAP_SPECIES
                 if( this_mod_species .EQ. soap3_spec_names(isoa) 
     &                 .AND. this_mod_species(1:4) .NE. 'HOA ' ) 
     &                       this_mod_species = soap2_spec_names(isoa) 
             enddo
         endif
         ierr = nf_inq_varid(iounit,this_mod_species,this_varid)
         if( ierr .NE. NF_NOERR .AND. luse_soap3 .AND. 
     &                   spname(ispc)(1:4) .EQ. 'HOA ') then
             this_mod_species = 'POA'
             ierr = nf_inq_varid(iounit,this_mod_species,this_varid)
         endif
         if( ierr .EQ. NF_NOERR ) then
c
c   --- echo that species was found in this file ---
c
            write(idiag,'(2A)') '   --- Found species: ',TRIM(spname(ispc))
            call flush(idiag)
c
c   --- now check if this species is already in master list ---
c
            lfound = .FALSE.
            do i=1,nemspc
              if(this_mod_species .EQ. emspcname(i) ) then
                 lfound = .TRUE.
                 if( emspcname(i)(1:4) .EQ. 'POA ' .AND. 
     &                          khoa .NE. nspec+1 ) lemmap(i) = khoa
              endif
            enddo
            if( .NOT. lfound ) then
              nemspc = nemspc + 1
              emspcname(nemspc) = this_mod_species
              lemmap(nemspc) = ispc
              if( .NOT. luse_soap3 .AND. 
     &                 emspcname(nemspc)(1:4) .EQ. 'POA '.AND.  
     &                          khoa .NE. nspec+1 ) lemmap(nemspc) = khoa
            endif
         endif
c
c  --- next species ---
c
      enddo
c
c  --- update mapping for HOA species
c
      if( luse_soap3 ) then
         do isoa=IDX_SOAP_POA_OP,IDX_SOAP_POA_BB
           lfound = .FALSE.
            do i=1,nemspc
              if(soap_emiss_names(isoa) .EQ. emspcname(i) ) then
                lfound = .TRUE.
                idx_ems_soap3(isoa) = i
                lemmap(i) = khoa
              endif
            enddo
            if( .NOT. lfound ) then
              nemspc = nemspc + 1
              emspcname(nemspc) = soap_emiss_names(isoa)
              idx_ems_soap3(isoa) = nemspc
              lemmap(nemspc) = khoa
            endif
         enddo
         lfound = .FALSE.
         do i=1,nemspc
            if(emspcname(i)(1:5) .EQ. 'HOA ' ) idx_ems_hoa = i
            if(emspcname(i)(1:5) .EQ. 'SVOA ' ) then
              lfound = .TRUE.
              if( ksvoa .NE. nspec+1 ) lemmap(i) = ksvoa
              idx_ems_svoa = i
            endif
         enddo
         if( .NOT. lfound ) then
            nemspc = nemspc + 1
            emspcname(nemspc) = 'SVOA       '
            if( ksvoa .NE. nspec+1 ) lemmap(nemspc) = ksvoa
            idx_ems_svoa = nemspc
         endif
      endif
c
c  --- if doing inline emissions, add species needed ---
c
      if( lixemis ) then
         do i=1,nemspc
            if(emspcname(i)(1:4) .EQ. 'HOI ' ) then
              lfound = .TRUE.
              if( khoi .NE. nspec+1 ) lemmap(i) = khoi
            endif
         enddo
         if( .NOT. lfound ) then
            nemspc = nemspc + 1
            emspcname(nemspc) = 'HOI        '
            if( khoi .NE. nspec+1 ) lemmap(nemspc) = khoi
         endif 
         do i=1,nemspc
            if(emspcname(i)(1:3) .EQ. 'I2 ' ) then
              lfound = .TRUE.
              if( ki2 .NE. nspec+1 ) lemmap(i) = ki2
            endif
         enddo
         if( .NOT. lfound ) then
            nemspc = nemspc + 1
            emspcname(nemspc) = 'I2         '
            if( ki2 .NE. nspec+1 ) lemmap(nemspc) = ki2
         endif 
      endif
c
c  --- do VBS or SOAP3 as a separate case ---
c
      if( lvbs .AND. LVBSPREPROC ) then
         do ispc=1,NUMVBS
            ierr = nf_inq_varid(iounit,this_var(ispc),this_varid)
            if( ierr .NE. NF_NOERR  ) cycle
            lfound = .FALSE.
            do i=1,nemspc
              if(this_var(ispc) .EQ. emspcname(i) ) then
                 lfound = .TRUE.
                 lemmap(i) = ispc
                 idx_ems_hoa = i
                 if( luse_soap3 .AND. khoa .NE. nspec+1 ) lemmap(i) = khoa
              endif
            enddo
            if( .NOT. lfound ) then
              nemspc = nemspc + 1
              emspcname(nemspc) = this_var(ispc)
              lemmap(nemspc) = ispc
              idx_ems_hoa = nemspc
              if( luse_soap3 .AND. khoa .NE. nspec+1 ) lemmap(nemspc) = khoa
            endif
c
c   --- echo that species was found in this file ---
c
            write(idiag,'(2A)') '   --- Found species: ',TRIM(this_var(ispc))
            call flush(idiag)
         enddo
      endif
c
c-----------------------------------------------------------------------
c    Error messages:
c-----------------------------------------------------------------------
c
c-----------------------------------------------------------------------
c    Return point:
c-----------------------------------------------------------------------
c
 9999 continue
      return
      end
 
