[Kst] extragear/graphics/kst/src/datasources/netcdf

Nicolas Brisset nicolas.brisset at eurocopter.com
Wed Aug 23 22:09:04 CEST 2006


SVN commit 576326 by brisset:

Add initial support for metadata from netCDF files from global attributes. For multi-valued attributes, only the first value is used (but strings are complete and certainly the most interesting kind of metadata). If there are requests for getting more values it should be easy to add. Also change kdDebug() to kstdDebug() and comment out debugging traces. Only failures still print out something. Now George can test this and try to make metadata strings usable in labels :-) I am committing directly as it is a very isolated change that won't affect users without netCDF support, and it is easy to revert in case of problems.

 M  +18 -13    netcdf.cpp  


--- trunk/extragear/graphics/kst/src/datasources/netcdf/netcdf.cpp #576325:576326
@@ -18,7 +18,7 @@
 
 #include "netcdf_source.h" // Local header for the kst netCDF datasource
 
-#include <kdebug.h>
+#include <ksdebug.h>
 
 #include <qfile.h>
 #include <qfileinfo.h>
@@ -59,16 +59,16 @@
 bool NetcdfSource::initFile() {
   _ncfile = new NcFile(_filename.latin1(), NcFile::ReadOnly);
   if (!_ncfile->is_valid()) {
-      kdDebug() << _filename << ": failed to open in initFile()" << endl;
+      kstdDebug() << _filename << ": failed to open in initFile()" << endl;
       return false;
     }
 
-  kdDebug() << _filename << ": building field list" << endl;
+  // kstdDebug() << _filename << ": building field list" << endl;
   _fieldList.clear();
   _fieldList += "INDEX";
 
   int nb_vars = _ncfile->num_vars();
-  kdDebug() << nb_vars << " vars found in total" << endl;
+  // kstdDebug() << nb_vars << " vars found in total" << endl;
 
   _maxFrameCount = 0;
 
@@ -80,8 +80,13 @@
     _frameCounts[var->name()] = fc;
   }
 
-  kdDebug() << "Frame counts: " << _maxFrameCount << endl;
-
+  // Get metadata
+  int globalAttributesNb = _ncfile->num_atts();
+  for (int i = 0; i < globalAttributesNb; ++i) {
+    // Get only first value, should be enough for a start especially as strings are complete
+    _metaData[QString(_ncfile->get_att(i)->name())] = QString(_ncfile->get_att(i)->as_string(0));
+  }
+  
   update(); // necessary?  slows down initial loading
   return true;
 }
@@ -116,7 +121,7 @@
   /* Values for one record */
   NcValues *record;// = new NcValues(dataType,numFrameVals);
 
-  kdDebug() << "Entering NetcdfSource::readField with params: " << field << ", from " << s << " for " << n << " frames" << endl;
+  // kstdDebug() << "Entering NetcdfSource::readField with params: " << field << ", from " << s << " for " << n << " frames" << endl;
 
   /* For INDEX field */
   if (field.lower() == "index") {
@@ -133,7 +138,7 @@
   /* For a variable from the netCDF file */
   NcVar *var = _ncfile->get_var(field.latin1());
   if (!var) {
-    kdDebug() << "Queried field " << field << " which can't be read" << endl;
+    kstdDebug() << "Queried field " << field << " which can't be read" << endl;
     return -1;
   }
 
@@ -171,7 +176,7 @@
         } else {
           for (int i = 0; i < n; i++) {
             record = var->get_rec(i+s);
-            kdDebug() << "Read record " << i+s << endl;
+            // kstdDebug() << "Read record " << i+s << endl;
             for (int j = 0; j < recSize; j++) {
               v[i*recSize + j] = record->as_int(j);
             }
@@ -213,13 +218,13 @@
       break;
 
     default:
-      kdDebug() << field << ": wrong datatype for kst, no values read" << endl;
+      kstdDebug() << field << ": wrong datatype for kst, no values read" << endl;
       return -1;
       break;
 
   }
 
-  kdDebug() << "Finished reading " << field << endl;
+  // kstdDebug() << "Finished reading " << field << endl;
 
   return oneSample ? 1 : n * recSize;
 }
@@ -293,13 +298,13 @@
     QFile f(filename);
 
     if (!f.open(IO_ReadOnly)) {
-      kdDebug() << "Unable to read file !" << endl;
+      kstdDebug() << "Unable to read file !" << endl;
       return 0;
     }
 
     NcFile *ncfile = new NcFile(filename.latin1());
     if (ncfile->is_valid()) {
-      kdDebug() << filename << " looks like netCDF !" << endl;
+      // kstdDebug() << filename << " looks like netCDF !" << endl;
       delete ncfile;
       return 80;
     } else {


More information about the Kst mailing list