# Inits
from vacumm.misc.grid.masking import Lakes
from vacumm.misc.grid.misc import meshbounds
import vacumm.misc.color as C
from vacumm.misc.plot import savefigs
import numpy as N, pylab as P

P.figure(figsize=(5.5, 5))
P.subplots_adjust(bottom=0.02, top=0.92, left=0.01, right=0.98, hspace=0.3)
cmap_mask = C.cmap_linear(((0.6, 0.8, 1), C.land))
cmap_lakes = C.cmap_srs(("w", "r", "g", "b"), stretch=0)

# Creer un mask avec des lacs
mask = N.ones((20, 30), "?")  # Terre partout
mask[10:, 10:] = False  # Ocean
mask[10:15, 13:14] = True  # Ocean
mask[0, 0] = False  # Ptit lac
mask[5:15, 2:4] = False  # Gros lac
xxb, yyb = meshbounds(N.arange(mask.shape[1] * 1.0), N.arange(mask.shape[0] * 1.0))
xlim = (xxb.min(), xxb.max())
ylim = (yyb.min(), yyb.max())
P.subplot(221)
P.pcolor(xxb, yyb, mask.astype("i"), cmap=cmap_mask)
P.xlim(xlim)
P.ylim(ylim)
P.xticks([])
P.yticks([])
P.title("Mask initial")

# Identification des lacs
lakes = Lakes(mask)
# Grid (15x15)
nx, ny = 15, 10
import numpy as N
from vacumm.misc.axes import create_lon, create_lat
lon = create_lon(N.linspace(-5.2, -4.4, nx))
lat = create_lat(N.linspace(48, 48.6, ny))

# Plot params
import pylab as P
import vacumm.misc.color as C
P.figure(figsize=(6, 5))
P.subplots_adjust(top=.92, left=.03, bottom=.03, wspace=.05, hspace=.2)
kwplot = dict(colorbar=False,cmap=C.cmap_linear([(.6, .8, 1), C.land]), \
    contour=False,show=False,fillcontinents=False,fill='pcolor',
    drawmeridians=False, drawparallels=False, res='h', key=True, xymasked=False)

# Mask using shoreline
from vacumm.misc.grid.masking import polygon_mask
from vacumm.misc.plot import map2
import MV2 as MV
# - mask if central point sur terre (mode = 0 = 'inside')
lon2, lat2 = N.meshgrid(lon, lat)
mask0 = polygon_mask((lon2, lat2), 'h', mode=0, ocean=False)
mask0 = MV.array(mask0, axes=[lat, lon])
mask0.long_name = "'inside'"
map2(mask0, subplot=221, **kwplot)
# - mask if cell more than 50% of land (mode = 1 = 'intersect')
thresholds = .5
mask1 = polygon_mask((lon2, lat2),
                     'h',
                     mode=1,
# Grid (15x15)
nx, ny = 15, 10
import numpy as N
from vacumm.misc.axes import create_lon, create_lat
lon = create_lon(N.linspace(-5.2, -4.4, nx))
lat = create_lat(N.linspace(48, 48.6, ny))

# Plot params
import pylab as P
import vacumm.misc.color as C
P.figure(figsize=(6, 5))
P.subplots_adjust(top=.92,left=.03,bottom=.03,wspace=.05,hspace=.2)
kwplot = dict(colorbar=False,cmap=C.cmap_linear([(.6, .8, 1), C.land]), \
    contour=False,show=False,fillcontinents=False,fill='pcolor',
    drawmeridians=False, drawparallels=False, res='h', key=True, xymasked=False)

# Mask using shoreline
from vacumm.misc.grid.masking import polygon_mask
from vacumm.misc.plot import map2
import MV2 as MV
# - mask if central point sur terre (mode = 0 = 'inside')
lon2,lat2 = N.meshgrid(lon,lat)
mask0 = polygon_mask((lon2, lat2), 'h', mode=0, ocean=False)
mask0 = MV.array(mask0, axes=[lat, lon])
mask0.long_name = "'inside'"
map2(mask0, subplot=221, **kwplot)
# - mask if cell more than 50% of land (mode = 1 = 'intersect')
thresholds = .5
mask1 = polygon_mask((lon2, lat2), 'h', mode=1, thresholds=thresholds, ocean=False)
mask1 = MV.array(mask1, axes=[lat, lon])
mask1.long_name = "'intersect': threshold=%g" %thresholds