Trying to change the variable dimensions when combining two netcdf files

19 views (last 30 days)
In the code below, attempt has been made to combine the two netcdf files and also change the dimensions of variable ua and va from 144 x 143 x 8 x 3650 to 144 x 143 x 1 x 3650 i.e. ua and va value corresponding 3rd position first index will only be taken. For this, the code has saved all values of ua from netcdf file to a variable ua and similarly for va. Then " ua_data = ua(:, :, plev_index, :); " has been used. But in final combined file the dimensions of ua and va are same as earlier. Please help if anyone can suugest something.
Code is as given below:
% Define input file names
file1 = 'ua_day_IPSL-CM5A-MR_rcp45_r1i1p1_20260101-20351231.nc';
file2 = 'va_day_IPSL-CM5A-MR_rcp45_r1i1p1_20260101-20351231.nc';
% Define output file name
outputFile = 'Netcdfcombined_file.nc';
% Open first file and read variables and attributes
ua = ncread(file1, 'ua');
attributes1 = ncinfo(file1);
dims1 = attributes1.Dimensions;
% Open second file and read variable and attributes
va = ncread(file2, 'va');
attributes2 = ncinfo(file2);
dims2 = attributes2.Dimensions;
% Find the index of pressure level corresponding to 100000 Pa
plev_values = ncread(file1, 'plev');
plev_index = find(plev_values == 100000);
if isempty(plev_index)
error('Pressure level corresponding to 100000 Pa not found in file.');
end
% Write dimensions and attributes to output file
ncwriteschema(outputFile, attributes1);
% Create 'va' variable in the output file
varDims = {'lon', 'lat', 'plev', 'time'};
varSize = [size(va,1), size(va,2), 1, size(va,4)]; % Assuming plev is 1 level
nccreate(outputFile, 'va', 'Dimensions', varDims, 'Datatype', 'double');
ua_data = ua(:, :, plev_index, :);
varSize(3) = 1; % Set plev dimension size to 1
ncwrite(outputFile, 'ua', ua_data, [1, 1, 1, 1]); % Specify start position only
% Transfer remaining variables from the first file to the output file
variables1 = {attributes1.Variables.Name};
variables1 = setdiff(variables1, 'ua'); % Remove ua
for i = 1:length(variables1)
data = ncread(file1, variables1{i});
ncwrite(outputFile, variables1{i}, data);
end
% Write va variable from the second file to the output file
plev_index_double = double(plev_index); % Convert plev_index to double
va_data = va(:, :, plev_index, :);
varSize(3) = 1; %Set plev dimension size to 1
if ~isempty(va)
ncwrite(outputFile, 'va', va_data, [1, 1, 1, 1]); % Specify start position only
end
  2 Comments
Prashant
Prashant on 13 Apr 2024 at 9:08
Hello Sir,
Actually I had replied to ur comment last time but somehow it did not get posted.
Anyways the problem is the files are two large to be shared, their size is around 4.5 GB
Thanks for the comment
with bedt regards,
Prashant M Soni

Sign in to comment.

Answers (1)

Ronit
Ronit on 25 Mar 2024
Hi Prashant,
It seems like you are trying to update the dimensions of the “ua” and “va” variables in the output file from their original dimensions (144 x 143 x 8 x 3650) to the new dimensions (144 x 143 x 1 x 3650). Specifically, when you create the “va” variable in the output file and attempt to write the “ua” data, you need to ensure that the dimensions defined in the output file match the new dimensions of your data.
Here's a corrected version of the relevant parts of your code:
% Define output file name
outputFile = 'Netcdfcombined_file.nc';
% Create the output file
ncid = netcdf.create(outputFile, 'CLOBBER');
% Define dimensions in the output file
lonDimId = netcdf.defDim(ncid, 'lon', size(ua, 1));
latDimId = netcdf.defDim(ncid, 'lat', size(ua, 2));
plevDimId = netcdf.defDim(ncid, 'plev', 1); % Only one pressure level
timeDimId = netcdf.defDim(ncid, 'time', size(ua, 4));
% Define the variables with the correct dimensions in the output file
uaVarId = netcdf.defVar(ncid, 'ua', 'double', [lonDimId, latDimId, plevDimId, timeDimId]);
vaVarId = netcdf.defVar(ncid, 'va', 'double', [lonDimId, latDimId, plevDimId, timeDimId]);
% End definition mode
netcdf.endDef(ncid);
% Prepare the data
ua_data = ua(:, :, plev_index, :);
va_data = va(:, :, plev_index, :);
% Write the data to the variables
netcdf.putVar(ncid, uaVarId, ua_data);
netcdf.putVar(ncid, vaVarId, va_data);
% Close the file
netcdf.close(ncid);
To learn more about functions like “netcdf.defDim”,netcdf.defVar” and “netcdf.putVar, please refer to the following documentation link - https://www.mathworks.com/help/matlab/network-common-data-form.html
Hope this helps!
  1 Comment
Prashant
Prashant on 5 Apr 2024 at 12:06
Hello Ronit Sir,
Thanks for the help, your code works well for the described problem. But I am facing issue while copying the attributes from the first file to the combined file. There is some issue with the file format as shown by matlab. Could you please suggest some solution for the same.
Thank you very much for your helping hand
with best regards,
Prashant Soni

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!