Extract SST data in a polygon from average monthly netcdf file
Show older comments
I am trying to extract sst data from a netcdf file in a lat/lon polygon of 25:45S and 145-215E using a script below. I have attached the text file which I created ('sst2') from the original netcdf ('202210.sst.avg.nc') file because I could not upload the original netcdf file due to unaccepted file format. When I run the script I only get an empty cell "Poly = []". Can someone help please?
data=netcdf.open('202210.sst.avg.nc','NC WRITE');
[ndim,nvar,natt,unlim]=netcdf.inq(data);
% Extract data
sst=netcdft.getVar(data,0);
lat=netcdft.getVar(data,1);
lon=netcdft.getVar(data,2);
sst(sst==-999)=NaN;
sst1=permute(sst,[2,1]);
% Reshape data to be from 90N to 90S
sst2=[sst1(180:-1:91,:);sst1(90:-1:1)];
la1=[lat(180:-1:91);lat(90:-1:1)];
T=table(sst2);
writetable(T,'sst2.txt')
%Define polygon
[X,Y]=meshgrid(lon,la1);
idx=inpolygon(X,Y,[140 215],[25 45]);
poly = nanmean(sst2(idx));
%'poly' is empty with closing square bracket when the script is run.
2 Comments
Hi Simbarashe,
When trying to create the file for debugging the issue, the file isn't loading. Can you try reuploading the correct file or sending the .nc file. Also, a suggestion to try the renaming the 202210.sst.avg.nc file to some standard name like 202210.nc this might help resolving the issue.
% Load data from text file
data = load('sst2.txt');
% Create NetCDF file
ncid = netcdf.create('sst2.nc','NC_WRITE');
Thanks
Simbarashe Chidzambwa
on 16 Mar 2023
Accepted Answer
More Answers (0)
Categories
Find more on NetCDF 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!