This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Hi Xxxxx, > Hello, I'm trying to find the answer to this specific problem : in my > program I would like to have a "dynamic" number of dimensions for my > variable "t" because it depends of 2 _or more_ elements (A, B, C, D, > ...). Here are few lines working fine in "static" mode : > > int t_id; > # define RANK_t 2 // how to make this rank dynamic ? > int t_dims[RANK_t]; > stat = nc_def_var(ncid, "t", NC_INT, RANK_t, t_dims, &t_id); > > It seems kinda simple, I tried everything but can't make it work, if you > could help me of point me to some document it would be nice. > Thanks for your answer, > Milan > p.s. I'm programming in C++ but this question is more language-independent. > ------------------------------------ > netcdf file_name { > dimensions: > A = 11 ; > B = 11 ; > ... > variables: > double A(A) ; > double B(B) ; > double ... > ... > int t(A, B, ...) ; > > data: > > A = 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ; > > B = 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 ; > > ... > > t = > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, > 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 > ... > } Sorry, but there is no way to create a variable with a dynamic rank in the netCDF API's, if you mean that you want to be able to change the rank of a variable at run time. Variables with variaable or undetermined rank are not part of the netCDF data model. If you just want to be able to define a netCDF variable with a rank that depends on a run-time parameter, that's easy. For example in C you would do something like int rank_t, t_id; int *t_dims; /* array of dimids to be allocated */ rank_t = ...; /* determine what rank you want at run time */ int *t_dims = malloc(rank_t * sizeof(int)); for(int i=0; i < rank_t; i++) { t_dims[i] = ... ; /* dimid of ith dimension for t */ } stat = nc_def_var(ncid, "t", NC_INT, rank_t, t_dims, &t_id); In C++ you could allocate the t_dims array with "new". If you meant something else and I've misunderstood your question, please let me know. --Russ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu Ticket Details =================== Ticket ID: XHT-915046 Department: Support netCDF Priority: Normal Status: Closed