Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8134)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8135)
@@ -59,4 +59,14 @@
 
  2006/10/19 Daniela Dorner
+
+   * datacenter/db/opticaldata.php:
+     - added (website to display data from KVA)
+
+   * datacenter/db/magicdefs.php:
+     - added values for opticaldata.php
+
+   * datacenter/macros/filloptical.C:
+     - added (macro to read *.instr files from KVA and fill the values
+       into the database)
 
    * datacenter/scripts/copyscript:
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 8134)
+++ trunk/MagicSoft/Mars/NEWS	(revision 8135)
@@ -2,4 +2,15 @@
 
  *** <cvs>
+
+   - database: Implemented ObservationMode flag in websites.
+
+   - database: For sequences there is now also the stoptime available on the
+     websites. 
+
+   - database: Order by is now working also on printtable.php and when 
+     group-by is chosen. 
+
+   - database: Removed ManuallyChanged flag from sequence-info websites, 
+     as it is not needed anymore with the new sequence building algorithm.
 
    - general: MRolke.[cc,h]: Modification of TRolke from root_v5.12.00b.
Index: trunk/MagicSoft/Mars/datacenter/db/magicdefs.php
===================================================================
--- trunk/MagicSoft/Mars/datacenter/db/magicdefs.php	(revision 8134)
+++ trunk/MagicSoft/Mars/datacenter/db/magicdefs.php	(revision 8135)
@@ -76,4 +76,7 @@
      "fLightConditionsName"              => CheckWhere("fLightConditionsKEY",              $_GET),
      "fParticleTypeName"                 => CheckWhere("fParticleTypeKEY",                 $_GET),
+     "fFitsFileName"                     => CheckWhere("fFitsFileKEY",                     $_GET),
+     "fObjectName"                       => CheckWhere("fObjectKEY",                       $_GET),
+     "fStatusName"                       => CheckWhere("fStatusKEY",                       $_GET),
     );
 
@@ -96,4 +99,7 @@
      "fLightConditionsName"              => CheckGroup("fLightConditionsKEY",              $_GET),
      "fParticleTypeName"                 => CheckGroup("fParticleTypeKEY",                 $_GET),
+     "fFitsFileName"                     => CheckGroup("fFitsFileKEY",                     $_GET),
+     "fObjectName"                       => CheckGroup("fObjectKEY",                       $_GET),
+     "fStatusName"                       => CheckGroup("fStatusKEY",                       $_GET),
     );
 
@@ -299,4 +305,14 @@
      "fFailedCode"                      => "Err<br>Code",
      "fFailedCodeAdd"                   => "Code<br>Add",
+     "fTimestamp"                       => "Time",
+     "fExposure"                        => "Exposure",
+     "fFitsFileName"                    => "FitsFile",
+     "fObjectName"                      => "Object",
+     "fSkyLevel"                        => "SkyLevel",
+     "fFWHM"                            => "FWHM",
+     "fApertureRadius"                  => "Ap.Rad.",
+     "fInstrumentalMag"                 => "Instr.Mag.",
+     "fInstrumentalMagErr"              => "Instr.Mag.Err.",
+     "fStatusName"                      => "StatusCode",
 //     "COUNT(*)"                         => "# Runs",
 //     "COUNT(*)"                         => "Sequence",
@@ -414,4 +430,7 @@
          "fLightConditionsName"              => $fromtable . ".fLightConditionsKEY",
          "fParticleTypeName"                 => $fromtable . ".fParticleTypeKEY",
+         "fFitsFileName"                     => $fromtable . ".fFitsFileKEY",
+         "fObjectName"                       => $fromtable . ".fObjectKEY",
+         "fStatusName"                       => $fromtable . ".fStatusKEY",
         );
 
@@ -559,4 +578,7 @@
          "fFailedCodeAdd"                    => "fFailedCodeAdd",
          "fParticleTypeName"                 => "ParticleType.fParticleTypeName",
+         "fFitsFileName"                     => "FitsFile.fFitsFileName",
+         "fObjectName"                       => "Object.fObjectName",
+         "fStatusName"                       => "Status.fStatusName",
         );
 
@@ -598,4 +620,7 @@
          "fLightConditionsName"              => " LEFT JOIN LightConditions              ON " . $fromtable . ".fLightConditionsKEY=LightConditions.fLightConditionsKEY",
          "fParticleTypeName"                 => " LEFT JOIN ParticleType                 ON " . $fromtable . ".fParticleTypeKEY=ParticleType.fParticleTypeKEY ",
+         "fFitsFileName"                     => " LEFT JOIN FitsFile                     ON " . $fromtable . ".fFitsFileKEY=FitsFile.fFitsFileKEY",
+         "fObjectName"                       => " LEFT JOIN Object                       ON " . $fromtable . ".fObjectKEY=Object.fObjectKEY ",
+         "fStatusName"                       => " LEFT JOIN Status                       ON " . $fromtable . ".fStatusKEY=Status.fStatusKEY ",
         );
 
Index: trunk/MagicSoft/Mars/datacenter/db/opticaldata.php
===================================================================
--- trunk/MagicSoft/Mars/datacenter/db/opticaldata.php	(revision 8135)
+++ trunk/MagicSoft/Mars/datacenter/db/opticaldata.php	(revision 8135)
@@ -0,0 +1,288 @@
+<?php
+{
+    function CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup)
+    {
+        $fromtable="OpticalData";
+
+        $groups = 0;
+        foreach ($checkgroup as $element)
+            if ($element==-1)
+                $groups++;
+
+        $query0 = "SELECT ";
+
+        if ($groups>0)
+        {
+            foreach ($checkgroup as $key => $element)
+                if ($element==-1)
+                    $query0 .=  $key . " as '" . $alias[$key] . "' " . ", ";
+            $query0 .= " COUNT(*) as '# Runs' ";
+        }
+        else
+        {
+            $query0 .= " fTimeStamp as 'Time' ";
+
+            foreach ($_GET as $key => $element)
+                if ($_GET[$key]=="On")
+                    if (empty($checkwhere[$key]) || $checkwhere[$key]==0)
+                        $query0 .= ", " . $key . " as '" . $alias[$key] . "' ";
+        }
+
+        $query0 .= " FROM " . $fromtable;
+
+        foreach ($_GET as $key => $element)
+            if (($_GET[$key]=="On" || $groups>0))// && !empty($joins[$key]))
+                $query0 .= GetJoin($fromtable, $key);
+
+        foreach ($checkwhere as $key => $element)
+        {
+            if (empty($element) || $element<=0)
+                continue;
+
+            if (strpos($query0, " WHERE ")==FALSE)
+                $query0 .= " WHERE ";
+            else
+                if ($element!=-1)
+                    if (strrpos($query0, " AND ")!=strlen($query0)-5)// this if clause doesn't work
+                        $query0 .= " AND ";
+
+            if ($element!=-1)
+                $query0 .= GetCheck($fromtable, $key) . "=" . $element;
+        }
+
+        if (strpos($query0, " WHERE ")==FALSE)
+            $query0 .= " WHERE ";
+        else
+            $query0 .= " AND ";
+
+        if (!empty($_GET["fStartDate"]))
+        {
+            $startdate=substr($_GET["fStartDate"], 0, 10);
+            if ($startdate=="0000-00-00")
+                $query0 .=" fTimestamp >= '" . $startdate . " 00:00:00' ";
+            else
+                $query0 .= " fTimestamp >= ADDDATE('" . $startdate . " 13:00:00', INTERVAL -1 DAY) ";
+        }
+
+        if (!empty($_GET["fStopDate"]))
+        {
+//            if (strpos(strrev($query0), " DNA ")!=0)
+//                $query0 .= " AND ";
+
+            $stopdate=substr($_GET["fStopDate"], 0, 10);
+            $query0 .= " AND fTimestamp < '" . $stopdate . " 13:00:00' ";
+        }
+
+        if ($groups>0)
+        {
+            $query0 .= " GROUP BY ";
+            $num = $groups;
+            foreach ($checkgroup as $key => $element)
+                if ($element==-1)
+                {
+                    $query0 .= GetCheck($fromtable, $key);
+                    if ($num-->1)
+                        $query0 .= ", ";
+                }
+        }
+
+        if (!empty($_GET["fSortBy"]))
+        {
+            $query0 .= " ORDER BY " . substr($_GET["fSortBy"], 0, -1) . " ";
+            if (substr($_GET["fSortBy"], -1)=="-")
+                $query0 .= "DESC";
+        }
+
+        if (empty($_GET["fNumStart"]))
+            $_GET["fNumStart"]=0;
+
+        if (empty($_GET["fSendTxt"]))
+            $query0 .= " LIMIT " . $_GET["fNumStart"] . ", " . $_GET["fNumResults"];
+
+        return $query0;
+    }
+
+    function InitGet($_GET)
+    {
+        // Find out whether it is the first call to the php script
+        $first = empty($_GET["fRunMin"]) && empty($_GET["fRunMax"]);
+
+        if (empty($_GET["fNumResults"]))
+            $_GET["fNumResults"]="20";
+            
+        if (empty($_GET["fTimestamp"]))
+            $_GET["fTimestamp"]=$first?"On":"";
+
+        if (empty($_GET["fExposure"]))
+            $_GET["fExposure"]=$first?"On":"";
+
+        if (empty($_GET["fSkyLevel"]))
+            $_GET["fSkyLevel"]=$first?"On":"";
+
+        if (empty($_GET["fFWHM"]))
+            $_GET["fFWHM"]=$first?"On":"";
+
+        if (empty($_GET["fApertureRadius"]))
+            $_GET["fApertureRadius"]=$first?"On":"";
+
+        if (empty($_GET["fInstrumentalMag"]))
+            $_GET["fInstrumentalMag"]=$first?"On":"";
+
+        if (empty($_GET["fInstrumentalMagErr"]))
+            $_GET["fInstrumentalMagErr"]=$first?"On":"";
+
+        if (empty($_GET["fFitsFileName"]))
+            $_GET["fFitsFileName"]=$first?"On":"";
+
+        if (empty($_GET["fObjectName"]))
+            $_GET["fObjectName"]=$first?"On":"";
+
+        if (empty($_GET["fStatusName"]))
+            $_GET["fStatusName"]=$first?"On":"";
+
+//        if (empty($_GET["fRunStop"]))
+//            $_GET["fRunStop"]="Off";
+
+    }
+
+    function PrintForm($_GET, $host, $user, $pw, $db)
+    {
+        printf("<center>\n");
+        printf("<form action=\"opticaldata.php\" METHOD=\"GET\">\n");
+        printf(" <table>\n");
+        printf("  <tr>\n");
+
+        CheckBox("fTimestamp",         "Time");
+        CheckBox("fExposure",      "Exposure");
+        CheckBox("fSkyLevel",     "Skylevel");
+        CheckBox("fFWHM",          "FWHM");
+
+        printf("  </tr><tr>\n");
+
+        CheckBox("fApertureRadius",     "Aperture radius");
+        CheckBox("fInstrumentalMag",    "instrumental magnitude");
+        CheckBox("fInstrumentalMagErr", "instrum. mag. error");
+
+        printf("  </tr>\n");
+        printf(" </table>\n");
+         printf(" <p>\n");
+
+        // pull down boxes
+
+        printf(" <table>\n");
+        printf("  <tr><td>\n");
+        PrintPullDown($host, $user, $pw, $db, "FitsFile",     "fFitsFileName",     "fFitsFileKEY", "Fits File");
+        printf("  </td><td>\n");
+        PrintPullDown($host, $user, $pw, $db, "Object",      "fObjectName",      "fObjectKEY", "Object Name");
+        printf("  </td><td>\n");
+        PrintPullDown($host, $user, $pw, $db, "Status",  "fStatusName",  "fStatusKEY", "Status Code");
+        printf(" </td></tr></table>\n");
+        printf(" <p>\n");
+
+        if (empty($_GET["fStartDate"]))
+            $timemin = GetMin("fTimestamp", "OpticalData", $host, $user, $pw, $db);
+        else
+            $timemin = $_GET["fStartDate"];
+
+        if (empty($_GET["fStopDate"]))
+            $timemax = GetMax("fTimestamp", "OpticalData", $host, $user, $pw, $db);
+        else
+            $timemax = $_GET["fStopDate"];
+
+        printf("Night&nbsp;(yyyy-mm-dd)&nbsp;from&nbsp;<input name=\"fStartDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">\n", $timemin);
+        printf("to&nbsp;<input name=\"fStopDate\" type=\"text\" size=\"10\" maxlength=\"10\" value=\"%s\">&nbsp;&nbsp;&nbsp;&nbsp;\n", $timemax);
+
+        printf(" Results:\n");
+        printf(" <select name=\"fNumResults\">\n");
+
+        $numres = array("10", "20", "50", "100", "200", "500", "1000", "2000");
+        foreach ($numres as $element)
+        {
+            if ($element==$_GET["fNumResults"])
+                printf("<option value=\"%s\" selected>%3s</option>\n", $element, $element);
+            else
+                printf("<option value=\"%s\">%3s</option>\n", $element, $element);
+        }
+        printf(" </select>\n");
+        printf(" &nbsp;&nbsp;&nbsp;\n");
+
+        ini_set("mysql.trace_mode", "Off");
+        ini_set("display_errors", "Off");
+
+        printf("<input class='Width' type='submit' value='Query Table'>&nbsp;&nbsp;&nbsp;\n");
+        printf("<input class='Width' type='button' value='Reset' onClick='self.location.href=\"opticaldata.php\"'>&nbsp;&nbsp;&nbsp;\n");
+        if (strchr($_SERVER["REQUEST_URI"], '?')!=FALSE)
+            printf("<input class='Width' type='button' value='Get .txt' onClick='self.location.href=\"%s&fSendTxt=1\"'>&nbsp;&nbsp;&nbsp;\n", $_SERVER["REQUEST_URI"]);
+        printf("</form>\n");
+        printf("</center>\n");
+        printf("</td>\n");
+        printf("</tr>\n");
+        printf("<tr class='Block'>\n");
+        printf("<td>\n");
+    }
+
+    function PrintPage($html, $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup)
+    {
+        $db_id = mysql_connect($host, $user, $pw);
+        if ($db_id==FALSE)
+        {
+            printf("mysql_connect returned the following error: %s\n", mysql_error());
+            die("");
+        }
+        mysql_select_db($db);
+        mysql_query("SET BIG_TABLES=1"); // necessary for mySQL <= 4
+
+
+        $query0 = CreateQuery($_GET, $alias, $checkwhere, $checkgroup, $checkstatusgroup);
+
+        $result0 = mysql_query($query0, $db_id);
+
+        if ($result0)
+        {
+            if ($html=="1")
+                PrintMagicTable($result0, $alias, $rightalign, "", "", "", $_GET);
+            else
+                PrintText($result0);
+
+            mysql_free_result($result0);
+        }
+        mysql_close($db_id);
+
+        PrintSubmittedQuery($query0, $html, $db, "old");
+    }
+
+    include ("include.php");
+    include ("db.php");
+    include ("magicdefs.php");
+
+    ini_set("display_errors",   "On");
+    ini_set("mysql.trace_mode", "On");
+
+    if (!empty($_GET["fSendTxt"]))
+    {
+        header("Content-type: application/octet");
+        header("Content-Disposition: attachment; filename=query-result.txt");
+
+        PrintPage("0", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup);
+    }
+    else
+    {
+        echo (file_get_contents("index-header.html"));
+
+        $environment = sizeof($_GET);
+
+        InitGet($_GET);
+        PrintForm($_GET, $host, $user, $pw, $db);
+
+        if ($environment==0)
+            printf("No query submitted yet.<BR>");
+        else
+            PrintPage("1", $host, $user, $pw, $db, $alias, $rightalign, $checkwhere, $checkgroup, $checkstatusgroup);
+
+        echo (file_get_contents("index-footer.html"));
+    }
+
+    ini_set("display_errors",   "Off");
+    ini_set("mysql.trace_mode", "Off");
+}
+?>
Index: trunk/MagicSoft/Mars/datacenter/macros/filloptical.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/filloptical.C	(revision 8135)
+++ trunk/MagicSoft/Mars/datacenter/macros/filloptical.C	(revision 8135)
@@ -0,0 +1,220 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2006
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// filloptical.C
+// =============
+//
+// This macro is used to read the files from KVA containing the results from
+// the optical observations.
+// Remark: Running it from the commandline looks like this:
+//   root -q -l -b filloptical.C+\(\"filename\"\,kFALSE\) 
+//
+// Make sure, that database and password are corretly set.
+//
+// Returns 0 in case of failure and 1 in case of success.
+//
+///////////////////////////////////////////////////////////////////////////
+#include <iostream>
+#include <iomanip>
+#include <fstream>
+
+#include <TEnv.h>
+#include <TRegexp.h>
+
+#include <TSQLRow.h>
+#include <TSQLResult.h>
+
+#include "MDirIter.h"
+#include "MSQLServer.h"
+#include "MSQLMagic.h"
+
+using namespace std;
+
+//
+// insert the entries from this optical data file into the db
+//
+int process(MSQLMagic &serv, TString fname)
+{
+    ifstream fin(fname);
+    if (!fin)
+    {
+        cout << "Could not open file " << fname << endl;
+        return 0;
+    }
+
+    TRegexp regexp("^20[0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9].*$", kFALSE);
+
+    Int_t numexp=0;
+    Int_t numstars=0;
+
+    TString timestamp;
+    TString exposure;
+    TString fitsfile;
+    TString object;
+    TString skylevel;
+    TString fwhm;
+    TString aperturer;
+    TString mag;
+    TString magerr;
+    TString status;
+    TString query;
+    Int_t statuskey;
+    Int_t objectkey;
+    Int_t fitsfilekey;
+    while (1)
+    {
+        TString line;
+        line.ReadLine(fin);
+        if (!fin)
+            break;
+
+        if (line=="KVA_Celestron_ST8  KVA_Celestron_R" || line.IsNull())
+            continue;
+
+        TObjArray *arr = line.Tokenize("  ");
+        if (!line(regexp).IsNull())
+        {
+            if (arr->GetEntries()!=4)
+            {
+                cout << "WARNING: Timestampline with less or more than 4 arguments found " << endl;
+                return 2;
+            }
+            numexp+=1;
+            numstars=0;
+            timestamp =Form("%s %s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
+            exposure   = (*arr)[2]->GetName();
+            fitsfile = (*arr)[3]->GetName();
+            continue;
+        }
+        else
+        {
+            if (arr->GetEntries()!=8)
+            {
+                cout << "WARNING: Objectline with less or more than 8 arguments found " << endl;
+                return 2;
+            }
+
+            numstars+=1;
+            object= Form("%s/%s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
+            skylevel   = (*arr)[2]->GetName();
+            if (skylevel.Contains("-"))
+                skylevel="NULL";
+            fwhm = (*arr)[3]->GetName();
+            if (fwhm=="-" || !fwhm.IsFloat())
+                fwhm="NULL";
+            aperturer = (*arr)[4]->GetName();
+            mag = (*arr)[5]->GetName();
+            if (!mag.IsFloat())
+                mag="NULL";
+            magerr = (*arr)[6]->GetName();
+            if (!magerr.IsFloat())
+                magerr="NULL";
+            status = (*arr)[7]->GetName();
+        }
+        delete arr;
+
+        if (numstars!=0)
+        {
+            /*
+            cout << numexp << "th exposure: star # " << numstars << endl;
+            cout << " timestamp: " << timestamp << endl;
+            cout << " exposure : " << exposure  << endl;
+            cout << " fitsfile : " << fitsfile  << endl;
+            cout << " object   : " << object << endl;
+            cout << " skylevel : " << skylevel  << endl;
+            cout << " fwhm     : " << fwhm      << endl;
+            cout << " aperturer: " << aperturer << endl;
+            cout << " mag      : " << mag << " +/- " << magerr << endl;
+            cout << " status   : " << status    << endl << endl;
+            */
+            statuskey = serv.QueryKeyOfName("Status", status.Data());
+            objectkey = serv.QueryKeyOfName("Object", object.Data());
+            fitsfilekey = serv.QueryKeyOfName("FitsFile", fitsfile.Data());
+            query=Form("fTimeStamp='%s', fExposure=%s, fFitsFileKEY=%d, "
+                       "fObjectKEY=%d, fSkyLevel=%s, fFWHM=%s, "
+                       "fApertureRadius=%s, fInstrumentalMag=%s, "
+                       "fInstrumentalMagErr=%s, fStatusKEY=%d ",
+                       timestamp.Data(), exposure.Data(), fitsfilekey,
+                       objectkey, skylevel.Data(), fwhm.Data(),
+                       aperturer.Data(), mag.Data(), magerr.Data(),
+                       statuskey);
+
+            if (serv.Insert("OpticalData", query)==kFALSE)
+                return 2;
+
+        }
+
+    }
+
+    cout << fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_KVA_C_R.*[.]instr", kFALSE))
+        << ": " << setw(2) << numexp << " exposures, " <<  setw(2) << numstars << " stars" << endl;
+
+    return 1;
+}
+
+// --------------------------------------------------------------------------
+//
+// loop over all files in this path
+//
+int filloptical(TString path, Bool_t dummy=kTRUE)
+{
+    TEnv env("sql.rc");
+
+    MSQLMagic serv(env);
+    if (!serv.IsConnected())
+    {
+        cout << "ERROR - Connection to database failed." << endl;
+        return 0;
+    }
+
+    serv.SetIsDummy(dummy);
+
+    cout << endl;
+    cout << "filloptical" << endl;
+    cout << "-----------" << endl;
+    cout << endl;
+    cout << "Connected to " << serv.GetName() << endl;
+    cout << "Search Path: " << path << endl;
+    cout << endl;
+
+    //get all runbook files in path
+    if (path.EndsWith(".instr"))
+        return process(serv, path);
+
+    //fill entries for each runbook file
+    MDirIter Next(path, "2*_KVA_C_R*.instr", -1);
+    while (1)
+    {
+        TString name = Next();
+        if (name.IsNull())
+            break;
+
+        if (!process(serv, name))
+            return 0;
+    }
+
+    return 1;
+}
