Hi Erdem,
I understand that you are trying to read all the GLDAS data from 2000 to 2022 on a monthly basis.
Here's an example on how you can read multiple GLDAS data files and calculate the TWS using the provided values:
% Define the directory where your GLDAS data files are stored
dataDir = 'path_to_gldas_data';
% Define the years for which you have data
years = 2000:2022;
% Loop over the years
for year = years
% Loop over the months
for month = 1:12
% Construct the filename for the current month's data
filename = fullfile(dataDir, sprintf('GLDAS_NOAH10_M.2.1/%d/GLDAS_NOAH10_M.A%d%02d.021.nc4', year, year, month));
% Read the GLDAS data for the current month
time = ncread(filename, 'time');
longitude = ncread(filename, 'lon');
latitude = ncread(filename, 'lat');
swe = ncread(filename, 'SWE_inst');
soil_moi10 = ncread(filename, 'SoilMoi0_10cm_inst');
soil_moi40 = ncread(filename, 'SoilMoi10_40cm_inst');
soil_moi100 = ncread(filename, 'SoilMoi40_100cm_inst');
soil_moi200 = ncread(filename, 'SoilMoi100_200cm_inst');
cws = ncread(filename, 'CanopInt_inst');
% Calculate the Total Water Storage (TWS) for the current month
tws = swe + (soil_moi10 + soil_moi40 + soil_moi100 + soil_moi200) + cws;
end
end
Replace 'path_to_gldas_data' with the actual path to your GLDAS data directory.
In this code, you define the directory where your GLDAS data files are stored (dataDir) and the years for which you have data (years). Then, you use nested loops to iterate over each year and each month within that year.