I'm trying to create a frequency-of-occurrence map with an array of time, lat, lon. I should end up with a 2d lat/lon array of frequencies. The code below outlines my approach, and I run into problems at step d, when I convert the inverted boolean array mask to numerical values. I accidentally found a way to do, but I don't know why it works (np.mean). I can't see why np.mean turned booleans to floats but then didn't actually calculate the mean along the requested axis. I had to apply np.mean again to get the desired result. I feel there must be a right way to do convert a boolean array to floats or integers. Also, if you can think of a more better way to accomplish the task, fire away. My numpy mojo is weak and this was the only approach I could come up with.
import numpy as np
# test 3D array in time, lat, lon; values are percents
# real array is size=(30,721,1440)
a = np.random.random_integers(0,100, size=(3,4,5))
print(a)
# Exclude all data outside the interval 0 - 20 (first quintile)
# Repeat for 21-40, 41-60, 61-80, 81-100
b = np.ma.masked_outside(a, 0, 20)
print "\n\nMasked array: "
print(b)
# Because mask is false where data within quintile, need to invert
c = [~b.mask]
print "\n\nInverted mask: "
print(c)
# Accidental way to turn True/False to 1./0., but that's what I want
d = np.mean(c, axis = 0)
print "\n\nWhy does this work? How should I be doing it?"
print(d)
# This is the mean I want. Gives desired end result
e = np.mean(d, axis = 0)
print "\n\nFrequency Map"
print(e)
How do I convert the boolean values in my (inverted) array mask to numerical (1 and 0)?