Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _prepare_gebco_height(xs, ys, gebco_fn=None):
# gebco bathymetry heights for underwater
if gebco_fn is None:
from .config import gebco_path
gebco_fn = gebco_path
tmpdir = tempfile.mkdtemp()
cornersc = np.array(((xs[0], ys[0]), (xs[-1], ys[-1])))
minc = np.minimum(*cornersc)
maxc = np.maximum(*cornersc)
span = (maxc - minc)/(np.asarray((len(xs), len(ys)))-1)
minx, miny = minc - span/2.
maxx, maxy = maxc + span/2.
tmpfn = os.path.join(tmpdir, 'resampled.nc')
try:
ret = subprocess.call(['gdalwarp', '-of', 'NETCDF',
'-ts', str(len(xs)), str(len(ys)),
'-te', str(minx), str(miny), str(maxx), str(maxy),
'-r', 'average',
gebco_fn, tmpfn])
span = (maxc - minc)/(np.asarray((len(xs), len(ys)))-1)
minx, miny = minc - span/2.
maxx, maxy = maxc + span/2.
tmpfn = os.path.join(tmpdir, 'resampled.nc')
try:
ret = subprocess.call(['gdalwarp', '-of', 'NETCDF',
'-ts', str(len(xs)), str(len(ys)),
'-te', str(minx), str(miny), str(maxx), str(maxy),
'-r', 'average',
gebco_fn, tmpfn])
assert ret == 0, "gdalwarp was not able to resample gebco"
except OSError:
logger.warning("gdalwarp was not found for resampling gebco. "
"Next-neighbour interpolation will be used instead!")
tmpfn = gebco_path
with xr.open_dataset(tmpfn) as ds_gebco:
height = (ds_gebco.rename({'lon': 'x', 'lat': 'y', 'Band1': 'height'})
.reindex(x=xs, y=ys, method='nearest')
.load()['height'])
shutil.rmtree(tmpdir)
return height