def testMaxAirmass(self):
     """Test config.maxAirmass
     """
     for maxAirmass in (1.2, 1.3):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxAirmass = maxAirmass
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.airmass > maxAirmass), ())
 def testMaxExposures(self):
     """Test config.maxExposures
     """
     for maxExposures in (0, 6):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxExposures = maxExposures
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         self.assertEqual(len(expInfoList), maxExposures)
 def testQuality(self):
     """Test config.quality
     """
     for quality in (1, 2, 3):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.quality = quality
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.quality < quality), ())
 def testTable(self):
     """Test config.table
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.table = "Bad_table_name_JutmgQEXm76O38VDtcNAICLrtQiSQ64y"
     task = SelectSdssImagesTask(config=config)
     for coordList in [
             None, getCoordList(333.746, -0.63606, 334.522, -0.41341)
     ]:
         filter = "g"
         with self.assertRaises(Exception):
             task.run(coordList, filter)
 def testMaxSky(self):
     """Test config.maxSky
     """
     for maxSky in (5.0e-9, 1.0e-8):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxSky = maxSky
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.sky > maxSky), ())
 def testCamcols(self):
     """Test config.camcols
     """
     for camcols in ((1, 3, 4), (2,)):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.camcols = camcols
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.dataId["camcol"] not in camcols), ())
 def testMaxExposures(self):
     """Test config.maxExposures
     """
     for maxExposures in (0, 6):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxExposures = maxExposures
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         self.assertEqual(len(expInfoList), maxExposures)
 def testFilterValidation(self):
     """Test filter name validation
     """
     coordList = getCoordList(333.7,-0.6,333.71,-0.59)
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     task = SelectSdssImagesTask(config=config)
     for charVal in range(ord("a"), ord("z")+1):
         filter = chr(charVal)
         if filter in ("u", "g", "r", "i", "z"):
             task.run(coordList=coordList, filter=filter)
         else:
             self.assertRaises(Exception, task.run, coordList, filter)
 def testMaxRuns(self):                
     """Test config.maxRuns
     """
     for maxRuns in (0, 2):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxRuns = maxRuns
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         runSet = set(expInfo.dataId["run"] for expInfo in expInfoList)
         self.assertEqual(len(runSet), maxRuns)
 def testMaxRuns(self):
     """Test config.maxRuns
     """
     for maxRuns in (0, 2):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxRuns = maxRuns
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         runSet = set(expInfo.dataId["run"] for expInfo in expInfoList)
         self.assertEqual(len(runSet), maxRuns)
 def testMaxAirmass(self):
     """Test config.maxAirmass
     """
     for maxAirmass in (1.2, 1.3):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxAirmass = maxAirmass
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         self.assertEqual(
             tuple(expInfo for expInfo in expInfoList
                   if expInfo.airmass > maxAirmass), ())
 def testQuality(self):
     """Test config.quality
     """
     for quality in (1, 2, 3):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.quality = quality
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         self.assertEqual(
             tuple(expInfo for expInfo in expInfoList
                   if expInfo.quality < quality), ())
 def testCamcols(self):
     """Test config.camcols
     """
     for camcols in ((1, 3, 4), (2, )):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.camcols = camcols
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         self.assertEqual(
             tuple(expInfo for expInfo in expInfoList
                   if expInfo.dataId["camcol"] not in camcols), ())
 def testMaxSky(self):
     """Test config.maxSky
     """
     for maxSky in (5.0e-9, 1.0e-8):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.maxSky = maxSky
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         self.assertEqual(
             tuple(expInfo for expInfo in expInfoList
                   if expInfo.sky > maxSky), ())
 def testStrip(self):
     """Test config.strip
     """
     for strip in ("S", "N", "Auto", "Both"):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.strip = strip
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
         dataId = {"filter": "g", "patch": "20,5"}
         runArgDict = task._runArgDictFromDataId(dataId)
         expInfoList = task.run(coordList=coordList, **runArgDict).exposureInfoList
         if strip in ("S","N"):
             self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.strip != strip), ())
         elif strip =="Auto":
             self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.strip != 'N'), ())
 def testWholeSky(self):
     """Test whole-sky search (slow so don't do much)
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     config.camcols = (2,)
     config.quality = 1
     config.rejectWholeRuns = False
     task = SelectSdssImagesTask(config=config)
     coordList = None
     filter = "g"
     expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
     self.assertEqual(tuple(expInfo for expInfo in expInfoList if expInfo.quality < config.quality), ())
     print "found %s exposures" % (len(expInfoList),)
     self.assertEqual(tuple(expInfo for expInfo in expInfoList \
         if expInfo.dataId["camcol"] not in config.camcols), ())
 def testCullBlacklisted(self):
     """Test config.cullBlacklisted
     """
     for cullBlacklisted in (False, True):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.quality = 1
         config.cullBlacklisted = cullBlacklisted
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(300,-0.63606,302,-0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
         blacklistedList = tuple(expInfo for expInfo in expInfoList if expInfo.isBlacklisted)
         if cullBlacklisted:
             self.assertEqual(blacklistedList, ())
         else:
             self.assertGreater(len(blacklistedList), 0)
 def testQScore(self):
     """Test QScore sorting
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     config.quality = 1
     config.rejectWholeRuns = False
     task = SelectSdssImagesTask(config=config)
     coordList = getCoordList(333.746,-0.63606,334.522,-0.41341)
     filter = "g"
     expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
     qscoreList = list(expInfo.qscore for expInfo in expInfoList)
     self.assertEqual(qscoreList, sorted(qscoreList))
     bestExp = expInfoList[0]
     worstExp = expInfoList[-1]
     self.assertGreater(worstExp.fwhm, bestExp.fwhm)
     self.assertGreater(worstExp.sky, bestExp.sky)
     self.assertGreater(bestExp.quality, worstExp.quality)
     self.assertEqual(bestExp.quality, 3)
 def testCullBlacklisted(self):
     """Test config.cullBlacklisted
     """
     for cullBlacklisted in (False, True):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.quality = 1
         config.cullBlacklisted = cullBlacklisted
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(300, -0.63606, 302, -0.41341)
         filter = "g"
         expInfoList = task.run(coordList=coordList,
                                filter=filter).exposureInfoList
         blacklistedList = tuple(expInfo for expInfo in expInfoList
                                 if expInfo.isBlacklisted)
         if cullBlacklisted:
             self.assertEqual(blacklistedList, ())
         else:
             self.assertGreater(len(blacklistedList), 0)
 def testWholeSky(self):
     """Test whole-sky search (slow so don't do much)
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     config.camcols = (2, )
     config.quality = 1
     config.rejectWholeRuns = False
     task = SelectSdssImagesTask(config=config)
     coordList = None
     filter = "g"
     expInfoList = task.run(coordList=coordList,
                            filter=filter).exposureInfoList
     self.assertEqual(
         tuple(expInfo for expInfo in expInfoList
               if expInfo.quality < config.quality), ())
     print("found %s exposures" % (len(expInfoList), ))
     self.assertEqual(
         tuple(expInfo for expInfo in expInfoList
               if expInfo.dataId["camcol"] not in config.camcols), ())
    def testConfigValidate(self):
        """Test validation of config
        """
        for maxExposures in (None, 1):
            for maxRuns in (None, 1):
                config = SelectSdssImagesTask.ConfigClass()
                config.database = Database
                config.maxExposures = maxExposures
                config.maxRuns = maxRuns
                if maxExposures and maxRuns:
                    with self.assertRaises(Exception):
                        config.validate()
                else:
                    config.validate()  # should not raise an exception

        config = SelectSdssImagesTask.ConfigClass()
        config.database = Database
        config.table = "invalid*name"
        with self.assertRaises(Exception):
            config.validate()
 def testQScore(self):
     """Test QScore sorting
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     config.quality = 1
     config.rejectWholeRuns = False
     task = SelectSdssImagesTask(config=config)
     coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
     filter = "g"
     expInfoList = task.run(coordList=coordList,
                            filter=filter).exposureInfoList
     qscoreList = list(expInfo.qscore for expInfo in expInfoList)
     self.assertEqual(qscoreList, sorted(qscoreList))
     bestExp = expInfoList[0]
     worstExp = expInfoList[-1]
     self.assertGreater(worstExp.fwhm, bestExp.fwhm)
     self.assertGreater(worstExp.sky, bestExp.sky)
     self.assertGreater(bestExp.quality, worstExp.quality)
     self.assertEqual(bestExp.quality, 3)
 def testStrip(self):
     """Test config.strip
     """
     for strip in ("S", "N", "Auto", "Both"):
         config = SelectSdssImagesTask.ConfigClass()
         config.database = Database
         config.strip = strip
         task = SelectSdssImagesTask(config=config)
         coordList = getCoordList(333.746, -0.63606, 334.522, -0.41341)
         dataId = {"filter": "g", "patch": "20,5"}
         runArgDict = task._runArgDictFromDataId(dataId)
         expInfoList = task.run(coordList=coordList,
                                **runArgDict).exposureInfoList
         if strip in ("S", "N"):
             self.assertEqual(
                 tuple(expInfo for expInfo in expInfoList
                       if expInfo.strip != strip), ())
         elif strip == "Auto":
             self.assertEqual(
                 tuple(expInfo for expInfo in expInfoList
                       if expInfo.strip != 'N'), ())
 def testAcrossWrap(self):
     """Test rejectWholeRuns across the RA 0/360 boundary
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     config.rejectWholeRuns = True
     task = SelectSdssImagesTask(config=config)
     minRa = 359
     maxRa = 1
     coordList = getCoordList(minRa,-0.63606,maxRa,-0.41341)
     filter = "g"
     expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
     runExpInfoDict = dict()
     for expInfo in expInfoList:
         run = expInfo.dataId["run"]
         if run in runExpInfoDict:
             runExpInfoDict[run].append(expInfo)
         else:
             runExpInfoDict[run] = [expInfo]
     
     self.assertEqual(len(runExpInfoDict), 6)
     self.checkExpList(minRa, maxRa, runExpInfoDict)
 def testRejectWholeRuns(self):
     """Test config.rejectWholeRuns
     """
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     config.maxFwhm = 1.25 # make sure to cut out some partial runs due to bad exposures
     config.rejectWholeRuns = True
     task = SelectSdssImagesTask(config=config)
     minRa = 333.746
     maxRa = 334.522
     coordList = getCoordList(minRa,-0.63606,maxRa,-0.41341)
     filter = "g"
     expInfoList = task.run(coordList=coordList, filter=filter).exposureInfoList
     runExpInfoDict = dict()
     for expInfo in expInfoList:
         run = expInfo.dataId["run"]
         if run in runExpInfoDict:
             runExpInfoDict[run].append(expInfo)
         else:
             runExpInfoDict[run] = [expInfo]
     
     self.checkExpList(minRa, maxRa, runExpInfoDict)
    def testAcrossWrap(self):
        """Test rejectWholeRuns across the RA 0/360 boundary
        """
        config = SelectSdssImagesTask.ConfigClass()
        config.database = Database
        config.rejectWholeRuns = True
        task = SelectSdssImagesTask(config=config)
        minRa = 359
        maxRa = 1
        coordList = getCoordList(minRa, -0.63606, maxRa, -0.41341)
        filter = "g"
        expInfoList = task.run(coordList=coordList,
                               filter=filter).exposureInfoList
        runExpInfoDict = dict()
        for expInfo in expInfoList:
            run = expInfo.dataId["run"]
            if run in runExpInfoDict:
                runExpInfoDict[run].append(expInfo)
            else:
                runExpInfoDict[run] = [expInfo]

        self.assertEqual(len(runExpInfoDict), 6)
        self.checkExpList(minRa, maxRa, runExpInfoDict)
    def testRejectWholeRuns(self):
        """Test config.rejectWholeRuns
        """
        config = SelectSdssImagesTask.ConfigClass()
        config.database = Database
        config.maxFwhm = 1.25  # make sure to cut out some partial runs due to bad exposures
        config.rejectWholeRuns = True
        task = SelectSdssImagesTask(config=config)
        minRa = 333.746
        maxRa = 334.522
        coordList = getCoordList(minRa, -0.63606, maxRa, -0.41341)
        filter = "g"
        expInfoList = task.run(coordList=coordList,
                               filter=filter).exposureInfoList
        runExpInfoDict = dict()
        for expInfo in expInfoList:
            run = expInfo.dataId["run"]
            if run in runExpInfoDict:
                runExpInfoDict[run].append(expInfo)
            else:
                runExpInfoDict[run] = [expInfo]

        self.checkExpList(minRa, maxRa, runExpInfoDict)
 def testFilterValidation(self):
     """Test filter name validation
     """
     coordList = getCoordList(333.7, -0.6, 333.71, -0.59)
     config = SelectSdssImagesTask.ConfigClass()
     config.database = Database
     task = SelectSdssImagesTask(config=config)
     for charVal in range(ord("a"), ord("z") + 1):
         filter = chr(charVal)
         if filter in ("u", "g", "r", "i", "z"):
             task.run(coordList=coordList, filter=filter)
         else:
             with self.assertRaises(Exception):
                 task.run(coordList, filter)
# see <http://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import print_function
from builtins import chr
from builtins import range
import unittest

import lsst.utils.tests
from lsst.daf.persistence import DbAuth
import lsst.afw.coord as afwCoord
import lsst.afw.geom as afwGeom
from lsst.obs.sdss.selectSdssImages import SelectSdssImagesTask

Database = "test_select_sdss_images"

config = SelectSdssImagesTask.ConfigClass()

# Some of the tests require loading SDSS images from "lsst-db.ncsa.illinois.edu" and
# require a login name and password. If the test is unable to connect to the external data,
# some of the tests are skipped.
noConnectionStr = ""
noConnection = False
try:
    DbAuth.username(config.host, str(config.port)),
except Exception as e:
    noConnectionStr = (
        "No remote connection to SDSS image database\n"
        "Did not find host={0}, port={1} in your db-auth file;\n"
        "Warning generated: {2} ".format(config.host, str(config.port), e))
    noConnection = True