Index: trunk/DataCheck/Tools/get_data.sh
===================================================================
--- trunk/DataCheck/Tools/get_data.sh	(revision 19032)
+++ trunk/DataCheck/Tools/get_data.sh	(revision 19033)
@@ -1,31 +1,31 @@
 #!/bin/bash
 
-# ----------------------------------------------------------------
-#  README README README README README README README README README
-# ----------------------------------------------------------------
-#                                                                #
-# To use this script, you need                                   #
-#   - a computer with access to the FACT database in La Palma    #
-#   - a file with the password of a valid mysql-user             #
-#   - to define the setup below for                              #
-#     a) the DB access                                           #
-#     b) the data you want to have                               #
-#                                                                #
-# To define the setup, search for SETUP in this script and       #
-#   read the details there (starting roughly line 295)           #
-#                                                                #
-# Per data request, you get up to 3 files:                       #
-#   *_internal.dat                                               #
-#   *_collaborators.dat                                          #
-#   *_external.dat (only if binning is 20min or nightly)         #
-#                                                                #
-# Please have in mind that this started as a tool for myself,    #
-#  then others started using it. Also the script is not yet      #
-#  finalized. In case you find problems and/or have a feature    #
-#  request, please send and email to dorner@astro.uni-wuerzburg.de                                 #
-#                                                                #
-# ----------------------------------------------------------------
-#  README README README README README README README README README
-# ----------------------------------------------------------------
+# ---------------------------------------------------------------- #
+#  README README README README README README README README README  #
+# ---------------------------------------------------------------- #
+#                                                                  #
+# To use this script, you need                                     #
+#   - a computer with access to the FACT database in La Palma      #
+#   - a file with the password of a valid mysql-user               #
+#   - to define the setup below for                                #
+#     a) the DB access                                             #
+#     b) the data you want to have                                 #
+#                                                                  #
+# To define the setup, search for SETUP in this script and         #
+#   read the details there                                         #
+#                                                                  #
+# Per data request, you get up to 3 files:                         #
+#   *_internal.dat                                                 #
+#   *_collaborators.dat                                            #
+#   *_external.dat (only if binning is 20min or nightly)           #
+#                                                                  #
+# Please have in mind that this started as a tool for myself, then #
+# others started using it. Also the script is not yet finalized.   #
+# In case you find problems and/or have a feature request, please  #
+# send and email to dorner@astro.uni-wuerzburg.de                  #
+#                                                                  #
+# ---------------------------------------------------------------- #
+#  README README README README README README README README README  #
+# ---------------------------------------------------------------- #
 
 
@@ -35,11 +35,9 @@
 # ToDo (notes DD): 
 # ----------------
-# - add file for collaborators
-# - update columns and content for 3 types of files
-# - limit creation of file for externals to 
+# - limit creation of file for externals to nightly and 20 min
+# - add zd, th for internal for minute binning (daily done)
 # - update function for correction
 # - update CU for QLA
 # - add CU for ISDC analysis
-# - add zd, th for internal
 # - add < 20121212 data for QLA
 # - check crab flux
@@ -111,108 +109,94 @@
    
    case $timeunit in
-      mjd)  start=" Mjd(Min(fRunStart)) "
-            stop=" Mjd(MAX(fRunStop)) "
-            deltat=" (Mjd(MAX(fRunStop))-Mjd(Min(fRunStart)))/2 "
-            time=" Mjd(Min(fRunStart))+"$deltat
-            start2=" Mjd(MIN(o.start)) "
-            stop2=" Mjd(MAX(o.stop)) "
-            deltat2=" (Mjd(MAX(o.stop))-Mjd(MIN(o.start)))/2 "
-            time2=" Mjd(MIN(o.start))+"$deltat2
+      mjd)  delta="(Mjd(MAX(fRunStop))-Mjd(Min(fRunStart)))/2"
+            start=" Mjd(Min(fRunStart)) AS start"
+            stop=" Mjd(MAX(fRunStop)) AS stop"
+            deltat=$delta" AS deltat"
+            time=" Mjd(Min(fRunStart))+"$delta" AS time"
+            delta2="(Mjd(MAX(o.stop))-Mjd(MIN(o.start)))/2"
+            start2=" Mjd(MIN(o.start)) AS start"
+            stop2=" Mjd(MAX(o.stop)) AS stop"
+            deltat2=$delta2" AS deltat"
+            time2=" Mjd(MIN(o.start))+"$delta2" AS time"
             ;;
-      unix) start="Unix_timestamp(CONVERT_TZ(Min(fRunStart), '+00:00', 'SYSTEM')) "
-            stop="Unix_timestamp(CONVERT_TZ(Max(fRunStop), '+00:00', 'SYSTEM')) "
-            deltat=" (Unix_timestamp(CONVERT_TZ(Max(fRunStop), '+00:00', 'SYSTEM')) - Unix_timestamp(CONVERT_TZ(Min(fRunStart), '+00:00', 'SYSTEM')))/2 "
-            time="  Unix_timestamp(CONVERT_TZ(Min(fRunStart), '+00:00', 'SYSTEM'))+"$deltat
-            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, "
-            time2="  (Unix_timestamp(CONVERT_TZ(Max(o.stop), '+00:00', 'SYSTEM')) - Unix_timestamp(CONVERT_TZ(Min(o.start), '+00:00', 'SYSTEM')))/2 "
-            time2="  Unix_timestamp(CONVERT_TZ(Min(o.start), '+00:00', 'SYSTEM'))+"$deltat2
+      unix) delta="(Unix_timestamp(CONVERT_TZ(Max(fRunStop), '+00:00', 'SYSTEM')) - Unix_timestamp(CONVERT_TZ(Min(fRunStart), '+00:00', 'SYSTEM')))/2"
+            start="Unix_timestamp(CONVERT_TZ(Min(fRunStart), '+00:00', 'SYSTEM')) AS start"
+            stop="Unix_timestamp(CONVERT_TZ(Max(fRunStop), '+00:00', 'SYSTEM')) AS stop"
+            deltat=$delta" AS deltat"
+            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"
+            deltat2=$delta2" AS deltat"
+            time2=" Unix_timestamp(CONVERT_TZ(Min(o.start), '+00:00', 'SYSTEM'))+"$delta2" AS time"
             ;;
-      *)    start=" MIN(fRunStart) "
-            stop=" MAX(fRunStop) "
-            deltat=" sec_to_time(time_to_sec(timediff(MAX(fRunStop), Min(fRunStart)))/2) "
-            time=" addtime(Min(fRunStart), "$deltat") "
-            start2=" MIN(o.start) "
-            stop2=" MAX(o.stop) "
-            deltat2=" sec_to_time(time_to_sec(timediff(MAX(o.stop), Min(o.start)))/2) "
-            time2=" addtime(Min(o.start), "$deltat2") "
+      *)    delta="sec_to_time(time_to_sec(timediff(MAX(fRunStop), Min(fRunStart)))/2)"
+            start=" MIN(fRunStart) AS start"
+            stop=" MAX(fRunStop) AS stop"
+            deltat=$delta" AS deltat"
+            time=" addtime(Min(fRunStart), "$delta") AS time"
+            delta2="sec_to_time(time_to_sec(timediff(MAX(o.stop), Min(o.start)))/2)"
+            start2=" MIN(o.start) AS start"
+            stop2=" MAX(o.stop) AS stop"
+            deltat2=$delta" AS deltat"
+            time2=" addtime(Min(o.start), "$delta2") AS time"
             ;;
    esac
-   ontime=" SUM("$ontimeif")/60."
-   ontime2=" SUM(o.ot)/60. "
-   
-   excrate=" SUM(fNumExcEvts)/SUM("$ontimeif")*3600 "
+   zenith="fZenithDistance"
+   thresh="IF(ISNULL(fThresholdMinSet),fThresholdMedian,fThresholdMinSet)"
+   # thomas correction factor
+   correvts=" fNumExcEvts*(pow(cos("$zenith"Mean*PI()/180),3)+14.8/21.9*pow(sin(2*"$zenith"Mean*PI()/180),5))/((1-0.00124/1.21*("$thresh"-500)*("$thresh">=500))) "
    excerr="ExcErr(Sum(fNumSigEvts), SUM(fNumBgEvts))"
-   significance="LiMa(Sum(fNumSigEvts), SUM(fNumBgEvts))"
-   numexc="Sum(fNumExcEvts)"
-   numsig="Sum(fNumSigEvts)"
-   numbg="Sum(fNumBgEvts)"
-   excrateerr=" "$excerr"/SUM("$ontimeif")*3600 "
-   # thomas correction factor
-   correvts=" fNumExcEvts*(pow(cos(fZenithDistanceMean*PI()/180),3)+14.8/21.9*pow(sin(2*fZenithDistanceMean*PI()/180),5))/((1-0.00124/1.21*(if(isnull(fThresholdMinSet),fThresholdMedian,fThresholdMinSet)-500)*(if(isnull(fThresholdMinSet),fThresholdMedian,fThresholdMinSet)>=500))) "
-   correxcrate=" SUM("$correvts")/SUM("$ontimeif")*3600 "
+   CU="SUM("$correvts"/CUQLA(fNight))/SUM("$ontimeif")*3600"
+   CUerr=$excerr"/SUM("$ontimeif")*3600*SUM("$correvts"/CUQLA(fNight))/SUM(fNumExcEvts)"
+   excerr2="ExcErr(SUM(o.sigevts),SUM(o.bgevts))"
+   CU2="SUM(o.corevts/o.cu)/SUM(o.ot)*3600"
+   CUerr2=$excerr2"/SUM(o.ot)*3600*SUM(o.corevts/o.cu)/(SUM(o.sigevts)-SUM(o.bgevts))"
+   
+   ontime=" ROUND(SUM("$ontimeif")/60., 1) AS ontime"
+   excrate=" ROUND(SUM(fNumExcEvts)/SUM("$ontimeif")*3600, 1) AS excrate"
+   significance="ROUND(LiMa(Sum(fNumSigEvts), SUM(fNumBgEvts)), 1) AS significance"
+   numexc="Sum(fNumExcEvts) AS numexc"
+   numsig="Sum(fNumSigEvts) AS numsig"
+   numbg="Sum(fNumBgEvts) AS numbg"
+   excrateerr=" ROUND("$excerr"/SUM("$ontimeif")*3600, 1) AS excrateerr"
+   correxcrate=" ROUND(SUM("$correvts")/SUM("$ontimeif")*3600, 1) AS correxcrate"
    # corerr = MMath::ErrorExc(excevtssum+bgevtssum, bgevtssum*5, 0.2)/ontimesum*3600.*corrate/excrate;
-   correxcrateerr=" "$excerr"/SUM("$ontimeif")*3600*SUM("$correvts")/SUM(fNumExcEvts) "
-   # correction on run basis (not relevant for hess)
-   #cu=$correxcrate"/"$cufactor
-   cu=" SUM("$correvts"/CUQLA(fNight))/SUM("$ontimeif")*3600 "
-   #cuerr=$correxcrateerr"/"$cufactor
-   cuerr=" "$excerr"/SUM("$ontimeif")*3600*SUM("$correvts"/CUQLA(fNight))/SUM(fNumExcEvts) "
-   flux=$cu" * "$crabflux
-   fluxerr=$cuerr" * "$crabflux
-
-   
-   excrate2=" (SUM(o.sigevts)-SUM(o.bgevts))/SUM(o.ot)*3600 "
-   excerr2="ExcErr(SUM(o.sigevts),SUM(o.bgevts))"
-   significance2="LiMa(SUM(o.sigevts),SUM(o.bgevts))"
-   numexc2="Sum(o.sigevts-o.bgevts)"
-   numsig2="Sum(o.sigevts)"
-   numbg2="Sum(o.bgevts)"
-   excrateerr2=" "$excerr2"/SUM(o.ot)*3600 "
-   correxcrate2=" SUM(o.corevts)/SUM(o.ot)*3600 "
-   correxcrateerr2=" "$excerr2"/SUM(o.ot)*3600*SUM(o.corevts)/(SUM(o.sigevts)-SUM(o.bgevts)) "
-   #cu2=$correxcrate2"/"$cufactor
-   cu2=" SUM(o.corevts/o.cu)/SUM(o.ot)*3600 "
-   #cuerr2=$correxcrateerr2"/"$cufactor
-   cuerr2=" "$excerr2"/SUM(o.ot)*3600*SUM(o.corevts/o.cu)/(SUM(o.sigevts)-SUM(o.bgevts)) "
-   flux2="$cu2*"$crabflux
-   fluxerr2="$cuerr2*"$crabflux
+   correxcrateerr=" ROUND("$excerr"/SUM("$ontimeif")*3600*SUM("$correvts")/SUM(fNumExcEvts), 1) AS correxcrateerr"
+   # correction on run basis
+   cu=" ROUND("$CU", 2) AS cu"
+   cuerr=" ROUND("$CUerr", 2) AS cuerr"
+   flux="ROUND("$CU" * "$crabflux", 2) AS flux"
+   fluxerr="ROUND("$CUerr" * "$crabflux", 2) AS fluxerr"
+   
+   ontime2=" ROUND(SUM(o.ot)/60., 1) AS ontime"
+   excrate2=" ROUND((SUM(o.sigevts)-SUM(o.bgevts))/SUM(o.ot)*3600, 1) AS excrate"
+   significance2=" ROUND(LiMa(SUM(o.sigevts),SUM(o.bgevts)), 1) AS significance"
+   numexc2="Sum(o.sigevts-o.bgevts) AS numexc"
+   numsig2="Sum(o.sigevts) AS numsig"
+   numbg2="Sum(o.bgevts) AS numbg"
+   excrateerr2=" ROUND("$excerr2"/SUM(o.ot)*3600, 1) AS excrateerr"
+   correxcrate2=" ROUND(SUM(o.corevts)/SUM(o.ot)*3600, 1) AS correxcrate"
+   correxcrateerr2=" ROUND("$excerr2"/SUM(o.ot)*3600*SUM(o.corevts)/(SUM(o.sigevts)-SUM(o.bgevts)), 1) AS correxcrateerr"
+   cu2=" ROUND("$CU2", 2) AS cu"
+   cuerr2=" ROUND("$CUerr2", 2) AS cuerr"
+   flux2="ROUND("$CU2" * "$crabflux", "$fluxprec") AS flux"
+   fluxerr2="ROUND("$CUerr2" *"$crabflux", "$fluxprec") AS fluxerr"
 
    
    if [ $bin -le 0 ]
    then 
-      queryint="SELECT "
+      num="#bin"
+      # first part of the query
+      querystart="SELECT "
       if [ $bin -eq 0 ]
       then 
-         queryint=$queryint" fPeriod as num, "
+         querystart=$querystart" fPeriod AS num, "
       else
-         queryint=$queryint" FLOOR((Mjd(fRunStart)-Mjd("$nightmin")-0.5)/"`echo $bin | sed -e 's/-//'`".) as num, "
+         querystart=$querystart" FLOOR((Mjd(fRunStart)-Mjd("$nightmin")-0.5)/"`echo $bin | sed -e 's/-//'`".) AS num, "
       fi
-      queryint=$queryint" "$time" as time, "$start" as start, "$stop" as stop, "
-      queryint=$queryint" round("$excrate", 1) as excrate, round("$correxcrate", 1) as correxcrate, "
-      queryint=$queryint" round("$cu", 2) as cu, "$flux" as flux, "
-      queryint=$queryint" "$deltat" as deltat, round("$ontime", 1) as ontime, "
-      queryint=$queryint" round("$excrateerr", 1) as excrateerr, round("$correxcrateerr", 1) as correxcrateerr, "
-      queryint=$queryint" round("$cuerr", 2) as cuerr, "$fluxerr" as fluxerr, "
-      queryint=$queryint" round("$significance", 1) as significance, "
-      queryint=$queryint" Min(fNight) as nightmin, "
-      queryint=$queryint" Max(fNight) as nightmax, "
-      queryint=$queryint" "$numexc" as numexc, "
-      queryint=$queryint" "$numsig" as numsig, "
-      queryint=$queryint" "$numbg" as numbg "
-      
-      queryext="SELECT "
-      if [ $bin -eq 0 ]
-      then 
-         queryext=$queryext" fPeriod as num, "
-      else
-         queryext=$queryext" FLOOR((Mjd(fRunStart)-Mjd("$nightmin")-0.5)/"`echo $bin | sed -e 's/-//'`".) as num, "
-      fi
-      queryext=$queryext" "$time" as time, "$start" as start, "$stop" as stop, "
-      queryext=$queryext" round("$correxcrate", 1) as correxcrate, round("$flux", "$fluxprec") as flux, "
-      queryext=$queryext" "$deltat" as deltat, round("$ontime", 1) as ontime, "
-      queryext=$queryext" round("$correxcrateerr", 1) as correxcrateerr, round("$fluxerr", "$fluxprec") as fluxerr, "
-      queryext=$queryext" round("$significance", 1) as significance "
-      
+      querystart=$querystart" "$time", "$start", "$stop", "
+      
+      # final part of the query
       querybase=$from$where
       querybase=$querybase" GROUP BY num "
@@ -224,27 +208,34 @@
       fi
       
+      # internal
+      queryint=$querystart
+      queryint=$queryint" "$excrate", "$correxcrate", "$cu", "$flux", "
+      queryint=$queryint" "$deltat", "$ontime", "
+      queryint=$queryint" "$excrateerr", "$correxcrateerr", "$cuerr", "$fluxerr", "
+      queryint=$queryint" "$significance", "
+      queryint=$queryint" Min(fNight) AS nightmin, Max(fNight) AS nightmax, "
+      queryint=$queryint" "$numexc", "$numsig", "$numbg", "
+      queryint=$queryint" Min("$zenith"Min) AS zdmin, Max("$zenith"Max) AS zdmax, "
+      queryint=$queryint" Min("$thresh") AS thmin, Max("$thresh") AS thmax "
       queryint=$queryint" "$querybase
-      queryext=$queryext" "$querybase
+      
+      # for collaborators
+      querycol=$querystart
+      querycol=$querycol" "$excrate", "$correxcrate", "$cu", "$flux", "
+      querycol=$querycol" "$deltat", "$ontime", "
+      querycol=$querycol" "$excrateerr", "$correxcrateerr", "$cuerr", "$fluxerr", "
+      querycol=$querycol" "$significance
+      querycol=$querycol" "$querybase
+      
+      # external
+      queryext=$querystart" "$excrate", "$deltat", "$excrateerr" "$querybase
+      
    else
-      queryint="SELECT "
-      queryint=$queryint" "$time2" as time, "$start2" as start, "$stop2" as stop, "
-      queryint=$queryint" round("$excrate2", 1) as excrate, round("$correxcrate2", 1) as correxcrate, "
-      queryint=$queryint" round("$cu2", 1) as cu, round("$flux2", "$fluxprec") as flux, "
-      queryint=$queryint" round("$excrateerr2", 1) as excrateerr, round("$correxcrateerr2", 1) as correxcrateerr, "
-      queryint=$queryint" "$deltat2" as deltat, round("$ontime2", 1) as ontime, "
-      queryint=$queryint" round("$cuerr2", 1) as cuerr, round("$fluxerr2", "$fluxprec") as fluxerr, "
-      queryint=$queryint" round("$significance2", 1) as significance, "
-      queryint=$queryint" avg(o.night) as night, "
-      queryint=$queryint" "$numexc2" as numexc, "
-      queryint=$queryint" "$numsig2" as numsig, "
-      queryint=$queryint" "$numbg2" as numbg "
-      
-      queryext="SELECT "
-      queryext=$queryext" "$time2" as time, "$start2" as start, "$stop2" as stop, "
-      queryext=$queryext" round("$correxcrate2", 1) as correxcrate, round("$flux2", "$fluxprec") as flux, "
-      queryext=$queryext" "$deltat2" as deltat, round("$ontime2", 1) as ontime, "
-      queryext=$queryext" round("$correxcrateerr2", 1) as correxcrateerr, round("$fluxerr2", "$fluxprec") as fluxerr, "
-      queryext=$queryext" round("$significance2", 1) as significance "
-      
+      num=
+      # first part of the query
+      querystart="SELECT "
+      querystart=$querystart" "$time2", "$start2", "$stop2", "
+      
+      # final part of the query
       querybase=" FROM (SELECT fNight, @ot:="$ontimeif" AS ot, fRunStart AS start, fRunStop AS stop, fNumSigEvts AS sigevts, fNumBgEvts AS bgevts, "
       querybase=$querybase" "$correvts" AS corevts, CUQLA(fNight) AS cu, "
@@ -252,8 +243,27 @@
       querybase=$querybase" IF (@night=fNight AND FLOOR((@os+@ot)/"$bin"./60.)<1, @os:=@os + @ot, @os := @ot) AS os, @night :=fNight AS night "
       querybase=$querybase$from" CROSS JOIN (SELECT @night :=0, @ot :=0, @os :=0, @bl:=0) PARAMS "
-      querybase=$querybase$where" ORDER BY fRunStart) o GROUP BY block HAVING ontime>0.75*"$bin
-      
-      queryint=$queryint" "$querybase" order by 'time'"
-      queryext=$queryext" "$querybase" order by 'time'"
+      querybase=$querybase$where" ORDER BY fRunStart) o GROUP BY block HAVING ontime>0.75*"$bin" ORDER BY 'time'"
+      
+      # internal
+      queryint=$querystart
+      queryint=$queryint" "$excrate2", "$correxcrate2", "$cu2", "$flux2", "
+      queryint=$queryint" "$deltat2", "$ontime2", "
+      queryint=$queryint" "$excrateerr2", "$correxcrateerr2", "$cuerr2", "$fluxerr2", "
+      queryint=$queryint" "$significance2", "
+      queryint=$queryint" avg(o.night) AS night, "
+      queryint=$queryint" "$numexc2", "$numsig2", "$numbg2" "
+      queryint=$queryint" "$querybase
+      
+      # for collaborators
+      querycol=$querystart
+      querycol=$querycol" "$excrate2", "$correxcrate2", "$cu2", "$flux2", "
+      querycol=$querycol" "$deltat2", "$ontime2", "
+      querycol=$querycol" "$excrateerr2", "$correxcrateerr2", "$cuerr2", "$fluxerr2", "
+      querycol=$querycol" "$significance2
+      querycol=$querycol" "$querybase
+      
+      # external
+      queryext=$querystart" "$excrate2", "$deltat2", "$ontime2", "$excrateerr2" "$querybase
+      
    fi
    
@@ -263,12 +273,10 @@
    then 
       echo "internal: "$fileint
-      echo "# this file was created at "`date` > $fileint
+      echo "# This file was created at "`date` > $fileint
+      # add query and policy here
    fi
-   if [ $bin -le 0 ]
-   then 
-      echo "# numbin time[mjd] start[mjd] stop[mjd] excrate[evts/h] corr.excrate[evts/h] flux[CU] flux[e-11/cm2/s] delta_time[mjd] ontime[min] excrate_err[evts/h] corr.excrate_err[evts/h] flux_err[CU] flux_err[e-11/cm2/s] significance nightmin, nightmax num_exc num_sig num_bg " >> $fileint
-   else
-      echo "# time[mjd] start[mjd] stop[mjd] excrate[evts/h] corr.excrate[evts/h] flux[CU] flux[e-11/cm2/s] delta_time[mjd] ontime[min] excrate_err[evts/h] corr.excrate_err[evts/h] flux_err[CU] flux_err[e-11/cm2/s] significance nightmin, nightmax num_exc num_sig num_bg " >> $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=$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 zdmin zdmax thmin thmax"
+   echo $headerint >> $fileint
    #echo "$queryint"
    mysql --defaults-file=$sqlpw -u factread --host=$host $dbname -s -e "$queryint" >> $fileint
@@ -276,25 +284,45 @@
    
    
-   fileext=$datapath"/FACT_preliminary_"$name".dat"
+   fileext=$datapath"/FACT_preliminary_"$name"_external.dat"
    if [ "$overwrite" = "yes" ]
    then 
       echo "external: "$fileext
-      echo "# this file was created at "`date` > $fileext
+      echo "# This file was created at "`date` > $fileext
+      # add query and policy here
    fi
-   if [ $bin -lt 0 ]
-   then 
-      echo "# numbin time[mjd] start[mjd] stop[mjd] corr.excrate[evts/h] flux[e-11/cm2/s] delta_time[mjd] ontime[min] corr.excrate_err[evts/h] flux_err[e-11/cm2/s] significance " >> $fileext
-   else
-      echo "# time[mjd] start[mjd] stop[mjd] corr.excrate[evts/h] flux[e-11/cm2/s] delta_time[mjd] ontime[min] corr.excrate_err[evts/h] flux_err[e-11/cm2/s] significance " >> $fileext
-   fi
+   headerext="# "$num" time["$timeunit"] start["$timeunit"] stop["$timeunit"] excrate[evts/h] delta_time["$timeunit"] excrate_err[evts/h] "
+   echo $headerext >> $fileext
    #echo "$queryext"
    mysql --defaults-file=$sqlpw -u factread --host=$host $dbname -s -e "$queryext" >> $fileext
    #mysql --defaults-file=$sqlpw -u factread --host=$host $dbname -e "$queryext"
+
+   filecol=$datapath"/FACT_preliminary_"$name"_collaborators.dat"
+   if [ "$overwrite" = "yes" ]
+   then 
+      echo "collaborators: "$filecol
+      echo "# This file was created at "`date` > $filecol
+      # add query and policy here
+   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=$headercol" excrate_err[evts/h] corr.excrate_err[evts/h] flux_err[CU] flux_err[e-11/cm2/s] significance "
+   echo $headercol >> $filecol
+   #echo "$querycol"
+   mysql --defaults-file=$sqlpw -u factread --host=$host $dbname -s -e "$querycol" >> $filecol
+   #mysql --defaults-file=$sqlpw -u factread --host=$host $dbname -e "$querycol
 }
 
-# SETUP:
-# ------
-# DB SETUP:
-# ---------
+# -------------------------------------------------------------------------------------- #
+#  SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP   #
+# -------------------------------------------------------------------------------------- #
+#                                                                                        #
+# The lines below define the basic setup for the database and give examples and          #
+# explanations for the various options available.                                        #
+# The request of the data itself is done with a smaller setup further down.              # 
+#                                                                                        #
+# -------------------------------------------------------------------------------------- #
+#
+# ----------
+#  DB SETUP
+# ----------
 # path to file with mysql password
 sqlpw=/home/$USER/.mysql.pw 
@@ -305,10 +333,8 @@
 # name of database
 dbname=factdata 
-# defaults for zd and threshold
-zdmax=90 # all data
-thmax=1500 # all data
 #
-# SETUP for your data: 
-# --------------------
+# -------------
+#  BASIC SETUP
+# -------------
 # output path
 path=`dirname $0`
@@ -338,22 +364,43 @@
 # time range
 nightmin=20111115
-nightmax=20171231
+nightmax=20201231
+# defaults for zd and threshold
+zdmax=90 # all data
+thmax=1500 # all data
 # overwrite dataset file? 
 # (useful to combine different binnings in one file -> set to "no")
 overwrite="yes"
-
-
-# example (adapt to your needs)
+# optional: require minimal ontime per bin (default 20 min)
+#ontimelimit=30 # 30 min
+ontimelimit= # default 20 min
+
+# -------------------------------------------------------------------------------------- #
+#  SETUP - GET YOUR DATA HERE - SETUP - GET YOUR DATA HERE - SETUP - GET YOUR DATA HERE  #
+# -------------------------------------------------------------------------------------- #
+#                                                                                        #
+# Adapt the lines below to your needs.                                                   #
+# Overwrite default settings above.                                                      #
+# The  data-request is sent with the line 'get_results.'                                 #
+# Minumum setup: Define source key and name for file.                                    #
+# The list of source keys can be found at                                                #
+#   https://fact-project.org/run_db/db/printtable.php?fTable=Source&fSortBy=fSourceKEY+  #
+# More examples can be found further down.                                               #
+#                                                                                        #
+# REMARKS:                                                                               #
+# - correction of effect of zd and threshold not yet finalized and only valid for QLA    #
+# - no CU-conversion available for ISDC-analysis so far (that for QLA is used instead)   #
+#                                                                                        #
+# -------------------------------------------------------------------------------------- #
 
 # 501 MAGIC 
 source=2
-name="Mrk501_2014_QLA"
-bin=-1
-nightmin=20140501
-nightmax=20140930
-get_results
-table="AnalysisResultsRunISDC"  # ISDC
-name="Mrk501_2014_ISDC"
-get_results
+name="Mrk501_2014_forMAGIC"
+bin=-1
+nightmin=20140714
+nightmax=20140805
+get_results
+#bin=20
+#name="Mrk501_2014_forMAGIC20"
+#get_results
 
 
