How to extract seasonal means?
Show older comments
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
Accepted Answer
More Answers (1)
Brian DeCicco
on 2 Mar 2021
0 votes
I have a question on this topic. I'm doing something similar, but my dataset contains data at 6-hrly intervals daily from 1979-2018 (total of 58440 time steps). Just trying to extract Jan, Feb, Dec from the original time series. Here's how I have part of my code setup so far, but I am unsure how to point Matlab to know which month is which based on this setup. I appreciate any help you might be able to provide!
startDate = datetime(1979,1,1,0,0,0);
endDate = datetime(2018,12,31,18,0,0;
dates = startDate:hours(6):endDate;
winterMonth = month(dates) == 1 | month(dates) == 2 | month(dates) == 12;
sst_JFD = sst(:,:,winterMonth);
2 Comments
Cris LaPierre
on 2 Mar 2021
The result of this code does not distinguish between the selected months. It's looking for any month.
The details about which month each value came from is stored in dates. You can see that with this code
dates(winterMonth)
% or
month(dates(winterMonth))
Brian DeCicco
on 2 Mar 2021
Thanks Chris! So if I am trying to extract all times (6-hrly time steps per day) that are just assocaited with all Jan, Feb, and Dec each year, should my code look like this? I don't think that my "winterMonth" line is correct because I'm getting some mismatches for the month values (i.e. Feb is showing up as "1").
% Load data from nc file
lon=ncread('sst.nc','lon');
lat=ncread('sst.nc','lat');
sst=ncread('sst.nc','sst');
% Create vector of dates to use for extracting winter months
startDate = datetime(1979,1,1,0,0,0);
endDate = datetime(2018,12,31,18,0,0);
dates = startDate:hours(6):endDate;
winterMonth = month(dates) == 1 | month(dates) == 2 | month(dates) == 12;
sst_JFD = sst(:,:,winterMonth);
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!