1

I have an image which has a pixel array of [7981,7891]. I have assigned each pixel a lat lon coordinate via interpolation. Now I want to try and cut out a square section of the image so that the rest of my code only works on this small section. Here is my attempt at masking:

def ostiamask(lat,lon): # lat and lon inputs are both 2d-arrays #

    ost_lat_max = 55.05 
    ost_lat_min = 55.00
    ost_lon_max = -0.95
    ost_lon_min = -1.00

    lat = np.ma.array(lat)
    for i in lat:
        lat = np.ma.masked_outside(lat,i >= ost_lat_max, i <= ost_lat_min)  

    lon = np.ma.array(lon)
    for i in lon:
        lon = np.ma.masked_outside(lon,i >= ost_lon_max, i <= ost_lon_min)  


    lat_mask = np.ma.getmask(lat)
    lon_mask = np.ma.getmask(lon)
    lat_mask = np.array(lat_mask, dtype=int)  
    lon_mask = np.array(lon_mask, dtype=int) 
    pixel_coverage = np.logical_not(lat_mask) * np.logical_not(lon_mask) 

    print 'pixel mask sum',  np.sum(pixel_coverage)    
    print 'pixel mask shape', ostia_pixel_coverage.shape #debugging purposes#

    return pixel_coverage

I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have also tried using np.ma.mask_where() but no matter what I tried I kept getting a mask size of [7981,7891] pixels. Any idea where I am going wrong and why my mask isn't working? Any more info required let me know!

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
JC1217
  • 79
  • 1
  • 3

0 Answers0