NetCDF variable errors please help

3 ビュー (過去 30 日間)
Abigail Waring
Abigail Waring 2021 年 4 月 28 日
Trying to create a plot for SST from a netcdf file.
It keeps throwing up the following errors at the variable section. Does anyone know what to do to fix it please?
Error using netcdflib
The NetCDF library encountered an error during execution of 'inqVar' function - 'Variable not
found (NC_ENOTVAR)'.
Error in netcdf.inqVar (line 26)
[varname,xtype,dimids,natts] = netcdflib('inqVar', ncid, varid);
Error in load_SST_2 (line 41)
[varname4, ~, ~, ~] = netcdf.inqVar(ncid,4);
Here is my code
% load_SST_2
%% Set some options...
% Choose netcdf file
nc_file = '../data/SST/SST_data.nc'; % put the full path to the netcdf file between the inverted commas
%% Look at netcdf file metadata
ncdisp(nc_file);
%% Open netcdf file
disp(['Loading ' nc_file]);
ncid = netcdf.open(nc_file);
%% Get variable names from netcdf file
[varname0, ~, ~, ~] = netcdf.inqVar(ncid,0);
% Get variable ID of the variable, given its name.
varid0 = netcdf.inqVarID(ncid,varname0);
% Get the value of the first variable, given its ID.
time = double(netcdf.getVar(ncid,varid0));
[varname1, ~, ~, ~] = netcdf.inqVar(ncid,1);
% Get variable ID of the variable, given its name.
varid1 = netcdf.inqVarID(ncid,varname1);
% Get the value of the first variable, given its ID.
latitude = double(netcdf.getVar(ncid,varid1));
[varname3, ~, ~, ~] = netcdf.inqVar(ncid,3);
% Get variable ID of the variable, given its name.
varid3 = netcdf.inqVarID(ncid,varname3);
% Get the value of the first variable, given its ID.
longitude = (netcdf.getVar(ncid,varid3));
[varname4, ~, ~, ~] = netcdf.inqVar(ncid,4);
% Get variable ID of the variable, given its name.
varid4 = netcdf.inqVarID(ncid,varname4);
% Get the value of the first variable, given its ID.
sst = double(netcdf.getVar(ncid,varid4));
% Using first version
sst = squeeze(sst(:,:,1,:));
% Swap rows and columns to be in MATLAB 'row major' format
sst = permute(sst, [2 1 3]);
% Assign the NoData value (see output from ncdisp) to NaN
sst(sst == -32767) = NaN;
%% Make grids of lat and lon (i.e. geographic location of each pixel)
disp('Preparing data for plotting...');
[lon_grid, lat_grid] = meshgrid(lon(:), lat(:));
%% Convert dates to MATLAB datetime
% netcdf time format: 'hours since 1900-01-01 00:00:00.0'
matlab_datetime = datetime(1960,1,1) + hours(time);
%matlab_datetime = datenum(matlab_datetime);
%% loop through time dimension
% choose region of interest
ROI = find(lat_grid > 80 & lat_grid < 83 & lon_grid > -65 & lon_grid < -50);
for ii = 1:length(matlab_datetime)
sst_tmp = sst(:,:,ii);
sst_ROI(ii) = median(sst_tmp(ROI),'omitnan');
end
disp('Plotting and saving a figure...');
% Make a figure
figure('units','normalized','outerposition',[0 0 1 1],'visible','on');
set(gcf,'color','w');
% Set axis font size
font_size = 12;
set(0, 'DefaultAxesFontSize', font_size);
plot(matlab_datetime(:),sst_ROI(:),'-r');
%%
%Set plot details
ylabel ('T2M (degrees celcius)');
xlabel ('Time (years)');
title ('Sea Suface Temperature Dataset at 81.5N & -63.5W, 2011-2021');
legend({'= SST'},'Location','southwest');
ax = gca;
ax.FontSize = 14;

回答 (0 件)

カテゴリ

Help Center および File ExchangeNetCDF についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by