Debian Codegraph @

geotiepoints/basic_interpolator.py

raw
1 import numpy as np
2 # from memory_profiler import profile
3 from pandas import DataFrame, Series
4
5 class BasicSatelliteInterpolator(object):
6 """
7 Handles interpolation of geolocation data from a grid of tie points.
8
9 Currently, it is assumed, that tie-points reach out til the edges if the
10 tiepoint grid. Methods for extrapolation need to be added if needed.
11
12 Uses numpy and pandas.
13
14 The constructor gets the tiepointed lat/lon data as *lat_data* and
15 *lon_data*.
16 """
17
18 def __init__(self, cols, rows, lat_data, lon_data):
19 self.row_indices = rows
20 self.col_indices = cols
21
22 self.lon_tiepoint = lon_data
23 self.lat_tiepoint = lat_data
24
25 self.longitudes = None
26 self.latitudes = None
27
28
29 def _interp(self, data):
30 """The interpolation method implemented here is a kind of a billinear
31 interpolation. The input *data* field is first interpolated along the
32 rows and subsequently along its columns.
33
34 The final size of the interpolated *data* field is determined by the
35 last indices in self.row_indices and self.col_indices.
36 """
37 row_interpol_data = self._interp_axis(data, 0)
38 interpol_data = self._interp_axis(row_interpol_data, 1)
39
40 return interpol_data
41
42
43 # @profile
44 def _interp_axis(self, data, axis):
45 """The *data* field contains the data to be interpolated. It is
46 expected that values reach out to the *data* boundaries.
47 With *axis*=0 this method interpolates along rows and *axis*=1 it
48 interpolates along colums.
49
50 For column mode the *data* input is transposed before interpolation
51 and subsequently transposed back.
52 """
53 if axis == 0:
54 return self._pandas_interp(data, self.row_indices)
55
56 if axis == 1:
57 data_transposed = data.as_matrix().T
58 data_interpol_transposed = self._pandas_interp(data_transposed,
59 self.col_indices)
60 data_interpol = data_interpol_transposed.as_matrix().T
61
62 return data_interpol
63
64
65 def _pandas_interp(self, data, indices):
66 """The actual transformation based on the following stackoverflow
67 entry: http://stackoverflow.com/a/10465162
68 """
69 new_index = np.arange(indices[-1] + 1)
70
71 data_frame = DataFrame(data, index=indices)
72 data_frame_reindexed = data_frame.reindex(new_index)
73 data_interpol = data_frame_reindexed.apply(Series.interpolate)
74
75 del new_index
76 del data_frame
77 del data_frame_reindexed
78
79 return data_interpol
80
81
82 def interpolate(self):
83 """Do the interpolation and return resulting longitudes and latitudes.
84 """
85 self.latitude = self._interp(self.lat_tiepoint)
86 self.longitude = self._interp(self.lon_tiepoint)
87
88 return self.latitude, self.longitude