20 | | |
21 | | #include <netcdfcpp.h> |
22 | | void write_to_nc ( NcFile &nc, mat &X, std::string Xn, Array<std::string> A ) { |
23 | | char tmpstr[200]; |
24 | | int Len = X.rows(); |
25 | | |
26 | | sprintf ( tmpstr,"%s.length",Xn.c_str() ); |
27 | | NcDim* lengt = nc.add_dim ( tmpstr, ( long ) Len ); |
28 | | for ( int j=0; j<X.cols(); j++ ) { |
29 | | if ( j<A.length() ) |
30 | | sprintf ( tmpstr,"%s_%s",Xn.c_str(), ( A ( j ) ).c_str() ); |
31 | | else |
32 | | sprintf ( tmpstr,"%s_%d",Xn.c_str(),j ); |
33 | | // Create variables and their attributes |
34 | | NcVar* P = nc.add_var ( tmpstr, ncDouble, lengt ); |
35 | | const double* Dp = X._data(); |
36 | | P->put ( &Dp[j*Len],Len ); |
37 | | } |
38 | | } |
39 | | |
148 | | //////////////// |
149 | | // Just Testing |
150 | | NcFile nc ( "pmsm_sim.nc", NcFile::Replace ); // Create and leave in define mode |
151 | | if ( ! nc.is_valid() ) { std::cerr << "creation of NCFile failed."<<endl;} |
152 | | |
153 | | write_to_nc ( nc,Xt,"X","{isa isb om th }" ); |
154 | | write_to_nc ( nc,XtM,"XtM","{q1 q2 isa isb om th }" ); |
155 | | write_to_nc ( nc,XtE,"XE","{isa isb om th }" ); |
156 | | write_to_nc ( nc,Dt,"Dt","{isa isb ua ub }" ); |