How to get decade means of monthly 3D satellite data?

4 views (last 30 days)
Hello Matlab World!
I have been trying to convert the monthly netcdf data (file attached "bob") to decade data and later plot a map of the same. I have visited a similar question asked before and tried the following code, but seem to get the wrong results:
lon=ncread('bob.nc','lon'); % longitude
lat=ncread('bob.nc','lat'); % latitude
time=ncread('bob.nc','time'); % time
temp=ncread('bob.nc','sst');
t=size(time,3);
[groups, groupID] = findgroups(floor(t/12)*10);
The temperature variable, temp= 17 x 15 x 444 (where 444 is the monthly data from January 1986 to December, 2019). So I basically need to get 4 groups (1986-95, 96-2005, 2006-2015 and 2016-2019). The above code gives 1 group which is obviously not correct.
Would be grateful to receive some help in this.

Accepted Answer

Cris LaPierre
Cris LaPierre on 3 Jun 2020
Edited: Cris LaPierre on 3 Jun 2020
Do you want a single temperature for each decade, or you you want an average for each lat/lon coordinate pair?
I wasn't able to get splitapply or groupsummary to work with a 3D dataset, but I was able to achieve group means with a for-loop.
The challenge here is perhaps identifying the groups. I want to convert your time variable to a datetime so I can group by year. The following code allows we to inspect the attributes of time.
info = ncinfo('keeganCarvalho_bob.nc')
From here, I can see that time is the number of days since 1800-1-1 00:00:00, and that is 1 day. I performed the convertion with the following code.
dTime = datetime(1800,1,1)+days(time)
I then extracted the year and used the discretize function to group the years into your specified bins.
yr = year(dTime);
G = discretize(yr,[1986 1996 2006 2016 2019]);
All that is left is to get the mean.
for d = 1:max(G)
decadeMean(d) = mean(temp(:,:,G==d),"all");
end
plot(decadeMean)
If instead you want the mean for each lat/lon location, you could try something like this. You'd have to change the plotting code to be something appropriate for matrix data.
for d = 1:max(G)
decadeMean(:,:,d) = mean(temp(:,:,G==d),3);
end
% rearrange data so decade means are in rows, and columns are lat/lon pairs
plot(reshape(permute(decadeMeanLL,[3,1,2]),max(G),[]))
  6 Comments
Cris LaPierre
Cris LaPierre on 3 Jun 2020
Looks like I'm the one confusing you now. I had proposed two different solutions. It looks like you are trying to combine pieces of both, which will not work. Now that I know what it is you want, let me propose a single solution.
dTime = datetime(1800,1,1)+days(time);
yr = year(dTime);
G = discretize(yr,[1986 1996 2006 2016 2019]);
decadeMean = zeros(size(temp,1),size(temp,2),max(G));
for d = 1:max(G)
decadeMean(:,:,d) = mean(temp(:,:,G==d),3);
end
Keegan Carvalho
Keegan Carvalho on 3 Jun 2020
Thanks Cris!
It was my bad but I understood the code now. Thank you once again for your continued help. Cheers!!

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions 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!