How to use inpolygon or other tool to mask netcdf with different lat/lon matrix size to shapefile having different lat/lon matrix size?

11 views (last 30 days)
I am trying to mask netcdf (unable to attach as zip file exceeds 5MB) based on a Mississippi river basin shapefile(attached) using inpolygon. But I get this following error:
Arrays have incompatible sizes for this operation.
Error in inpolygon (line 76)
mask = (x >= min(xv)) & (x <= max(xv)) & (y>=min(yv)) & (y<=max(yv));
Error in plot_soilice (line 10)
[in,on] = inpolygon(lon,lat,C.X,C.Y);
Below is the code I used:
%% shape must be in lat Lon projection
C = shaperead('\MRB_Shapefile\mrb.shp');
%shapeinfo('\Miss_RiverBasin\Miss_RiverBasin.shp');
%corresponding name of lat, Lon and variable at netcdf
% lat and lon are matrixes of, like.. lat[nrow,ncol] .. same for var[nrow,ncol]
lat=ncread('\Noah.monthlymean.01.nc','lat');
lon=ncread('\Noah.monthlymean.01.nc','lon');
SMC=ncread('\\Noah.monthlymean.01.nc','SMC');
SMC_n = squeeze(SMC(:,1,:));
[in,on] = inpolygon(lon,lat,C.X,C.Y);
numel(lat(~in)) + numel(lat(in)); % must be the same size of ncol*nrow in var[nrow,ncol]
%in2 is the mask used to extract the values
in2 = double(in);
clf
mymap=pcolor(lon,lat,in2); %plot mask to verify, Is it what you want?
%%based on the indexes you can calculate max, min, mean.... whatever you want...
I assume this will happen with any shapefile as the lat lon grids of shapefile will be different from the netcdf lat/lon grids. Eventually, the matrices of lat/lon from shapefile and netcdf lat/lon matrice will always be different. So, I am looking for a generic solution that can be applied for any shapefile and any netcdf file meaning different matrix sizes of lat lon. Does anybody have any suggestions for this problem and a generic solution?
Any suggestions or help is appreciated.
Thanks

Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!