def increase_grid_resolution(self, res=0.2, polyorder=2):
"""Gaussian pyramide based upscaling of topographic grid
This function checks the current topographic resolution in the center
of the grid. Based on this, an upsacling factor is determined and the
:func:`cv2.pyrUp` is used to increase the resolution using
interpolation. Note, that this does not increase the actual resolution
of the topographic data grid.
:param float res: desired grid resolution in km (default: 0.2)
:param int polyorder: order of polynomial used for interpolation
(default: 2)
:returns:
- :class:`TopoData`, new object with desired grid resolution
.. note::
This functionality is only available, if :mod:`cv2` is installed
"""
lons = self.lons
lats = self.lats
vals = self.data
if not all(mod(x, 2) == 0 for x in [len(lons), len(lats)]):
print ("Fatal error, odd array size detected, no upscaling possible."
" Return current data dict")
return False
c_lon = len(lons) / 2 - 1 #center longitude index
c_lat = len(lats) / 2 - 1 #center latitude index
p1 = LatLon(lats[c_lat], lons[c_lon])
p2 = LatLon(lats[c_lat + 1], lons[c_lon + 1])
dist = (p2 - p1).magnitude #distance between 2 points on grid
res_fac = dist / res #factor for increasing the spatial resolution
if res_fac <= 1:
print ("No interpolation of topodata necessary: topo raw data "
"already has desired resolution...\nCurrent resolution: %s"
+ str(dist) + "km\nDesired resolution: %s km" %(dist, res))
return self
fac = int(ceil(log2(res_fac))) #the corresponding up-factor for the scale space
print("Increasing spatial topography resolution by factor %s" %(2**fac))
for k in range(fac):
vals = pyrUp(vals)
p_lons = poly1d(polyfit(arange(len(lons)), lons, polyorder))
p_lats = poly1d(polyfit(arange(len(lats)), lats, polyorder))
lons_new = p_lons(linspace(0, len(lons) - 1, vals.shape[1]))
lats_new = p_lats(linspace(0, len(lats) - 1, vals.shape[0]))
return TopoData(lats_new, lons_new, vals, self.data_id + "_interp")
评论列表
文章目录