C********************************************************************
C  Main program to remap GOES smoke AOD and density on NAM grid into GRIB1 format
C  Created by Jian Zeng (Earth Resources Technology, Inc.)
C  Date: 2006-08-08
C********************************************************************
C
C      integer*2 nx,ny
      integer*4 nx,ny
      integer*4 nlon,nlat
      integer*4 npoint,lenarr,mlon,mlat,maod,mcount,itot,itot1
C      integer*8 npoint,lenarr,mlon,mlat,maod,mcount,ito
      parameter(nx=801,ny=534)
      PARAMETER (LENPDS=28,LENGDS=32)
      parameter(npoint=nx*ny,lenarr=3+npoint*4,mlon=4,
     &  mlat=mlon+npoint,maod=mlat+npoint,mconc=maod+npoint)
      parameter(IBITL=16)
      parameter(INUM=npoint*IBITL+88,NLEFT=MOD(INUM,16))
      parameter(itot1=8+LENPDS+LENGDS+(INUM+16-NLEFT)/8+4)
      CHARACTER*1 KBUF1(ITOT1),KBUF2(ITOT1)

      real*4 lon(nx,ny),lat(nx,ny),aod(nx,ny),conc(nx,ny),
     &  arr(lenarr)
      real*4 GMAX1,GMIN1,GMAX2,GMIN2

      equivalence (nlon,arr(2)),(nlat,arr(3)),(lon(1,1),arr(mlon)),
     & (lat(1,1),arr(mlat)),(aod(1,1),arr(maod)),(conc(1,1),arr(mconc))

      CHARACTER*130 FILEIN,FILEOUT
C
      INTEGER IGRD, IDMODL, IDPARM, IDLVL, IDSCAL, YY, DD, MM, NF
      INTEGER I, J, HR,MN
      
C
      OPEN(10,FILE='infile0',FORM='FORMATTED',STATUS='OLD')
      READ(10,'(A)') FILEIN
      READ(10,'(A)') FILEOUT
      READ(10,'(5i2.2)') YY,MM,DD,HR,MN
      close(10)


      open(11,file=filein,form='unformatted',
     &  access='direct',recl=lenarr*4)
      read (11,rec=1) arr
      close(11)

      do j=1,ny
        do i=1,nx
c          call swap4(aod(i,j))
          if(aod(i,j).lt.-9998.0) then
            aod(i,j)=-9.0
          endif
	  if(conc(i,j).lt.-9998.0) then
            conc(i,j)=-9E-9
          else if (conc(i,j) .eq. -3.) then
            conc(i,j)=-3E-9
          else if (conc(i,j) .eq. -2.) then
            conc(i,j)=-2E-9
	  else if (conc(i,j) .eq. -1.) then
            conc(i,j)=-1E-9
	  endif
        enddo
      enddo

      GMAX1=-99999
      GMIN1=99999

      do j=1,ny
       do i=1,nx
         GMAX1=MAX(aod(I,j),GMAX1)
         GMIN1=MIN(aod(I,j),GMIN1)
        enddo
      enddo
      WRITE(6,*) 'AOD RANGE:', GMAX1,GMIN1

      GMAX2=-99999
      GMIN2=99999

      do j=1,ny
       do i=1,nx
         GMAX2=MAX(conc(I,j),GMAX2)
         GMIN2=MIN(conc(I,j),GMIN2)
        enddo
      enddo
      WRITE(6,*) 'CONC RANGE:', GMAX2,GMIN2

C
C-----------------------------------------------------------------
C
C      call sub_gsmoke_aod_grib_NAM3(AOD,YY,MM,DD,HR,MN,KBUF1)
C      call sub_gsmoke_conc_grib_NAM3(CONC,YY,MM,DD,HR,MN,KBUF2)
C      NF = 51
C

      IF (GMAX1 .GT. GMIN1) THEN
         call sub_gsmoke_aod_grib_NAM3(AOD,YY,MM,DD,HR,MN,KBUF1)
      ENDIF
      IF (GMAX2 .GT. GMIN2) THEN
          call sub_gsmoke_conc_grib_NAM3(CONC,YY,MM,DD,HR,MN,KBUF2)
      ENDIF
      if ((GMAX1 .GT. GMIN1) .AND. (GMAX2 .GT. GMIN2)) then
        NF = 51

      OPEN(NF,FILE=FILEOUT,FORM='UNFORMATTED',ACCESS='DIRECT',
     &  STATUS='REPLACE',RECL=itot1)
C

      WRITE(NF,rec=1) KBUF1
      WRITE(NF,rec=2) KBUF2

      CLOSE(NF)

      endif

C      print *,itot

C      do i=itot-4,itot
C        print *, i, KBUF2(i)
C      enddo

C      RETURN
      END
