Extract SST data in a polygon from average monthly netcdf file

2 vues (au cours des 30 derniers jours)
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');
Error using netcdf.open
Could not open file '202210.sst.avg.nc'.
[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 commentaires
Abhijeet
Abhijeet le 9 Mar 2023
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');
Error using load
Unknown text on line number 1 of ASCII file sst2.txt
"sst2_1".
% Create NetCDF file
ncid = netcdf.create('sst2.nc','NC_WRITE');
Thanks
Simbarashe Chidzambwa
Simbarashe Chidzambwa le 16 Mar 2023
Thank you Abhijeet, unfortunately I cannot load the netcdf file due to file format restrictions.

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 13 Mar 2023
Simbarashe, readtable and readmatrix read your file just fine, so I don't think that's the issue:
>> sst2 = readmatrix("sst2.txt");
>> whos
Name Size Bytes Class Attributes
sst2 180x360 518400 double
You have not provided the lat and lon vectors, and I'm not really able to follow your code to glean what they are. But if what you want is the data in a rectangular subset of your data (that assumes it's on a rectangular grid in lat/lon space), I would think you can just use row/column indexing and skip the thing with meshgrid and inpolygon. Something like
>> nanmean(sst2(1:10,1:10))
ans =
-1.686 -1.6809 -1.6667 -1.6505 -1.6336 -1.6096 -1.574 -1.5394 -1.5111 -1.4503
>> nanmean(sst2(1:10,1:10),"all") % or maybe
ans =
-1.6002
But it's pretty hard to tell what you have. If the output of nanmean is really [] as you say, then you have an issue with idx, which, as I said, seems like you can avoid that whole calculation.

Plus de réponses (0)

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by