Interpolating global data with geointerp

10 views (last 30 days)
Hey folks!
I am working on a project where I need to interpolate data from its native lat/lon grid to a new grid that is compatible with other data that I have. I originally was using interp2() to do this, but I would prefer bilinear interpolation, as used by geointerp(), instead of linear interpolation, used by interp2().
The problem I am having is that the lat/lon grid that I am interpolating to is not square. I am attempting to move from a 180x360 grid to a 72x144 grid, and am using the following code block to do so:
%% Interpolate to a common grid (use the same grid as the kernels)
[a,b,c,d,e]=size(ctl_clisccp);
latlim=[-89.14152 89.14152];
lonlim=[0 358.875];
rastersize=[160 320];
R = maprefcells(latlim,lonlim,rastersize)
avgctl_clisccp_int=nan*ones(nmonth,b,c,72,144);
for M=1:nmonth
for T=1:b
for P=1:c
avgctl_clisccp_int(M,T,P,:,:)=mapinterp(squeeze(ctl_clisccp(M,T,P,:,:)),R,kern_lat,kern_lon);
end
end
end
Where kern_lat and kern_lon are of size 72x1 and 1x144 respectively. This block gives me the following error:
Error using mapinterp (line 50)
xq and yq must have the same size.
I am wondering why xq and yq must be the same size, and how to interpolate data to a global grid that is not square. Any help or guidance would be much appreciated!
Thanks,
Calvin

Accepted Answer

Raghav
Raghav on 5 May 2023
Hi,
Based on your question, it can be understand that you are facing issue in interpolation.
The error message you are getting suggests that the size of the latitude and longitude grids you are interpolating to using mapinterp() must be the same. This is because mapinterp() uses the same set of xq and yq values to interpolate the data. In your case, the size of the latitude grid is 72x1 and the size of the longitude grid is 1x144, which are not equal. Therefore, you need to reshape your kern_lat and kern_lon grids to be of equal size, such as 72x144.
To do this, you can use the following code:
kern_lon = repmat(kern_lon, 72, 1);
kern_lat = repmat(kern_lat', 1, 144);
This will replicate the original kern_lat and kern_lon vectors to create two matrices with the same size of 72x144. You can then use these resized grids as inputs to the mapinterp().
The official documentation for repmat function & mapinterp function is mentioned below:
Hope this helps,
Raghav Bansal

More Answers (0)

Categories

Find more on Interpolating Gridded Data in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!