Reputation: 724
Let's say I have a netcdf file with a ton of variables, one of which is named 'time' and has a size of 500.
nc=netcdf(ncfile, 'write')
size(nc{'time'})
ans =
500
Now I want to store a longer time series in the same variable (say 750 members), but I want to keep the rest of the file same. How do I change the dimension of this variable to 750 while keeping the rest of the variables in the netcdf file as they are?
I tried simply appending to the array, which gave no errors but the array size doesn't change (which means it doesn't write the value at all)
nc{'time'}(750)=1
I tried to write a I tried putting the file into redefine mode and change the size, but cannot change the existing variable.
dimid=netcdf.defDim(ncid,'time',750)
Error using netcdflib
The NetCDF library encountered an error during execution of 'defDim' function - 'String match to
name in use (NC_ENAMEINUSE)'.
I cannot imagine it to be too difficult to do, but I am surprised that it is not as easy as I thought. Any suggestions? What am I missing?
Upvotes: 2
Views: 3745
Reputation: 16347
NetCDF allows dimensions to be either fixed length or UNLIMITED length. You cannot really "resize" an array of an existing netcdf file. You can only append onto the end of an existing UNLIMITED dimension or create a new file. So first check if your netcdf file has a UNLIMITED time dimension. You can check this in Matlab by doing:
ncid = netcdf.open('myfile.nc','WRITE');
dimids = netcdf.inqUnlimDims(ncid)
If dimids
is empty or does not include time, you will have to create a new file with unlimited time dimension and copy your data into it.
If dimids
contains the time dimension then you can simply write your data into it. For example, if the current number of time values is 500, but you want to write a time value of 23.5 at index 750, just do:
timeid = netcdf.inqVarID(ncid,'time')
[varname, xtype, dimids, natts] = netcdf.inqVar(ncid,timeid)
netcdf.putVar(ncid,timeid,750,1,23.5)
netcdf.close(ncid)
that is UNLIMITED and currently has 500 records, but you want to write to record 750, you can just go ahead and write it.
Upvotes: 2