      PROGRAM PTSMRG

      CHARACTER*4 IFILE(10), NOTE(60), MSPEC(10,200), MNAME(10)
      CHARACTER*4  SPNAME(10,200), NEWSPEC(10,200)
      REAL       EMISS(900000,200)

      character*150 INFILE
      character*200 fname
      character*10 cname

      REAL*4     DUMX(900000),DUMY(900000),IDUMX(900000),IDUMY(900000)
      REAL       DUMXX(900000),DUMYY(900000)                 
      REAL       ICELL(900000),JCELL(900000),FLOW(900000),PLUMHT(900000)     
      REAL       KCELL(900000) 

      character*10 cspec,intspec(200)
      integer state,county,zz,ii,lastpt,firsttime,intspecs
      integer uamid,mask(900000),nxc,nyc,nfiles,mrgpts,iv(200,20)
      integer iunit,infilespec(20),newjdate
      real xorg,yorg,lx
      real rlon0,rlat0,tlat1,tlat2,rlon,rlat

c      read(*,'(10x,a)') infile
c      open(20,file=infile,form='formatted',status='new')
c      write(*,*) 'Opened ASCII Q/A file: ',infile

      read(*,'(10x,a)') fname
      read(fname,*) newjdate 
      write(*,*)'Julian date for emissions file (YYDDD):',newjdate

      read(*,'(10x,a)') infile
      open(9,file=infile,form='unformatted')
      write(*,*) 'Opened new emissions file: ',infile

      read(*,'(10x,a)') fname
      read(fname,*) nfiles
      write(*,*)'Number of point files to merge:',nfiles

      mrgpts = 1

      do numfiles = 1,nfiles

      iunit = numfiles + 20
      read(*,'(10x,a)') infile
      open(iunit,file=infile,form='unformatted',status='old',err=999)
      write(*,*) 'Opened emissions file: ',infile
      
      read(*,'(10x,a)') fname
      read(fname,*) nstack
      write(*,*)'Number for this source group:',nstack
 

      READ (iunit) IFILE,NOTE,NSEG,NSPECS,IDATE,BEGTIM,JDATE,ENDTIM
      READ (iunit) ORGX,ORGY,IZONE,UTMX,UTMY,DELTAX,DELTAY,NX,NY,
     $ NZ,NZLOWR,NZUPPR,HTSUR,HTLOW,HTUPP

      WRITE (*,1007) IDATE, BEGTIM, JDATE, ENDTIM 

      READ  (iunit) IX,IY,NXCLL,NYCLL
      READ  (iunit) ((MSPEC(I,J),I=1,10),J=1,NSPECS)

       infilespec(numfiles) = NSPECS

       if(numfiles.eq.1) then !make species index

       do J = 1,NSPECS
       write(cspec,'(10a1)') (MSPEC(i,J),i=1,10)
       intspec(J) = cspec
       iv(J,numfiles) = J

        do i=1,10
        write(NEWSPEC(i,J),'(a)') cspec(i:i)
        enddo

       enddo
       intspecs = NSPECS

       else !check on list or add to list

         do J = 1, NSPECS
       write(cspec,'(10a1)') (MSPEC(i,J),i=1,10)
       flag = 0
        do K = 1,intspecs
       if (cspec .eq. intspec(K)) then
       flag = 1
       iv(J,numfiles) = K 
       endif
        enddo
       if(flag.eq.0) then !new specie
       intspec(intspecs+1) = cspec
       intspecs = intspecs + 1
       iv(J,numfiles) = intspecs

        do i=1,10
        write(NEWSPEC(i,intspecs),'(a)') cspec(i:i)
        enddo

       endif
         enddo

       endif

       do J = 1, NSPECS
      WRITE (*,1022) (MSPEC(I,J),I=1,10),iv(J,numfiles),J
       enddo
 
       write(*,*) 'Species:',NSPECS,intspecs

C  TIME INVARIANT DATA
 
      READ (iunit) ISEGM,NPMAX

      lastpt = NPMAX + mrgpts - 1
      write(*,*)'Total # of stacks',NPMAX,mrgpts,lastpt

      READ (iunit) (DUMX(II),DUMY(II),IDUMX(II),IDUMY(II),DUMXX(II),
     &   DUMYY(II),II=mrgpts,lastpt)

          mrgpts = mrgpts + NPMAX
          firsttime = 0

          enddo !end loop over number of files

c-----------write camx final merged file header

      do J = 1, intspecs
       write(*,*) J,intspec(J)
      enddo

      WRITE(9)IFILE,NOTE,NSEG,intspecs,newjdate,BEGTIM,newjdate,ENDTIM
      WRITE(*,*)IFILE,NOTE,NSEG,intspecs,newjdate,BEGTIM,newjdate,ENDTIM
      WRITE(9) ORGX,ORGY,IZONE,UTMX,UTMY,DELTAX,DELTAY,NX,NY,
     $ NZ,NZLOWR,NZUPPR,HTSUR,HTLOW,HTUPP
      WRITE (9) IX,IY,NXCLL,NYCLL
      write(*,*) ORGX,ORGY,IZONE,UTMX,UTMY,DELTAX,DELTAY,NX,NY

      WRITE (9) ((NEWSPEC(I,J),I=1,10),J=1,intspecs)
      WRITE (*,1013) ((NEWSPEC(I,J),I=1,10),J=1,intspecs)

      NPMAX = lastpt

      WRITE(9) ISEGM,NPMAX
      write(*,*)'Total # of stacks',NPMAX,lastpt
      WRITE(9) (DUMX(II),DUMY(II),IDUMX(II),IDUMY(II),DUMXX(II),
     &   DUMYY(II),II=1,NPMAX)

c-----------Time variant section

      DO IH = 1,24

      mrgpts = 1

      do numfiles = 1,nfiles

      iunit = numfiles + 20

          READ(iunit) IBGDAT,BEGTIM,IENDAT,ENDTIM
c          WRITE(*,1007) IBGDAT,BEGTIM,IENDAT,ENDTIM,IH

             READ(iunit) ISEGNM,NUMPTS

             lastpt = NUMPTS + mrgpts - 1

             READ(iunit) (ICELL(II),JCELL(II),KCELL(II),FLOW(II), 
     &          PLUMHT(II),II=mrgpts,lastpt) 

             do ii=mrgpts,lastpt
                 mask(ii)=-nstack
             enddo
                         
              DO K=1,infilespec(numfiles)
          READ (iunit) ISEGNM,(SPNAME(II,iv(K,numfiles)),II=1,10),
     &      (EMISS(II,iv(K,numfiles)),II=mrgpts,lastpt)   
              ENDDO !end nspecs loop

          mrgpts = mrgpts + NUMPTS

       enddo !end loop over number of files

c---- write to output file

          write(9) newjdate,real(IH-1),newjdate,real(IH)
          
          NPMAX = lastpt
   
             WRITE(9) ISEGNM,NPMAX
             WRITE(9)(ICELL(II),JCELL(II),mask(II),FLOW(II),
     &          PLUMHT(II),II=1,NPMAX)

              DO K=1,intspecs  
            WRITE(9) ISEGNM,(NEWSPEC(II,K),II=1,10),(EMISS(II,K),
     &             II=1,NPMAX) 
              ENDDO !end nspecs loop 

           write(*,*) NPMAX,IBGDAT,BEGTIM,IENDAT,ENDTIM
          ENDDO !end loop over 24 hours

c-----------End write out final file

 1007 FORMAT(2(I10,F10.2),I10)
 1013 FORMAT(1X,10A1)
 1022 FORMAT(1X,10A1,1X,i3,1x,i3)

  999 STOP
      END 

