[Kst] branches/work/kst/portto4/kst/src/datasources/netcdf

Nicolas Brisset nicolas.brisset at eurocopter.com
Thu Nov 18 21:25:08 CET 2010


SVN commit 1198533 by brisset:

With 2.0.2 out the door and hopefully stable enough to keep people busy a couple of days, I'd like to start looking at metadata a 
bit.
NetCDF is quite cool in that respect, as it supports a lot of stuff:
- strings
- scalars
- vectors
- matrices
- per-primitive strings (aka attributes)

I'd like to reach the point where the sample file at the following location: 
http://www.unidata.ucar.edu/software/netcdf/examples/GOTEX.C130_N130AR.LRT.RF06.PNI.nc 
can be loaded "completely".

This commit adds support for data-source-level strings (global attributes), but there is a problem: I can't easily check them, as 
the View->Strings dialog is too primitive and gathers only kst application-level strings, i.e. the strings defined in the 
application itself. 
We need to do something like for scalars, maybe even generalizing the ScalarTreeItem from scalarmodel to handle strings as well 
as scalars. I'm not a template guru, so help with that would be appreciated. I wouldn't want to start duplicating the code from 
the scalar case!


 M  +57 -2     kstnetcdf.cpp  
 M  +7 -1      kstnetcdf.h  


--- branches/work/kst/portto4/kst/src/datasources/netcdf/kstnetcdf.cpp #1198532:1198533
@@ -79,10 +79,52 @@
 }
 
 
+//
+// String interface
+//
 
+class DataInterfaceNetCdfString : public DataSource::DataInterface<DataString>
+{
+public:
+  DataInterfaceNetCdfString(NetcdfSource& s) : netcdf(s) {}
 
+  // read one element
+  int read(const QString&, DataString::ReadInfo&);
 
+  // named elements
+  QStringList list() const { return netcdf._stringList; }
+  bool isListComplete() const { return true; }
+  bool isValid(const QString&) const;
 
+  // T specific
+  const DataString::DataInfo dataInfo(const QString&) const { return DataString::DataInfo(); }
+  void setDataInfo(const QString&, const DataString::DataInfo&) {}
+
+  // meta data
+  QMap<QString, double> metaScalars(const QString&) { return QMap<QString, double>(); }
+  QMap<QString, QString> metaStrings(const QString&) { return QMap<QString, QString>(); }
+
+
+private:
+  NetcdfSource& netcdf;
+};
+
+
+int DataInterfaceNetCdfString::read(const QString& string, DataString::ReadInfo& p)
+{
+  return netcdf.readString(p.value, string);
+}
+
+
+bool DataInterfaceNetCdfString::isValid(const QString& string) const
+{
+  return  netcdf._stringList.contains( string );
+}
+
+
+
+
+
 //
 // Vector interface
 //
@@ -217,10 +259,12 @@
   Kst::DataSource(store, cfg, filename, type),
   _ncfile(0L),
   is(new DataInterfaceNetCdfScalar(*this)),
+  it(new DataInterfaceNetCdfString(*this)),
   iv(new DataInterfaceNetCdfVector(*this)),
   im(new DataInterfaceNetCdfMatrix(*this))
 {
   setInterface(is);
+  setInterface(it);
   setInterface(iv);
   setInterface(im);
 
@@ -282,7 +326,7 @@
     }
   }
 
-  // Get metadata
+  // Get strings
   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
@@ -294,7 +338,7 @@
       delete[] attString;
       //TODO port
       //KstString *ms = new KstString(KstObjectTag(attrName, tag()), this, attrValue);
-      _metaData.insert(attrName, attrValue);
+      _stringList += attrName;
     }
     delete att;
   }
@@ -339,6 +383,17 @@
   return 1;
 }
 
+int NetcdfSource::readString(QString *stringValue, const QString& stringName)
+{
+  // TODO more error handling?
+  NcAtt *att = _ncfile->get_att((NcToken) stringName.toLatin1().data());
+  if (att) {
+    *stringValue = QString(att->as_string(0));
+    return 1;
+  }
+  delete att;
+  return 0;
+}
 
 int NetcdfSource::readField(double *v, const QString& field, int s, int n) {
   NcType dataType = ncNoType; /* netCDF data type */
--- branches/work/kst/portto4/kst/src/datasources/netcdf/kstnetcdf.h #1198532:1198533
@@ -27,6 +27,7 @@
 
 
 class DataInterfaceNetCdfScalar;
+class DataInterfaceNetCdfString;
 class DataInterfaceNetCdfVector;
 class DataInterfaceNetCdfMatrix;
 
@@ -46,6 +47,8 @@
 
     int readScalar(double *v, const QString& field);
 
+    int readString(QString *stringValue, const QString& stringName);
+
     int readField(double *v, const QString& field, int s, int n);
 
     int readMatrix(double *v, const QString& field);
@@ -68,18 +71,21 @@
     int _maxFrameCount;
     NcFile *_ncfile;    
 
-    QMap<QString, QString> _metaData;
+    // QMap<QString, QString> _metaData;
 
     // TODO remove friend
     QStringList _scalarList;
     QStringList _fieldList;
     QStringList _matrixList;
+    QStringList _stringList;
 
 
     friend class DataInterfaceNetCdfScalar;
+    friend class DataInterfaceNetCdfString;
     friend class DataInterfaceNetCdfVector;
     friend class DataInterfaceNetCdfMatrix;
     DataInterfaceNetCdfScalar* is;
+    DataInterfaceNetCdfString* it;
     DataInterfaceNetCdfVector* iv;
     DataInterfaceNetCdfMatrix* im;
 };


More information about the Kst mailing list