MATLAB: Question with interp3

MATLAB

Hi all,
I got an error using interp3 as:
% Error using griddedInterpolant
% The grid vectors do not define a grid of points that match the given values.
My code is as:
addpath('nctoolbox-1.1.3');
load('matlab.mat')
setup_nctoolbox;
grib = ncgeodataset('2018_Jan.grib');
grib.variables;
grib.size('10_metre_U_wind_component_surface');
U_wind1 = grib.geovariable('10_metre_U_wind_component_surface');
grib.dimensions('10_metre_U_wind_component_surface');
size(U_wind1);
class(U_wind1);
U_wind = U_wind1.data(:, :, :);
U_wind = permute(U_wind,[2 3 1]);
U_wind = squeeze(double(U_wind));
U_wind = flipud(U_wind);
skip = length(U_wind)/2;
U_wind = circshift(U_wind,[0,skip]);
coords_latitude = -90:0.75:90;
coords_longitude = -180:0.75:179.25;
coords_lat = coords_latitude';
coords_lon = coords_longitude';
path_time_total_grib = path_time_total/6;
for i=1:size(path_lat_value,1)
for j=1:size(path_lat_value,2)
U_wind_route(i,j) = interp3(coords_lon,coords_lat,...
coords_time,U_wind,path_lon_value(i,j),path_lat_value(i,j),path_time_total_grib(i,j));
end
end
I also attach the path_lon_value, path_lat_value, path_time_total as matlab.mat and Grib file as this google drive link https://drive.google.com/open?id=1ifuEnEnSThdhsq6FHbUXDfy-DyhhB_Ba
Thank you for your help

Best Answer

First error I saw was you were using lat/lon vectors that were 0.75 degree resolution where the wind data was 1 degree resolution. I instead read the lat/lon from the file to make sure the data matched the coordinates.
Next, the data's longitude range was 0:360 where the path longitude rage was -180:180. So I did the modulus of the longitude path data by 360 to fix that.
Lastly, you do not want to loop through data. Just do interp3 on complete dataset, much faster.
Try this:
addpath('nctoolbox-1.1.3');
load('matlab.mat')
setup_nctoolbox;
grib = ncgeodataset('2018_Jan.grib');
U_wind1 = grib.geovariable('10_metre_U_wind_component_surface');
U_wind = U_wind1.data(:, :, :);
U_wind = permute(U_wind,[2 3 1]);
time = grib.geovariable('time');
grib_time = double(time.data(:));
lat = grib.geovariable('lat');
grib_lat = double(lat.data(:));
lon = grib.geovariable('lon');
grib_lon = double(lon.data(:));
path_time_total_grib = path_time_total/6;
path_lon_value_grib = mod(path_lon_value,360);
U_wind_route = interp3( grib_lon,grib_lat,grib_time, U_wind,...
path_lon_value_grib,path_lat_value,path_time_total_grib);