From c9af79313d187e69a0a37b68e89cb6c0d3a74772 Mon Sep 17 00:00:00 2001
From: Ger van Diepen <diepen@astron.nl>
Date: Wed, 26 Jan 2011 13:14:42 +0000
Subject: [PATCH] bug 1634: First version

---
 CEP/pyparmdb/src/gsm.py | 203 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 203 insertions(+)
 create mode 100755 CEP/pyparmdb/src/gsm.py

diff --git a/CEP/pyparmdb/src/gsm.py b/CEP/pyparmdb/src/gsm.py
new file mode 100755
index 00000000000..3f2952c32d3
--- /dev/null
+++ b/CEP/pyparmdb/src/gsm.py
@@ -0,0 +1,203 @@
+#!/usr/bin/env python
+
+import sys
+
+# ra and dec both in degrees
+# integrated flux [in Jy] above which the sources will be selected
+# You should know that:
+#  cat_id = 3 => NVSS
+#  cat_id = 4 => VLSS
+#  cat_id = 5 => WENSS
+def gsmSelect (outfile, ra_st, ra_end, dec_min, dec_max, fluxi_min, cat_ids):
+
+    import os, errno
+    import monetdb.sql as db
+    from monetdb.sql import Error as Error
+    import logging
+
+    db_type = "MonetDB"
+    db_host = "ldb001"
+    db_port = 50000
+    db_dbase = "gsm"
+    db_user = "gsm"
+    db_passwd = "msss"
+    #db_lang = "sql"
+
+    if (ra_st <= ra_end):
+        ra_where = 'c1.ra BETWEEN %f AND %f' % (ra_st, ra_end)
+    else:
+        ra_where = '(c1.ra BETWEEN %f AND 360 OR c1.ra BETWEEN 0 AND %f)' % (ra_st, ra_end)
+
+    conn = db.connect(hostname=db_host \
+                          ,port=db_port \
+                          ,database=db_dbase \
+                          ,username=db_user \
+                          ,password=db_passwd \
+                          )
+
+    # Remove output file if already existing.
+    if os.path.isfile(outfile):
+        os.remove(outfile)
+
+    try:
+        cursor = conn.cursor()
+        # This query concatenates the requested columns per row in a single string in the correct BBS format.
+        cursor.execute(#"COPY " + \
+                  "SELECT t.line " + \
+                  "  FROM (SELECT CAST('format = Name, Type, Ra, Dec, I, Q, U, V, MajorAxis, MinorAxis, Orientation, ReferenceFrequency=\\'60e6\\', SpectralIndexDegree=\\'0\\', SpectralIndex:0=\\'0.0\\', SpectralIndex:1=\\'0.0\\'' AS VARCHAR(300)" + \
+                  "                   ) AS line " + \
+                  "        UNION " + \
+                  "        SELECT CAST(CONCAT(t0.catsrcname, CONCAT(',',  " + \
+                  "                    CONCAT(t0.src_type, CONCAT(',',  " + \
+                  "                    CONCAT(ra2bbshms(t0.ra), CONCAT(',',  " + \
+                  "                    CONCAT(decl2bbsdms(t0.decl), CONCAT(',',  " + \
+                  "                    CONCAT(t0.i, CONCAT(',',  " + \
+                  "                    CONCAT(t0.q, CONCAT(',',  " + \
+                  "                    CONCAT(t0.u, CONCAT(',',  " + \
+                  "                    CONCAT(t0.v, CONCAT(',',  " + \
+                  "                    CONCAT(t0.MajorAxis, CONCAT(',',  " + \
+                  "                    CONCAT(t0.MinorAxis, CONCAT(',',  " + \
+                  "                    CONCAT(t0.Orientation, CONCAT(',',  " + \
+                  "                    CONCAT(t0.ReferenceFrequency, CONCAT(',',  " + \
+                  "                    CONCAT(t0.SpectralIndexDegree, CONCAT(',',  " + \
+                  "                    CONCAT(t0.SpectralIndex_0, CONCAT(',', " + \
+                  "                           t0.SpectralIndex_1)))))))))))))))))))))))))))" + \
+                  "                          ) AS VARCHAR(300)) AS line " + \
+                  "          FROM (SELECT CAST(TRIM(c1.catsrcname) AS VARCHAR(20)) AS catsrcname " + \
+                  "                      ,CASE WHEN c1.pa IS NULL " + \
+                  "                            THEN CAST('POINT' AS VARCHAR(20)) " + \
+                  "                            ELSE CAST('GAUSSIAN' AS VARCHAR(20)) " + \
+                  "                       END AS src_type " + \
+                  "                      ,CAST(c1.ra AS VARCHAR(20))  AS ra " + \
+                  "                      ,CAST(c1.decl AS VARCHAR(20)) AS decl " + \
+                  "                      ,CAST(c1.i_int_avg AS VARCHAR(20)) AS i " + \
+                  "                      ,CAST(0 AS VARCHAR(20)) AS q " + \
+                  "                      ,CAST(0 AS VARCHAR(20)) AS u " + \
+                  "                      ,CAST(0 AS VARCHAR(20)) AS v " + \
+                  "                      ,CASE WHEN c1.pa IS NULL " + \
+                  "                            THEN CAST('' AS VARCHAR(20)) " + \
+                  "                            ELSE CASE WHEN c1.major IS NULL " + \
+                  "                                      THEN CAST('' AS VARCHAR(20)) " + \
+                  "                                      ELSE CAST(c1.major AS varchar(20)) " + \
+                  "                                 END " + \
+                  "                       END AS MajorAxis " + \
+                  "                      ,CASE WHEN c1.pa IS NULL " + \
+                  "                            THEN CAST('' AS VARCHAR(20)) " + \
+                  "                            ELSE CASE WHEN c1.minor IS NULL " + \
+                  "                                      THEN CAST('' AS VARCHAR(20)) " + \
+                  "                                      ELSE CAST(c1.minor AS varchar(20)) " + \
+                  "                                 END " + \
+                  "                       END AS MinorAxis " + \
+                  "                      ,CASE WHEN c1.pa IS NULL " + \
+                  "                            THEN CAST('' AS VARCHAR(20)) " + \
+                  "                            ELSE CAST(c1.pa AS varchar(20)) " + \
+                  "                       END AS Orientation " + \
+                  "                      ,CAST(c1.freq_eff AS VARCHAR(20)) AS ReferenceFrequency " + \
+                  "                      ,CASE WHEN si.spindx_degree IS NULL " + \
+                  "                            THEN CAST('' AS VARCHAR(20)) " + \
+                  "                            ELSE CAST(si.spindx_degree AS VARCHAR(20)) " + \
+                  "                       END AS SpectralIndexDegree " + \
+                  "                      ,CASE WHEN si.spindx_degree IS NULL  " + \
+                  "                            THEN CASE WHEN si.c0 IS NULL " + \
+                  "                                      THEN CAST(0 AS varchar(20)) " + \
+                  "                                      ELSE CAST(si.c0 AS varchar(20)) " + \
+                  "                                 END " + \
+                  "                            ELSE CASE WHEN si.c0 IS NULL " + \
+                  "                                      THEN CAST('' AS varchar(20)) " + \
+                  "                                      ELSE CAST(si.c0 AS varchar(20)) " + \
+                  "                                 END " + \
+                  "                       END AS SpectralIndex_0 " + \
+                  "                      ,CASE WHEN si.c1 IS NULL  " + \
+                  "                            THEN CAST('' AS varchar(20)) " + \
+                  "                            ELSE CAST(si.c1 AS varchar(20)) " + \
+                  "                       END AS SpectralIndex_1 " + \
+                  "                  FROM catalogedsources c1 " + \
+                  "                       LEFT OUTER JOIN spectralindices si ON c1.catsrcid = si.catsrc_id " + \
+                  "                 WHERE c1.cat_id IN %s " + \
+                  "                   AND %s " + \
+                  "                   AND c1.decl BETWEEN %f AND %f " + \
+                  "                   AND c1.i_int_avg > %f " + \
+                  "               ) t0 " + \
+                  "       ) t ", (str(cat_ids), ra_where, decl_min, decl_max, fluxi_min))
+        y = cursor.fetchall()
+        cursor.close()
+        #print "y:", y
+
+    except db.Error, e:
+        logging.warn("writeBBSFile %s failed " % (outfile))
+        logging.warn("Failed on query in writeBBSFile() for reason: %s " % (e))
+        logging.debug("Failed writeBBSFile() select query: %s" % (e))
+
+        file = open(outfile,'w')
+        for i in range(len(y)):
+            file.write(y[i][0] + '\n')
+        file.close()
+        print "BBS File written in: ", outfile
+
+
+def gsmMain (name, argv):
+    if len(argv) < 5:
+        print ''
+        print 'Insufficient arguments given; run as:'
+        print ''
+        print '   %s outfile stRA endRA stDEC endDEC [minFluxI [catalogs]]' % name
+        print ''
+        print '      outfile    path-name of the output file'
+        print '                 It will be overwritten if already existing'
+        print '      stRA       start Right Ascension (J2000, degrees)'
+        print '      endRA      end   Right Ascension (J2000, degrees)'
+        print '         stRA can be > endRA indicating crossing 360 deg'
+        print '      minDEC     start Declination (J2000, degrees)'
+        print '      maxDEC     end   Declination (J2000, degrees)'
+        print '      minFluxI   minimum flux (integrated Stokes I in Jy)'
+        print '                 default = 0.5'
+        print '      catalogs   names of catalogs to search (case-insensitive)'
+        print '                 (NVSS, VLSS, and/or WENSS)'
+        print '                 If multiple, separate by commas'
+        print '                 Default is WENSS'
+        return 0
+    outfile = argv[0]
+    stRA    = float(argv[1])
+    endRA   = float(argv[2])
+    stDEC   = float(argv[3])
+    endDEC  = float(argv[4])
+    for ra in [stRA,endRA]:
+        if ra < 0  or  ra > 360:
+            print 'RA', ra, 'is invalid: <0 or >360 degrees'
+            return 1
+    for dec in [stDEC,endDEC]:
+        if dec < -90  or  dec > 90:
+            print 'DEC', dec, 'is invalid: <-90 or >90 degrees'
+            return 1
+    if stDEC > endDEC:
+        print 'invalid DEC: start', stDEC, '> end', endDEC
+        return 1
+    minFlux = 0.5
+    if len(argv) > 5:
+        minFlux = float(argv[5])
+    #  cat_id = 3 => NVSS
+    #  cat_id = 4 => VLSS
+    #  cat_id = 5 => WENSS
+    cats = ['WENSS']
+    if len(argv) > 6:
+        cats = argv[6].split (sep=',')
+    cat_ids = []
+    for cat in cats:
+        cat = cat.upper()
+        if cat == 'NVSS':
+            cat_ids.append (3)
+        elif cat == 'VLSS':
+            cat_ids.append (4)
+        elif cat == 'WENSS':
+            cat_ids.append (5)
+        else:
+            print cat, 'is an invalid catalog name'
+            return 1
+    # Do the selection and create the makesourcedb file.
+    gsmSelect (outfile, stRA, endRA, stDEC, endDEC, minFlux, cat_ids)
+    return 0
+
+
+# This is the main entry.
+if __name__ == "__main__":
+    sys.exit (gsmMain (sys.argv[0], sys.argv[1:]))
-- 
GitLab