I am facing a problem that I can’t solve. I have radar data, from which I want to plot the grid structure (rectangles) of a certain area. So far, I have the following:
#-- Get coordinates in the metric crs
x = np.linspace(dataset.bounds.left, dataset.bounds.right, dataset.width) #dataset.width
y = np.linspace(dataset.bounds.top, dataset.bounds.bottom, dataset.height) #dataset.height
x_grid, y_grid = np.meshgrid(x, y)
#-- Initalize lon/lat arrys
lon_grid = np.zeros_like(x_grid)
lat_grid = np.zeros_like(y_grid)
#-- Transform every point from the metric crs to the global wgs84
for i in range(len(x)):
for j in range(len(y)):
x_point = x_grid[j, i]
y_point = y_grid[j, i]
lat, lon = transformer.transform(x_point, y_point)
#-- set all lat/lon-values as NaN if they are outside a certain domain
if lboundary<=lon<=rboundary and bboundary<=lat<=tboundary:
lon_grid[j, i] = lon
lat_grid[j, i] = lat
else:
lon_grid[j,i] = np.nan
lat_grid[j,i] = np.nan
#-- find the indices of the array entries that are within the domain
mask_y, mask_x = np.where(~np.isnan(lon_grid))
#-- create a mask for the values which are in the domain [lboundary,rboundary, bboundary, tboundary]
mask = np.zeros_like(lon_grid, dtype=bool)
mask[mask_y, mask_x] = True
#-- mask lon/lat
masked_lon_grid = lon_grid[mask]
masked_lat_grid = lat_grid[mask]
#-- get variable
var = data[0]
#-- mask the variable
masked_var = var[mask]
#-- define the number of cells
ncells = masked_var.shape[0]
#-- Initialize the polygones
polygones = np.zeros((ncells, 4, 2), np.float32)
half_lon_width = round((masked_lon_grid[1] - masked_lon_grid[0])/2,5)
half_lat_width = round((masked_lat_grid[1] - masked_lat_grid[0])/2,5)
# Schleife über lon und lat
for ncell in range(masked_var.shape[0]):
lon0 = masked_lon_grid[ncell] - half_lon_width
lon1 = masked_lon_grid[ncell] + half_lon_width
lat0 = masked_lat_grid[ncell] - half_lat_width
lat1 = masked_lat_grid[ncell] + half_lat_width
polygones[ncell] = np.array([[lon0, lat1], [lon1, lat1], [lon1, lat0], [lon0, lat0]])
#-- set color index of all cells in between levels
sum_polygones = []
for m in range(ncol):
vind = []
for ncell in range(masked_var.shape[0]):
if masked_var[ncell] >= levels[m] and masked_var[ncell] < levels[m+1]:
colors[ncell,:] = cmap(m)
vind.append(ncell)
sum_polygones.append(len(vind))
del vind
#-- create a PolyCollection
poly_collection = PolyCollection(polygones,
edgecolors='black',
facecolors=colors,
linewidth=.5,
transform=ccrs.Geodetic())
ax.add_collection(poly_collection)
The shape of the Lon/Lat and var-grid is (13450,850). When I mask these arrays, then I get a 1-dim array of shape (4058,0). If I add my PolyCollection than I the polygones do not get created and my whole area is blue.