- datetime - https://www.mathworks.com/help/matlab/ref/datetime.html
- hours - https://www.mathworks.com/help/matlab/ref/duration.hours.html
- squeeze - https://www.mathworks.com/help/matlab/ref/squeeze.html
How do I create a time series from netCDF?
12 views (last 30 days)
Show older comments
How do I plot a time series of D from a netCDF file? D is 4D, size: 25x41x37x1464, equivilent to lat x lon x pressure levels x time. I have extracted each sub variable to create 1 by X variables of lat, lon, level, time using:
ncfile= 'divergence.nc';
D=ncread(ncfile,'d');
%Gregorian_time: hours since 1900-01-01 00:00:00.0'
time=ncread(ncfile,'time'); %size 1x1464
%Pressure_level: millibars %size 1x37
level=double(ncread(ncfile,'level'));
%Latitude: degrees_north %size 1x25
lat=double(ncread(ncfile,'latitude'));
%Longitude: degrees_east %size 1x41
lon=double(ncread(ncfile,'longitude'));
How do I create a time series of D?
Thanks!
0 Comments
Answers (1)
Paras Gupta
on 19 May 2024
Hi Raymond,
To create a time series plot of the variable 'D' from your netCDF file, you'll first need to decide on the specific slice of data you want to plot over time. Since D is a 4-dimensional variable, you'll have to select specific values for three of the dimensions (latitude, longitude, and pressure level) to plot the time series for the remaining dimension (time).
The following code shows how to acheive the same in MATLAB:
ncfile= 'divergence.nc';
D=ncread(ncfile,'d');
%Gregorian_time: hours since 1900-01-01 00:00:00.0'
time=ncread(ncfile,'time'); %size 1x1464
%Pressure_level: millibars %size 1x37
level=double(ncread(ncfile,'level'));
%Latitude: degrees_north %size 1x25
lat=double(ncread(ncfile,'latitude'));
%Longitude: degrees_east %size 1x41
lon=double(ncread(ncfile,'longitude'));
% Select specific latitude, longitude, and pressure level indices
latIndex = 1; % Example: first latitude
lonIndex = 1; % Example: first longitude
levelIndex = 1; % Example: first pressure level
% Convert the time variable to datetime format
% Convert 'time' from hours since 1900-01-01 00:00:00.0 to datetime
refDate = datetime(1900,1,1,0,0,0); % Reference date
timeInDatetime = refDate + hours(time - time(1)); % Adjust based on the first time point to avoid precision loss
% Extract the time series data for the selected indices
D_timeSeries = squeeze(D(latIndex, lonIndex, levelIndex, :));
% Plot the time series
figure; % Create a new figure
plot(timeInDatetime, D_timeSeries);
xlabel('Time'); % Label the x-axis
ylabel('D value'); % Label the y-axis
title(sprintf('Time Series of D at Lat %.2f, Lon %.2f, Level %d mb', lat(latIndex), lon(lonIndex), level(levelIndex)));
grid on; % Add a grid for easier reading
Please refer to the following documentations for more information on the functions used in the code above:
Hope this helps.
0 Comments
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!