MATLAB: How to extract seasonal means

extractmapnetcdfplotseasonal

Hey MATLAB universe!
I have a file 360 x 40 x 444 (longitude x latitude x time). These are MONTHLY temperature values. The 444 are time values ("days since 1800"). I wanted to extract seasonal mean data for each year.
Example: Winter- Dec,Jan,Feb for each year and then take a mean for each year. So technically I should get a matrix of 360 x 40 x 37 (since this data set contains monthly values from January 1982 to December 2018). I have used the following code but I get wrong values if I map them:
lon=ncread('sstarc.nc','lon');
lat=ncread('sstarc.nc','lat');
sst=ncread('sstarc.nc','sst');
DJF = sort([[1:12:444],[2:12:444],[12:12:444]]);
Wacc = cumsum(sst(:,:,DJF));
Wavg = Wacc(:,:,3:3:end)/3;
I would like to know where I am going wrong. (Data file is attached and arranged as Jan-1982, Feb-1982,…Dec-2018.)
Thank you

Best Answer

For those just coming here, you can see additional details and explanation here.
You seem to be grouping Jan, Feb and Dec from the same year together. I would be more inclined to group the winters as Dec yr1 with Jan yr2, Feb yr2. In doing so, I see the challenge being.
  1. Grouping Dec from one year with Jan, Feb of the next
  2. The first year will only have Jan, Feb while the last year will only have Dec.
  3. If no data is removed, this means your will have data covereing 38 winters (Jan, Feb in last year as well as Dec in last year).
You asked what you were doing wrong. I don't think cumsum is doing what you want. The first value is itself. The second is the first plus the second. This pattern continues until the last value is the sum of all previous values plus itself. Instead, you want to sum each group separately.
Here's an approach that uses findgroups/splitapply
% Load data from nc file
lon=ncread('sstarc.nc','lon');
lat=ncread('sstarc.nc','lat');
sst=ncread('sstarc.nc','sst');
% Create vector of dates to use for extracting winter months
startDate = datetime(1982,1,1,"Format","MMM-uuuu");
endDate = datetime(2018,12,31,"Format","MMM-uuuu");
dates = startDate:calmonths(1):endDate;
winterMonth = month(dates) == 1 | month(dates) == 2 | month(dates) == 12;
% Extract winter data from sst
sstWinter = sst(:,:,winterMonth);
%% Create grouping variable for winter year
% (treating consecutive Dec, Jan Feb as winter that year)
% Extract winter year as the year corresponding to January
winterYr = year(dates(winterMonth));
winterYr(month(dates(winterMonth))==12) = winterYr(month(dates(winterMonth))==12)+1;
% Find groups
G = findgroups(winterYr);
% Splitapply can't be used on 3D matrices. Reshaped to 2D to take the mean
sstTemp = reshape(sstWinter,size(sstWinter,1)*size(sstWinter,2),size(sstWinter,3));
f = @(x) mean(x,2,"omitnan");
tempYrMean = splitapply(f,sstTemp,G);
% reshape back to original number of rows, columns
sstYrMean = reshape(tempYrMean,size(sstWinter,1),size(sstWinter,2),[]);
%% Visualizing data
% Create variables for visualizing
[LAT, LON] = meshgrid(lat,lon);
winter1 = sstYrMean(:,:,1); % Change the index of the 3rd dimension to change the year
% Visualize the data for Winter 1982
geoscatter(LAT(:),LON(:),[],winter1(:))
colorbar(gca,"eastoutside")
title("Winter " + num2str(winterYr(1)))