Why using interp2 gives NaN
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to collocate two datesets (Satellite data). I have matched dates and time correcctly but my lat lon collocation step gives me NaN. Here is my code:
% Read Satellite 1 data.
Data = table2array(DCC);
col_names = {'Year','Month', 'Day', 'HH', 'MM', 'Lat', 'Lon', 'Data_C', 'Salt'};
Sat1_data = double(Data);
Sat1_date = NaN(size(Sat1_data,1),1);
Sat1_time = Sat1_date;
%
for kkk = 1:size(Sat1_data,1)
Sat1_date(kkk,1) = Sat1_data(kkk,1)*1E4+Sat1_data(kkk,2)*1E2+Sat1_data(kkk,3);
Sat1_time(kkk,1) = Sat1_data(kkk,4)*1E2+Sat1_data(kkk,5);
end
% read data from 2nd Satellite
Sat2_path = 'E:\aer 2017\';
Sat2_list = dir(fullfile(Sat2_path,'*.nc4'));
Sat2_date = NaN(size(Sat2_list,1),1);
for iii = 1:size(Sat2_list,1)
Sat2_date(iii,1) = str2double(Sat2_list(iii).name(28:35));
end
% define output table
Sat1_Sat2 =NaN(size(Sat1_data,1),10);
%% start loop
for i = 1 : length(Sat2_list)
ind_Sat1 = find(Sat2_date(i) == Sat1_date)
if isempty(ind_Sat1) == 0
fname_Sat2 = fullfile(Sat2_path,Sat2_list(i).name)
if isempty (fname_Sat2)==0
%% read Sat2 DATA
Sat2_lat2 = ncread(fname_Sat2, 'lat'); %101x1 double
Sat2_lon2 = ncread(fname_Sat2, 'lon'); %113x1 double
[X,Y] = meshgrid(Sat2_lat2,Sat2_lon2); %113x101 double
Carbon = squeeze(ncread(fname_Sat2, 'CAMASS')); %113x101x24 double
S_Salt = squeeze(ncread(fname_Sat2, 'SAMASS')); %113x101x24 double
Sat1_sub = Sat1_data(ind_Sat1,:);
Sat1_time_sub = Sat1_time(ind_Sat1);
Sat1_aer_sub = NaN(length(ind_Sat1),3)
for j = 1:length(ind_Sat1)
Sat1_lat = Sat1_sub(j,6)
Sat1_lon = Sat1_sub(j,7)
Sat1_time1 = Sat1_time_sub(j,1)
Sat1_time2 = Sat1_time1/100
if Sat1_time2>=4 && Sat1_time2<5
Sub_Carbon4 = Carbon(:,:,4); %113x101 double
data_Carbon4 = interp2(X,Y,Sub_Carbon4,Sat1_lon, Sat1_lat);
Sub_S_Salt4 = S_Salt(:,:,4); %113x101 double
data_S_Salt4 = interp2(X,Y,Sub_S_Salt4,Sat1_lon, Sat1_lat);
elseif MODIS_time2>=5 && MODIS_time2<6
Sub_Carbon5 = Carbon(:,:,5); %113x101 double
data_Carbon5 = interp2(X,Y,Sub_Carbon5,Sat1_lon, Sat1_lat);
Sub_S_Salt5 = S_Salt(:,:,5); %113x101 double
data_S_Salt5 = interp2(X,Y,Sub_S_Salt5,Sat1_lon, Sat1_lat);
elseif MODIS_time2>=6 && MODIS_time2<7
Sub_Carbon6 = Carbon(:,:,6); %113x101 double
data_Carbon6 = interp2(X,Y,Sub_Carbon6,Sat1_lon, Sat1_lat);
Sub_S_Salt6 = S_Salt(:,:,6); %113x101 double
data_S_Salt6 = interp2(X,Y,Sub_S_Salt6,Sat1_lon, Sat1_lat);
end
end
end
end
end
After extracting the data at matched time from second satellite 'Sub_Carbon4 = Carbon(:,:,4);' I did interpolation to extract the data from satellite 2 at matched lat lon. I need to do interpolation becuase lat from satellite 1 has values 40.65, 24.28 and so on. Similarly longitude values from satellite 1 are 89.95 114.63. But satellite 2 lat is like this as it was a column vector so I used meshgrid (same goes for lon):
This line returns NaN.
data_Carbon4 = interp2(X,Y,Sub_Carbon4,Sat1_lon, Sat1_lat);
I can't figure out why it is giving me NaN. Kindly tell me what mistake I've made. I'll be grateful.
0 commentaires
Réponse acceptée
Jan
le 3 Mar 2022
interp2() replies NaNs, if the interpolated points are outside the given set of points, so it is a "extrapolation". So stop the omputations in this line and check, if the points to be interpolated at are inside the defined range.
There is no need to use meshgrid, because the first 2 inouts X and Y of interp2 can be vectors. This is even faster.
You can simplify:
Sat1_date = NaN(size(Sat1_data,1),1);
Sat1_time = Sat1_date;
for kkk = 1:size(Sat1_data,1)
Sat1_date(kkk,1) = Sat1_data(kkk,1)*1E4+Sat1_data(kkk,2)*1E2+Sat1_data(kkk,3);
Sat1_time(kkk,1) = Sat1_data(kkk,4)*1E2+Sat1_data(kkk,5);
end
to
Sat1_date = Sat1_data(:, 1:3) .* [1E4, 1E2, 1];
Sat1_time = Sat1_data(:, 4) * 1E2 + Sat1_data(:, 5);
2 commentaires
Jan
le 3 Mar 2022
You have swapped Sat1_lon and Sat1_lat in the interp2 command. This could make the difference.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Reference Applications dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!