Index: trunk/MagicSoft/Mars/datacenter/macros/filloptical.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/filloptical.C	(revision 8338)
+++ trunk/MagicSoft/Mars/datacenter/macros/filloptical.C	(revision 8404)
@@ -35,5 +35,7 @@
 // Make sure, that database and password are corretly set.
 //
-// Returns 0 in case of failure and 1 in case of success.
+// Returns 1 in case of success.
+// Returns 2 in case of invalid file or line in file.
+// Returns 3 in case of a missing object name in the database.
 //
 ///////////////////////////////////////////////////////////////////////////
@@ -102,7 +104,5 @@
     TVector3 v;
 
-    cout << fname << endl;
-
-    if (fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_R[_]?[12]?[.]instr", kFALSE)).IsNull())
+    if (fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE)).IsNull())
     {
         cout << "Found not valid file: " << fname << endl;
@@ -115,5 +115,8 @@
         line.ReadLine(fin);
         if (!fin)
+        {
+            cout << numstars << " objects inserted for this exposure. " << endl;
             break;
+        }
 
         if (line.IsNull())
@@ -123,4 +126,5 @@
 
         if (line=="KVA_Celestron_ST8  KVA_Celestron_R" ||
+            line=="KVA_Celestron_ST8  KVA_Celestron_V" ||
             line=="Tuorla_ST1001E  Tuorla_R")
         {
@@ -147,4 +151,6 @@
             }
             numexp+=1;
+            if (numstars)
+                cout << numstars << " objects inserted for this exposure. " << endl;
             numstars=0;
             timestamp =Form("%s %s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
@@ -164,4 +170,5 @@
             }
 
+            //calculation of zd if ra and dec are available
             if (numstars==0)
             {
@@ -174,37 +181,37 @@
 
                 TSQLRow *row=res->Next();
-
                 if (!row)
                 {
-                    cout << "Query failed: " << select << endl;
-                    continue;
+                    cout << "Couldn't get ZD - Query failed: " << select << endl;
+                    cout << "Position of the object is missing in the DB." << endl;
+                    return 3;
                 }
-
-                ra =(*row)[0]?atof((*row)[0]):0;
-                dec=(*row)[1]?atof((*row)[1]):0;
+                else
+                {
+                    ra =atof((*row)[0]);
+                    dec=atof((*row)[1]);
+                    v.SetMagThetaPhi(1, TMath::Pi()/2-(dec*TMath::DegToRad()), ra*TMath::DegToRad()*15);
+                    v *= MAstroSky2Local(t, obs);
+                    zd = v.Theta()*TMath::RadToDeg();
+                }
                 delete res;
-
-                v.SetMagThetaPhi(1, TMath::Pi()/2-(dec*TMath::DegToRad()), ra*TMath::DegToRad()*15);
-                v *= MAstroSky2Local(t, obs);
-
-                zd = v.Theta()*TMath::RadToDeg();
-
             }
 
-            object= Form("%s/%s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
-            skylevel   = (*arr)[2]->GetName();
+            object    = Form("%s/%s", (*arr)[0]->GetName(),(*arr)[1]->GetName());
+            skylevel  = (*arr)[2]->GetName();
+            fwhm      = (*arr)[3]->GetName();
+            aperturer = (*arr)[4]->GetName();
+            mag       = (*arr)[5]->GetName();
+            magerr    = (*arr)[6]->GetName();
+            status    = (*arr)[7]->GetName();
+            //make sure that no nonsense values enter the db
             if (skylevel.Contains("-"))
-                skylevel="NULL";
-            fwhm = (*arr)[3]->GetName();
+                skylevel = "NULL";
             if (fwhm.Contains("-") || !fwhm.IsFloat())
-                fwhm="NULL";
-            aperturer = (*arr)[4]->GetName();
-            mag = (*arr)[5]->GetName();
+                fwhm     = "NULL";
             if (!mag.IsFloat())
-                mag="NULL";
-            magerr = (*arr)[6]->GetName();
+                mag      = "NULL";
             if (!magerr.IsFloat())
-                magerr="NULL";
-            status = (*arr)[7]->GetName();
+                magerr   = "NULL";
             numstars+=1;
         }
@@ -238,24 +245,23 @@
                        "fInstrumentalMagErr=%s, fStatusKEY=%d, fCCDKEY=%d, "
                        "fFilterKEY=%d, fTelescopeKEY=%d, fBandKEY=%d, "
-                       "fZenithDistance=",
+                       "fZenithDistance=%.1f ",
                        timestamp.Data(), exposure.Data(), fitsfilekey,
                        objectkey, skylevel.Data(), fwhm.Data(),
                        aperturer.Data(), mag.Data(), magerr.Data(),
-                       statuskey, ccdkey, filterkey, telkey, bandkey);
-
-            if (ra==0 || dec==0)
+                       statuskey, ccdkey, filterkey, telkey, bandkey, zd);
+
+            /*
+            if (ra==0 || dec==0 || zd==0)
                 query+="NULL";
             else
                 query+=Form("%.1f", zd);
-
+            */
             if (serv.Insert("OpticalData", query)==kFALSE)
                 return 2;
-
-        }
-
-    }
-
-    cout << fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_R[_]?[12]?[.]instr", kFALSE))
-        << ": " << setw(2) << numexp << " exposures, " <<  setw(2) << numstars << " stars" << endl;
+        }
+    }
+
+    cout << fname(TRegexp("20[0-9][0-9]_[0-1][0-9]_[0-3][0-9]_.*_[RV][_]?[12]?[.]instr", kFALSE))
+        << ": " << setw(2) << numexp << " exposures." << endl << endl;
 
     return 1;
