0
votes

I have three different arrays.

One is a latitude array (-90 to 90) another is a longitude array (0 to 360) and the last is a 2D temperature array with the shape (len(lats), len(lons) where len(lats) != len(lons).

I have obtained a longitude mask via other means and have created a latitude mask via:

latmask = np.ma.masked_where(np.logical_or(lat < -60, lat > 60), lat).mask

So now I have two 1D masks that I want to apply to the 2D data along the appropriate axis with "or" logic (aka if either the lat or lon in that index was masked, then the 2D data should be masked).

I have tried combining the two 1D masks into a 2D mask using :

2dmask = np.logical_or(latmask , lonmask)
2dmask = latmask * lonmask

but these give an error saying the two arrays are not the same shape.

2dmask = latmask[np.newaxis, :] & lonmask[:, np.newaxis]

but when I try to apply this mask to my data like so and plot the result:

testdata = np.ma.masked_array(nt[0,50,:,:], mask = 2dmask)

I get the following plot:

Bad Masking

but this plot should have data above and below +/- 60 degrees lat masked and data except some continuous strip of longitudes masked (basically plucking out a rectangle of data).

I have spent the last 30-45 minutes searching the documentation and stack overflow for similar problems with no luck.

Thanks for any help!

-Will

1

1 Answers

1
votes

My guess is your solution is almost right, just flip dimensions:

twodmask = latmask[:, None] & lonmask[None, :]

and perhaps use or instead of and? (Not sure about that.)

twodmask = latmask[:, None] | lonmask[None, :]