Commit 8b07db06 authored by Clément Haëck's avatar Clément Haëck
Browse files

Move grid generation in script

add plot
parent d7d316f1
"""Generate grid of indexed boxes."""
import matplotlib.pyplot as plt
import matplotlib.patches as mp
from os import path
import lib
import lib.box
import lib.zones
def add_args(parser):
......@@ -15,6 +19,34 @@ def add_args(parser):
parser.add_argument('-size_lat', type=float, default=5)
def generate_grid(args, zone, land):
bounds = lib.box.Box(args['lon_min'], args['lon_max'],
args['lat_min'], args['lat_max'])
boxes = []
lon = bounds.lon_min
idx_lon = 0
while lon < bounds.lon_max:
lat = bounds.lat_max
idx_lat = 0
while lat > bounds.lat_min:
box = lib.box.IndexBox(
lon, lon+args['size_lon'], lat, lat+args['size_lat'],
idx_lon=idx_lon, idx_lat=idx_lat)
box_shape = box.get_shape()
intersect = zone.shape.intersection(box_shape)
if intersect.area/box_shape.area > 0.60:
# if zone.intersects(box.lon_bounds, box.lat_bounds):
land_b = box.extract(land)
if land_b.sum()/land_b.size < 0.40:
boxes.append(box)
lat -= args['size_lat']
idx_lat += 1
lon += args['size_lon']
idx_lon += 1
return boxes
def main():
args = lib.get_args(['region'], add_args=add_args)
......@@ -22,20 +54,29 @@ def main():
args['size_lon'], args['size_lat'])
grid_file = lib.box.IndexBox.GetGridFile(args)
bounds = lib.box.Box(args['lon_min'], args['lon_max'],
args['lat_min'], args['lat_max'])
grid = lib.box.IndexBox.GenerateGrid(bounds,
args['size_lon'],
args['size_lat'])
zone = lib.zones.zones['total']
land = lib.zones.get_land(args, grid='4km_EPSG4326')['land']
grid = generate_grid(args, zone, land)
idx_lon_max = max(b.idx_lon for b in grid)
idx_lat_max = max(b.idx_lat for b in grid)
print('Grid of {:d} boxes, {:d}x{:d}'.format(
len(grid), idx_lon_max, idx_lat_max))
print('Grid of {:d} boxes'.format(len(grid)))
print("Writing to '{}'".format(grid_file))
lib.box.IndexBox.WriteGridText(grid, grid_file)
print("Plotting boxes")
fig, ax = plt.subplots(1, 1)
land.plot.imshow(ax=ax, add_colorbar=False, cmap='Greys')
for b in grid:
b.add_to_plot(ax, fill=None, color='r')
ax.annotate(b.idx_str, b.center, ha='center', va='center',
color='r')
ax.add_artist(mp.Polygon(zone.points, fill=False, ec='b'))
ax.set_xlim(args['lon_min'], args['lon_max'])
ax.set_ylim(args['lat_min'], args['lat_max'])
if __name__ == '__main__':
main()
......@@ -30,6 +30,7 @@ if [ "$year" == "none" ]; then
exit 1
fi
# Same as /Download/era5_land_mask.py
lon_min=275.
lon_max=360.
lat_min=0.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment