[Kst] branches/work/kst/portto4/kst

Peter Kümmel syntheticpp at gmx.net
Fri Jan 14 15:27:49 CET 2011


SVN commit 1214399 by kuemmel:

- fix crashes when loading nercdf data
- don't exit when a field is not found

 M  +25 -3     src/datasources/netcdf/netcdfsource.cpp  
 M  +3 -0      src/datasources/netcdf/netcdfsource.h  
 M  +10 -0     tests/testhistogram.cpp  


--- branches/work/kst/portto4/kst/src/datasources/netcdf/netcdfsource.cpp #1214398:1214399
@@ -179,8 +179,12 @@
 
 QMap<QString, double> DataInterfaceNetCdfVector::metaScalars(const QString& field)
 {
+  NcVar *var = netcdf._ncfile->get_var(field.toLatin1().constData());
+  if (!var) {
+    KST_DBG qDebug() << "Queried field " << field << " which can't be read" << endl;
+    return QMap<QString, double>();
+  }
   QMap<QString, double> fieldScalars;
-  NcVar *var = netcdf._ncfile->get_var(field.toLatin1().constData());
   fieldScalars["NbAttributes"] = var->num_atts();
   for (int i=0; i<var->num_atts(); ++i) {
     NcAtt *att = var->get_att(i);
@@ -201,9 +205,13 @@
 
 QMap<QString, QString> DataInterfaceNetCdfVector::metaStrings(const QString& field)
 {
+  NcVar *var = netcdf._ncfile->get_var(field.toLatin1().constData());
+  if (!var) {
+    KST_DBG qDebug() << "Queried field " << field << " which can't be read" << endl;
+    return QMap<QString, QString>();
+  }
   QMap<QString, QString> fieldStrings;
   QString tmpString;
-  NcVar *var = netcdf._ncfile->get_var(field.toLatin1().constData());
   for (int i=0; i<var->num_atts(); ++i) {
     NcAtt *att = var->get_att(i);
     // Only handle char/unspecified attributes as fieldStrings, the others as fieldScalars
@@ -256,6 +264,9 @@
 
   QByteArray bytes = matrix.toLatin1();
   NcVar *var = netcdf._ncfile->get_var(bytes.constData());  // var is owned by _ncfile
+  if (!var) {
+    return DataMatrix::DataInfo();
+  }
 
   if (var->num_dims() != 2) {
     return DataMatrix::DataInfo();
@@ -296,6 +307,7 @@
 NetcdfSource::NetcdfSource(Kst::ObjectStore *store, QSettings *cfg, const QString& filename, const QString& type, const QDomElement &element) :
   Kst::DataSource(store, cfg, filename, type),
   _ncfile(0L),
+  _ncErr(NcError::silent_nonfatal),
   is(new DataInterfaceNetCdfScalar(*this)),
   it(new DataInterfaceNetCdfString(*this)),
   iv(new DataInterfaceNetCdfVector(*this)),
@@ -352,6 +364,9 @@
 
   for (int i = 0; i < nb_vars; i++) {
     NcVar *var = _ncfile->get_var(i);
+    if (!var) {
+      continue;
+    }
     if (var->num_dims() == 0) {
       _scalarList += var->name();
     } else if (var->num_dims() == 1) {
@@ -403,6 +418,9 @@
   int nb_vars = _ncfile->num_vars();
   for (int j = 0; j < nb_vars; j++) {
     NcVar *var = _ncfile->get_var(j);
+    if (!var) {
+      continue;
+    }
     int fc = var->num_vals() / var->rec_size();
     _maxFrameCount = qMax(_maxFrameCount, fc);
     updated = updated || (_frameCounts[var->name()] != fc);
@@ -417,9 +435,12 @@
   // TODO error handling
   QByteArray bytes = field.toLatin1();
   NcVar *var = _ncfile->get_var(bytes.constData());  // var is owned by _ncfile
+  if (var) {
   var->get(v);
   return 1;
 }
+  return 0;
+}
 
 int NetcdfSource::readString(QString *stringValue, const QString& stringName)
 {
@@ -427,9 +448,9 @@
   NcAtt *att = _ncfile->get_att((NcToken) stringName.toLatin1().data());
   if (att) {
     *stringValue = QString(att->as_string(0));
+    delete att;
     return 1;
   }
-  delete att;
   return 0;
 }
 
@@ -592,6 +613,7 @@
   QByteArray bytes = field.toLatin1();
   NcVar *var = _ncfile->get_var(bytes.constData());
   if (!var) {
+    KST_DBG qDebug() << "Queried field " << field << " which can't be read" << endl;
     return 0;
   }
   return var->rec_size();
--- branches/work/kst/portto4/kst/src/datasources/netcdf/netcdfsource.h #1214398:1214399
@@ -73,6 +73,9 @@
     int _maxFrameCount;
     NcFile *_ncfile;    
 
+    // we must hold an NcError to overwrite the exit-on-error behaviour of netCDF
+    NcError _ncErr;
+
     // QMap<QString, QString> _metaData;
 
     // TODO remove friend
--- branches/work/kst/portto4/kst/tests/testhistogram.cpp #1214398:1214399
@@ -30,8 +30,18 @@
 void TestHistogram::testHistogram() {
   Kst::GeneratedVectorPtr gvp = Kst::kst_cast<Kst::GeneratedVector>(_store.createObject<Kst::GeneratedVector>());
   Q_ASSERT(gvp);
+  QCOMPARE(gvp->length(), 1);
+
   gvp->changeRange(0, 10, 100);
+  QCOMPARE(gvp->max(), 0.0);
+  QCOMPARE(gvp->length(), 100);
+
+  gvp->internalUpdate();
+  QCOMPARE(gvp->max(), 10.0);
+  QCOMPARE(gvp->length(), 100);
+
   Kst::VectorPtr vp(gvp);
+  QCOMPARE(vp->max(), 10.0);
 
   Kst::HistogramPtr h1 = Kst::kst_cast<Kst::Histogram>(_store.createObject<Kst::Histogram>());
   h1->change(vp, 0, 10, 10, Kst::Histogram::Number);


More information about the Kst mailing list