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.
the primary problem is that you are not allocating memory big enough to hold the total contents of the "coordinates" variable. You have allocated: float Vec[3*na*40] where na = 3000. But in fact you need to allocate float Vec[2*na*nf] This means that memory past the Vec variable will be overwritten which is probably causing the fault. > I'm trying to read trajectory data made by Molecular Dynamics program > 'AMBER', whose file format is NetCDF.I tried to read variable called > "coordinates". > The dimensions of coordinates are as follow: > dimensions > frame = UNLIMITED ; // (20000 currently) spatial = 3 ; atom = > 3000 ; > variablesfloat coordinates(frame, atom, spatial) ; > I tried to read and write the data by following C language code, but > segmentation fault occured. If I reduced data size that I try to read by > changing count vector, It worked. Are data size which can be read at once > limited? then Are there any way to read whole data? > > #include <stdio.h>#include <netcdf.h> > #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}#define > FILE_NAME "w1000.tp3.nvtmd2.nc" > intmain(){int ncid, retval, VecCoord;int na=3000, nf=20000;static size_t > iv_strt[] = {0, 0, 0};static size_t iv_cnt[] = {20000, 3000, 3}; // 1, 2, > 3float Vec[3*na*40];int i, j, rec, k, l; > /*Open the file. NC_NOWRITE tells netCDF we want read-only access * to the > file*/ > if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid))) ERR(retval); > if ((retval = nc_inq_varid(ncid, "coordinates", &VecCoord))) ERR(retval); > if ((retval = nc_get_vara_float(ncid, VecCoord,iv_strt, iv_cnt, &Vec[0]))) > > > > if ((retval=nc_close(ncid))) ERR(retval); > FILE *fout;//fout= fopen("home/janghm17/NetCDF/DataFile.txt", "wt"); > > fout= fopen("DataFile.txt", "wt"); if(fout==NULL) { printf("Error: > Can't open output file.\n"); exit(1); } > printf("start\n"); > for(j=0; j<na*40; j++){ fprintf(fout, "%f %f %f\n", > Vec[3*j], Vec[3*j+1], Vec[3*j+2]); } > printf("end\n"); > fclose(fout); return 0;} > > Thanks, > > =Dennis Heimbigner Unidata Ticket Details =================== Ticket ID: AFZ-136343 Department: Support netCDF Priority: Normal Status: Open =================== NOTE: All email exchanges with Unidata User Support are recorded in the Unidata inquiry tracking system and then made publicly available through the web. If you do not want to have your interactions made available in this way, you must let us know in each email you send to us.