Re: [netcdfgroup] problems writing to file

Hi Rob,

I did end the definition mode in the code. I just forgot to put it in the snippet I sent.
see the correction below....

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);

Print("Ending definition mode"); status = nc_enddef(ncid); 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);

 Print("Closing the file...");
 status = nc_close(ncid);
 if(status != NC_NOERR) handle_error(status);
....



Marston Johnston, PhD Student
Radio and Space Science
Chalmers University of Technology
Gothenburg Sweden
marston at chalmers.se
cell: +46 736 637 909
Skype ID: marston.johnston

On May 25, 2009, at 6:05 PM, Roy Mendelssohn wrote:

I don't see a call to end definition mode - nc_enddef - needs to be done after defining dimensions and attributes and before writing data.

-Roy

On May 25, 2009, at 8:52 AM, Marston Johnston wrote:

Hi,

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






Marston Johnston, PhD Student
Radio and Space Science
Chalmers University of Technology
Gothenburg Sweden
marston at chalmers.se
cell: +46 736 637 909
Skype ID: marston.johnston

_______________________________________________
netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe,  visit: 
http://www.unidata.ucar.edu/mailing_lists/

**********************
"The contents of this message do not reflect any position of the U.S. Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
1352 Lighthouse Avenue
Pacific Grove, CA 93950-2097

e-mail: Roy.Mendelssohn@xxxxxxxx (Note new e-mail address)
voice: (831)-648-9029
fax: (831)-648-8440
www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected"




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