title: Filter data, based on Latitude and Longitudes - Numpy
body: <p>I have a dataset <a href="ftp://sidads.colorado.edu/pub/DATASETS/nsidc0079_gsfc_bootstrap_seaice/final-gsfc/south/daily/2013/bt_20130221_f17_v02_s.bin" rel="nofollow noreferrer">here</a> and it's specif <a href="ftp://sidads.colorado.edu/pub/DATASETS/seaice/polar-stereo/tools/pss25lats_v3.dat" rel="nofollow noreferrer">latitude</a> and <a href="ftp://sidads.colorado.edu/pub/DATASETS/seaice/polar-stereo/tools/pss25lons_v3.dat" rel="nofollow noreferrer">longitude</a></p>
  
  <pre><code>import numpy as np
  
  f = open('bt_20130221_f17_v02_s.bin', 'rb')
  data = np.fromfile(f, dtype=np.uint16).reshape(332, 316)
  f.close()
  
  raw_lat = open('pss25lats_v3.dat', 'rb')
  lats = np.fromfile(raw_lat, dtype='&lt;i4').reshape(332,316) / 100000.
  raw_lat.close()
  
  raw_lon = open('pss12lons_v3.dat', 'rb')
  lons = np.fromfile(raw_lat, dtype='&lt;i4').reshape(332,316) / 100000.
  raw_lon.close()
  </code></pre>
  
  <p>The data values are presented here as:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  plt.imshow(data)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/yhvKJ.png" alt="enter image description here"></p>
  
  <p>Based on those values, I desire to filter a regular portion of this data.
  such as:</p>
  
  <pre><code>north = -59.7183
  south = -65.3099
  west = -65.743
  east = -48.55
  
  mask_lons = np.ma.masked_where(((lons &gt; east) | (lons &lt; west)), lons)
  mask_lats = np.ma.masked_where(((lats &lt; south) | (lats &gt; north)), lats)
  
  data_filtered = np.where(((lats == mask_lats) &amp; (lons == mask_lons)),
                      data, 999)
  </code></pre>
  
  <p>this is the resultant image:
  <img src="https://i.stack.imgur.com/B6q1M.png" alt="enter image description here"></p>
  
  <p>First question:
  How can I slice this data_filtered to acquire only the valid values (i.e. only the matrix containing values != 999)?</p>
  
  <p>Second question:
  How do I do the same for lats and lons? How should I get only the non masked values as a sigle 2D array for each variable?
  since mask_lons is:</p>
  
  <pre><code>In [176]: mask_lons
  Out[176]: 
  masked_array(data =
   [[-- -- -- ..., -- -- --]
   [-- -- -- ..., -- -- --]
   [-- -- -- ..., -- -- --]
   ..., 
   [-- -- -- ..., -- -- --]
   [-- -- -- ..., -- -- --]
   [-- -- -- ..., -- -- --]],
               mask =
   [[ True  True  True ...,  True  True  True]
   [ True  True  True ...,  True  True  True]
   [ True  True  True ...,  True  True  True]
   ..., 
   [ True  True  True ...,  True  True  True]
   [ True  True  True ...,  True  True  True]
   [ True  True  True ...,  True  True  True]],
         fill_value = 1e+20)
  </code></pre>