How to do matrix regridding in matlab

Hello, I am trying to use matlab to regrid a dataset. The original dataset is 128x193x12 (longitude x latitude x time) and I want to regrid it to a 180x193x12 matrix. I have the latitude of the original dataset as a 64x1 single (Dust_Data_lat), the longitude as a 128x1 single (Dust_Data_lon). For the 'final' grid (180x193x12), I have a latitude matrix 180x193 (constant columns, DUST_IN_lat) and a longitude matrix 180x193 (constant rows, DUST_IN_lon). I think the function griddata is best for this as the grid is irregular. If I try however, it complains about 'Z', which I don't know, see here for the first time dimension of the data:
a=Dust_LGM(:,:,1);
data_regridded = griddata(Dust_Data_lon,Dust_Data_lat,a,DUST_IN_lon,DUST_IN_lat);
The error I get is:
Error using griddata (line 109)
The lengths of X and Y must match the size of Z.
Would you know where the mistake is?

2 Comments

You should close your previous question by accept-clicking Jos' answer, since it appears you were happy with that answer. This will increase the likelihood of responses to future questions.
Ok, thanks for letting me know!

Sign in to comment.

Answers (1)

Matt J
Matt J on 6 Apr 2018
Edited: Matt J on 6 Apr 2018
In griddata(x,y,z,xq,yq) the input arrays x,y,z must have the same number of elements. You have 128*193=24704 elements in a. You must therefore have 24704 corresponding elements in both Dust_Data_lon and Dust_Data_lat.

15 Comments

Thank you, I used meshgrid to make a grid of the lon lat data:
[Xlon Ylat] = meshgrid(Dust_Data_lat, Dust_Data_lon);
The result looks very strange however, so I think something is still wrong - mixing up lon and lat maybe.. Here is the full script I use now:
clear all
% Input path
Tpath = '/home/mypath';
% Get the dust input data
ncid = netcdf.open([Tpath 'INPDUST_mhw.nc'],'NC_NOWRITE');
varid1 = netcdf.inqVarID(ncid,'DUST');
fillval = netcdf.getAtt(ncid,varid1,'_FillValue');
Dust_IN = netcdf.getVar(ncid,varid1,'double');
varid2 = netcdf.inqVarID(ncid,'lon');
DUST_IN_lon = netcdf.getVar(ncid,varid2,'double');
varid3 = netcdf.inqVarID(ncid,'lat');
DUST_IN_lat = netcdf.getVar(ncid,varid3,'double');
% Get the dust input data
ncid = netcdf.open([Tpath 'DustFlux_Lambert2015_cp.nc'],'NC_NOWRITE');
varid4 = netcdf.inqVarID(ncid,'Flux');
Dust_Data = netcdf.getVar(ncid,varid4,'double');
varid5 = netcdf.inqVarID(ncid,'lon');
Dust_Data_lon = netcdf.getVar(ncid,varid5,'double');
varid6 = netcdf.inqVarID(ncid,'lat');
Dust_Data_lat = netcdf.getVar(ncid,varid6,'double');
varid7 = netcdf.inqVarID(ncid,'time');
Dust_Data_time = netcdf.getVar(ncid,varid7,'double');
% get the LGM period
Dust_LGM = Dust_Data(:,:,:,2);
% Prepare the Lambert Data lon and lat as a grid
[Xlat Ylon] = meshgrid(Dust_Data_lat, Dust_Data_lon);
% Regrid
for i=1:size(Dust_LGM,3)
%linear interpolation is applied. some boundary values can not be interpolated. these are filled with nearest values
Dust_LGM_regridded(:,:,i) = griddata(Ylon,Xlat,Dust_LGM(:,:,i),DUST_IN_lon,DUST_IN_lat,'linear');
if Dust_LGM_regridded==NaN
Dust_LGM_regridded=griddata(Ylon,Xlat,Dust_LGM(:,:,i),DUST_IN_lon,DUST_IN_lat,'nearest');
end
end
% create a netcdf
delete('DustFlux_Lambert2015_regridded.nc');
outfile_nc = 'DustFlux_Lambert2015_regridded.nc'; % New output file
nccreate(outfile_nc,'lon','Dimensions',{'x',size(Dust_LGM_regridded,1),'y',size(Dust_LGM_regridded,2)},'Datatype','double','Format','classic');
nccreate(outfile_nc,'lat' ,'Dimensions',{'x',size(Dust_LGM_regridded,1),'y',size(Dust_LGM_regridded,2)},'Datatype','double','Format','classic');
nccreate(outfile_nc,'time','Dimensions',{'time',size(Dust_LGM_regridded,3)},'Datatype','double');
nccreate(outfile_nc,'DUST','Dimensions',{'x','y','time'},'Datatype','double');
% Write data to the created variables
ncwrite(outfile_nc, 'lon',DUST_IN_lon);
ncwrite(outfile_nc, 'lat',DUST_IN_lat);
ncwrite(outfile_nc, 'time',Dust_Data_time);
ncwrite(outfile_nc, 'DUST',Dust_LGM_regridded);
The netcdf files used are here: https://www.dropbox.com/sh/i9m5p15ni8ybpzc/AABzJA_a8TTy3sWOCT4jkga1a?dl=0
Where does it go wrong?
You said initially that your data was scattered, not gridded. If that is still true, then it's hard to see how meshgrid would make sense.
Hey Matt, thank you again for answering. I wrote that my data have an irregular grid (is that scattered for matlab?): the amount of degrees between to latitudes is not necessarily the same. That is why I chose meshgrid, but maybe I should pick something else? The result of meshgrid looked OK I think, at least quite similar to DUST_IN_lon and DUST_IN_lat.
Matt J
Matt J on 9 Apr 2018
Edited: Matt J on 9 Apr 2018
If your data is gridded (irregular or not), you should probably use griddedInterpolant or interpn, rather than griddata.
How can I check whether the data are 'gridded'? I will be able to work in this again tomorrow. Thank you again for your help thus far
Matt J
Matt J on 9 Apr 2018
Edited: Matt J on 9 Apr 2018
"Gridded" means that the set of all (x,y) coordinates corresponding to the different input z data points can be generated using meshgrid() or ndgrid(). In other words, they lie at the nodes of some rectangular lattice. If this is not the case, the data is said to be "scattered".
interpn has the same result as griddata. I don't understand what griddedInterpolant needs as input.
I think the result is wrong because Lon and Lat somehow get mixed up. In my DUST_IN_lon and in Ylon the rows are constant and in DUST_IN_lat and Xlat, the columns are constant. This seems the wrong way around, and might be the cause of the strange result of the interpolation. I don't know how to solve that however, so any advice on that would be appreciated. As I have now spent quite some time on this already, I might otherwise give up..:S
I suggest that you attach a .mat file containing Dust_Data_lon,Dust_Data_lat,Dust_LGM,DUST_IN_lon,DUST_IN_lat. That way, we can all play with it.
Dear Matt, here are all the data. Just to summarize: The Dust_IN data have the grid I want, as described by DUST_IN_lon and DUST_IN_lat. The data that need to be changed to that grid are in Dust_LGM, which grid is described by Dust_Data_lon and Dust_Data_lat.
Matt J
Matt J on 11 Apr 2018
Edited: Matt J on 11 Apr 2018
In my DUST_IN_lon and in Ylon the rows are constant.
I don't see constant rows or columns in DUST_IN_lon. There are funny things happening at the edges.
Mmm, that is odd. Thank you for your help. I think I will try to find more information about the source of this dataset. Do you think it is useful to let this question stay here on mathworks? Or shall I remove it?
You cannot (and should not) remove it. If my response leads to the resolution of your issue, you should also Accept-click it.
It looks like your range of longitudes cross the prime meridian (0 longitude). You may want to make all longitudes positive (for example adding 360 to the longitudes less than zero) before moving forward.
Hey Yoichi, thank you for your suggestion. This post is already very old, and I learned CDO and NCO to handle netcdf data in a more efficient way. I can recommend them to anyone that has to analyze/regrid/handle a lot of netcdf files:)
Hey Anne. I am struggling how to regrid with lon and lat right now. Do you have any recommandation? Thank you very much!

Sign in to comment.

Categories

Find more on External Language Interfaces in Help Center and File Exchange

Asked:

on 4 Apr 2018

Commented:

on 19 Jul 2019

Community Treasure Hunt

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

Start Hunting!