! Program to parse files for data relevant to Reno.
REAL lat(1000),lon(1000),AOD(1000)
REAL scaleFac,latLow,latHi,lonLow,lonHi
INTEGER i,j
LOGICAL latTest,lonTest,Test

! Open the files containing the latitude, longitude, and optical depth.
! These files are obtained by exporting to text from the HDF data files using HDFview.
OPEN(1,FILE='Latitude_1June2012.txt')
OPEN(2,FILE='Longitude_1June2012.txt')
OPEN(3,FILE='Optical_Depth_Land_And_Ocean_1June2012.txt')

! Open the output file. This file contains data from within the bounds selected.
OPEN(4,FILE='RenoAreaAOD_1June2012.txt')
WRITE(4,*) 'Latitude Longitude AOD' ! Write the output file header.

scaleFac=0.001

! Set the lat and long bounds for the data.
! The sign of lat and lon affect the orientation of logical tests below.
latLow=38.0 ; latHi=40.5
lonLow=-118.0 ; lonHi=-122.0

DO i=1,203 ! Rows of data.

READ(1,*) (lat(j),j=1,135) ! Columns of data.
READ(2,*) (lon(j),j=1,135)
READ(3,*) (AOD(j),j=1,135)

! Scale the AOD using the scale factor from the HDF file, and
! check to see if the data is from the bounding box.

DO j=1,135
IF (AOD(j).GE.-990.0) AOD(j)=AOD(j)*scaleFac
latTest=((lat(j).GE.latLow).AND.(lat(j).LE.latHi))
lonTest=((lon(j).LE.lonLow).AND.(lon(j).GE.lonHi)) ! Be careful with this.
Test=(latTest.AND.lonTest)
IF (Test) WRITE(4,*) lat(j),lon(j),AOD(j)
ENDDO

END DO ! DO i=1,203 ! Rows of data.

DO i=1,4 ; CLOSE(i) ; ENDDO ! Close all files.

END