Interpolating data to get values at specific depth

Dear experts, please help. I was searching and trying for anentire day and I couldn't figure it out by my own.
I have X, Y, Z, data.
X is longitude, Y is latitude, Z is depth, and data is temperature for example.
X and Y are 2D matrices (224x164). Z and data are 3D matrices (224x164x37).
The depth is unevenly spaced, for exemple, 1.0, 2.3, 5.6, 8.3 and so on, but I need the values of temperature exactly at 5m for example.
What I need is to get the data values at a specif depth all over the domain to plot a colored 2D map.
Please, could someone help me on how is such interpolation?
P.S. I know how to plot, I did it by plotting the surface Z(:,:,1), but as I mentioned, I need other depth layer.
Thank you so much in advance!

 Accepted Answer

Torsten
Torsten on 23 Feb 2024
Edited: Torsten on 23 Feb 2024
Make X and Y also 224x164x37 (just by repeating the planar profile 37 times) and use interp3 to interpolate to the depth of your choice.
Then make your contourf or surf plot.

5 Comments

Thank you for your reply, Torsten!
I created a new matrix (dptU) for depth (224,164,37), in which the depth dimension increases by 100 from 0 to 3600.
From Vq = interp3(X,Y,Z,data,xq,yq,zq) I tried:
Vq = interp3(lon, lat, depth, temp, lon, lat, dptU);
I didn't know what to put as xq and yq, so I repeated it since it has to stay the same, I just want to change de values among depths.
All the variables has the same size(224,164,37), but I got the error "Input grid is not a valid MESHGRID".
Please, do you know why and what I should do to solve it?
Thank you very much!
Do your X and Y matrices define a valid MESHGRID for longitude and latitude, i.e. something like
x = 5:10;
y = 30:40;
[X,Y] = meshgrid(x,y);
ans = 1×2
11 6
ans = 1×2
11 6
plot(X,Y,'ob')
Or are (X(i,j),Y(i,j)) 1<=i<=224, 1<=j<=164 are just 224*164 wildly spread points in the X-Y-plane ?
The grid is unevenly spaced, so they are wildly spread points in the X-Y-plane.
I guess your data will be well-behaved.
This code only takes the temperature data at the point (X(i,j),Y(i,j)) over the 37 heights and makes a 1d-interpolation to the required height (0.5 m) for all these points. I don't know if the results get better if you include temperatures in neighbouring points, but the interpolation will become more complicated. You will most probably have to use "ScatteredInterpolant".
X = rand(224,164); % random X-coordinates
Y = rand(224,164); % random Y-coordinates
Z = 10*rand(224,164,37); % random heights at points (X,Y)
data = 20 + rand(224,164,37)*20; % random temperatures at points (X,Y,Z)
h = 5; % interpolation level
for i = 1:224
for j = 1:164
[z,idx] = sort(squeeze(Z(i,j,:)));
d = squeeze(data(i,j,idx));
temp_5(i,j) = interp1(z,d,h);
end
end
surf(X,Y,temp_5)
Now it worked perfectly. Thank you very much, Torsten! Best!

Sign in to comment.

More Answers (1)

x=(0:10); y=(0:11)'; z=[1.0, 2.3, 5.6, 8.3]; % vectors x,y,z
[X,Y]=meshgrid(x,y);
%Make data() with desired dimensions, same at each depth
data=repmat(sin(2*pi*y/11)*cos(2*pi*x/10),[1,1,length(z)]);
% Make data different at different depths
for k=1:length(z), data(:,:,k)=data(:,:,k)+z(k)^2/10; end
% check that the array sizes are as desired
fprintf('Size X, Y, Z= %dx%d, %dx%d, %dx%dx%d.\n',size(X),size(Y),size(data))
Size X, Y, Z= 12x11, 12x11, 12x11x4.
% Plot data(x,y,z=5.6), as a test
surf(X,Y,data(:,:,3));
grid on; xlabel('X'); ylabel('Y'), zlabel('Data'); title('Data(:,:,z=5.6)')
data5=zeros(size(X)); % allocate array for data interpolated to z=5
for i=1:length(y), for j=1:length(x)
data5(i,j)=interp1(z,squeeze(data(i,j,:)),5);
end, end
% Plot data(x,y,z=5)
surf(X,Y,data5);
grid on; xlabel('X'); ylabel('Y'), zlabel('Data');
title('Data, Interpolated to z=5')
OK

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!