| 1 | /*
 | 
|---|
| 2 |  * QSeisXMLParser.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Apr 5, 2011
 | 
|---|
| 5 |  *      Author: bierbach
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | // include config.h
 | 
|---|
| 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 10 | #include <config.h>
 | 
|---|
| 11 | #endif
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include <QtCore/QDir>
 | 
|---|
| 14 | #include <QtCore/QList>
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "UIElements/Views/Qt4/Plotting/QSeisPageRegistry.hpp"
 | 
|---|
| 17 | #include "UIElements/Views/Qt4/Plotting/QSeisPlotPage.hpp"
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "QSeisXMLParser.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | // property tree uses alignment new
 | 
|---|
| 22 | #include <boost/property_tree/ptree.hpp>
 | 
|---|
| 23 | #include <boost/property_tree/xml_parser.hpp>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | // have this after(!) all Qt includes
 | 
|---|
| 26 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include <boost/foreach.hpp>
 | 
|---|
| 29 | #include <string>
 | 
|---|
| 30 | #include <set>
 | 
|---|
| 31 | #include <exception>
 | 
|---|
| 32 | #include <iostream>
 | 
|---|
| 33 | #include <sstream>
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | /** Constructor for QSeisXMLParser.
 | 
|---|
| 40 |  *
 | 
|---|
| 41 |  */
 | 
|---|
| 42 | QSeisXMLParser::QSeisXMLParser()
 | 
|---|
| 43 | {
 | 
|---|
| 44 |         unitmap["mass"] = "mass";
 | 
|---|
| 45 |         unitmap["length"] = "length";
 | 
|---|
| 46 |         unitmap["time"] = "time";
 | 
|---|
| 47 |         unitmap["current"] = "current";
 | 
|---|
| 48 |         unitmap["temperature"] = "temperature";
 | 
|---|
| 49 |         unitnames.push_back("mass");
 | 
|---|
| 50 |         unitnames.push_back("length");
 | 
|---|
| 51 |         unitnames.push_back("time");
 | 
|---|
| 52 |         unitnames.push_back("current");
 | 
|---|
| 53 |         unitnames.push_back("temperature");
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /** Destructor for QSeisXMLParser.
 | 
|---|
| 57 |  *
 | 
|---|
| 58 |  */
 | 
|---|
| 59 | QSeisXMLParser::~QSeisXMLParser()
 | 
|---|
| 60 | {
 | 
|---|
| 61 |   // remove all types
 | 
|---|
| 62 |   for(std::vector<plottype*>::iterator it=types.begin();it!=types.end();++it) {
 | 
|---|
| 63 |     delete *it;
 | 
|---|
| 64 |   }
 | 
|---|
| 65 |   types.clear();
 | 
|---|
| 66 |   // remove all curves
 | 
|---|
| 67 |   for(std::vector<plotcurve*>::iterator it=curves.begin();it!=curves.end();++it) {
 | 
|---|
| 68 |                 delete *it;
 | 
|---|
| 69 |         }
 | 
|---|
| 70 |         curves.clear();
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /** Returns list of desired files.
 | 
|---|
| 74 |  *
 | 
|---|
| 75 |  * Desired files are all listed in the parsed .xml file, with prefix full
 | 
|---|
| 76 |  * file names are constructed and returned.
 | 
|---|
| 77 |  *
 | 
|---|
| 78 |  * @param prefix prefix of filenames
 | 
|---|
| 79 |  * @return list of all desired files
 | 
|---|
| 80 |  */
 | 
|---|
| 81 | QStringList QSeisXMLParser::getFiles(QString prefix) {
 | 
|---|
| 82 |             QStringList file_names;
 | 
|---|
| 83 |             qDebug("List of desired curves:");
 | 
|---|
| 84 |             for(size_t i=0;i<(curves.size());i++) {
 | 
|---|
| 85 |               QString name = prefix+QString(".")+QString((*curves[i]).get_suffix().c_str());
 | 
|---|
| 86 |               qDebug("\t%s", name.toAscii().constData());
 | 
|---|
| 87 |                 file_names.append(name);
 | 
|---|
| 88 |             }
 | 
|---|
| 89 |             return file_names;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | /** Loads an .xml file.
 | 
|---|
| 93 |  *
 | 
|---|
| 94 |  *  Loads the XML file, then parses types and curves contained.
 | 
|---|
| 95 |  *
 | 
|---|
| 96 |  * @param file file name
 | 
|---|
| 97 |  */
 | 
|---|
| 98 | void QSeisXMLParser::load(std::istream& file) {
 | 
|---|
| 99 |         curves_by_suffix.clear();
 | 
|---|
| 100 |         using boost::property_tree::ptree;
 | 
|---|
| 101 |         ptree pt;
 | 
|---|
| 102 |         qDebug("Parsing XML file ... ");
 | 
|---|
| 103 |         read_xml(file,pt);
 | 
|---|
| 104 |         qDebug("Parsing types ... ");
 | 
|---|
| 105 |         BOOST_FOREACH(ptree::value_type &i,pt.get_child("seismolo.plottypes")) {
 | 
|---|
| 106 |                 std::string name=i.second.get<std::string>("name");
 | 
|---|
| 107 |                 std::string style=i.second.get<std::string>("style");
 | 
|---|
| 108 |                 std::string xlabel=i.second.get<std::string>("xlabel");
 | 
|---|
| 109 |                 std::string ylabel=i.second.get<std::string>("ylabel");
 | 
|---|
| 110 |                 std::vector<int> xunits;
 | 
|---|
| 111 |                 xunits.push_back(i.second.get<int>("xunits.mass"));
 | 
|---|
| 112 |                 xunits.push_back(i.second.get<int>("xunits.length"));
 | 
|---|
| 113 |                 xunits.push_back(i.second.get<int>("xunits.time"));
 | 
|---|
| 114 |                 xunits.push_back(i.second.get<int>("xunits.current"));
 | 
|---|
| 115 |                 xunits.push_back(i.second.get<int>("xunits.temperature"));
 | 
|---|
| 116 |                 std::vector<int> yunits;
 | 
|---|
| 117 |                 yunits.push_back(i.second.get<int>("yunits.mass"));
 | 
|---|
| 118 |                 yunits.push_back(i.second.get<int>("yunits.length"));
 | 
|---|
| 119 |                 yunits.push_back(i.second.get<int>("yunits.time"));
 | 
|---|
| 120 |                 yunits.push_back(i.second.get<int>("yunits.current"));
 | 
|---|
| 121 |                 yunits.push_back(i.second.get<int>("yunits.temperature"));
 | 
|---|
| 122 |                 plottype * tmp = new plottype(name,xlabel,ylabel,style,xunits,yunits, unitnames,unitmap);
 | 
|---|
| 123 |                 types.push_back(tmp);
 | 
|---|
| 124 |                 qDebug("\ttype: %s, %p", name.c_str(), types.back());
 | 
|---|
| 125 |                 types_by_name.insert(std::make_pair(types.back()->get_name(),types.back()));
 | 
|---|
| 126 |         }
 | 
|---|
| 127 |         qDebug("Parsing curves ... ");
 | 
|---|
| 128 |         BOOST_FOREACH(ptree::value_type &i,pt.get_child("seismolo.plotcurves")) {
 | 
|---|
| 129 |                 std::string name=i.second.get<std::string>("name");
 | 
|---|
| 130 |                 std::string type=i.second.get<std::string>("type");
 | 
|---|
| 131 |                 std::string suffix=i.second.get<std::string>("suffix");
 | 
|---|
| 132 |                 std::string color=i.second.get<std::string>("color");
 | 
|---|
| 133 |                 int column=i.second.get<int>("column");
 | 
|---|
| 134 |                 // check that type exists
 | 
|---|
| 135 |                 ASSERT(types_by_name.count(type),
 | 
|---|
| 136 |                                 "QSeisXMLParser::load() - type "+type+" for curve "+name+" not found.");
 | 
|---|
| 137 | 
 | 
|---|
| 138 |                 std::stringstream tmp;
 | 
|---|
| 139 |                 tmp<<column;
 | 
|---|
| 140 |                 if (suffix[0] == '.') // erase leading dot if present
 | 
|---|
| 141 |                   suffix.erase(suffix.begin());
 | 
|---|
| 142 |                 plotcurve* curv=new plotcurve(name,type,suffix,color,column);
 | 
|---|
| 143 |                 curves.push_back(curv);
 | 
|---|
| 144 |     qDebug("\tcurve: %s", curves.back()->get_name().c_str());
 | 
|---|
| 145 |                 curves_suff.insert(std::make_pair(suffix+tmp.str(),curv));
 | 
|---|
| 146 |                 curves_by_suffix.insert(std::make_pair(suffix+tmp.str(),type));
 | 
|---|
| 147 |                 ASSERT(curves_by_suffix.size()!=0,"curves by suffix is empty!");
 | 
|---|
| 148 |         }
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | /** Creates plot page for each \a types.
 | 
|---|
| 152 |  *
 | 
|---|
| 153 |  */
 | 
|---|
| 154 | void QSeisXMLParser::createPlotPages() {
 | 
|---|
| 155 |         for(size_t i=0;i<types.size();i++) {
 | 
|---|
| 156 |                 const std::string name=types[i]->get_name();
 | 
|---|
| 157 |                 QSeisPageRegistry::getInstance().getByName(name);
 | 
|---|
| 158 |         }
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /** Associates each curve in \a curves with its plot page.
 | 
|---|
| 162 |  *
 | 
|---|
| 163 |  */
 | 
|---|
| 164 | void QSeisXMLParser::associateCurves() {
 | 
|---|
| 165 |         for(quint32 i=0;i<(curves.size());i++) {
 | 
|---|
| 166 |                 std::string temp=(*curves[i]).get_type();
 | 
|---|
| 167 |                 int u=(*curves[i]).get_column();
 | 
|---|
| 168 |                 std::stringstream ostrs;
 | 
|---|
| 169 |                 QString watchPath; //=QSeisFileRegistry::getInstance().getWatchPath();
 | 
|---|
| 170 |                 ostrs << prefix.toStdString() << (*curves[i]).get_suffix() << u;
 | 
|---|
| 171 |                 QSeisPlotPage *instance = QSeisPageRegistry::getInstance().getByName(temp);
 | 
|---|
| 172 |                 instance->addCurve(ostrs.str());
 | 
|---|
| 173 |         }
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | /** Getter for type for a given curve.
 | 
|---|
| 177 |  *
 | 
|---|
| 178 |  * @param name name of curve
 | 
|---|
| 179 |  * @return type of curve, i.e. the plot page
 | 
|---|
| 180 |  */
 | 
|---|
| 181 | QString QSeisXMLParser::getTypeOfName(const QString &name) {
 | 
|---|
| 182 |         std::string suffix(name.toStdString());
 | 
|---|
| 183 |         int found=suffix.find(".");
 | 
|---|
| 184 |         ASSERT(found!=-1,"Name of curve " + suffix + " is invalid.");
 | 
|---|
| 185 |         suffix=suffix.substr(found+1,suffix.size()-(found+1));
 | 
|---|
| 186 | 
 | 
|---|
| 187 | //      Debugausgabe
 | 
|---|
| 188 |         std::map<std::string,std::string>::iterator m;
 | 
|---|
| 189 |         for(m=curves_by_suffix.begin();m!=curves_by_suffix.end();++m) {
 | 
|---|
| 190 |                 std::cerr << m->first << "----" << m->second << std::endl;
 | 
|---|
| 191 |         }
 | 
|---|
| 192 |         if (curves_by_suffix.count(suffix))
 | 
|---|
| 193 |                 return QString(curves_by_suffix[suffix].c_str());
 | 
|---|
| 194 |         else
 | 
|---|
| 195 |                 return QString();
 | 
|---|
| 196 | }
 | 
|---|
| 197 | 
 | 
|---|
| 198 | std::string QSeisXMLParser::getCurveColor(std::string suff_col) {
 | 
|---|
| 199 |         if(curves_suff.count(suff_col)!=0) {
 | 
|---|
| 200 |                 return curves_suff[suff_col]->get_color();
 | 
|---|
| 201 |         }
 | 
|---|
| 202 |         else return std::string();
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | plottype* QSeisXMLParser::return_plottype_by_name(std::string name) {
 | 
|---|
| 206 |         if(types_by_name.count(name)!=0) {
 | 
|---|
| 207 |                 return types_by_name[name];
 | 
|---|
| 208 |         } else return NULL;
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | void QSeisXMLParser::set_units(const std::vector<std::string>& unitnames_xml,const std::map<std::string,std::string>& unitmap_xml) {
 | 
|---|
| 212 |         unitmap=unitmap_xml;unitnames=unitnames_xml;
 | 
|---|
| 213 | }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | CONSTRUCT_SINGLETON(QSeisXMLParser)
 | 
|---|