Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Masked Array – Count the value in a specific interval

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading