[netcdfgroup] [netCDF #ZPJ-875297]: problems writing to file

Hi Marston,

> I'm trying to write a series of arrays to a single netcdf file that
> will be about 1/2 Gb when done. The datasets are 1D, 2D, and 3D arrays
> of packed data.
> I'm having trouble writing the data correctly using the examples given
> in the manual and the netcdf example directory.
> 
> My problem is that when I check the data before writing, everything
> looks fine but when I check the data written to netcdf file the arrays
> are all screwed up with the max and mins for each level being
> incorrect and some slices of the 3D array being garbled.
> 
> any help would be greatly appreciated!
> 
> /Marston
> 
> Here's how I setup the file for writing:
> 
> 
> // create netCDF dataset with perfomance options: enter define mode
> status = nc_create(outfile,NC_64BIT_OFFSET,&ncid);
> if(status != NC_NOERR) handle_error(status);
> 
> // Define a title for the file
> status = nc_put_att_text(ncid,NC_GLOBAL,"Title",strlen(title),title);
> 
> // define dimensions: from name and length
> status = nc_def_dim(ncid,"Lat",latlen,&lat_dimid);
> if (status != NC_NOERR) handle_error(status);
> 
> status = nc_def_dim(ncid,"Lon",lonlen,&lon_dimid);
> if (status != NC_NOERR) handle_error(status);
> 
> status = nc_def_dim(ncid,"Lev",levlen,&lev_dimid);
> if (status != NC_NOERR) handle_error(status);
> 
> dim[0]   = lev_dimid;
> 
> dim2d[0] = lat_dimid;
> dim2d[1] = lon_dimid;
> 
> dim3d[0] = lev_dimid;
> dim3d[1] = lat_dimid;
> dim3d[2] = lon_dimid;
> 
> Print("Define variables: from name, type");
> status = nc_def_var(ncid,"Latitude",NC_FLOAT,2,dim2d,&laid);
> if(status != NC_NOERR) handle_error(status);
> status = nc_def_var(ncid,"Longitude",NC_FLOAT,2,dim2d,&loid);
> if(status != NC_NOERR) handle_error(status);
> 
> status = nc_def_var(ncid,"Levels",NC_SHORT,1,dim,&leid);
> if(status != NC_NOERR) handle_error(status);
> 
> 
> // Setting up the temperature variable
> 
> status = nc_def_var(ncid,"Temperature",NC_SHORT,3,dim3d,&tid);
> if(status != NC_NOERR) handle_error(status);
> status =
> nc_put_att_text(ncid,tid,"Units",strlen(TEMP_UNITS),TEMP_UNITS);
> if (status != NC_NOERR) handle_error(status);
> pos = RetPos(sptr,130);
> status = nc_put_att_double(ncid,tid,"scale",NC_DOUBLE,1,&sptr[pos]-
> >scale);
> if (status != NC_NOERR) handle_error(status);
> status = nc_put_att_double(ncid,tid,"offset",NC_DOUBLE,1,&sptr[pos]-
> >offset);
> if (status != NC_NOERR) handle_error(status);
> 
> 
> // Fetching the data, packing it, checking it before writing to file
> 
> pos = RetPos(sptr,130);
> swd->t = GetNlevsP(f,wk->array_size,sptr[pos]->nlev,sptr[pos]-
> >pnum,sptr[pos]->scale,sptr[pos]->offset);
> 
> // Checking the Maxs and mins for each level
> int lev;
> for(lev = 0; lev < wk->nlev; lev++) {
> printf("Lev: %d\t",lev);
> MMSI(swd->t[lev],wk->nlat,wk->nlon,sptr[pos]->scale,sptr[pos]-
> >offset);
> }
> 
> status = nc_put_var_short(ncid,tid,&swd->t[0][0][0]);
> if(status != NC_NOERR) handle_error(status);

Does the swd->t array have the same dimensions in your program as you
have declared for the "Temperature" variable in the netCDF file?
For the nc_put_var_short() call, the data array swd->t must be of the 
shape
 
   short t[levlen][latlen][lonlen]

for the call to do what you intend.  

If the shape of the swd->t array differs from the netCDF array, you
may have to use multiple calls to nc_put_vara_short() or
nc_put_varm_short() to write the data.

Also note that the scale and offset attributes you are setting for the
Temperature variable are not interpreted by the netCDF C API, so you
have to handle packing and unpacking the data values in your program.
If you expect to use other applications that handle packed netCDF
data, it would be better to use the conventional attribute names
"scale_factor" and "add_offset" as described here:

  
http://www.unidata.ucar.edu/netcdf/docs/BestPractices.html#Packed%20Data%20Values

--Russ

Russ Rew                                         UCAR Unidata Program
russ@xxxxxxxxxxxxxxxx                     http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: ZPJ-875297
Department: Support netCDF
Priority: Normal
Status: Closed



  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: