IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 5, 2013, 2:23:09 PM (13 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ipp-20130207/ippToPsps
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130207/ippToPsps

  • branches/eam_branches/ipp-20130207/ippToPsps/jython/ipptopspsdb.py

    r35097 r35235  
    44import sys
    55import os
     6import math
    67import logging
    78
     
    997998
    998999            self.skychunk.boxSize = rs.getDouble(10)
    999             self.skychunk.halfBox = self.skychunk.boxSize/2.0
    1000             self.skychunk.boxSizeWithBorder = self.skychunk.boxSize + (self.skychunk.BORDER * 2)
     1000            # self.skychunk.halfBox = self.skychunk.boxSize/2.0
     1001            # self.skychunk.boxSizeWithBorder = self.skychunk.boxSize + (self.skychunk.BORDER * 2)
    10011002
    10021003            self.skychunk.basePath = rs.getString(11)
     
    10311032    '''
    10321033    Gets id of box with these coords
     1034    XXX EAM : this code implies we can have multiple boxes at the same location for the same
     1035    XXX EAM : skychunk distinguished by having different sizes.  that seems quite bad
    10331036    '''
    10341037    def getBoxId(self, ra, dec):
     1038
     1039        # for the float comparison, need to use a small window
     1040        minRA = ra - 0.01
     1041        maxRA = ra + 0.01
     1042        minDEC = dec - 0.01
     1043        maxDEC = dec + 0.01
    10351044
    10361045        sql = "SELECT id FROM box \
    10371046               WHERE skychunk  = '" + self.skychunk.name + "' \
    1038                AND ra_center = " + str(ra) + " \
    1039                AND dec_center = " + str(dec) + " \
    1040                AND box_side = " + str(self.skychunk.boxSize)
    1041 
     1047               AND ra_center BETWEEN " + str(minRA) + " AND " + str(maxRA) + " \
     1048               AND dec_center BETWEEN " + str(minDEC) + " AND " + str(maxDEC)
     1049
     1050               # AND box_side = " + str(self.skychunk.boxSize)
     1051
     1052        # print "sql for boxid: ", sql
     1053       
    10421054        id = -1
    10431055        try:
     
    10791091    Inserts new box into box table. If it already exisits, it returns existing box id
    10801092    '''
    1081     def isBoxIngested(self, id):
     1093    def isBoxIngested(self, id, host):
    10821094
    10831095        # set the field 'ingested' to 1 for this box
     
    10881100                print "no matching boxes for ", str(id)
    10891101                raise
    1090             status = rs.getInt(1)
     1102            value = rs.getString(1)
     1103        except:
     1104            print "failed to find box for ", str(id)
     1105            raise
     1106
     1107        if value == host:
     1108            return 1
     1109
     1110        if value == "none":
     1111            return 0
     1112
     1113        if value is None:
     1114            return 0
     1115
     1116        print "box ingested host ", value, " does not match db host ", host
     1117        raise
     1118
     1119    '''
     1120    Inserts new box into box table. If it already exisits, it returns existing box id
     1121    '''
     1122    def setIngestedBox(self, id, host):
     1123
     1124        # set the field 'ingested' to 1 for this box
     1125        sql = "update box set ingested = '" + host + "' where id = " + str(id)
     1126        try:
     1127            self.execute(sql)
    10911128        except:
    10921129            print "failed to set box ", str(id), "as ingested"
    10931130            raise
    10941131
    1095         return status
    1096     '''
    1097     Inserts new box into box table. If it already exisits, it returns existing box id
    1098     '''
    1099     def setIngestedBox(self, id):
     1132    '''
     1133    Clear ingested flag for boxes owned by this host
     1134    '''
     1135    def clearIngestedBoxes(self, host):
    11001136
    11011137        # set the field 'ingested' to 1 for this box
    1102         sql = "update box set ingested = 1 where id = " + str(id)
     1138        sql = "update box set ingested = 'none' where ingested = '" + host + "'"
    11031139        try:
    11041140            self.execute(sql)
    11051141        except:
    1106             print "failed to set box ", str(id), "as ingested"
     1142            print "failed to clear ingested state for boxes owned by host ", host
    11071143            raise
    11081144
    11091145    '''
    1110     Inserts new box into box table. If it already exisits, it returns existing box id
    1111     '''
    1112     def clearIngestedBoxes(self):
    1113 
    1114         # set the field 'ingested' to 1 for this box
    1115         sql = "update box set ingested = 0"
    1116         try:
    1117             self.execute(sql)
    1118         except:
    1119             print "failed to clear boxes as ingested"
    1120             raise
    1121 
    1122     '''
    11231146    Inserts new box ids into the pending table
    11241147    '''
    11251148    def insertPending(self, box_id, batchType, ids):
     1149
     1150        # print "starting insert pending"
    11261151
    11271152        # first delete old pending items
     
    11291154                WHERE box_id = " + str(box_id) + " \
    11301155                AND batch_type = '" + batchType + "'")
     1156
     1157        # print "deleted old items"
    11311158
    11321159        for id in ids:
     
    11361163               (" + str(box_id) + ", '" + batchType + "', " + str(id) + ")"
    11371164
     1165            # print "sql: ", sql
    11381166            self.execute(sql)
    11391167
     
    11681196            rs.close()
    11691197        except:
    1170             self.logger.errorPair("No free stripes", "trying to share with other client...")
     1198            self.logger.errorPair("No free stripes", "trying to steal boxes from other client...")
    11711199            sql = "SELECT ra_center, COUNT(*) AS numPending\
    11721200                   FROM box \
     
    11841212                rs.close()
    11851213            except:
    1186                 self.logger.errorPair("No available stripes to share", "no stripe obtained")
     1214                self.logger.errorPair("No outstanding boxes to steal", "no stripe obtained")
     1215
     1216        # tests against ra_center need to use a finite box
     1217        minRA = raCenter - 0.01
     1218        maxRA = raCenter + 0.01
    11871219
    11881220        self.logger.infoPair("Using RA stripe", "%f" % raCenter)
     
    11901222               FROM box \
    11911223               JOIN pending ON (id = box_id) \
    1192                WHERE ra_center = " + str(raCenter) + " \
     1224               WHERE ra_center BETWEEN " + str(minRA) + " AND " + str(maxRA) + " \
    11931225               AND batch_type = '" + batchType + "' \
    11941226               AND skychunk = '" + self.skychunk.name + "' \
     
    12031235
    12041236        self.registerStripe(host, pid, raCenter)
     1237
     1238        self.unlockTables()
     1239
     1240        return ids
     1241
     1242    '''
     1243    Returns ids for all boxes with pending items in a stripe of RA not already being processed
     1244    by another client. If none are available, then it uses the stripe already used by another
     1245    client that has the most pending items
     1246    '''
     1247    def getStripeBoxIdsAnyBatch(self, host, pid):
     1248
     1249        self.execute("LOCK TABLES box READ, pending READ, stripe WRITE, clients WRITE")
     1250
     1251        ids = []
     1252        raCenter = -1.0
     1253        sql = "SELECT ra_center \
     1254               FROM box \
     1255               JOIN pending ON (id = box_id) \
     1256               WHERE skychunk = '" + self.skychunk.name + "' \
     1257               AND ra_center NOT IN \
     1258               (SELECT ra_center \
     1259                FROM stripe \
     1260                WHERE skychunk = '" + self.skychunk.name + "') \
     1261               GROUP BY ra_center LIMIT 1"
     1262
     1263        try:
     1264            rs = self.executeQuery(sql)
     1265            rs.first()
     1266            raCenter = rs.getFloat(1)
     1267            rs.close()
     1268        except:
     1269            self.logger.errorPair("No free stripes", "trying to steal boxes from other client...")
     1270            sql = "SELECT ra_center, COUNT(*) AS numPending\
     1271                   FROM box \
     1272                   JOIN pending ON (id = box_id) \
     1273                   WHERE skychunk = '" + self.skychunk.name + "' \
     1274                   GROUP BY ra_center \
     1275                   ORDER BY numPending \
     1276                   DESC LIMIT 1"
     1277
     1278            try:
     1279                rs = self.executeQuery(sql)
     1280                rs.first()
     1281                raCenter = rs.getFloat(1)
     1282                rs.close()
     1283            except:
     1284                self.logger.errorPair("No outstanding boxes to steal", "no stripe obtained")
     1285
     1286        # tests against ra_center need to use a finite box
     1287        minRA = raCenter - 0.01
     1288        maxRA = raCenter + 0.01
     1289
     1290        self.logger.infoPair("Using RA stripe", "%f" % raCenter)
     1291        sql = "SELECT DISTINCT id \
     1292               FROM box \
     1293               JOIN pending ON (id = box_id) \
     1294               WHERE ra_center BETWEEN " + str(minRA) + " AND " + str(maxRA) + " \
     1295               AND skychunk = '" + self.skychunk.name + "' \
     1296               ORDER BY dec_center DESC"
     1297
     1298        try:
     1299            rs = self.executeQuery(sql)
     1300            while (rs.next()): ids.append(rs.getInt(1))
     1301            rs.close()
     1302        except:
     1303            self.logger.errorPair("Can't get stripe boxes", sql)
    12051304
    12061305        self.unlockTables()
     
    12501349        boxDim['DEC'] = rs.getFloat(2)
    12511350        boxDim['SIDE'] = rs.getFloat(3)
     1351
     1352        # SIDE is the linear full-width of the box
     1353
     1354        # XXX EAM : need to do something special at the poles...
     1355        if (boxDim['DEC'] > 85): raise
     1356
     1357        # a given box centered at RA,DEC with (linear) size of SIDE has the following bounds
     1358        dR = 0.5*boxDim['SIDE'] / math.cos(math.radians(boxDim['DEC']))
     1359        dD = 0.5*boxDim['SIDE']
     1360
     1361        boxDim['minRA']  = boxDim['RA']  - dR
     1362        boxDim['maxRA']  = boxDim['RA']  + dR
     1363        boxDim['minDEC'] = boxDim['DEC'] - dD
     1364        boxDim['maxDEC'] = boxDim['DEC'] + dD
    12521365
    12531366        return boxDim
     
    13631476    Gets all items in the all_pending temporary table within the bounds of this box
    13641477    '''
    1365     def getItemsInThisThisBox(self, ra, dec):
    1366 
    1367         halfSide = self.skychunk.boxSize/2.0
    1368         minRa =  ra-halfSide
    1369         maxRa = ra+halfSide
    1370         minDec = dec-halfSide
    1371         maxDec = dec+halfSide
     1478    def getItemsInThisBox(self, minRA, maxRA, minDEC, maxDEC):
     1479
     1480        # the old linear size is ill-conceived
     1481        ## XX halfSide = self.skychunk.boxSize/2.0
     1482        ## XX minRa =  ra-halfSide
     1483        ## XX maxRa = ra+halfSide
     1484        ## XX minDec = dec-halfSide
     1485        ## XX maxDec = dec+halfSide
    13721486
    13731487        ids = []
     
    13751489        sql = "SELECT DISTINCT stage_id \
    13761490               FROM all_pending \
    1377                WHERE ra_bore BETWEEN " + str(minRa) + " AND " + str(maxRa) + " \
    1378                AND dec_bore BETWEEN " + str(minDec) + " AND " + str(maxDec)
     1491               WHERE ra_bore BETWEEN " + str(minRA) + " AND " + str(maxRA) + " \
     1492               AND dec_bore BETWEEN " + str(minDEC) + " AND " + str(maxDEC)
    13791493
    13801494        try:
Note: See TracChangeset for help on using the changeset viewer.