      Program add_lat_lon_v4
************************************************************************
*  Program to add latitude and longitude coordinates to Xindi's LLJ
*  output and to extract area of interest
* 
*  First Draft:  ckw 03-17-13
*  v2 ckw 03-21-13: select subarea using i,j coordinates instead of lat,lon
*  v3 ckw 06-03-13: changed subarea to i:190-205; j:110-137; output in 
*                   new directory /Users/c_walters/NARR_LLJs_stab/
*  v4 mh 07-25-13:  modify to run on imac
************************************************************************
      implicit none
     
      character*1 c1
      character*18 coordfile
      character*8 dattim
      character*104 header
      character*14 infile
      character*49 jetpath
      character*52 jetpathll
      character*16 outfile
      character*104 string
      character*4 yyyy
      character*2 mm

      integer eof,i,j,iloop,jloop,itotal,jtotal,loop
      
      real imin,imax,jmin,jmax
      
      parameter (itotal=349,jtotal=277)
      parameter (imin=190.0,imax=205.0,jmin=110.0,jmax=137.0)
     
      character*2 hh (8)
      data hh /'00','03','06','09','12','15','18','21'/
      real lat(jtotal,itotal)
      real lon(jtotal,itotal)
      
      open (31,file='debug.file',status='unknown')
      
      write (*,*) 'starting program add_lat_lon'
      
      jetpath = '/Users/ckwalters/Research/NLLJ_Climate/NARR_jets/'
      jetpathll = '/Users/ckwalters/Research/NLLJ_Climate/NARR_jets_ll/'
      
c      write(*,*) jetpath
c      write(*,*) jetpathll
      write (*,*) 'input dattim <yyyymmdd>'
      read (*,*) dattim
      write (*,*) 'dattim = ',dattim
      
      coordfile = 'narr-i-j-x-y-z.txt'
	  open (9, file = coordfile, status = 'unknown')
	  do 10, jloop = 1,jtotal
		do 20, iloop = 1,itotal
		  read (9,1000) i,j,lon(i,j),lat(i,j)
20		continue
10	  continue	

      
      do 30, loop = 1,8
         
        infile = dattim//hh(loop)//'.txt'
		outfile = dattim//hh(loop)//'ll.txt'
      
        write (*,*) 'infile = ',infile
        write (*,*) 'outfile = ',outfile
      
c 
c --- open files, read in lat,lon coordinates
c

        open (10, file = jetpath//infile, status = 'unknown')
	    open (20, file = jetpathll//outfile, status = 'unknown') 
 
c 
c --- read in i,j location of jet profile signatures, output appended input
c
		read (10,'(a104)',iostat=eof) header
c        write (*,'(a104)') header
		write (20,1001) header,'lon','lat'
			
		read (10,1002,iostat=eof) string
		read (string,'(i4,i5),95x)') i,j
		write (*,*) string
		write (*,*) i,j
			
		do while (eof.ne.-1)
c		  if (i.ge.imin.and.i.le.imax.and.j.ge.jmin.and.j.le.jmax) then	 
		    write (20,1003) string,lon(i,j),lat(i,j)
c	      endif	 
		  read (10,1002,iostat=eof) string
c		  write (*,*) string
		  read (string,'(i4,i5),95x)') i,j
		end do

		close (10)
		close (20)
			
30     continue  
       close (9)

      
1000  format (2(i5),2(f12.5))
1001  format (a104,2(9x,a3))
1002  format (a104)
1003  format (a104,2(f12.5))
999   end
    

         
