[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #EQF-173486]: Read 3D netcdf data with c++



Hello,

I would suggest updating to the latest C++ release, 4.3.0, available from:

* https://github.com/Unidata/netcdf-cxx4/releases

As there were serious issues discovered in the 4.2.1 release which may or may 
not be contributing to the errors you are seeing.

Additionally, you might try running the code via a debugger such as gdb; it 
appears that there is a memory error of some sort, and gdb would tell you at 
which exact line it is occurring on.

-Ward

> Dear Sir or Madame,
> 
> I have tried to read netcdf-4 data with the c++ libraries but it
> failed although I get no error back from the script. Here is my c++
> code:
> 
> #include <iostream>
> #include </localdata/giersch/netcdf/netcdf-cxx-v4.2.1/include/netcdf>
> #include <stdio.h>
> #include <string.h>
> 
> using namespace std;
> using namespace netCDF;
> using namespace netCDF::exceptions;
> 
> 
> // Return this in event of a problem.
> static const int NC_ERR = 2;
> 
> 
> int main()
> {
> try
> {
> // Open the file for read access
> NcFile
> dataFile("Assist_gauss_6_3d_t_end/Assist_gauss_6_3d_t_end_netcdf4.nc",
> NcFile::read);
> 
> // Essential for determining the dimension. Calling of getSize
> becomes possible
> NcDim dim;
> 
> // Retrieve the variable named "u", "v", and "w"
> NcVar data_u=dataFile.getVar("u");
> if(data_u.isNull()) return NC_ERR;
> 
> NcVar data_v=dataFile.getVar("v");
> if(data_v.isNull()) return NC_ERR;
> 
> NcVar data_w=dataFile.getVar("w");
> if(data_w.isNull()) return NC_ERR;
> 
> // Retrieve the dimension of the z-axis
> dim = data_u.getDim(1);
> size_t dimz = dim.getSize();
> 
> // Retrieve the dimension of the y-axis
> dim = data_u.getDim(2);
> size_t dimy = dim.getSize();
> 
> // Retrieve the dimension of the x-axis
> dim = data_u.getDim(3);
> size_t dimx = dim.getSize();
> 
> // We are reading 3D data
> int NX = int(dimx);
> int NY = int(dimy);
> int NZ = int(dimz);
> 
> //  Array Iterators
> int i, j, k;
> 
> 
> // Allocate 3D Array
> float*** dataIn = new float**[NZ]();
> 
> 
> for(k = 0; k != NZ; ++k)
> {
> dataIn[k] = new float*[NY]();
> 
> for(j = 0; j != NY; ++j)
> {
> dataIn[k][j] = new float[NX]();
> }
> 
> }
> 
> // Declare start Vector specifying the index in the variable where
> the first
> // of the data values will be read.
> std::vector<size_t> start(4);
> 
> start[0] = 0;
> start[1] = 0;
> start[2] = 0;
> start[3] = 0;
> 
> // Declare count Vector specifying the edge lengths along each dimension of
> // the block of data values to be read.
> std::vector<size_t> count(4);
> 
> count[0] = 1;
> count[1] = 130;
> count[2] = 257;
> count[3] = 257;
> 
> data_u.getVar(start,count,dataIn);
> 
> 
> // The netCDF file is automatically closed by the NcFile destructor
> cout << "*** SUCCESS reading file
> Assist_gauss_6_3d_t_end_netcdf4.nc!" << endl;
> 
> 
> return 0;
> 
> }catch(NcException& e)
> {
> e.what();
> cout<<"FAILURE*************************************"<<endl;
> return NC_ERR;
> }
> }
> 
> When i compile the program with g++ -I$NETCDF_HOME/include
> -L$NETCDF_HOME/lib64 -lnetcdf_c++4 -g3 -O0 ./inputFG.cpp -o input2 I
> get no error. The execution ./input2 causes:
> 
> *** SUCCESS reading file Assist_gauss_6_3d_t_end_netcdf4.nc!
> Segmentation fault
> 
> I can not find the error. Pleas help me.
> 
> Some further information. I use the 4.2.1 cxx netcdf libraries and
> include files. ncdump -h of my file which i would like to read has the
> following output:
> 
> netcdf Assist_gauss_6_3d_t_end_netcdf4 {
> dimensions:
> time = UNLIMITED ; // (1 currently)
> zu_3d = 130 ;
> y = 257 ;
> x = 257 ;
> xu = 257 ;
> yv = 257 ;
> zw_3d = 130 ;
> variables:
> float p(time, zu_3d, y, x) ;
> p:long_name = "p" ;
> p:units = "Pa" ;
> double time(time) ;
> time:units = "seconds" ;
> float u(time, zu_3d, y, xu) ;
> u:long_name = "u" ;
> u:units = "m/s" ;
> float v(time, zu_3d, yv, x) ;
> v:long_name = "v" ;
> v:units = "m/s" ;
> float w(time, zw_3d, y, x) ;
> w:long_name = "w" ;
> w:units = "m/s" ;
> double x(x) ;
> x:units = "meters" ;
> double xu(xu) ;
> xu:units = "meters" ;
> double y(y) ;
> y:units = "meters" ;
> double yv(yv) ;
> yv:units = "meters" ;
> double zu_3d(zu_3d) ;
> zu_3d:units = "meters" ;
> double zw_3d(zw_3d) ;
> zw_3d:units = "meters" ;
> 
> // global attributes:
> :Conventions = "COARDS" ;
> :title = "PALM 4.0  Rev: 1710  run: Assist_gauss_6.01
> host: lccrayh  05-11-15 08:30:14" ;
> :VAR_LIST = ";u;v;w;p;" ;
> :history = "Tue Sep 27 13:36:44 2016: ncks -4
> Assist_gauss_6_3d_t_end.nc Assist_gauss_6_3d_t_end_netcdf4.nc\nThu Apr
> 28 09:05:16 2016: ncks -d time,95,95 Assist_gauss_6_3d.1.nc
> Assist_gauss_6_3d_t_end.nc" ;
> :NCO = "4.4.6" ;
> }
> 
> 
> Best regards,
> Sebastian Giersch
> 
> 
> 


Ticket Details
===================
Ticket ID: EQF-173486
Department: Support netCDF
Priority: Normal
Status: Closed
===================
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.