MATLAB Answers

How to interpolate gridded data for a specific point

3 views (last 30 days)
Ebtesam Farid
Ebtesam Farid on 19 May 2020
Commented: Ameer Hamza on 19 May 2020
I have a meteorological netcdf file contains (Geopotential heigh z, air-temperauter t, Specific Humidity q, Relative Humidity r) over North America with grid 0.75 x 0.75 resolution, and I want to get theses parameters over some stations with specific (latitude/longitude) so I tried to interpolate the gridded data, I used interp2 function but when I run my program I get an error message
folder = 'C:\New folder\';
f_name = '';
data =[];
file = sprintf('%s%s',folder,f_name);
info = ncinfo(file);
Conventions = ncreadatt(file,'/','Conventions');
history = ncreadatt(file,'/','history');
longitude = ncread(file,'longitude');
latitude = ncread(file,'latitude');
level = ncread(file,'level');
time = ncread(file,'time');
z = ncread(file,'z');
t = ncread(file,'t');
q = ncread(file,'q');
r = ncread(file,'r');
% to get a portion of q at lat/long
[lonref,latref] = meshgrid(longitude(:),latitude(:));
for i = 1:length(level)
q2d = q(:,:,i,1);
qlatlon(i) = interp2(lonref(:),latref(:),q2d(:),longitude,latitude); % Note this assumes that q is organized lon/lat/altitude/time,% if it is lat/lon/altitude/time, switch lat and lon in this expression
plot(qlatlon,levels) % Single profile for the first time at lat/lon
the error message I get is
[Error in interp2 (line 128)
F = makegriddedinterp({X, Y}, V, method,extrap);
Error in read_cn (line 26)
qlatlon(i) = interp2(lonref(:),latref(:),q2d(:),longitude,latitude); ]
any help please to what's wrong or what's the best way to interpolate this gridded data
this is the link on google drive of the file just because it's big file netcdf file

Accepted Answer

Ameer Hamza
Ameer Hamza on 19 May 2020
Change the line inside for-loop to this
qlatlon{i} = interp2(lonref,latref,q2d.',longitude,latitude);
% replace longitude,latitude with the quey point, both should be of equal size
Also I used cell array because the output of interp2 can be a vector that cannot be assigned as an element of a numeric array.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!