[SOLVED] Masked Array – Count the value in a specific interval

Issue

I am quite new to masked arrays and satellite data. I am trying to figure out how to count the number of elements in a masked_array that are in between an interval e.g. say 40 to 80. This is what I have:

This is the summary of my masked array named ‘grid’.

masked_array(
  data=[[[120, 120, 120, ..., 200, 200, 200],
         [120, 120, 120, ..., 200, 200, 200],
         [120, 120, 120, ..., 200, 200, 200],
         ...,
         [120, 120, 120, ..., 200, 200, 200],
         [120, 120, 120, ..., 200, 200, 200],
         [120, 120, 120, ..., 200, 200, 200]]],
  mask=False,
  fill_value=999999,
  dtype=uint8)

I want to calculate the % of elements in the masked_array that are between 40 and 80. I tried.

masked  = ma.masked_where((grid >= 40) & (grid <= 80), grid)
green_ratio = masked.count()/grid.count()

but this is returning 1 which is quite unlikely given that I see that there are values larger than 120.

Any idea on how to do this?

Solution

masked.count() still gives you the total unfiltered number of values which is of course equal to grid.count().

Since masked.mask is a boolean array, you could do for example masked.mask.sum()/grid.count() to limit the ratio to those values which meet your condition.

Answered By – mcsoini

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *