def get_shoreline_mask(self): """Get the shoreline mask""" # We already have it if self._shoreline_mask is not None: return self._shoreline_mask # We skip if self._shoreline_arg is None: return # We compute it lon = self.get_lon() lat = self.get_lat() xres, yres = self.get_res() clip = [lon.getValue().min()-self._shoreline_margin*xres, lat.getValue().min()-self._shoreline_margin*yres, lon.getValue().max()+self._shoreline_margin*xres, lat.getValue().max()+self._shoreline_margin*yres] if not isinstance(self._shoreline_arg, ShoreLine): shoreline = get_shoreline(self._shoreline_arg, clip=clip) if shoreline is None: return else: shoreline = shoreline.clip(clip) # Get the polygons polys = shoreline.get_shapes() # Create the mask self._shoreline_mask = polygon_mask(self.get_grid(), polys) del polys return self._shoreline_mask
""" Masquages évolués Modules: :mod:`vacumm.misc.grid.masking` """ from vcmq import * from vacumm.misc.grid.masking import polygon_mask, erode_coast, get_coastal_indices, \ get_coastal_indices, envelop, polygons, polygon_select, get_dist_to_coast, \ convex_hull # Creation d'un masque via un trait de cote grid = create_grid((-6., -4., 0.1), (47.5, 49.1, 0.1)) # grille de l'iroise resolution = 'h' # resolution gshhs : testez 'i' mask = polygon_mask(grid, resolution, thresholds=0.5) # testez threshold = 0.1 mvmask = MV2.array(mask) set_grid(mvmask, grid) map2(mvmask, fillcontinents=False, drawcoastlines_color='r', drawcoastlines_zorder=100, res=resolution, proj='merc', fill='pcolor', drawcoastlines_linewidth=2, cmap=cmap_rs(['1', '0.6']), contour=False, colorbar=False, savefig ='courses_masking_0.png', show=True) # Cote via mask indices = get_coastal_indices(mask) dist = get_dist_to_coast(grid, mask) # Erosion de la cote f = cdms2.open(data_sample('mars3d.xy.nc'))
def run(self): if self.netcdf3: io.netcdf3() else: io.netcdf4(level=self.netcdf4level) cdms2.setAutoBounds(0) self.notice('Masking %s to %s', self.inpfile, self.outfile) inpfile = cdms2.open(self.inpfile) if os.path.isfile(self.outfile): if os.path.samefile(self.inpfile, self.outfile): raise Exception('Cannot use same input and output file') if self.overwrite: os.remove(self.outfile) else: raise Exception('Output file already exists and overwriting is not requested') outfile = cdms2.open(self.outfile, 'w') # Copy global attributes for a,v in inpfile.attributes.items(): setattr(outfile, a, v) # Keep grid masks in memory to improve performances (TODO: use a file cache as for basemaps ?) mask_cache = dict() stats = True # self.is_verbose() # Iterate over input variables for varid,filevar in inpfile.variables.items(): # Process only gridded/specified variables grid = filevar.getGrid() maskit = grid is not None if maskit and self.included_variables and varid not in self.included_variables: maskit = False if maskit and self.excluded_variables and varid in self.excluded_variables: maskit = False if not maskit and self.masked_only: self.verbose('Ignoring %s: variable has no grid and masked_only was specified') continue self.logger.info(bases.psinfo()) self.notice('Processing variable: %s, grid: %s', varid, grid.shape if grid else None) # NOTE: With scalar variables, memvar could be numpy.<type> instead of cdms2.tvariable.TransientVariable memvar = filevar() self.info(bases.describe(memvar, stats=stats)) self.logger.info(bases.psinfo()) if maskit: # Build the mask, check if it is already cached ? cache_id = id(grid) self.info('Get mask for grid %s', cache_id) mask = mask_cache.get(cache_id, None) if mask is None: self.notice('Loading mask: %s', dict(resolution=self.resolution, mode=self.mode, thresholds=self.thresholds, reverse=self.reverse)) mask = masking.polygon_mask(grid, self.resolution, mode=self.mode, thresholds=self.thresholds) if self.reverse: mask = ~mask mask_cache[cache_id] = mask self.info(bases.describe(mask, stats=stats)) self.logger.info(bases.psinfo()) # Mask variable # TODO: check/handle dimensions count and order self.notice('Masking variable: %s, mask: %s', memvar.shape, mask.shape) mask = MV2.resize(mask, filevar.shape) memvar[:] = MV2.masked_where(mask, memvar) self.info(bases.describe(memvar, stats=stats)) # Special scalar case which could fail if directly written (because fill_value is None) if not filevar.shape: fill_value = filevar.getMissing() if fill_value is None: fill_value = -memvar memvar = cdms2.createVariable( memvar, id=filevar.id, shape=(), typecode=filevar.typecode(), fill_value=fill_value, attributes=filevar.attributes) # Write masked variable to output file self.notice('Writing variable to file') outfile.write(memvar) self.logger.info(bases.psinfo()) outfile.close() inpfile.close()
# 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 map2(mask1, subplot=222, **kwplot) # - mask if cell if more than 30% of land thresholds = .3
# -*- coding: utf-8 -*- """ Masquages évolués Modules: :mod:`vacumm.misc.grid.masking` """ from vcmq import * from vacumm.misc.grid.masking import polygon_mask, erode_coast, get_coastal_indices, \ get_coastal_indices, envelop, polygons, polygon_select, get_dist_to_coast, \ convex_hull # Creation d'un masque via un trait de cote grid = create_grid((-6., -4., 0.1), (47.5, 49.1, 0.1)) # grille de l'iroise resolution = 'h' # resolution gshhs : testez 'i' mask = polygon_mask(grid, resolution, thresholds=0.5) # testez threshold = 0.1 mvmask = MV2.array(mask) set_grid(mvmask, grid) map2(mvmask, fillcontinents=False, drawcoastlines_color='r', drawcoastlines_zorder=100, res=resolution, proj='merc', fill='pcolor', drawcoastlines_linewidth=2, cmap=cmap_rs(['1', '0.6']), contour=False, colorbar=False, savefig='courses_masking_0.png', show=True)
# 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 map2(mask1, subplot=222, **kwplot) # - mask if cell if more than 30% of land thresholds = .3 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 map2(mask1, subplot=223, **kwplot)