class PointCollectionExecutionTarget:

    def __init__( self, collection_index, ncollections, init_args=None, **cfg_args ):
        self.point_collection = MultiVarPointCollection() 
        self.point_collection.setDataSlice( collection_index, istep=ncollections )
        self.collection_index = collection_index
        self.ncollections = ncollections
        self.init_args = init_args
        self.cfg_args = cfg_args

    def printLogMessage(self, msg_str ):
        print " PointCollectionExecutionTarget %d: %s" % ( self.collection_index, msg_str )
        sys.stdout.flush()      

    def __call__( self, args_queue, result_queue ):
        self.results = result_queue
        self.initialize()
        while True:
            args = list( args_queue.get( True ) )
            self.execute( args )
                
    def initialize( self ):
        self.point_collection.initialize( self.init_args, **self.cfg_args )
        self.point_collection.setDataSlice( self.collection_index, istep=self.ncollections )
        self.results.put( self.packPointsData() )
        self.results.put( self.packVarData() )
                       
    def execute( self, args ):
        self.point_collection.execute( args )
        if args[0] == 'indices':
            data_packet = self.packIndexData()
        elif args[0] == 'points':
            data_packet = self.packPointHeightsData()
        elif args[0] == 'timestep':
            data_packet = self.packVarData()
        elif args[0] == 'ROI':
            data_packet = self.packPointsData()
        data_packet[ 'args' ] = args
        self.results.put( data_packet )

    def packVarData(self):
#        print "Pack VARDATA"; sys.stdout.flush()
        data_packet = ExecutionDataPacket( ExecutionDataPacket.VARDATA, self.collection_index, self.point_collection.getVarData() )
        data_packet[ 'vrange' ] = self.point_collection.getVarDataRange() 
        data_packet[ 'grid' ] = self.point_collection.getGridType()  
        data_packet[ 'nlevels' ] = self.point_collection.getNLevels()
        data_packet[ 'bounds' ] = self.point_collection.getBounds()
        return data_packet

    def packPointsData( self ):
#        print "Pack POINTS"; sys.stdout.flush()
        data_packet = ExecutionDataPacket( ExecutionDataPacket.POINTS, self.collection_index, self.point_collection.getPoints() )
        return data_packet

    def packPointHeightsData( self ):
#        print " ExecutionTarget-%d: packPointHeightsData" % ( self.collection_index )
        data_packet = ExecutionDataPacket( ExecutionDataPacket.HEIGHTS, self.collection_index, self.point_collection.getPointHeights() )
        data_packet[ 'bounds' ] = self.point_collection.getBounds()
        return data_packet

    def packIndexData( self ):
        data_packet = ExecutionDataPacket( ExecutionDataPacket.INDICES, self.collection_index, self.point_collection.getPointIndices() )
        target = self.point_collection.getThresholdTargetType()
#        range_type = 'trange' if ( target == "vardata" ) else "crange"
#        data_packet[ target ] = self.point_collection.getThresholdTargetType() 
        data_packet[ 'target' ] = target
        data_packet[ 'trange' ] = self.point_collection.getThresholdedRange()
        return data_packet
class PointCollectionExecutionTarget:
    def __init__(self,
                 collection_index,
                 ncollections,
                 init_args=None,
                 **cfg_args):
        self.point_collection = MultiVarPointCollection()
        self.point_collection.setDataSlice(collection_index,
                                           istep=ncollections)
        self.collection_index = collection_index
        self.ncollections = ncollections
        self.init_args = init_args
        self.cfg_args = cfg_args

    def printLogMessage(self, msg_str):
        print " PointCollectionExecutionTarget %d: %s" % (
            self.collection_index, msg_str)
        sys.stdout.flush()

    def __call__(self, args_queue, result_queue):
        self.results = result_queue
        self.initialize()
        while True:
            args = list(args_queue.get(True))
            self.execute(args)

    def initialize(self):
        self.point_collection.initialize(self.init_args, **self.cfg_args)
        self.point_collection.setDataSlice(self.collection_index,
                                           istep=self.ncollections)
        self.results.put(self.packPointsData())
        self.results.put(self.packVarData())

    def execute(self, args):
        self.point_collection.execute(args)
        if args[0] == 'indices':
            data_packet = self.packIndexData()
        elif args[0] == 'points':
            data_packet = self.packPointHeightsData()
        elif args[0] == 'timestep':
            data_packet = self.packVarData()
        elif args[0] == 'ROI':
            data_packet = self.packPointsData()
        data_packet['args'] = args
        self.results.put(data_packet)

    def packVarData(self):
        #        print "Pack VARDATA"; sys.stdout.flush()
        data_packet = ExecutionDataPacket(ExecutionDataPacket.VARDATA,
                                          self.collection_index,
                                          self.point_collection.getVarData())
        data_packet['vrange'] = self.point_collection.getVarDataRange()
        data_packet['grid'] = self.point_collection.getGridType()
        data_packet['nlevels'] = self.point_collection.getNLevels()
        data_packet['bounds'] = self.point_collection.getBounds()
        return data_packet

    def packPointsData(self):
        #        print "Pack POINTS"; sys.stdout.flush()
        data_packet = ExecutionDataPacket(ExecutionDataPacket.POINTS,
                                          self.collection_index,
                                          self.point_collection.getPoints())
        return data_packet

    def packPointHeightsData(self):
        #        print " ExecutionTarget-%d: packPointHeightsData" % ( self.collection_index )
        data_packet = ExecutionDataPacket(
            ExecutionDataPacket.HEIGHTS, self.collection_index,
            self.point_collection.getPointHeights())
        data_packet['bounds'] = self.point_collection.getBounds()
        return data_packet

    def packIndexData(self):
        data_packet = ExecutionDataPacket(
            ExecutionDataPacket.INDICES, self.collection_index,
            self.point_collection.getPointIndices())
        target = self.point_collection.getThresholdTargetType()
        #        range_type = 'trange' if ( target == "vardata" ) else "crange"
        #        data_packet[ target ] = self.point_collection.getThresholdTargetType()
        data_packet['target'] = target
        data_packet['trange'] = self.point_collection.getThresholdedRange()
        return data_packet