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

조회 수: 6 (최근 30일)
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
  댓글 수: 1
J A
J A 2022년 2월 3일
Matlab Community,
Please suggest or let me know if something is unclear in the question.
I appreciate your time and effort.
Thanks

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 NetCDF에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by