Index: trunk/DataCheck/Monitoring/CheckSwiftSchedule.sh
===================================================================
--- trunk/DataCheck/Monitoring/CheckSwiftSchedule.sh	(revision 18420)
+++ trunk/DataCheck/Monitoring/CheckSwiftSchedule.sh	(revision 18421)
@@ -2,44 +2,97 @@
 
 
+source `dirname $0`/../Sourcefile.sh
+printprocesslog "INFO starting $0"
+
+# list of sources
 sources=( "Mrk 421" "Mkn 421" "1959" "V404" "0323" "Crab" "CRAB" "Mrk 501" "Mkn 501" )
 
-dates=(`date +"%Y-%m-%d" --date="-12hour"` `date +%Y-%m-%d --date="+12hour"`)
 
-date2=`echo ${dates[0]} | sed -e 's/-/\//g'`
-schedulepath=/home/factwww/scheduling/$date2
-schedulefile=$schedulepath/swift.schedule
-schedulefile2=$schedulepath/swift.schedule.formated
-schedulefile3=$schedulepath/swift.schedule.txt
+# get dates
+if [ "$certaindate" != "" ]
+then
+   getdates $certaindate
+else
+   if [ "$1" = "" ]
+   then 
+      # get next 3 night
+      getdates +3
+   else
+      getdates $1
+   fi
+fi
 
-# Get Swift Data
-echo "" > $schedulefile
 
-#echo "Downloading the schedule for..."
+
 for date in ${dates[@]}
 do 
-   #echo "    "$date"..."
-   lynx -width 200 -nolist -dump https://www.swift.psu.edu/operations/obsSchedule.php?d=$date | grep $date >> $schedulefile
+   printprocesslog "INFO getting Swift schedule for the night of "$date
+   echo "INFO getting Swift schedule for the night of "$date
+   # paths and files needed to store schedule
+   date2=`echo $date | sed -e 's/\//-/g'`
+   schedulepath=/home/factwww/scheduling/$date
+   if [ ! -d $schedulepath ]
+   then
+      printprocesslog "WARNING "$schedulingpath" does not exist."
+      echo "WARNING "$schedulingpath" does not exist."
+      continue
+   fi
+   schedulefile=$schedulepath/swift.schedule
+   schedulefile2=$schedulepath/swift.schedule.formated
+   schedulefile3=$schedulepath/swift.schedule.txt
+   
+   # overwrite already existing file
+   echo "" > $schedulefile
+   # Get Swift Data
+   #   for the date of sunset
+   lynx -width 200 -nolist -dump https://www.swift.psu.edu/operations/obsSchedule.php?d=$date2 | grep $date2 >> $schedulefile
+   #   for the date of sunrise
+   nextday=`date +%Y-%m-%d --date=$date2"+24hour"`
+   lynx -width 200 -nolist -dump https://www.swift.psu.edu/operations/obsSchedule.php?d=$nextday | grep $nextday >> $schedulefile
+
+   # check if schedule is empty
+   checkcontent=`cat $schedulefile`
+   if [ "$checkcontent" = "" ]
+   then
+      printprocesslog "INFO No Schedule found for the night of "$date
+      continue
+   fi
+   
+   # grep only the columns which are needed
+   cat $schedulefile | sed -r -e 's/[0-9]{5}\ \ \ [0-9]{1,3}\ \ \ //' | grep -o -E '^\ \ \ \ 20[1-9][0-9]-[01][0-9]-[0-3][0-9]\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\ \ \ 20[1-9][0-9]-[01][0-9]-[0-3][0-9]\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\ \ \ [A-Z1-4]([A-Za-z0-9][\ \.\+\-]?){3,20}' > $schedulefile2
+   
+   # output list of sources
+   echo -n "Searched the schedule for the following source names: " >$schedulefile3
+   for (( i=0; i<${#sources[@]}; i++ ))
+   do
+      echo -n ${sources[$i]} >>$schedulefile3
+      i2=`echo " $i + 1" | bc -l`
+      if [ $i2 -lt ${#sources[@]} ]
+      then
+         echo -n ", " >>$schedulefile3
+      else
+         echo "" >>$schedulefile3
+      fi
+   done
+   
+   printprocesslog "INFO searching the Swift schedule for the following sources: "${sources[@]}
+   # search for source names in schedule file
+   for (( i=0; i<${#sources[@]}; i++ ))
+   do
+      grep "${sources[$i]}" $schedulefile2 >>$schedulefile3
+   done
+   
+   # check if a source was found
+   numfound=`cat $schedulefile3 | wc -l`
+   if [ $numfound -eq 1 ]
+   then
+      echo -e "\nNone of the sources was found in the Swift schedule." >> $schedulefile3
+   fi
+
+   # output link to full schedule
+   echo -e "\nThe full schedule can be found <a href='../scheduling/"$date"/"`basename $schedulefile2`"'>here</a>\n" >>$schedulefile3
+   
+   printprocesslog "INFO schedule output stored to "$schedulefile3
 done
 
-cat $schedulefile | sed -r -e 's/[0-9]{5}\ \ \ [0-9]{1,3}\ \ \ //' | grep -o -E '^\ \ \ \ 20[1-9][0-9]-[01][0-9]-[0-3][0-9]\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\ \ \ 20[1-9][0-9]-[01][0-9]-[0-3][0-9]\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\ \ \ [A-Z1-4]([A-Za-z0-9][\ \.\+\-]?){3,20}' > $schedulefile2
-
-#echo ""
-echo -n "Searched the schedule for the following source names: " >$schedulefile3
-for (( i=0; i<${#sources[@]}; i++ ))
-do
-   echo -n ${sources[$i]} >>$schedulefile3
-   i2=`echo " $i + 1" | bc -l`
-   if [ $i2 -lt ${#sources[@]} ]
-   then
-      echo -n ", " >>$schedulefile3
-   else
-      echo "" >>$schedulefile3
-   fi
-done
-for (( i=0; i<${#sources[@]}; i++ ))
-do
-   grep "${sources[$i]}" $schedulefile2 >>$schedulefile3
-done
-
-echo -e "\nThe full schedule can be found <a href='../scheduling/"$date2"/"`basename $schedulefile2`"'>here</a>\n" >>$schedulefile3
-
+finish
