In [1]:
from cate import ops
import xarray as xr
import numpy as np
In [2]:
ds = xr.Dataset({
'first': (['time', 'lat', 'lon'], np.random.random((2,10,20))),
'second': (['time', 'lat', 'lon'], np.random.random((2,10,20))),
'lat': np.linspace(-81, 81, 10),
'lon': np.linspace(-171, 171, 20),
'time': np.array([1, 2])})
In [3]:
%matplotlib inline
import matplotlib.patches as patches
import matplotlib.pyplot as plt
ax = ops.plot_map(ds=ds, var='first')
In [4]:
reg = [(-30, 30), (30, 30), (30, -30), (-30, -30)]
ds1 = ops.subset_spatial(ds=ds, region=reg)
ax = ops.plot_map(ds1, var='first')
ax.add_patch(patches.Polygon(reg, fill=False))
plt.xticks(ds.lon.values)
plt.yticks(ds.lat.values)
Out[4]:
In [5]:
reg = [(-42, 42), (42, 42), (42, -42), (-42, -42)]
ds2 = ops.subset_spatial(ds=ds, region=reg)
ax = ops.plot_map(ds2, var='first')
ax.add_patch(patches.Polygon(reg, fill=False))
plt.xticks(ds.lon.values)
plt.yticks(ds.lat.values)
Out[5]:
In [6]:
poly = ((-18.37890625,17.034305851402284),(22.40234375,34.79449777888723),(39.453125,3.0512170864479766),(25.0390625,-24.628443675479943),(-4.66796875,-15.730295059062692),(-18.37890625,17.034305851402284))
ds3 = ops.subset_spatial(ds=ds, region=poly)
ax = ops.plot_map(ds3, var='first')
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(ds.lon.values)
plt.yticks(ds.lat.values)
Out[6]:
In [7]:
# A polygon that fits completely inside a pixel, but includes pixel center
africa = '-20.0, -40.0, 60.0, 40.0'
ax = ops.plot_map(ds3, var='first', region=africa)
poly = ((9.5,-8.5),(10,10),(29,5),(30,-12))
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(ds.lon.sel(lon=slice(-20, 60)))
plt.yticks(ds.lat.sel(lat=slice(-40, 40)))
Out[7]:
In [8]:
# A polygon that fits completely inside a pixel, but includes pixel center
africa = '-20.0, -40.0, 60.0, 40.0'
poly = ((9.5,-8.5),(10,10),(29,5),(30,-12))
ds4 = ops.subset_spatial(ds3, region=poly, mask=True)
ax = ops.plot_map(ds4, var='first', region=africa)
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(ds.lon.sel(lon=slice(-20, 60)))
plt.yticks(ds.lat.sel(lat=slice(-40, 40)))
Out[8]:
In [9]:
poly = ((2,-16),(4,-2),(16, -4),(14, -14))
#ds4 = ops.subset_spatial(ds3, region=poly)
ax = ops.plot_map(ds4, var='first', region=africa)
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(ds.lon.sel(lon=slice(-20, 60)))
plt.yticks(ds.lat.sel(lat=slice(-40, 40)))
Out[9]:
In [10]:
ds5 = ops.subset_spatial(ds4, region=poly)
print(ds5)
#ax = ops.plot_map(ds5, var='first', region=africa)
#ax.add_patch(patches.Polygon(poly, fill=False))
#plt.xticks(ds.lon.sel(lon=slice(-20, 60)))
#plt.yticks(ds.lat.sel(lat=slice(-40, 40)))
In [11]:
ds6 = ops.subset_spatial(ds5, region=poly, mask=True)
print(ds6)
In [12]:
# Test with the polygon crossing lat boundary, but not including any vertices and centers
poly = ((2, 2), (4, 16), (34, 15), (30, 5))
ax = ops.plot_map(ds4, var='first', region=africa)
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(ds.lon.sel(lon=slice(-20, 60)))
plt.yticks(ds.lat.sel(lat=slice(-40, 40)))
Out[12]:
In [13]:
ds5 = ops.subset_spatial(ds4, region=poly)
print(ds5)
In [14]:
poly = ((2, 2), (4, 16), (16, 15), (15, 5))
ds5 = ops.subset_spatial(ds5, region=poly)
print(ds5)
In [ ]:
# Test with the polygon crossing lon boundary, but not including any vertices and centers
poly = ((4, 16), (16, 15), (14, -16), (2, -15))
ax = ops.plot_map(ds4, var='first', region=africa)
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(ds.lon.sel(lon=slice(-20, 60)))
plt.yticks(ds.lat.sel(lat=slice(-40, 40)))
Out[ ]:
In [20]:
ds5 = ops.subset_spatial(ds4, region=poly)
print(ds5)
In [ ]:
poly = ((2,-16),(4,-2),(16, -4),(14, -14))
ds5 = subset_spatial_impl(ds5, region=poly, mask=True)
print(ds5)
In [ ]:
dataset = xr.Dataset({
'first': (['lat', 'lon', 'time'], np.random.random([6, 12, 3])),
'second': (['lat', 'lon', 'time'], np.random.random([6, 12, 3])),
'lat': np.linspace(-75, 75, 6),
'lon': np.linspace(-165, 165, 12),
'time': [1,2,3]})
In [ ]:
ax = ops.plot_map(dataset, var='first')
poly = ((32,2),(34,28),(58,29),(54,4))
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(dataset.lon.values)
plt.yticks(dataset.lat.values)
In [ ]:
ax = ops.plot_map(dataset, var='first')
poly = ((25,2),(27,40),(58,38),(54,4))
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(dataset.lon.values)
plt.yticks(dataset.lat.values)
In [ ]:
ax = ops.plot_map(dataset, var='first')
poly = ((25, 2), (27, 47), (12, 52), (10, 4))
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(dataset.lon.values)
plt.yticks(dataset.lat.values)
In [ ]:
ax = ops.plot_map(dataset, var='first')
poly = ((32, 32), (34, 58), (56, 56), (58, 33))
ax.add_patch(patches.Polygon(poly, fill=False))
plt.xticks(dataset.lon.values)
plt.yticks(dataset.lat.values)
In [ ]: