Clear Filters
Clear Filters

How to compare lat lon from text file with netcdf file?

1 view (last 30 days)
Hi
I am trying to extract data based on lat lon values (it's a netcdf file). Before doing this step I need to compare lat lon values from 2 datasets.
I have a text file that contains 2 columns (lat and lon) as shown below:
lat lon
39.74 132.01
13.38 126.41
24.28 127.22
lat lon from netcdf file was in the form of column vector. So, I used meshgrid:
M_lat = ncread(fname, 'lat');
M_lon = ncread(fname, 'lon');
[X,Y] = meshgrid(M_lon, M_lat);
Now lat has become: (same goes for longitude)
Problem is my lat in text file is 39.74 and values in netcdf are 39.5 and 40. What is the possible solution for this kind of issue?
Based on index I need to extract data from my netcdf file. Thank you

Answers (1)

KSSV
KSSV on 28 Feb 2022
REad about interp2. You can do the interpolation and extract your required data.
data2 = interp2(X1,Y1,data1,X2,Y2) ;
% where X1, Y1, data1 are from netcdf
% X2, Y2 is your grid from text file
If you don't want to do interpolation; you can get the nearest points using knnsearch and extract the data by indexing.
  4 Comments
IMC
IMC on 28 Feb 2022
I didn't get your point. What is meant by 'Each data'?
KSSV
KSSV on 28 Feb 2022
[m1,n1,p1] = size(data2) ;
[m2,n2] = size(X2) ;
data2 = zeros(m2,n2,p1) ;
for i = 1:p1
data2(:,:,i) = interp2(X1,Y1,data1(:,:,i)',X2,Y2)' ;
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!