How to reproject the data in Matlab?
17 views (last 30 days)
Show older comments
I have two different datasets for the same region, i want to reproject one of them according to the lat/long from the other file.
Data1 is 361*361, associated lat/long file of the size 361*361
Data 2 is 119*177, lat1/long1file of the 119*177
How can i reproject one of them according to the lat/long from other file.
2 Comments
Answers (2)
Kelly Kearney
on 5 Dec 2016
What sort of spatial distribution do your datasets have? Is it a grid, but not one sitting along straight parallels and meridians?
Unfortunately, none of Matlab's interpolation routines are ideally suited for gridded-but-not-meshgrid/ndgrid-style datasets. If you have this type of data (and a lot of geographic data fits this description), then I think your best bet it to try scatteredInterpolant. Beware of problems along the edges of your grid, though; scatteredInterpolant doesn't like it when its input data includes a lot of colinear points (and this sort of grid will include a lot of colinearity), and can sometimes return very out-of-range values, even with a nearest neighbor interpolant.
3 Comments
Kelly Kearney
on 16 Dec 2016
It's a bit difficult to determine what's going wrong in your code, since we don't have the data you're using. However, here's a short example of how I'd go about an interpolation given the two data grids you include in the .fig file:
% Extract your data grids
hfig = open('~/Downloads/lat_long_nsidc_osisaf.fig')
h1 = findall(hfig, 'color', 'r');
h2 = findall(hfig, 'color', 'b');
A.lat = cell2mat(get(h1, 'ydata'));
A.lon = cell2mat(get(h1, 'xdata'));
B.lat = cell2mat(get(h2, 'ydata'));
B.lon = cell2mat(get(h2, 'xdata'));
close(hfig);
% Downsample B, just to save time in this example
B.lat = B.lat(1:10:end,1:10:end);
B.lon = B.lon(1:10:end,1:10:end);
% Add some fake data on the A grid
[nr,nc] = size(A.lon);
A.val = peaks(max(nr,nc));
A.val = A.val(1:nr,1:nc);
% Set up a function to calculate distance in geographic space, formatted
% for pdist2
ell = referenceEllipsoid('earth');
distfun = @(ltln1, ltln2) distance(ltln1(1), ltln1(2), ltln2(:,1), ltln2(:,2), ell);
% Project A data onto B grid, nearest neighbor
nb = numel(B.lat);
idx = zeros(nb,1);
nchunk = 100;
for ii = 1:(ceil(nb/nchunk))
fprintf('Iteration %d of %d\n', ii, ceil(nb/nchunk));
tmp = (1:nchunk)' + nchunk*(ii-1);
tmp = tmp(tmp <= nb);
[~, idx(tmp)] = pdist2([A.lat(:) A.lon(:)], [B.lat(tmp) B.lon(tmp)], distfun, 'Smallest', 1);
end
B.val = reshape(A.val(idx), size(B.lat));
% Plot
Cst = load('coast');
figure('color', 'w');
ax(1) = axes('position', [0 0.5 1 0.5]);
axesm('ortho', 'origin', [90 0 0], 'frame', 'on');
pcolorm(A.lat, A.lon, A.val);
plotm(Cst.lat, Cst.long, 'k');
ax(2) = axes('position', [0 0 1 0.5]);
axesm('ortho', 'origin', [90 0 0], 'frame', 'on');
pcolorm(B.lat, B.lon, B.val);
plotm(Cst.lat, Cst.long, 'k');
set(ax, 'visible', 'off');
If you'd rather do inverse distance weighting as opposed to nearest neighbor, you can change the pdist2 calculation to return more points, as well as the distance values, and go from there.
The nearest neighbor calculation is slow... I usually break things into chunks (as I've done in this example), but even so, it's slow and memory-intensive. It could probably be sped up through use of quadtrees or the like, but there aren't any pre-written Matlab functions to do that in geographic space, so I've just suffered through the slowness of the exhaustive search myself.
Walter Roberson
on 30 Nov 2016
[Lat1, Long1] = meshgrid(lat1, long1);
[Lat2, Long2] = meshgrid(lat2, long2);
estimated_Data1 = interp2(Lat1, Long1, Data1, Lat2, Long2);
3 Comments
Walter Roberson
on 30 Nov 2016
If your lat1 and lat2 and long1 and long2 are already 2 dimensional rather than vectors then just use
result = interp2(lat1, long1, data, lat2, long2)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!