Time series extraction from netcdf
3 views (last 30 days)
Show older comments
I'm trying to extract a timeseries from a netcdf file linked here: " https://www.dropbox.com/s/qi3dv1ohpxlcp3y/MetO-NWS-WAV-hi_1612364408068.nc?dl=0 ", but I get the error message: "Index in position 3 is invalid. Array indices must be positive integers or logical values".
The code I have used is below:
filename = ('METO-NWS-WAV-hi_1612364408068.nc');
info = ncinfo(filename);
ncid=netcdf.open(filename,'nowrite');
% Get information about the contents of the file.
[numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncid);
disp(' '),disp(' '),disp(' ')
disp('________________________________________________________')
disp('^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~')
disp(['VARIABLES CONTAINED IN THE netCDF FILE: ' filename ])
disp(' ')
for i = 0:numvars-1
[varname, xtype, dimids, numatts] = netcdf.inqVar(ncid,i);
disp(['--------------------< ' varname ' >---------------------'])
flag = 0;
for j = 0:numatts - 1
attname1 = netcdf.inqAttName(ncid,i,j);
attname2 = netcdf.getAtt(ncid,i,attname1);
disp([attname1 ': ' num2str(attname2)])
if strmatch('add_offset',attname1)
offset = attname2;
end
if strmatch('scale_factor',attname1)
scale = attname2;
flag = 1;
end
end
disp(' ')
if flag
eval([varname '= double(double(netcdf.getVar(ncid,i))*scale + offset);'])
else
eval([varname '= double(netcdf.getVar(ncid,i));'])
end
end
disp('^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~')
disp('________________________________________________________')
disp(' '),disp(' ')
% Load in a few selected variables
dtime = ncread(filename,'time');
dtime = dtime/(60*60*24)+datenum(1900,1,1); %Convert to Matlab time
VHM0 = ncread(filename,'VHM0');
VHM0(VHM0==0)=NaN; %Remove some bad points
VMDR = ncread(filename,'VMDR');
VMDR(VMDR==0)=NaN; %Remove some bad points
VTM02 = ncread(filename,'VTM02');
VTM02(VTM02==0)=NaN; %Remove some bad points
lon = ncread(filename,'longitude');
lon(lon==0)=NaN; %Remove some bad points
lat = ncread(filename,'latitude');
lat(lat==0)=NaN; %Remove some bad points
location_lat = knnsearch(lat,51.9);
location_lon = knnsearch(lon,1.6);
myVHM0Data = squeeze(VHM0(location_lon,location_lat,dtime));
myVHM0Datenum = dtime(time);
% Time Series Plots
figure(1)
subplot(3,1,1)
plot(myVHM0Datenum,myVHM0Data,'.')
datetick('keeplimits');
ylabel('Hs (m)')
subplot(3,1,2)
plot(myVHM0Datenum,VMDR,'.')
datetick('keeplimits');
ylabel('VMDR (degrees)')
title(source,'interpreter','none');
set(gca,'ydir','reverse'); %Flip the y-axis
subplot(3,1,3)
plot(myVHM0Datenum,VTM02,'.')
datetick('keeplimits');
ylabel('Tp (s)')
I would appreciate any help.
Answers (0)
See Also
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!