calculate mean of 4D array
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Gurumoorthi K
el 11 de Mayo de 2024
Respondida: Nivedita
el 23 de Jul. de 2024
I have 41 years monthly data of temperature (lat,lon,depth,time,temperature). I wanted to calculate annual mean and seasonal mean [(summer: Dec, Jan, Feb), (autumn: Mar, Apr, May), (winter = Jun, Jul, Aug), (spring = Sep, Oct, Nov)] of each grid, eache depth level for the period of 1982-2022.
2 comentarios
Paul
el 11 de Mayo de 2024
Hi @Gurumoorthi K.,
You're more likely to get help if you upllad your data (or something like it) in .mat file using the Paperclip icon on the Insert menu so that people can see exactly how the data is organized.
Respuesta aceptada
Nivedita
el 23 de Jul. de 2024
Hi Gurumoorthi,
As I do not have access to the data,I am providing the answering based on my undertanding from your query. You can make necessary modifications according to the data available with you. We can achieve your requirement by following the processes which involves loading the data, reshaping it, and then calculating the means for each grid point and depth level.
- We will first initialize the annual_mean and seasonal_mean variables to accumulate temperature data.
- Then we will loop through each year from 1982 to 2022 and each month from January to December.
- For each month, we will construct the filename and load the temperature data using ncread.
- We then accumulate the temperature data for annual and seasonal means.
- After accumulating the data, we will calculate the annual mean by dividing by the total number of months (41 years * 12 months). Similarly, we will also calculate the seasonal means by dividing by the number of months in each season (41 years * 3 months per season).
- Finally, we will save the annual and seasonal means to .mat files.
% Define the path to your data files
dataDir = 'E:\GURU_2024\metoffice_data_1982_2022_croped_SO\';
% Initialize variables to accumulate the data
annual_mean = [];
seasonal_mean = struct('summer', [], 'autumn', [], 'winter', [], 'spring', []);
% Loop through each year and month
for year = 1982:2022
for month = 1:12
% Construct the filename
filename = sprintf('%s%d%02d.nc', dataDir, year, month);
% Load the temperature data
temperature = ncread(filename, 'temperature');
% Accumulate the temperature data for annual mean
if isempty(annual_mean)
annual_mean = temperature;
else
annual_mean = annual_mean + temperature;
end
% Determine the season and accumulate the temperature data for seasonal mean
if ismember(month, [12, 1, 2])
% Summer
if isempty(seasonal_mean.summer)
seasonal_mean.summer = temperature;
else
seasonal_mean.summer = seasonal_mean.summer + temperature;
end
elseif ismember(month, [3, 4, 5])
% Autumn
if isempty(seasonal_mean.autumn)
seasonal_mean.autumn = temperature;
else
seasonal_mean.autumn = seasonal_mean.autumn + temperature;
end
elseif ismember(month, [6, 7, 8])
% Winter
if isempty(seasonal_mean.winter)
seasonal_mean.winter = temperature;
else
seasonal_mean.winter = seasonal_mean.winter + temperature;
end
elseif ismember(month, [9, 10, 11])
% Spring
if isempty(seasonal_mean.spring)
seasonal_mean.spring = temperature;
else
seasonal_mean.spring = seasonal_mean.spring + temperature;
end
end
end
end
% Calculate the annual mean by dividing the accumulated sum by the number of months
annual_mean = annual_mean / (41 * 12);
% Calculate the seasonal means by dividing the accumulated sums by the number of months in each season
seasonal_mean.summer = seasonal_mean.summer / (41 * 3);
seasonal_mean.autumn = seasonal_mean.autumn / (41 * 3);
seasonal_mean.winter = seasonal_mean.winter / (41 * 3);
seasonal_mean.spring = seasonal_mean.spring / (41 * 3);
% Save the results to .mat files
save('annual_mean.mat', 'annual_mean');
save('seasonal_mean.mat', 'seasonal_mean');
Make sure to adjust the dataDir variable to point to the directory containing your NetCDF files. This script assumes that the files are named in the format YYYYMM.nc. If your file naming convention is different, you may need to adjust the filename construction accordingly.
For more information on handling NetCDF files and computing means over different dimensions, please refer to these following community queries:
https://www.mathworks.com/matlabcentral/answers/203585-how-to-calculate-average-of-netcdf-daily-data
For more information on the "ncread" function, type the following command in the command window:
web(fullfile(docroot, 'matlab/ref/ncread.html'))
Hope this helps!
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Geology en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!