Error in netcdf.putVar due to START argument.

I'm running a model with a daily time-step and I want to archive monthly averages of each variable of interest. So, I'm trying to write average monthly results into netCDF variables using netcdf.putVar
Here's a piece of my code to help you understand:
% logical array, true if time step corresponds to the end of an archiving time step (=end of the month)
if Arch.islast(it)
% Determining the size of each variable of interest. avg is a nvar x 1 cell array. Each cell contains the monthly
% averages of the variable considered.
isd = cellfun(@(x) isequal(size(Grd.z), size(x)), avg); % in the case of a 3 x 2 array
isde = cellfun(@(x) isequal([1 size(Grd.x,2)], size(x)), avg); %in the case of a 1 x 2 array
iss = cellfun(@(x) isscalar(x), avg); % in the case of a scalar
% Vectors specifying the index where to write the section of values. Arch.bin = 1 x nt array, index of archiving
% time step to which each model time step corresponds
start = cell(size(avg));
[start{isd}] = deal([0 0 Arch.bin(it)-1]);
[start{isde}] = deal([Arch.bin(it)-1 0]);
[start{iss}] = deal([Arch.bin(it)-1]);
%Vectors specifying the extent to which the section of values is written.
count = cell(size(avg));
[count{isd}] = deal([Grd.nz Grd.nx 1]);
[count{isde}] = deal([1 Grd.nx]);
[count{iss}] = deal([1]);
for iv = 1:length(avg)
netcdf.putVar(ncid, vid(iv), start{iv}, count{iv}, avg{iv});
end
% Zero out avg arrays
avg = cellfun(@(x) zeros(size(x)), avg, 'uni', 0);
end
Here's the error I get for one of the variables (i.e. Ql):
Error using netcdflib
The length of the START argument for Ql must be 0 instead of 2.
Error in netcdf.putVar (line 84)
netcdflib(funcstr,ncid,varid,varargin{:});
Error in WCVIE2E_archivemldata (line 87)
netcdf.putVar(ncid, vid(iv), start{iv}, count{iv}, avg{iv});
In total there are 12 variables of interest in avg. This error doesn't pop up for most variables, only for 2 of them. One of them, Ql, is a 1 x 2 array. I have already verified whether previous functions in my model did produce a 1x2 array for this variable and it seems to be the case. This error message doesn't pop up for other variables that have the same size, which I don't understand...
If anyone of you is willing to look more closely into my model, here's my GitHub: https://github.com/virginie01/WCVI-E2E. Just need to download all folders&files. To run the entire model, one needs to run the mixedlayer_examples.m code and change the "addpaths" accordingly.
I hope some of you are able to enlighten me on this ! Cheers, Virginie

Answers (1)

KSSV
KSSV on 6 Nov 2018
Edited: KSSV on 7 Nov 2018
YOu need not to use such a complex functions.......reading netCDF in MATLAB is very easy. Just you want to know two functions.
ncdisp, ncread
Read about those two functions. It should make everything easy.

3 Comments

Hi KSSV,
Thank you for taking the time to answer my question. I don't want to read netcdf variables, I want to write data to netcdf variables. The error arises when I use the function netcdf.putVar
Okay..writitng too is very easy in MATLAB. YOu need to know just two funcitons..... nccreate and ncwrite. Try out with these.....if any error. LEt me know..I shall help you.
Thank you ! So I replaced the function netcdf.putVar with ncwrite and everything works just fine now ;)

Sign in to comment.

Asked:

on 6 Nov 2018

Edited:

on 7 Nov 2018

Community Treasure Hunt

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

Start Hunting!