Using a polygon (long, lat) to define and extract a region of the model output

Hello everyone.

Here is an example of how to extract a geographical region from the model output using a polygon, rather than a regular shape like a square.

In this example, I used a polygon (specifically, pairs of longitudes and latitudes) that define the Australian Antarctic Basin, xarray DataArrays, and the python package ‘regionmask’.


import regionmask

basin_PJ10_number = 31
longs_of_basin, lats_of_basin = import_PJ10_basin_boundaries(basin_PJ10_number)
# create my own region for mask
basin_of_interest = np.vstack((longs_of_basin,lats_of_basin)).T # rows = co-ordinate pairs; cols = long, lat
region = regionmask.Regions([basin_of_interest])
# create mask  
mask = region.mask(gridded_temperature.longitude, gridded_temperature.latitude, lon_name='longitude', lat_name='latitude') # tell the code what long and lat are called in the data set that you are tryig to mask.
# use mask to extract region of interest
masked_gridded_temperature = gridded_temperature.where(mask == 0)


Awesome! Thanks for providing this worked example @kathy.gunn so others can learn from it.