problems writing netcdf file with 3-d variable

1 view (last 30 days)
Hello,
I am trying to write some data I have to a netcdf file. Here is the code:
st_num = 1:370;
year = 1895:2011;
month = 1:12;
% Open netCDF file.
ncid = netcdf.create('SPEI1.nc','NC_WRITE');
% Define the dimensions of the variable.
IDdimid = netcdf.defDim(ncid,'station_ID',length(st_num));
modimid = netcdf.defDim(ncid,'month',length(month));
yeardimid = netcdf.defDim(ncid,'year',length(year));
data_ids = ([IDdimid modimid yeardimid]);
data_ids
whos
yearID = netcdf.defVar(ncid,'year','double',yeardimid);
moID = netcdf.defVar(ncid,'month','double',modimid);
stnumID = netcdf.defVar(ncid,'station_ID','double',IDdimid);
dataID = netcdf.defVar(ncid,'SPEI1','double',data_ids);
% Leave define mode and enter data mode to write data.
netcdf.endDef(ncid)
% Write data to variable.
netcdf.putVar(ncid,dataID,SPEI1);
netcdf.close(ncid);
My SPEI1 variable is a 117x12x370 double. The data that is written into SPEI1 is not in the correct location and much of the newly written data does not exist in my original SPEI1 Matlab variable.
When I dump the netcdf file all the dimensions seem to be as they should though...:
NetCDF SPEI1.nc {
dimensions:
station_ID = 370 ;
month = 12 ;
year = 117 ;
variables:
// Preference 'PRESERVE_FVD': false,
// dimensions consistent with ncBrowse, not with native MATLAB netcdf package.
double year(year), shape = [117]
double month(month), shape = [12]
double station_ID(station_ID), shape = [370]
double SPEI1(year,month,station_ID), shape = [117 12 370]
Do anyone have any suggestions for my problem?
Thanks, Dan
  4 Comments
Dan
Dan on 8 Nov 2011
I found a fix for the problem, at the beginning of script:
SPEI1 = permute(SPEI1,[3 2 1])
I'm sure exactly why I need to do this, but it works!
Ashish Uthama
Ashish Uthama on 8 Nov 2011
Dan, if data_ids = ([IDdimid modimid yeardimid]); i.e the variable dimensions ought to match the order of the defined dimensions (370x12x117).

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!