我在MATLAB中使用griddata命令插入三维数组时遇到问题。我尝试了不同的方法,但不幸的是我无法处理它。应该插值的数组是182x125x35阵列(经度,纬度和深度),它是模型输出,我想在观测数据点上插值。感谢您花时间帮助我。
clear;
clc;
% Observation Data
t_obs = ncread('/disk1/Observation/data_from_data.nc','date_time');
lat_obs = ncread('/disk1/Observation/data_from_data.nc','latitude');
long_obs = ncread('/disk1/Observation/data_from_data.nc','longitude');
depth_obs = ncread('/disk1/Observation/data_from_data.nc','var1');
temp_obs = ncread('/disk1/Observation/data_from_data.nc','var2');
% Model Data
lat_m = ncread('/disk1/.../a.nc','GLat');
lon_m = ncread('/disk1/.../a.nc','GLong');
H = ncread('/disk1/.../a.nc', 'H');
sigma = ncread('/disk1/.../a.nc','SIGMAZZ');
dom = ncread('/disk1/.../a.nc','DOM');
% Rearrangement of date string to number for observation time
toffset1 = datenum([1990 01 01 00 00 00]) + datenum([0000 00 00 00 00 00]);
t = t_obs - 1;
time_obs = toffset1 + t;
% Selection of model file and date
t_mod1 = input('[yyyy mm dd] = ');
t_mod2 = input('[yyyy mm dd] = ');
toffset = datenum([1990 01 01 00 00 00]) + datenum([0000 00 00 00 00 00]);
t_mod3 = t_mod1+(0.00000001);
t_mod4 = t_mod2+(0.00000001);
date1 = datenum(t_mod3);
date2 = datenum(t_mod4);
tind_m1 = date1;
tind_m2 = date2;
time_mod = [tind_m1:tind_m2]';
ind1 = zeros(16887,1);
ind2 = zeros(16887,1);
for i = 1:16887
d = sort(abs(time_mod - time_obs(i)));
ind1(i) = find(abs(time_mod - time_obs(i)) == d(1));
ind2(i) = find(abs(time_mod - time_obs(i)) == d(2));
end
% Find temporal point of parameter for temporal interpolation
for i = 1:16887
d = sort(abs(time_mod - time_obs(i)));
ind1(i) = find(abs(time_mod - time_obs(i)) == d(1));
ind2(i) = find(abs(time_mod - time_obs(i)) == d(2));
end
% Temporal and spatial interpolation and writing the output in a .nc file
c = 1;
for n = ind1(1):ind1(16887)
for m = ind2(1):ind2(16887)
for l = 1:16887
str1 = sprintf('%4.4d',n);
str2 = sprintf('%4.4d',m);
sfile1 = (['/media/.../restart.',str1,'.nc']);
sfile2 = (['/media/.../restart.',str2,'.nc']);
temp1 = ncread(sfile1,'t',[1 1 1], [182, 125, 35]);
temp2 = ncread(sfile2,'t',[1 1 1], [182, 125, 35]);
temp_new = temp1 + ((temp2 - temp1)/(m-n))*(t(l) - n);
temp_new = griddata(lon_m,lat_m,depth_m,temp_new,long_o,lat_o,depth_obs);
c = n;
str = sprintf('%4.4d', c);
ncid = netcdf.create(['/disk1/.../tempinter/restartint.',str,'.nc'],'NC_WRITE');
x = 1:182;
y = 1:125;
z = 1:35;
dimid1 = netcdf.defDim(ncid,'xdim',length(x));
dimid2 = netcdf.defDim(ncid,'ydim',length(y));
dimid3 = netcdf.defDim(ncid,'zdim',length(z));
varidt = netcdf.defVar(ncid,'temp_new','double',[dimid1 dimid2 dimid3]);
netcdf.endDef(ncid)
netcdf.putVar(ncid,varidt,temp_new);
netcdf.close(ncid);
c = c+1;
end
end
end