Why using interp2 gives NaN

16 vues (au cours des 30 derniers jours)
Zhou Ci
Zhou Ci le 3 Mar 2022
Commenté : Jan le 3 Mar 2022
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.

Réponse acceptée

Jan
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
Zhou Ci
Zhou Ci le 3 Mar 2022
Hi @Jan Thank you for your response.
I cahnged the line from
data_Carbon4 = interp2(X,Y,Sub_Carbon4,Sat1_lon, Sat1_lat);
To
data_Carbon = interp2(Sat2_lat2, Sat2_lon2, Sub_Carbon4, Sat1_lat, Sat1_lon)
It gives me value now.
But I don't understand that if I put my values like syntax below it gives me NaN:
And using this gives me value
Vq = interp2(Y,X,V,Yq,Xq)
Why it's like this?
And thankyou for showing the simple way to extract day and time from sat1.
Jan
Jan le 3 Mar 2022
You have swapped Sat1_lon and Sat1_lat in the interp2 command. This could make the difference.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Reference Applications dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by