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

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)

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

@KSSV It gave me this error:
Error using .'
TRANSPOSE does not support N-D arrays. Use PAGETRANSPOSE/PAGECTRANSPOSE to transpose pages or PERMUTE to reorder
dimensions of N-D arrays.
My values (data) to extracted from netcdf is 113x101x24. Is this causing error?
It seems your data is 3d, run a loop for each data.
I didn't get your point. What is meant by 'Each data'?
[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.

Asked:

IMC
on 28 Feb 2022

Commented:

on 28 Feb 2022

Community Treasure Hunt

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

Start Hunting!