From c4073d86c25aefe404efa30fed493e3b88b577ba Mon Sep 17 00:00:00 2001
From: Ger van Diepen <diepen@astron.nl>
Date: Fri, 24 Feb 2012 15:35:16 +0000
Subject: [PATCH] Task #2812 Added handling of source info

---
 CEP/Imager/ImageLofar/src/addImagingInfo | 61 +++++++++++++++++++-----
 1 file changed, 48 insertions(+), 13 deletions(-)

diff --git a/CEP/Imager/ImageLofar/src/addImagingInfo b/CEP/Imager/ImageLofar/src/addImagingInfo
index ebfcdf9f56c..70e110c9b70 100755
--- a/CEP/Imager/ImageLofar/src/addImagingInfo
+++ b/CEP/Imager/ImageLofar/src/addImagingInfo
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 
 import pyrap.tables as pt
-##import lofar.parmdb as pdb
+import lofar.parmdb as pdb
 
 """ Add a subtable of an MS to the image """
 def addSubTable (image, msName, subName, removeColumns=[]):
@@ -21,9 +21,9 @@ def addSubTable (image, msName, subName, removeColumns=[]):
         subNameOut = "LOFAR_" + subNameOut
     subtab = sel.copy (image.name() + "/" + subNameOut, deep=True)
     image.putkeyword (subNameOut, subtab)
+    print "Added subtable", subNameOut, "containing", subtab.nrows(), "rows"
     subtab.close()
     sel.close()
-    print "Added subtable", subNameOut
 
 """ Create the empty LOFAR_QUALITY subtable """
 def addQualityTable (image):
@@ -32,7 +32,7 @@ def addQualityTable (image):
                    "QUALITY_MEASURE string, VALUE string, FLAG_ROW bool")
     image.putkeyword ("LOFAR_QUALITY", tab)
     tab.close()
-    print "Added subtable LOFAR_QUALITY"
+    print "Added subtable LOFAR_QUALITY containing 0 rows"
 
 """ Create the LOFAR_ORIGIN subtable and fill from all MSs """
 def addOriginTable (image, msNames):
@@ -87,20 +87,21 @@ def addOriginTable (image, msNames):
             except:
                 pass
         subtab.putcell ("SUBBAND", i, subband)
+    # Ready
     subtab.close()
     sel.close()
-    print "Added subtable LOFAR_ORIGIN"
+    print "Added subtable LOFAR_ORIGIN containing", len(msNames), "rows"
 
 """ Create the LOFAR_SOURCE subtable and fill from the SourceDB """
-def addSourceTable (image, sourcedbName):
+def addSourceTable (image, sourcedbName, times):
     # Create the table using TaQL.
     tab = pt.taql ("create table '" + image.name() + "/LOFAR_SOURCE' " + 
                    "SOURCE_ID int, \TIME double, INTERVAL double, " +
-                   "NUM_LINES int, NAME string, CODE string, " +
+                   "NUM_LINES int, NAME string, " +
                    "DIRECTION double shape=[2], " +
                    "PROPER_MOTION double shape=[2], " +
                    "FLUX double shape=[4], " +
-                   "SPECTRAL_INDEX double, " +
+                   "SPINX double, " +
                    "SHAPE double shape=[3]")
     tab.putcolkeyword ("TIME", "QuantumUnits", ["s"])
     tab.putcolkeyword ("INTERVAL", "QuantumUnits", ["s"])
@@ -112,10 +113,42 @@ def addSourceTable (image, sourcedbName):
     tab.putcolkeyword ("DIRECTION", "MEASINFO", {"Ref":"J2000", "type":"direction"})
     image.putkeyword ("LOFAR_SOURCE", tab)
     # Get all parameters from the source parmdb.
-##    sourcedb = pdb.parmdb(sourcedbName)
-##    names = pdb.getNames ("*RA")
+    midtime = (times[0] + times[1]) / 2
+    inttime = times[1] - times[0]
+    sourcedb = pdb.parmdb(sourcedbName)
+    # Get all source names by getting the Ra parms from DEFAULTVALUES
+    names = [name[3:] for name in sourcedb.getDefNames ("Ra:*")]
+    values = sourcedb.getDefValues()
+    sourcedb = 0   # close
+    row = 0
+    tab.addrows (len(names))
+    # Add the info of all sources.
+    # The field names below are as used in SourceDB.
+    fldnames = ["Ra", "Dec", "I", "Q", "U", "V", "SpectralIndex:0",
+                "Orientation", "MajorAxis", "MinorAxis"]
+    vals = [0. for fld in fldnames]
+    for name in names:
+        for i in range(len(fldnames)):
+            key = fldnames[i] + ":" + name
+            if values.has_key (key):
+                vals[i] = values[key][0][0]
+            else:
+                vals[i] = 0.
+        tab.putcell ("SOURCE_ID", row, row)
+        tab.putcell ("TIME", row, midtime);
+        tab.putcell ("INTERVAL", row, inttime);
+        tab.putcell ("NUM_LINES", row, 0);
+        tab.putcell ("NAME", row, name);
+        tab.putcell ("DIRECTION", row, vals[:2]);
+        tab.putcell ("PROPER_MOTION", row, (0.,0.));
+        tab.putcell ("FLUX", row, vals[2:6]);
+        tab.putcell ("SPINX", row, vals[6]);
+        tab.putcell ("SHAPE", row, vals[7:10]);
+        print name, vals
+        row += 1
+    # Ready.
     tab.close()
-    print "Added subtable LOFAR_SOURCE"
+    print "Added subtable LOFAR_SOURCE containing", row, "rows"
 
 """ Update times and frequencies in the LOFAR_OBSERVATION subtable """
 def updateObsTable (image):
@@ -142,6 +175,7 @@ def updateObsTable (image):
     obstab.close()
     oritab.close()
     print "Updated subtable LOFAR_OBSERVATION"
+    return (mintime[0], maxtime[0])
 
 """ Add all imaging info """
 def addImagingInfo (imageName, msNames, sourcedbName):
@@ -159,9 +193,9 @@ def addImagingInfo (imageName, msNames, sourcedbName):
     # Create the LOFAR_ORIGIN subtable from all MSs.
     addOriginTable (image, msNames)
     # Update times/frequencies in the LOFAR_OBSERVATION table.
-    updateObsTable (image)
+    times = updateObsTable (image)
     # Add the LOFAR_SOURCE table.
-    addSourceTable (image, sourcedbName)
+    addSourceTable (image, sourcedbName, times)
     # Flush and close the image.
     image.close()
 
@@ -171,7 +205,8 @@ if __name__ == "__main__":
     if len(sys.argv) < 4:
         print "Insufficient arguments; run as:"
         print "   addImagingInfo image sourcedb ms1 ms2 ..."
-        print "      image        name of image"
+        print "      image        name of the image"
+        print "      sourcedb     name of SourceDB containing the sources found"
         print "      ms1 ms2 ...  names of MSs image is made of"
         print "                   the names can be individual arguments or a"
         print "                   comma-separated list of names (or a mix)"
-- 
GitLab