Hello Habtamu,
To calculate the mean monthly rainfall data from your "3D NetCDF file", you need to ensure that you are correctly iterating through the years and months, and accumulating the monthly precipitation data.
clear all;
meanMonthlyPrecipitation = zeros(300, 240, 12);
for mm = 1:12
% Initialize a temporary matrix to accumulate monthly precipitation
tempMonthlySum = zeros(300, 240);
yearCount = 0;
for yy = 2002:2017
E = eomday(yy, mm);
dy = YYYYMMDD2doy(yy, mm, 1);
% Construct the file path and add here
if exist(hits1, 'file')
ncid = netcdf.open(hits1);
% Read the precipitation data for the current month
p1 = ncread(hits1, 'precip', [4269, 1061, dy], [300, 240, E]);
% Replace NaN values with zeros for summing
p1(isnan(p1)) = 0;
% Sum the daily precipitation data to get monthly totals
p1_sum = sum(p1, 3);
% Accumulate the monthly totals
tempMonthlySum = tempMonthlySum + p1_sum;
yearCount = yearCount + 1;
netcdf.close(ncid);
end
end
% Calculate the mean monthly precipitation by dividing by the number of years
if yearCount > 0
meanMonthlyPrecipitation(:, :, mm) = tempMonthlySum / yearCount;
end
end
% Now, meanMonthlyPrecipitation contains the mean monthly data over 16 years
Make sure that the "YYYYMMDD2doy" function is correctly defined and available in your environment.
Hope it helps!