Commit 610868681d8eb9d730ddedc5f84a9d416c3b7c09

Authored by Benjamin Renard
1 parent d38b46f2

Implements getInfo for local database in netCDF format (#6087)

src/ParamGetImpl/LocalFileInterface/FileReaderASCII.cc
... ... @@ -31,8 +31,7 @@ bool FileReaderASCII::open(std::string filePath)
31 31 {
32 32 if (isOpened())
33 33 {
34   - LOG4CXX_ERROR(gLogger, "FileReaderASCII::open - A file is already opened");
35   - return false;
  34 + return true;
36 35 }
37 36 // Open ascii stream
38 37 _asciiStream.open (filePath, std::ios::in);
... ...
src/ParamGetImpl/LocalFileInterface/FileReaderNetCDF.cc
... ... @@ -23,6 +23,7 @@ FileReaderNetCDF::FileReaderNetCDF()
23 23  
24 24 FileReaderNetCDF::~FileReaderNetCDF()
25 25 {
  26 + resetFile();
26 27 }
27 28  
28 29 bool FileReaderNetCDF::open(std::string filePath)
... ... @@ -193,8 +194,73 @@ FileReaderStatus FileReaderNetCDF::getParamPacketData(std::string& timeId, std::
193 194 return FRS_EOF;
194 195 }
195 196  
196   -bool FileReaderNetCDF::getInfo(const char* /*pInfoName*/, std::vector<double>& /*res*/)
  197 +bool FileReaderNetCDF::getInfo(const char* pInfoName, std::vector<double>& res)
197 198 {
  199 + for (std::vector<NetCDFVar>::iterator it = _file.vars.begin(); it != _file.vars.end(); ++it) {
  200 + if (std::strcmp(it->name, pInfoName) == 0) {
  201 + if (it->dimids.empty()) {
  202 + LOG4CXX_ERROR(gLogger, "FileReaderNetCDF::getInfo - dims error")
  203 + return false;
  204 + }
  205 + int nbValues = 1;
  206 + for (int i = 0; i < (int)it->dimids.size(); ++i) {
  207 + nbValues *= _file.dims[it->dimids[i]].length;
  208 + }
  209 + int valuesize = 0;
  210 + switch (it->type) {
  211 + case NC_FLOAT :
  212 + valuesize = sizeof(float);
  213 + break;
  214 + case NC_DOUBLE :
  215 + valuesize = sizeof(double);
  216 + break;
  217 + case NC_SHORT :
  218 + valuesize = sizeof(short);
  219 + break;
  220 + case NC_INT :
  221 + valuesize = sizeof(int);
  222 + break;
  223 + default:
  224 + LOG4CXX_ERROR(gLogger, "FileReaderNetCDF::getInfo - var type not supported")
  225 + return false;
  226 + }
  227 + int varid;
  228 + int status = nc_inq_varid (_file.id, it->name, &varid);
  229 + if (status != NC_NOERR) {
  230 + LOG4CXX_ERROR(gLogger, "FileReaderNetCDF::getInfo - Cannot load variable data : " << getErrorMessage(status))
  231 + return false;
  232 + }
  233 + void* buffer = std::malloc(nbValues*valuesize);
  234 + status = nc_get_var(_file.id, varid, buffer);
  235 + if (status != NC_NOERR) {
  236 + LOG4CXX_ERROR(gLogger, "FileReaderNetCDF::getInfo - Cannot load info data : " << getErrorMessage(status))
  237 + std::free(buffer);
  238 + return false;
  239 + }
  240 + for (int i = 0; i < nbValues; ++i) {
  241 + switch (it->type) {
  242 + case NC_FLOAT :
  243 + res.push_back(((float*)buffer)[i]);
  244 + break;
  245 + case NC_DOUBLE :
  246 + res.push_back(((double*)buffer)[i]);
  247 + break;
  248 + case NC_SHORT :
  249 + res.push_back(((short*)buffer)[i]);
  250 + break;
  251 + case NC_INT :
  252 + res.push_back(((int*)buffer)[i]);
  253 + break;
  254 + default:
  255 + return false;
  256 + }
  257 + }
  258 + std::free(buffer);
  259 + return true;
  260 + }
  261 + }
  262 +
  263 + LOG4CXX_ERROR(gLogger, "FileReaderNetCDF::getInfo - Cannot retrieve data for " << pInfoName)
198 264 return false;
199 265 }
200 266  
... ...
src/ParamGetImpl/LocalFileInterface/VirtualInstrument.cc
... ... @@ -388,7 +388,7 @@ FileReaderStatus VirtualInstrument::getParamPacketData(std::string&amp; paramId, int
388 388 stopTime, packet);
389 389  
390 390 //close the file
391   - if (!_fileReaderPtr->close())
  391 + if ((status != FRS_MORE) && !_fileReaderPtr->close())
392 392 {
393 393 LOG4CXX_ERROR(gLogger, "VirtualInstrument::getParamData - Cannot close file " << filePath);
394 394 }
... ...