Separating colors isn't very complicated. First, output the image with dcraw -4 -d
In the dcraw source code it tells different color arrangements for different cameras. Lets say that it is:
R G R G ...
G B G B ...
R G R G ...
and so on. Then, read in the output file, and keep track of the x and y coordinates. In python, you can create two dictionaries, one for the sum and one for the sum squared, with the key being (x % 2,y % 2). % is the remainder function in python.
So when you have the pixel value, and the x and y coordinates do something like:
key = (x % 2,y % 2)
sum[key] = sum.get(key,0.0) + value
sum2[key] = sum2.get(key,0.0) + value**2
Then, at the end, you have two dictionaries with sum[(1,1)] for blue, sum[(0,0)] for red, and two places for the green.
Hope that helps.