def ParallelFocalAnalysis(connectDict, nodeDatasets):
    """

    """

    master = psqlLib(connectDict)
    nodeRasterTableIds = master.PartitionRaster(connectDict["raster_table"],
                                                len(connectDict["nodes"]))

    nodeQueries = []
    for n, node in enumerate(nodeDatasets['nodes']):

        focalQuery = """SELECT st_mapalgebrafctngb(rast, 1, NULL, 1, 1, 'st_mean4ma(double precision[][][],text,text[])'::regprocedure, 'ignore', NULL) as rast FROM {raster_table}""".format(
            **nodeDatasets)

    if len(connectDict["nodes"]) > 1:
        whereQuery = """WHERE rid BETWEEN {min} AND {max} """.format(
            **nodeRasterTableIds[n])
    else:
        whereQuery = ""

    query = focalQuery + whereQuery
    nodeQueries.append(query)

    return nodeQueries
def ParallelReclassification(connectDict, nodeDatasets, pixelValue,
                             reclassValue):
    """
    Function for reclassifying the raster dataset. Returning raster objects.
    """

    master = psqlLib(connectDict)
    nodeRasterTableIds = master.PartitionRaster(connectDict["raster_table"],
                                                len(connectDict["nodes"]))

    nodeQueries = []
    for n, node in enumerate(nodeDatasets['nodes']):

        reclassQuery = """With analytic as ( SELECT rid, ST_Reclass(rast, 1, '%s:%s', '8BUI', 0) as rast """ % (
            pixelValue, reclassValue)
        fromQuery = """FROM {raster_table}) SELECT rid FROM analytic """.format(
            **nodeDatasets)
        if len(connectDict["nodes"]) > 1:
            whereQuery = """WHERE rid BETWEEN {min} AND {max} """.format(
                **nodeRasterTableIds[n])
        else:
            whereQuery = ""

        query = reclassQuery + fromQuery + whereQuery
        nodeQueries.append(query)

    return nodeQueries
def ParallelRasterAdd(connectDict, nodeDatasets):
    """
    Function for adding two raster datasets together
    """

    master = psqlLib(connectDict)
    nodeRasterTableIds = master.PartitionRaster(connectDict["raster_table"],
                                                len(connectDict["nodes"]))

    nodeQueries = []
    for n, node in enumerate(nodeDatasets['nodes']):

        selectStatement = """SELECT ST_MapAlgebra(r1.rast, 1, r2.rast, 1, '[rast1]*[rast2]', '8BUI') as rast """
        fromStatement = """FROM {raster_table} r1, {raster_table} r2 """.format(
            **nodeDatasets)

        if len(connectDict["nodes"]) > 1:
            whereStatement = """ WHERE r.rid BETWEEN {min} AND {max} """.format(
                **nodeRasterTableIds[n])
        else:
            whereStatement = ""

        query = selectStatement.replace("\n",
                                        "") + fromStatement + whereStatement
        nodeQueries.append(query)

    return nodeQueries
def ParallelZonalAnalysis(connectDict, nodeDatasets):
    """
    Function for counting the number of pixels in a geographic feature
    """

    master = psqlLib(connectDict)
    nodeRasterTableIds = master.PartitionRaster(connectDict["raster_table"],
                                                len(connectDict["nodes"]))

    #print(nodeRasterTableIds)
    #print(nodeDatasets)

    nodeQueries = []
    for n, node in enumerate(nodeDatasets['nodes']):

        selectStatement = """
        SELECT p.gid, p.name, (ST_SummaryStatsAgg(ST_Clip(r.rast, p.geom), 1, True)).* 
        FROM {boundary_table} p inner join {raster_table} r on ST_Intersects(r.rast, p.geom) """.format(
            **nodeDatasets)
        if len(connectDict["nodes"]) > 1:
            whereStatement = """ WHERE r.rid BETWEEN {min} AND {max} """.format(
                **nodeRasterTableIds[n])
        else:
            whereStatement = ""
        groupStatement = """ GROUP BY p.gid, p.name """
        query = selectStatement.replace("\n",
                                        "") + whereStatement + groupStatement
        nodeQueries.append(query)

    return nodeQueries
def ParallelPixelCount(connectDict, nodeDatasets, pixelValue):
    """
    Function for counting the number of pixels in a dataset
    """

    master = psqlLib(connectDict)
    nodeRasterTableIds = master.PartitionRaster(connectDict["raster_table"],
                                                len(connectDict["nodes"]))

    nodeQueries = []
    for n, node in enumerate(nodeDatasets['nodes']):

        topQuery = """
        With raster_value_count as
        (
        SELECT rid, (ST_ValueCount(rast)).*
        FROM {raster_table}
        """.format(**nodeDatasets)

        if len(connectDict["nodes"]) > 1:
            whereQuery = """
                        WHERE rid BETWEEN {min} AND {max}
                        """.format(**nodeRasterTableIds[n])
        else:
            whereQuery = ""

        returnQuery = """
        )
        SELECT value, sum(count) as count
        FROM raster_value_count
        WHERE value = %s 
        GROUP BY value
        """ % (pixelValue)
        query = topQuery + whereQuery + returnQuery
        nodeQueries.append(query)

    return nodeQueries