!----------------------------------------------------------------------
!!F90
!
!!DESCRIPTION:
!                This program reads in L1B-granule corners, and checks if a 
!		 given latitude and longitude are located within the granule
!
!!Input Files:  FindL1BMet_V1Coord.check:  Coordinates to look for
!		Corners.txt:		   Corners of L1B-granule
!!Output Files: temp.txt:		   year, date and time of files with successful match
!		temp2.txt:		   coordinates of successful found files 
!
! Caveats:  
!		1) This program works only for identifying daytime granules
!	        2) This program cannot deal with the international date line
!		3) This program cannot identify regions over the poles
!REFERENCES and CREDITS:
!
!    Written by:
!       Rob Levy                   11/30/2000
!       SSAI / NASA-GSFC Code 913
!
!!!!!!!!!!!!!!!


	PROGRAM FindL1BMet

	REAL	CheckLat, CheckLon

	REAL, Dimension(4)	::	LatCorners, LonCorners	
	REAL	Slope21, Slope32, Slope43, Slope14
	REAL 	Int21, Int32, Int43, Int14
	INTEGER, Dimension(4)	::	NegLon
	INTEGER  Daytime
	CHARACTER*80 Filename
        CHARACTER*4 HHMM
        CHARACTER*3 DDD
        CHARACTER*4 YYYY
	CHARACTER Header
        INTEGER strloc

	OPEN(10,FILE = 'FindL1BMet_V1Coord.check',FORM = 'formatted',STATUS = 'OLD')
	OPEN(11,FILE = 'Corners.txt',FORM = 'formatted',STATUS = 'OLD')
	OPEN(12,FILE = 'temp.txt',FORM = 'formatted')
 	OPEN(13,FILE = 'temp2.txt',FORM = 'formatted')

	READ(10,*) Header
	READ(10,*) CheckLat, CheckLon

	READ(11,FMT='(A80)') Filename
	READ(11,*) LatCorners
	READ(11,*) LonCorners

	PRINT*, Filename
	PRINT*, LatCorners
	PRINT*, LonCorners

      ! Identify date and time of input L1B file 
        strloc = INDEX(filename, '.A200')
        PRINT*, strloc
        YYYY = filename(strloc+2:strloc+5)
        DDD = filename(strloc+6:strloc+8)
        HHMM = filename(strloc+10:strloc+13)

        PRINT*, YYYY,' ', DDD,' ', HHMM

	PRINT*, 'Average Lat', (SUM(LatCorners)/4.)
	IF (ABS(SUM(LatCorners)/4.) .GT. 65.) THEN
	   PRINT*, 'This granule is over the pole, too complicated'
	   GOTO 999
	ENDIF

	!IF (LatCorners(1) .EQ. MAX(LatCorners)) THEN 
	IF (LatCorners(1) .GT. LatCorners(2) .AND. &  
     &	    LatCorners(1) .GT. LatCorners(3) .AND. & 
     &	    LatCorners(1) .GT. LatCorners(4)) THEN  
	   Daytime = 1
	ELSE
	   Daytime = 0
	   PRINT*, 'Night time Granule'
	   GOTO 999
	ENDIF

	DO i = 1, 4
	   IF (LonCorners(i) .LT. 0.) THEN 
	     NegLon(i) = 1
	   ELSE
	     NegLon(i) = 0
	   ENDIF
	ENDDO

	IF (LonCorners(4) .GT. 0. .AND. LonCorners(2) .LT. 0.) THEN
	   PRINT*, 'This granule straddles the date line'
	   !LonCorners(*) = LonCorners(*) + (NegLon(*) * 360.)
	ENDIF

	slope21 = (LatCorners(2) - LatCorners(1)) / (LonCorners(2) - LonCorners(1))
	slope32 = (LatCorners(3) - LatCorners(2)) / (LonCorners(3) - LonCorners(2))
	slope43 = (LatCorners(4) - LatCorners(3)) / (LonCorners(4) - LonCorners(3))
	slope14 = (LatCorners(1) - LatCorners(4)) / (LonCorners(1) - LonCorners(4))
	
	int21 = LatCorners(2) - slope21 * LonCorners(2)
	int32 = LatCorners(3) - slope32 * LonCorners(3)
	int43 = LatCorners(4) - slope43 * LonCorners(4)
	int14 = LatCorners(1) - slope14 * LonCorners(1)

	PRINT*, int21 + slope21*CheckLon, CheckLat
	PRINT*, int14 + slope14*CheckLon, CheckLat
	PRINT*, int32 + slope32*CheckLon, CheckLat
	PRINT*, int43 + slope43*CheckLon, CheckLat

 	IF ((int21 + slope21*CheckLon .GT. CheckLat) .AND. &
     & 	    (int14 + slope14*CheckLon .GT. CheckLat) .AND. &
     & 	    (int32 + slope32*CheckLon .LT. CheckLat) .AND. &
     & 	    (int43 + slope43*CheckLon .LT. CheckLat)) THEN

	  PRINT*, '********* Point in Granule'
	  WRITE(12,12) YYYY,DDD,HHMM
	  WRITE(13,*) Filename
          WRITE(13,*) 'LatCorners   LonCorners'  
          WRITE(13,*) LatCorners(1),LonCorners(1)
          WRITE(13,*) LatCorners(2),LonCorners(2)
          WRITE(13,*) LatCorners(3),LonCorners(3)
          WRITE(13,*) LatCorners(4),LonCorners(4)
          WRITE(13,*) LatCorners(1),LonCorners(1)
          WRITE(13,*)


	ENDIF
12       FORMAT(A4,1X,A3,1x,A4)

  999	CONTINUE
	
	END
