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.
>To: "address@hidden" <address@hidden> >cc: "Bob' 'Keeley (E-mail)" <address@hidden> >From: Naveenta Anand <address@hidden> >Subject: netCDF: Problem displaying array of values >Organization: UCAR/Unidata >Keywords: 200206181701.g5IH1K603595 Hi Naveenta, > Kindly help if you have any experience with the follows. I am using netCDF > library for C. Those, who are using any other language than C can also help > since I see a problem with netCDF function calls (i.e.netCDF routines). > > PROBLEM SCENARIO: > > My variable is called station_parameter and this should display as follows: > "TEMP" > "PSAL" > "PRES" > There are three possible values as above for station_parameter > > And the dimension set of this variable comprises of 3 dimensions i.e. > N_PROF,N_PARAM_LEN,STRING4 where: > N_PROF = 1 > N_PARAM_LEN = 3 > STRING4 =4 > > The variable structure finally is > station_parameter(N_PROF,N_PARAM_LEN,STRING4) > > -------------- Variable definition ----------------- > if (n_param_len != 0) { > station_parameters_dims[0] = n_prof_dim; > station_parameters_dims[1] = n_param_dim; > station_parameters_dims[2] = strln4_dim; > stat=nc_def_var(ncid,"STATION_PARAMETERS",NC_CHAR,RANK_station_parameters, > station_parameters_dims,&station_parameters_id); > check_err(stat,__LINE__,__FILE__); > } > where RANK_station_parameters = 3. > > ------------ Adding values to variables ----------- > > { > size_t astart[] = {0,0,0}; > size_t acount[] = {1,3,4}; > acount[0] = n_prof_len; > acount[1] = n_param_len; > acount[2] = strln4_len; > astart[0]=0; > for (i=0;i < n_param_len;i ++) > { > if ( i == (n_param_len -1)) > strcpy(stn.prof[i].prof_type,"PRES"); > stat = nc_put_vara_text(ncid, station_parameters_id, astart, > acount,stn.prof[i].prof_type); > check_err(stat,__LINE__,__FILE__); > astart[0]++; > } > } > where: stn.prof[i].prof_type is - > TEMP when i=1 > PSAL when i=2 > PRES when i=3. > > With the above routines, my variable displays as follows in the netCDF file: > STATION_PARAMETERS = > "TEMP", > "\000N\0007", > "\0001" ; > > WHEREAS: > > As mentioned above, I am trying to output the following pattern from > station_parameter: > "TEMP" > "PSAL" > "PRES" > There are three possible values as above for station_parameter. > > ---------------------------------------------------------------------------- The array acount[] should specify the "shape" of the slab of values you are writing, that is the number of indices along each dimension. Since you are writing only 4 characters along the last (strln4) dimension, this shape is 1 by 1 by 4, so you want acount initialized as {1, 1, 4} and not changed after that: size_t acount[] = {1,1,4}; The array astart[] should specify where each write starts in the 1 by 3 by 4 cube of characters. The first write will start at the {0,0,0} position but the next write will start at {0,1,0} and the last write at {0,2,0}. So you can use your i index for astart[1], or you can increment astart[1] each time through the loop. So here is one version to do what you want: { size_t astart[] = {0,0,0}; size_t acount[] = {1,1,4}; for (i=0;i < n_param_len;i ++) { stat = nc_put_vara_text(ncid, station_parameters_id, astart, acount, stn.prof[i].prof_type); check_err(stat,__LINE__,__FILE__); astart[1]++; } } Another way to do it would be: { size_t astart[] = {0,0,0}; size_t acount[] = {1,1,4}; for (i=0;i < n_param_len;i ++) { astart[1] = i; stat = nc_put_vara_text(ncid, station_parameters_id, astart, acount, stn.prof[i].prof_type); check_err(stat,__LINE__,__FILE__); } } I haven't checked these by compiling and running them, because I didn't have the context for the fragment of code, but I think either of the above should work. If not, please write back ... --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu