#!/usr/bin/env jython

import sys

'''
A class encapsulating a ippToPsps processing skychunk. This information
is stored in the 'skychunk' table of the ipptopsps database
'''
class Skychunk(object):

    '''
    Constructor

    Basically reads the entire skychunk and stores values to class variables
    '''
    def __init__(self, logger):

        # name of skychunk to work on (edit / all have special meanings)
        self.name = sys.argv[1]

        self.createNewSkychunk = False
        self.rotateSkychunks = False

        # a new skychunk?
        if self.name == "edit": self.createNewSkychunk = True

        # should we rotate skychunks for this program?
        if self.name == "all": self.rotateSkychunks = True

        print "name: ", self.name
        print "new: ", self.createNewSkychunk
        print "all: ", self.rotateSkychunks

        self.logger = logger

        # this is the border (in degrees) that we place around any loading box of 
        # PS1 pointings to ensure we pull a large enough area out of DVO

        # XXX this is a poor place to put this information -- it is completely gpc-specific.  
        # probably should go elsewhere
        self.BORDER = 1.65
        self.isLoaded = False

    '''
    Prints everything for this skychunk
    '''
    def printAll(self):

        self.logger.infoTitle("Skychunk")

        try:
            self.logger.infoSeparator()
            self.logger.infoPair("Skychunk name", self.name)
            self.logger.infoPair("Survey", self.survey)
            self.logger.infoPair("Publishing to PSPS as survey", self.pspsSurvey)
            self.logger.infoPair("Loading epoch", self.epoch)
            self.logger.infoPair("Data release", "%d" % self.dataRelease)
            for batchType in self.batchTypes: self.logger.infoPair("Queuing batch type", batchType)
        except:
            pass

        self.printDvoInfo()
        self.printDatastoreInfo()
        self.printBoxCoords()
        self.printDeletionPolicy()
        try: self.logger.infoSeparator()
        except: pass

    '''
    Queuing this batch type?
    '''
    def queuingThisBatchType(self, batchType):
       if batchType in self.batchTypes: return 1
       return 0

    '''
    Creates a logger object and returns it
    '''
    def getLogger(self, host, pid, stdout=1, sendToFile=0):
        
        logging.setLoggerClass(PSLogger)
        self.logger = logging.getLogger(self.programName)
        self.logger.setup(self.programName, self.logPath, self.name, host, pid, stdout, sendToFile)

        return self.logger

    '''
    Prints the currently set DVO info
    '''
    def printDvoInfo(self):

        try:
            self.logger.infoPair("DVO label", self.dvoLabel)
            self.logger.infoPair("DVO location", self.dvoLocation)
        except:
            pass
   
    '''
    Prints datastore info
    '''
    def printDatastoreInfo(self):

        try:
            self.logger.infoBool("Datastore publishing?", self.datastorePublishing)
            if self.datastorePublishing:
                self.logger.infoPair("Datastore type", self.datastoreProduct)
                self.logger.infoPair("Datastore product", self.datastoreType)
        except:
            pass
   
    '''
    Prints the current deletion policy
    '''
    def printDeletionPolicy(self):

        try:
            self.logger.infoBool("Deleting local?", self.deleteLocal)
            self.logger.infoBool("Deleting datastore?", self.deleteDatastore)
            self.logger.infoBool("Deleting DXLayer?", self.deleteDxLayer)
        except:
            pass
   
    '''
    Prints the currently set RA/Dec bounding box
    '''
    def printBoxCoords(self):

        try:
            self.logger.infoPair("RA limits", "%.1f -> %.1f" % (self.minRa, self.maxRa))
            self.logger.infoPair("Dec limits", "%.1f -> %.1f" % (self.minDec, self.maxDec))
            self.logger.infoPair("Loading box size", "%.1f degrees" % self.boxSize)
        except:
            pass
