How to regrid data based on longitude-latitude variables?
Show older comments
Hey Matlab world!
I am trying to regrid the data (file attached "air") from a 2.5 by 2.5 grid to a 0.5 by 0.5 grid of a 3d matrix ("air"). The air variable is characterized as lon x lat x time (144 x 73 x 72). So I wanted to change the longitude and latitude resolution without affecting the time variable i.e. from 144 x 73 x 72 -to- 721 x 361 x 72.
I did try interp2 and still get an error "Index in position 3 exceeds array bounds (must not exceed 72)".
Here's the code I have used thus far:
lon=ncread('air.nc','lon');
lat=ncread('air.nc','lat');
time=ncread('air.nc','time');
air=ncread('air.nc','air');
ox=0:0.5:360;
oy=-90:0.5:90;
new=interp2(lon,lat,air(:,:,time),ox,oy);
This might be a silly problem, but I can't seem to understand the error, since I am doing a 2d interpolation. Looking forward to your help
1 Comment
Olawale Ikuyajolu
on 12 May 2020
Why not use CDO to regrid this data
Answers (2)
Olawale Ikuyajolu
on 12 May 2020
lon=ncread('air.nc','lon');
lat=ncread('air.nc','lat');
time=ncread('air.nc','time');
air=ncread('air.nc','air');
ox=[0:0.5:360-0.5]; %0 degrees and 360 degrees (180 east and west are the same). it is continuous
oy=[-90:0.5:90]';
for time = 1: size(air, 3) %loop through each time
new(:,:,time) =interp2(lon,lat,air(:,:,time)',ox,oy);
end
13 Comments
Keegan Carvalho
on 12 May 2020
Edited: Keegan Carvalho
on 15 May 2020
Walter Roberson
on 16 May 2020
What is size(lon), size(lat), size(air), size(time) ?
Keegan Carvalho
on 16 May 2020
Edited: Keegan Carvalho
on 16 May 2020
Walter Roberson
on 16 May 2020
lon=ncread('air.nc','lon');
lat=ncread('air.nc','lat');
time=ncread('air.nc','time');
air=ncread('air.nc','air');
qlon=[0:0.5:360-0.5]; %0 degrees and 360 degrees (180 east and west are the same). it is continuous
qlat=[-90:0.5:90];;
[QLON, QLAT] = ndgrid(qlon, qlat);
nair = size(air,3);
new = zeros(size(QLON,1), size(QLON,2), nair);
for time = 1 : nair
new(:,:,time) = interp2(lon, lat, air(:,:,time).', QLON, QLAT);
end
for time = 1 : nair; surf(QLON, QLAT, new(:,:,time), 'edgecolor', 'none'); drawnow limitrate; end
Keegan Carvalho
on 16 May 2020
Maurício Andrade
on 5 Sep 2022
If I have my lat long related to the final result of my interpolation is a non-linear grid, could I do this regrid? I am dealing with this problem here. I need to adjust data to a specific lat and long, but my lat is not linear.
Walter Roberson
on 5 Sep 2022
The interp2() code that I posted above does not care whether the grid of query points QLON, QLAT is linear or not.
Maurício Andrade
on 13 Sep 2022
Thank you, Walter. But unfortunately, when I interpolate, my outcome matrix is strange. The outcome, when plotted, is something like this picture attached. All blue dots are NaN instead of salinity data in the ocean.

Walter Roberson
on 13 Sep 2022
The only part of that that looks potentially wrong to me, is just a bit right from the top center. Starting from the top center you have data towards the right. Then that orange-yellow section ends. If you look along the top a bit further right, there is a small light blue streak. I estimate that small streak marks the end of the selection area following a line of longitude. At the moment I do not see any reason there should not be orange/yellow data between the current end and the small blue streak. Not unless there is some land there that I am not making out properly. I can see that there is indeed some land there, but it seems to me that there should be salinity data between the two islands.
Everything else about the map looks fine to me. One longitude boundary at (probably) the prime meridian, another longitude boundary at what looks to be roughly 75 East, north pole to south pole. Seems fine.
Maurício Andrade
on 14 Sep 2022
I think I didn't explain it well. I'm sorry for it. The outcome of this interoplation should be a global salinity distribution. So, I use a global salinity data from a database (DATA1) to regrid to another lat-long from a second dataset (DATA2). However, after interpolation, that figure is a result: the blue part of global ocean is NaN when it should be interpolated data. Probably, at least I am supposing it, something is going wrong on interpolation because it does not considering the whole global data, once the outcome is limited by long instead of global salinity distribution. Should it be an issue at my machine or version used? (MATLAB2016a)
Here, DATA1 has linear lat (320x1) and long (720x1). DATA2 has linear long (320x1) and non-linear lat (220x1). Lat from DATA2 has high resolution in low latitudes, it decreases in mid-latitudes and increases a bit towards high latitudes again.
This interpolation to a DATA2 grid is because I need to calculate bias.
Maurício Andrade
on 14 Sep 2022
Brief update. The issue I have been dealing with is just a long issue. To use my interp2, data need to be continous, but positive (highlighted to make it easier for other users to find).
Thank you very much for your help (and persistence being here contributing) and assessment, Walter.
Maurício Andrade
on 23 Nov 2022
@Walter Roberson I would like to check out with you if possible if this command works well from tripolar to regular grid as well. I am struggling to find out information like this. Do you know if it works?
Walter Roberson
on 23 Nov 2022
I do not know how tripolar data is represented? I would not expect you to be able to use interp2 to for tripolar data.
Bjorn Gustavsson
on 12 May 2020
First of all, your time-variable contains integers between 1411296 and 1463136, when you try to use those as indices you're looking for components in air way outside the size of that array. So you have to index with integers between 1 and 72. In my version of matlab this works fine:
new=interp2(lon,lat,air(:,:,1)',ox,oy')';
Where I had to transpose the oy to make interp2 happy.
HTH
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!