3hr time avarage plot of data

Hello everyone
I am trying to plot .cdf file with a 3hr moving avaerage and i started the code as follwos . I couldnt attach the data as it says data format is not supported. But here is the website to get the data from
"https://swarm-diss.eo.esa.int/#swarm%2FAdvanced%2FPlasma_Data%2F2_Hz_Langmuir_Probe_Extended_Dataset%2FSat_A"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=('SW_EXTD_EFIA_LP_HM_20131216T000000_20131216T235959_0106.cdf');
Ne = cell2mat(cdfread(data,'Variables',{'n'})); % Electron density
ti=(cdfread(data,'Variables',{'Timestamp'})); % Time
%%%%%%%%%%%%%%%%%%%%%% Time conversion to get HH:MM:SS format
for k=1:length(ti)
datenum2(k) = todatenum(ti{k});
end
T = datetime(datenum2, 'Format','HH:mm:ss', 'ConvertFrom','excel');
t1=T';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
avg_Ne = movmean(Ne, 3*60*60/median(diff(t1)));
plot(t1, avg_Ne);
xlabel('Time');
ylabel('Ne');
title('3-Hour Time Average');
%%%%%%%%% The Error I am having
Error using cdf_3hr_average (line 82)
The denominator in matrix division for duration
arrays must be a real scalar double value.
I would be happy to hear any comments .
Thank you

 Accepted Answer

This problem is made much simpler if you leverage the capabilities of cdfread. It can read in cdfepochs as datetimes, simplifiing that step. You can then use movmean to create a 3-hour moving mean (see this example).
% Obtain info about what is in the CDF file
filename = "SW_EXTD_EFIA_LP_HM_20131216T000000_20131216T235959_0106.cdf";
ci = cdfinfo(filename)
ci.Variables
% Load the specified variables
data = cdfread(filename,'Variables',{'n','Timestamp'},"DatetimeType","datetime"); % Electron density, Time
Ne = vertcat(data{:,1}); % Electron density
ti = vertcat(data{:,2}); % Time
% compute a 3 hour moving mean
avg_Ne = movmean(Ne, hours(3), 'SamplePoints', ti)
% Visualize
plot(ti, avg_Ne);
xlabel('Time');
ylabel('Ne');
title('3-Hour Time Average');
Since the data is too large to load here, here is a screenshot of what the final plot looked like for me. I have not gone through the data to verify any of the results.

5 Comments

Admitting that I am not familiar with what is appropriate in this field of study, it is my observation that a 3 hour average does not appear to track the raw data very well.
Consider using a shorter window to better represent the raw data. Here is what a 20 minute window looks like.
Thank you so much for your prompt response.
Your code seems not working for me...here is the error displayed
***********************************************
Error using cdfread>parse_inputs (line 525)
Expected input to match one of these values:
'variables', 'records', 'slices',
'convertepochtodatenum', 'combinerecords'
The input, 'DatetimeType', did not match any of
the valid values.
Error in cdfread (line 132)
options = parse_inputs(filename,varargin{:});
Error in average_3hr (line 9)
data =
cdfread(filename,'Variables',{'n','Timestamp'},"DatetimeType","datetime");
% Electron density, Time
Cris LaPierre
Cris LaPierre on 27 Mar 2023
Edited: Cris LaPierre on 27 Mar 2023
What version of MATLAB are you using?
The "DatetimeType","datetime" name-value pair was added in R2022b (see here).
If you are using a pre-R2022b version of MATLAB, try using this instead
% pre R2022b (no DatetimeType option)
data = cdfread(filename,'Variables',{'n','Timestamp'},"ConvertEpochToDatenum",true); % Electron density, Time
Ne = vertcat(data{:,1}); % Electron density
ti = datetime(vertcat(data{:,2}),"ConvertFrom","datenum"); % Time
I am using Matlab_R2020b version.
And now, it worked. Thank you so much!!!

Sign in to comment.

More Answers (0)

Asked:

on 27 Mar 2023

Commented:

on 27 Mar 2023

Community Treasure Hunt

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

Start Hunting!