예제 #1
0
	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()
예제 #2
0
# Creation de l'axe des profondeurs cibles
depth_out = create_depth(depths)

# Interpolation
xmap = (0, 2, 3) # la profondeur varie en T/Y/X
xmapper = np.rollaxis(depths_in, 1, 4) # profondeur = dernier axe
data_out = regrid1d(data_in, depth_out, axi=depths_in, axis=1, method='linear', extrap=1)

# Plot
kw = dict(show=False, vmin=10, vmax=14, xhide='auto', add_grid=True, ymax=0)
section2(data_in[0, :, 10], yaxis=depths_in[0, :, 10], subplot=211, title='Sigma', **kw)
s = section2(data_out[0, :, 10], subplot=212, title='Z', savefigs=__file__,
    close=True, **kw)

# Sauvegarde
outfile = __file__[:-2]+'nc'
if os.path.isfile(outfile):
  os.remove(outfile)
netcdf3()
f2 = cdms2.open(outfile,'w')
f2.write(data_out)
f2.close()
print 'Saved to', outfile

# Temps de calcul
print "Whole computation took %.2f s" % (time() - t0)
print "End : " + strftime(print_time_format)


예제 #3
0
f.close()

# Creation de l'axe des profondeurs cibles
depth_out = create_depth(depths)

# Interpolation
xmap = (0, 2, 3) # la profondeur varie en T/Y/X
xmapper = np.rollaxis(depths_in, 1, 4) # profondeur = dernier axe
data_out = regrid1d(data_in, depth_out, axi=depths_in, axis=1, method='linear', extrap=1)

# Plot
kw = dict(show=False, vmin=10, vmax=14, xhide='auto', add_grid=True, ymax=0)
section2(data_in[0, :, 10], yaxis=depths_in[0, :, 10], subplot=211, title='Sigma', **kw)
s = section2(data_out[0, :, 10], subplot=212, title='Z', savefigs=__file__, **kw)

# Sauvegarde
outfile = __file__[:-2]+'nc'
if os.path.isfile(outfile):
  os.remove(outfile)
netcdf3()
f2 = cdms2.open(outfile,'w')
f2.write(data_out)
f2.close()
print 'Saved to', outfile

# Temps de calcul
print "Whole computation took %.2f s" % (time() - t0)
print "End : " + strftime(print_time_format)