Index: /trunk/DataCheck/Tools/get_data.sh
===================================================================
--- /trunk/DataCheck/Tools/get_data.sh	(revision 19056)
+++ /trunk/DataCheck/Tools/get_data.sh	(revision 19057)
@@ -35,6 +35,5 @@
 # ToDo (notes DD): 
 # ----------------
-# - fix bug for timeunit=timestamp (min-binning)
-# - add information on souce, binning, etc to data-file 
+# - finalize policy and references
 # must-have
 # - update function for zd/th-correction
@@ -47,4 +46,5 @@
 # - functionality to determine start time for seaon-binning
 # - offer several predefined datachecks?
+# - file as attachment to email ofr download.php
 
 #
@@ -93,5 +93,5 @@
       echo "#                           href='http://iopscience.iop.org/1748-0221/9/10/P10012"
       echo "#   FACT quick look analysis: https://fact-project.org/monitoring"
-     echo "#                             http://adsabs.harvard.edu/abs/2015arXiv150202582D"
+      echo "#                             http://adsabs.harvard.edu/abs/2015arXiv150202582D"
       echo "# If you intend to use data or information from this website, please let us know for reference."
    else
@@ -101,6 +101,52 @@
    echo "#" 
 }
-    
-
+
+print_selection()
+{
+   echo "#" 
+   echo "# Your Selection: " 
+   sourcename=`mysql --defaults-file=$sqlpw -u factread --host=$host $dbname -s -e "SELECT fSourceName FROM Source WHERE fSourceKey="$source`
+   echo "#   Source: "$sourcename 
+   echo "#   Time range: "$nightmin"-"$nightmax 
+   echo "#   Time format: "$timeunit 
+   if [ $bin -lt 0 ]
+   then 
+      unit="night(s)"
+   else
+      if [ $bin -eq ]
+      then 
+         unit="periods"
+      else
+         unit="minutes"
+      fi
+   fi
+   binning=`echo $bin | sed -e 's/-//'`" "$unit
+   echo "#   Binning: "$binning
+   if [ "$expert" = "yes" ]
+   then
+      echo "# Additional Internal Selection: "
+      if [ "$zdmax" != "" ]
+      then
+         echo "#   Maximum Zenith Distance: "$zdmax" degree"
+      fi
+      if [ "$thmax" != "" ]
+      then
+         echo "#   Maximum Trigger Threshold: "$thmax" DAC counts"
+      fi
+      if [ "$light" != "" ]
+      then
+         echo "#   Light Condition Cut: "$lightcut
+      fi
+      if [ "$dust" != "" ]
+      then
+         echo "#   Calima Cut: dust < "$dust" ug/cm3"
+      fi
+      if [ "$usedch" == "yes" ]
+      then
+         echo "#   You are using the a data quality selection based on the cosmic ray rate."
+      fi
+   fi
+   echo "#" 
+}
 
 function get_results()
@@ -183,6 +229,6 @@
             time=" Unix_timestamp(CONVERT_TZ(MIN(fRunStart), '+00:00', 'SYSTEM'))+"$delta" AS time"
             delta2="(Unix_timestamp(CONVERT_TZ(MAX(o.stop), '+00:00', 'SYSTEM')) - Unix_timestamp(CONVERT_TZ(MIN(o.start), '+00:00', 'SYSTEM')))/2"
-            startstop2=" Unix_timestamp(CONVERT_TZ(MIN(o.start), '+00:00', 'SYSTEM')) AS start"
-            startstop2=$starstop2" Unix_timestamp(CONVERT_TZ(MAX(o.stop), '+00:00', 'SYSTEM')) AS stop"
+            start2=" Unix_timestamp(CONVERT_TZ(MIN(o.start), '+00:00', 'SYSTEM')) AS start"
+            stop2=" Unix_timestamp(CONVERT_TZ(MAX(o.stop), '+00:00', 'SYSTEM')) AS stop"
             deltat2=$delta2" AS deltat"
             time2=" Unix_timestamp(CONVERT_TZ(MIN(o.start), '+00:00', 'SYSTEM'))+"$delta2" AS time"
@@ -196,5 +242,5 @@
             start2=" MIN(o.start) AS start"
             stop2=" MAX(o.stop) AS stop"
-            deltat2=$delta" AS deltat"
+            deltat2=$delta2" AS deltat"
             time2=" addtime(MIN(o.start), "$delta2") AS time"
             ;;
@@ -231,11 +277,15 @@
    fi
    # light condition cut
-   if [ "$light" == "no moon" ]
+   if [ "$light" == "nomoon" ]
    then
-      where=$where" AND fZenithDistanceMoon>90"
+      lightcut=" fZenithDistanceMoon>90"
    fi
    if [ "$light" == "dark" ]
    then
-      where=$where" AND fMoonZenithDistance>90 AND fSunZenithDistance>108 "
+      lightcut=" fMoonZenithDistance>90 AND fSunZenithDistance>108 "
+   fi
+   if [ "$light" != "" ]
+   then 
+      where=$where" AND "$lightcut
    fi
    querybase=$from$where
@@ -255,22 +305,25 @@
    if [ $bin -le 0 ]
    then 
-      num="#bin"
       # first part of the query
       querystart="SELECT "
+      querystart=$querystart" "$time", "$start", "$stop", "
+      # final part of the query
       if [ $bin -eq 0 ]
       then 
-         querystart=$querystart" fPeriod AS num, "
+         orderby=" fPeriod "
+         #querystart=$querystart" fPeriod AS num, "
+         queryend=" GROUP BY fPeriod "
       else
-         querystart=$querystart" FLOOR((Mjd(fRunStart)-Mjd("$nightmin")-0.5)/"`echo $bin | sed -e 's/-//'`".) AS num, "
-      fi
-      querystart=$querystart" "$time", "$start", "$stop", "
-      
-      # final part of the query
-      queryend=" GROUP BY num "
+         num=" FLOOR((Mjd(fRunStart)-Mjd("$nightmin")-0.5)/"`echo $bin | sed -e 's/-//'`".) "
+         orderby=$num
+         #querystart=$querystart" FLOOR((Mjd(fRunStart)-Mjd("$nightmin")-0.5)/"`echo $bin | sed -e 's/-//'`".) AS num, "
+         queryend=" GROUP BY "$num
+      fi
+      #queryend=" GROUP BY num "
       if [ "$ontimelimit" = "" ]
       then 
-         queryend=$queryend" HAVING SUM("$ontimeif")>1200 ORDER BY num " # 20 min
+         queryend=$queryend" HAVING SUM("$ontimeif")>1200 ORDER BY "$orderby
       else
-         queryend=$queryend" HAVING SUM("$ontimeif")>"$ontimelimit" ORDER BY num "
+         queryend=$queryend" HAVING SUM("$ontimeif")>"$ontimelimit" ORDER BY "$orderby
       fi
       
@@ -301,5 +354,4 @@
       
    else
-      num=
       # first part of the query
       querystart="SELECT "
@@ -354,5 +406,6 @@
          print_policy >> $fileext
       fi
-      headerext="# "$num" time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] delta_time["$timeunit"] excrate_err[evts/h] "
+      print_selection >> $fileext
+      headerext="# time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] (stop-start)/2["$timeunit"] excrate_err[evts/h] "
       echo $headerext >> $fileext
       #echo "$queryext"
@@ -374,9 +427,10 @@
       echo "# This file was created at "`date` > $fileint
       print_policy >> $fileint
+      print_selection >> $fileint
       echo "# The following query was used: " >> $fileint
       echo "# "$queryint >> $fileint
       echo "#" >> $fileint
    fi
-   headerint="# "$num" time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] corr.excrate[evts/h] flux[CU] flux[e-11/cm2/s] delta_time["$timeunit"] ontime[min]"
+   headerint="# time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] corr.excrate[evts/h] flux[CU] flux[e-11/cm2/s] (stop-start)/2["$timeunit"] ontime[min]"
    headerint=$headerint" excrate_err[evts/h] corr.excrate_err[evts/h] flux_err[CU] flux_err[e-11/cm2/s] significance night num_exc num_sig num_bg "
    headerint=$headerint" zdmin zdmax thmin thmax avg(cufactor) avg(R750cor) avg(R750ref) "
@@ -396,9 +450,10 @@
       echo "# This file was created at "`date` > $filecol
       print_policy >> $filecol
+      print_selection >> $filecol
       echo "# The following query was used: " >> $filecol
       echo "# "$querycol >> $filecol
       echo "#" >> $filecol
    fi
-   headercol="# "$num" time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] corr.excrate[evts/h] flux[CU] flux[e-11/cm2/s] delta_time["$timeunit"] ontime[min]"
+   headercol="# time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] corr.excrate[evts/h] flux[CU] flux[e-11/cm2/s] (stop-start)/2["$timeunit"] ontime[min]"
    headercol=$headercol" excrate_err[evts/h] corr.excrate_err[evts/h] flux_err[CU] flux_err[e-11/cm2/s] significance "
    echo $headercol >> $filecol
@@ -417,5 +472,7 @@
    # setup
    datapath="/home/factwww/dch/data"
+   #datapath="./data"
    sqlpw=/home/fact/.mysql.pw 
+   #sqlpw=/home/fact/.mysql.pw2 
    host=10.0.100.21 
    dbname=factdata 
@@ -532,5 +589,5 @@
 # apply additional predefined cuts
 # light conditions
-#light="no moon" # only data with no moon (but twilight allowed)
+#light="nomoon" # only data with no moon (but twilight allowed)
 #light="dark" # only dark night data
 # TNG dust - cut away data with calima
