Clear Filters
Clear Filters

calculate mean of 4D array

15 views (last 30 days)
Gurumoorthi K
Gurumoorthi K on 11 May 2024
Answered: Nivedita ungefär 9 timmar ago
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 Comments
Paul
Paul on 11 May 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.
Gurumoorthi K
Gurumoorthi K on 11 May 2024
Moved: Voss on 11 May 2024
>> ncdisp('198201.nc'); % display .nc file
Source:
E:\GURU_2024\metoffice_data_1982_2022_croped_SO\198201.nc
Format:
netcdf4_classic
Global Attributes:
CDI = 'Climate Data Interface version 2.0.4 (https://mpimet.mpg.de/cdi)'
Conventions = 'CF-1.0'
_NCProperties = 'version=2,netcdf=4.8.1,hdf5=1.10.7'
institution = 'UK Met Office'
title = 'Temperature and salinity analysis'
DSD_entry_id = 'UKMO-L4UHFnd-GLOB-v01'
references = 'Website and paper: https://www.metoffice.gov.uk/hadobs/en4/; Good, S. A., M. J. Martin and N. A. Rayner, 2013. EN4: quality controlled ocean temperature and salinity profiles and monthly objective analyses with uncertainty estimates, Journal of Geophysical Research: Oceans, 118, 6704-6716, doi:10.1002/2013JC009067'
contact = 'Rachel Killick - rachel.killick@metoffice.gov.uk'
GDS_version_id = 'v1.7'
netcdf_version_id = '3.5'
creation_date = '2021-03-20 20:33:41.770 -00:00'
product_version = '1.0'
grid_resolution = ' 1.00000 degree'
start_date = '2001-01-01 UTC'
start_time = '00:00:00 UTC'
stop_date = '2001-01-01 UTC'
stop_time = '00:00:00 UTC'
southernmost_latitude = -90.5
northernmost_latitude = 89.5
westernmost_longitude = 0.5
easternmost_longitude = 362.5
file_quality_index = '0'
licence = 'EN4 is distributed under the Non Commercial Government Licence: http://www.nationalarchives.gov.uk/doc/non-commercial-government-licence/version/2/. The data are available for non-commercial use with attribution to the data providers, please see the product website (see global attribute: references) for terms and conditions.'
history = 'Fri May 10 12:04:04 2024: cdo sellonlatbox,-180,180,-75,0 EN.4.2.2.f.analysis.g10.198201.nc EN.4.2.2.f.analysis.g10.198201_SO.nc'
CDO = 'Climate Data Operators version 2.0.4 (https://mpimet.mpg.de/cdo)'
Dimensions:
time = 1 (UNLIMITED)
bnds = 2
lon = 360
lat = 76
depth = 42
Variables:
time
Size: 1x1
Dimensions: time
Datatype: single
Attributes:
standard_name = 'time'
bounds = 'time_bnds'
units = 'days since 1800-01-01 00:00:00'
calendar = 'standard'
axis = 'T'
time_bnds
Size: 2x1
Dimensions: bnds,time
Datatype: double
lon
Size: 360x1
Dimensions: lon
Datatype: single
Attributes:
standard_name = 'longitude'
long_name = 'longitude'
units = 'degrees_east'
axis = 'X'
lat
Size: 76x1
Dimensions: lat
Datatype: single
Attributes:
standard_name = 'latitude'
long_name = 'latitude'
units = 'degrees_north'
axis = 'Y'
depth
Size: 42x1
Dimensions: depth
Datatype: single
Attributes:
standard_name = 'depth'
long_name = 'depth'
units = 'metres'
positive = 'down'
axis = 'Z'
bounds = 'depth_bnds'
depth_bnds
Size: 2x42
Dimensions: bnds,depth
Datatype: single
temperature
Size: 360x76x42x1
Dimensions: lon,lat,depth,time
Datatype: single
Attributes:
standard_name = 'sea_water_potential_temperature'
long_name = 'temperature'
units = 'kelvin'
add_offset = 273.15
scale_factor = 1
_FillValue = -32768
missing_value = -32768
salinity
Size: 360x76x42x1
Dimensions: lon,lat,depth,time
Datatype: single
Attributes:
standard_name = 'sea_water_salinity'
long_name = 'salinity'
units = '1'
_FillValue = -32768
missing_value = -32768
temperature_uncertainty
Size: 360x76x42x1
Dimensions: lon,lat,depth,time
Datatype: single
Attributes:
long_name = 'temperature error standard deviation'
units = 'kelvin'
_FillValue = -32768
missing_value = -32768
salinity_uncertainty
Size: 360x76x42x1
Dimensions: lon,lat,depth,time
Datatype: single
Attributes:
long_name = 'salinity error standard deviation'
units = '1'
_FillValue = -32768
missing_value = -32768
temperature_observation_weights
Size: 360x76x42x1
Dimensions: lon,lat,depth,time
Datatype: single
Attributes:
long_name = 'temperature observation weights'
_FillValue = -32768
missing_value = -32768
comment = 'The total weighting given to the observation increments when forming the analysis'
salinity_observation_weights
Size: 360x76x42x1
Dimensions: lon,lat,depth,time
Datatype: single
Attributes:
long_name = 'salinity observation weights'
_FillValue = -32768
missing_value = -32768
comment = 'The total weighting given to the observation increments when forming the analysis'

Sign in to comment.

Answers (1)

Nivedita
Nivedita ungefär 9 timmar ago
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.
  1. We will first initialize the annual_mean and seasonal_mean variables to accumulate temperature data.
  2. Then we will loop through each year from 1982 to 2022 and each month from January to December.
  3. For each month, we will construct the filename and load the temperature data using ncread.
  4. We then accumulate the temperature data for annual and seasonal means.
  5. 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).
  6. 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:
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!

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!