Daily average of 8 years with data set every 3 hours

2 views (last 30 days)
I have a data set consisting of 8 years from 2006-2013 of SST taken every 3 hours. There are some gaps in the data. I have already created a matrix 74*115*27192 (74 is the lon, 115 is the lat, and 27192 is the SST for all years. Now I have to calculate the daily mean. The Daily mean (Daily_mean) should generate a matrix of 74*115*X (X being the number of day). Instead it is generating a 74*115 matrix only. Any help. Thanks
clear all; close all;
fileList = dir('C:\Users\User\Desktop\Master Thesis\data\yearly\Rosario');
iniTime = datenum(1990, 1, 1, 0, 0, 0);
cnt = 1;
fullSST = zeros (74,115,21792);
fullTime = zeros (2179,1);
for f = 1:numel(fileList);
filename = fileList(f).name;
[dirname name ext] = fileparts(filename);
if (strcmp(ext, '.nc'))
disp(['Processing file: ', filename]);
info = ncinfo (filename);
dataLon = ncread (filename, 'longitude'); %loading longitude data
dataLat = ncread (filename, 'latitude'); %loading latitude data
dataSST = ncread (filename, 'potemp'); %loading SST
dataTime = ncread(filename, 'time');
dataSST=squeeze(dataSST);
for t = 1:1:size(dataSST, 3)
SST = dataSST(:, :, t);
Time = double(dataTime(t));
currDate = addtodate(iniTime, Time, 'hour');
fullSST (:,:,cnt) = SST;
fullTime (cnt) = currDate;
cnt = cnt +1;
end
end
end
%Full data set
startDate = datenum([2007 1 1 0 0 0]);
EndDate = datenum ([2013 12 31 0 0 0]);
currDate = startDate;
while (currDate <= EndDate)
currDateVect = datevec(currDate);
aDate = datenum([currDateVect(1) currDateVect(2) currDateVect(3) 0 0 0]);
bDate = datenum([currDateVect(1) currDateVect(2) currDateVect(3) 23 59 59]);
% find corresponding indices
start_index = find(fullTime >= aDate);%find data > than the StartTime
end_index = find(fullTime <= bDate); %find index of data ending in EndTime
index = intersect(start_index, end_index);
if (numel(index) > 0)
Daily_mean = mean(fullSST(:, :, index), 3);
Daily_time = currDate;
end
currDate = addtodate(currDate, 1, 'day');
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Aug 2016
MF - look closely at your code
if (numel(index) > 0)
Daily_mean = mean(fullSST(:, :, index), 3);
Daily_time = currDate;
end
You reset Daily_mean on each iteration of your while loop so you lose whatever it was initialized to previously and so when you program completes, you are left with the mean of the last day only.
If you know how many days there are, then you can pre-initialize this array as
numDays = 42;
Daily_mean = zeros(74,115,numDays);
atDay = 1;
while (currDate <= EndDate)
% etc.
if (numel(index) > 0)
Daily_mean(:,:, atDay) = mean(fullSST(:, :, index), 3);
Daily_time = currDate;
end
atDay = atDay + 1;
end
Note how we use atDay to update Daily_mean. If you don't know the number of days, then you can do the same as above but just initialize Daily_mean as
Daily_mean = [];
  2 Comments
Tanziha Mahjabin
Tanziha Mahjabin on 28 Feb 2020
Hi, i want to plot a Hovmoller diagram or heatmap from satellite SST of several years. I have the data in daily average. I want to do weekly average. So i need to find every week (monday:sunday). How can i do that?
If tvec is the time (attached example data),
t=datetime(tvec)
d2 = dateshift(t,'dayofweek','monday') %this supposed to give each monday
But i am not sure how else am i supposed to that? Any help will be appreciated.

Sign in to comment.

More Answers (1)

Chad Greene
Chad Greene on 16 Aug 2016
A simpler solution might be downsample_ts.

Tags

Community Treasure Hunt

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

Start Hunting!