#!/bin/sh

#...............................................................................
# FindL1Bmet finds specific MODIS Level-1 granules that have been previously created
#  by the DAAC. The user enters in the latitude and longitute they wish to find, 
#  and the dates in  which they wish to find the data. FindL1Bmet then searches 
#  through windhoek's archive of L1B meta data for granule matches. 
# The code assumes that the DAAC has been pushing L1B.meta data files to windhoek, 
#  in the form of MOD021KM*.hdf.met, located on windhoek in the directory, 
#  ///
#
# FindL1Bmet.sh: This Bourne shell script interfaces between the windhoek data 
# structure, and the FindL1Bmet*.f90 family of L1Bmet matching code. 
#
# Created by Rob Levy: SSAI/NASA-GSFC Code 913, sometime during 2000. 
# 
# Note that this code is intended to run on windhoek, and is not guaranteed. 
#
# Lines that the user may need to change are marked with #**
#...............................................................................

#... set pathname for directory in which L1BFind is running
#** 
L1BFindRoot=`pwd`

#... set pathname for directory in which L1B meta data is located
#** 
L1BmetPathRoot="/ingest/L1Bmet/"

#... go to the L1BFind directory and cleanup leftover files
cd $L1BFindRoot
rm -f $L1BFindRoot/FoundL1Bmet.txt
rm -f $L1BFindRoot/Coordinates.txt

#... loop through L1Bmet directories by day number:
#... right now the script uses the current date and julian day as input, 
#... then searches back a certain number of days (70) to look for old data. 
#... It looks for leapyears when computing previous days.
#**

currentyear=`date +%Y`
currentjday=`date +%j`
echo $currentyear
echo $currentjday
jday1=`expr $currentjday - 70`
echo $jday1

while test $jday1 -lt $currentjday
do
 iyear=$currentyear
 jday=$jday1

 if test $jday -lt 1
 then
   iyear=`expr $iyear - 1`
   jday=`expr $jday1 + 365`
   leapyear=`expr $iyear % 4`
   echo "leapyear" $leapyear
   if test $leapyear -eq 0
   then
      jday=`expr $jday + 1`
    fi
  fi
  jday1=`expr $jday1 + 1`
  echo $jday $iyear

#... Find a directory to look for L1Bmet data, L1BmetPath

 #L1BmetPath="$L1BmetPathRoot/$iyear/$jday"
 L1BmetPath="$L1BmetPathRoot/$jday"
 echo $L1BmetPath
 for L1BmetDir in $L1BmetPath
 do

#... Set a generic filename for the L1Bmet File List

   L1BmetFileList="$L1BmetDir/MOD021KM*"

#... loop through each L1Bmet file in the L1BmetPath directory
#... Use Grep to read the granule corners into a file named Corners.txt

   for L1BmetFile in $L1BmetFileList
   do
       echo ""
       echo ">>> processing $L1BmetFile"
       substring1=${L1BmetFile##*"MOD021KM"}
       substring2=${substring1%"."*}
       substring3=${substring2%"."*}
       cornerfile="Corners.txt"
       echo $L1BmetFile > temp
       grep -E "Value = (.*\.[0-9][0-9][0-9][0-9].*, .*, .*, .*)" $L1BmetFile | sed 's/ *Value = (//' | sed 's/)//' >> temp
       cp temp $cornerfile

#... Clean up some more files
#... Run Fortran code FindL1BMet_V1Coord.f90 to add to a list of found files, FoundL1Bmet.txt

rm  -f $L1BFindRoot/temp.txt
rm -f $L1BFindRoot/temp2.txt
$L1BFindRoot/FindL1BMet_V1Coord
cat $L1BFindRoot/temp.txt >>  $L1BFindRoot/FoundL1Bmet.txt
cat $L1BFindRoot/temp2.txt >> $L1BFindRoot/Coordinates.txt

done    #... end of L1Bmet file loop

done    #... end of L1Bmet day directory loop
done    #... end of jday loop
