IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 30, 2012, 2:49:37 PM (14 years ago)
Author:
eugene
Message:

merge changes from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/ippToPsps/jython/gpc1db.py

    r32852 r33638  
    2020    Constructor
    2121    '''
    22     def __init__(self, logger, doc):
    23         super(Gpc1Db, self).__init__(logger, doc, "gpc1database")
     22    def __init__(self, logger, config):
     23        super(Gpc1Db, self).__init__(logger, config, "gpc1database")
    2424
    2525    '''
     
    3232
    3333    '''
    34     Gets a list of ids in this DVO database for this stage, could be cam or staticsky, and a box can be defined with equatorial coordinates
    35     '''
    36     def getIDsInThisDVODbForThisStage(self, dvoDb, batchType, minRA=-999, maxRA=999, minDec=-999, maxDec=999):
    37 
    38         if batchType == "P2": # TODO define these someplace
     34    Gets a list of ids, ra and dec in this DVO database for this stage, could be cam or staticsky, for a box with defined center and side length
     35    '''
     36    def getItemsInThisDVODbForThisStageInThisBox(self, dvoDb, batchType, ra=180., dec=0., side=181.):
     37
     38        halfSide = side/2.0
     39        return self.getItemsInThisDVODbForThisStage(dvoDb, batchType, ra-halfSide, ra+halfSide, dec-halfSide, dec+halfSide)
     40
     41    '''
     42    Gets a list of ids, ra and dec in this DVO database for this stage, could be cam or staticsky, and a box can be defined with equatorial coordinates
     43    NB this uses Bill's potentially temporary 'skycells' database when finding RA/Dec for stacks.
     44    '''
     45    def getItemsInThisDVODbForThisStage(self, dvoDb, batchType, minRA=-999, maxRA=999, minDec=-999, maxDec=999):
     46
     47        self.logger.debugPair("GPC1 RA range", "%.2f -> %.2f" % (minRA, maxRA))
     48        self.logger.debugPair("GPC1 Dec range", "%.2f -> %.2f" % (minDec, maxDec))
     49
     50        rows = []
     51
     52        if batchType == "P2": # TODO define these some place
    3953
    4054            stage = "cam"
    41             sql = "SELECT DISTINCT stage_id \
     55            sql = "SELECT DISTINCT stage_id, DEGREES(ra), DEGREES(decl) \
    4256                   FROM addRun \
    4357                   JOIN minidvodbRun USING(minidvodb_name) \
     
    4660                   JOIN chipRun USING (chip_id) \
    4761                   JOIN rawExp USING (exp_id) \
     62                   JOIN mergedvodbRun using (minidvodb_id) \
     63                   WHERE mergedvodbRun.mergedvodb = '" + dvoDb + "' \
     64                   AND minidvodbRun.state = 'merged' \
     65                   AND mergedvodbRun.state = 'full' \
     66                   AND minidvodbProcessed.fault = 0 \
     67                   AND addRun.stage = '" + stage + "' \
     68                   AND addRun.state = 'full' \
     69                   AND decl BETWEEN RADIANS(" + str(minDec) + ") AND RADIANS(" + str(maxDec) + ") \
     70                   AND ra BETWEEN RADIANS(" + str(minRA) + ") AND RADIANS(" + str(maxRA) + ")"
     71
     72        elif batchType == "ST":       
     73
     74            stage = "staticsky_multi"
     75            sql = "SELECT DISTINCT stack_id, ra, decl FROM staticskyInput \
     76                   JOIN addRun ON(staticskyInput.sky_id = addRun.stage_id) \
     77                   JOIN minidvodbRun USING(minidvodb_name) \
     78                   JOIN minidvodbProcessed USING(minidvodb_id) \
     79                   JOIN  stackRun USING(stack_id) JOIN skycells.skycell USING(skycell_id) \
    4880                   WHERE minidvodbRun.minidvodb_group = '" + dvoDb + "' \
    4981                   AND minidvodbRun.state = 'merged' \
     
    5183                   AND addRun.stage = '" + stage + "' \
    5284                   AND addRun.state = 'full' \
    53                    AND decl BETWEEN RADIANS(" + str(minDec) + ") AND RADIANS(" + str(maxDec) + ") \
    54                    AND ra BETWEEN RADIANS(" + str(minRA) + ") AND RADIANS(" + str(maxRA) + ")"
    55 
    56         elif batchType == "ST":       
    57 
    58             stage = "staticsky"
    59             sql = "SELECT DISTINCT stack_id FROM staticskyInput \
    60                    JOIN addRun ON(staticskyInput.sky_id = addRun.stage_id) \
    61                    JOIN minidvodbRun USING(minidvodb_name) \
    62                    JOIN minidvodbProcessed USING(minidvodb_id) \
    63                    WHERE minidvodbRun.minidvodb_group = '" + dvoDb + "' \
    64                    AND minidvodbRun.state = 'merged' \
    65                    AND minidvodbProcessed.fault = 0 \
    66                    AND addRun.stage = '" + stage + "' \
    67                    AND addRun.state = 'full'"
    68 
    69         try:
    70             rs = self.executeQuery(sql)
     85                   AND decl BETWEEN " + str(minDec) + " AND " + str(maxDec) + " \
     86                   AND ra BETWEEN " + str(minRA) + " AND " + str(maxRA)
     87
     88        try:
     89            rs = self.executeQuery(sql)
     90            while (rs.next()):
     91               rows.append([rs.getInt(1), rs.getFloat(2), rs.getFloat(3)])
     92            rs.close()
    7193        except:
    7294            self.logger.exception("Can't query for ids in DVO")
    73 
    74         ids = []
    75         while (rs.next()):
    76             ids.append(rs.getInt(1))
    77 
    78         rs.close()
    79 
    80         self.logger.debug("Found %d items in DVO database '%s' for stage='%s'" % (len(ids), dvoDb, stage))
    81 
    82         return ids
     95            return rows
     96
     97        self.logger.debug("Found %d items in DVO database '%s' for stage='%s'" % (len(rows), dvoDb, stage))
     98
     99        return rows
    83100
    84101    '''
     
    193210
    194211        try:
    195         # get path to base dir of cmf files
     212            # get path to base dir of cmf files
    196213            path = rs.getString(1)
    197             path = path[0:path.rfind("/")]
    198214        except:
    199215            self.logger.errorPair("No smf files found for cam_id", str(camID) )
     
    204220        if path.startswith("neb"):
    205221
    206             f=os.popen("neb-ls -p "+path+"/%smf")
     222            f=os.popen("neb-ls -p "+path+"%smf")
    207223            for i in f.readlines():
    208224                files.append(i.rstrip())
     
    210226        # or not a neb path
    211227        else:
    212             files = glob.glob(path + "/*.smf")
     228            files = glob.glob(path + ".smf")
    213229
    214230        if len(files) < 1: return None
    215231
    216         return Fits(self.logger, self.doc, files[0]) # TODO just returning first file - check
     232        return Fits(self.logger, self.config, files[0]) # TODO just returning first file - check
    217233
    218234
    219235    '''
    220236    Gets the path to the cmf file for this stack_id
    221     '''
    222     def getStackStageCmf(self, stackID):
    223      
    224         sql = "SELECT staticskyResult.path_base \
    225                FROM staticskyRun \
    226                JOIN staticskyInput USING(sky_id) \
     237   
     238    The logic here is fairly ridiculous due to issues with how GPC1 stores info regarding multi-filter stacks. In partcular, it
     239    is impossible to find a full path for a cmf file for a given stack_id
     240
     241    Instead, we get a path_base to a directory with a collection of cmfs. neb-ls will list 5 files, but a number < 5 may actually exist
     242    meaning that neb-ls -p will fail.
     243
     244    our logic is as follows:
     245   
     246    - use stack_id and the DVO label to find a path base for all cmfs
     247    - loop through all the potential cmfs, run neb-ls -p on each
     248    - if neb-ls -p fails, then the file does not exists and we ignore it
     249    - if neb-ls -p succeeds, we open that cmf file and check the STK_ID value in its header to see if it is the one we want
     250    - if it is, great, exit
     251    - if not, try the next one in the list
     252
     253    '''
     254    def getStackStageCmf(self, dvoDb, stackID):
     255     
     256        '''
     257        sql = "SELECT DISTINCT staticskyResult.path_base \
     258               FROM staticskyRun  \
     259               JOIN staticskyInput USING(sky_id)  \
    227260               JOIN staticskyResult USING(sky_id) \
    228                JOIN stackRun USING(stack_id) \
     261               JOIN stackRun USING(stack_id)  \
    229262               JOIN stackSumSkyfile USING(stack_id) \
    230                WHERE stack_id = %s" % stackID
    231 
     263               JOIN addRun ON sky_id = stage_id \
     264               JOIN minidvodbRun USING (minidvodb_name) \
     265               WHERE stack_id = " + str(stackID) + " \
     266               AND minidvodbRun.minidvodb_group = '" + dvoDb + "' \
     267               AND minidvodbRun.state = 'merged' \
     268               AND stage = 'staticsky_multi'"
     269        '''
     270
     271        sql = "SELECT DISTINCT staticskyResult.path_base \
     272               FROM staticskyRun  \
     273               JOIN staticskyInput USING(sky_id)  \
     274               JOIN staticskyResult USING(sky_id) \
     275               JOIN stackRun USING(stack_id)  \
     276               JOIN stackSumSkyfile USING(stack_id) \
     277               JOIN addRun ON sky_id = stage_id \
     278               JOIN minidvodbRun USING (minidvodb_name) \
     279               JOIN mergedvodbRun using (minidvodb_id) \
     280               WHERE stack_id = " + str(stackID) + " \
     281               AND minidvodbRun.state = 'merged' \
     282               AND mergedvodbRun.state = 'full' \
     283               AND mergedvodbRun.mergedvodb = '" + dvoDb + "' \
     284               AND stage = 'staticsky_multi'"
     285
     286
     287        # get single path base for the directory containing all cmf files, one of which is hopefully for our stack_id
    232288        try:
    233289            rs = self.executeQuery(sql)
     
    235291            pathBase = rs.getString(1)
    236292
    237             # now find ALL cmf files at this path_base as there can be more than one
    238             f=os.popen("neb-ls -p " + pathBase + "%cmf")
    239             for i in f.readlines():
    240 
    241                 path = i.rstrip()
    242                 fits = Fits(self.logger, self.doc, path)
    243 
    244                 # we need to check if this is the correct cmf file, and if so then break and return
    245                 if fits.getPrimaryHeaderValue("STK_ID") == str(stackID):
    246                     return fits
    247 
    248         except:
    249             self.logger.exception("Can't query for stack cmfs")
    250 
    251         self.logger.error("Could not find stack cmf")
     293            # now find and loop through all cmf files at this path_base
     294            files=os.popen("neb-ls " + pathBase + "%cmf")
     295            for i in files.readlines():
     296
     297                nebPath = i.rstrip()
     298
     299                # now attempt to run 'neb-ls -p' to actually read this cmf file
     300                try:
     301                    self.logger.debug("Trying to neb-ls -p on '" + nebPath + "'")
     302                    paths=os.popen("neb-ls -p " + nebPath)
     303                    path = paths.readline().rstrip()
     304                    if len(path) < 1:
     305                       self.logger.debug("zero length path - skipping")
     306                       raise
     307
     308                    # if we get here, then the cmf is readable, now check the stack_id
     309                    fits = Fits(self.logger, self.config, path)
     310                    if fits.getPrimaryHeaderValue("STK_ID") == str(stackID):
     311                        # we have the right file!
     312                        self.logger.debug("SUCCESS!: %s == %s"% (fits.getPrimaryHeaderValue("STK_ID"), str(stackID)))
     313                        return fits
     314                    else:
     315                        # this is not the correct file
     316                        self.logger.debug("STACK ID: %s != %s"% (fits.getPrimaryHeaderValue("STK_ID"), str(stackID)))
     317
     318                # an exception here means that nebulous could not read this file, so we just skip it
     319                except:
     320                    self.logger.debug("NEB FAILED SKIPPING")
     321                    pass
     322
     323        except:
     324            self.logger.errorPair("Can't query for stack cmfs", "stack_id = %d" % stackID)
     325
     326        self.logger.errorPair("Couldn't find stack cmf for", "stack_id = %d" % stackID)
    252327        return None
    253328
Note: See TracChangeset for help on using the changeset viewer.