plots.cpp

Go to the documentation of this file.
00001 /**
00002  * @file plots.cpp
00003  */
00004 
00005 /*
00006  * $Revision: 368 $
00007  * $Date: 2010-01-03 19:46:26 -0500 (Sun, 03 Jan 2010) $
00008  */
00009 
00010 // Copyright 2002  California Institute of Technology
00011 
00012 
00013 #ifdef WIN32
00014 #pragma warning(disable:4786)
00015 #pragma warning(disable:4503)
00016 #endif
00017 
00018 #include "plots.h"
00019 
00020 using namespace std;
00021 
00022 namespace Cantera {
00023 
00024   //    Write a Plotting file
00025   /*
00026    * @param fname      Output file name
00027    * @param fmt        Either TEC or XL or CSV
00028    * @param plotTitle  Title of the plot
00029    * @param names      vector of variable names 
00030    * @param data       N x M data array. 
00031    *                     data(n,m) is the m^th value of the n^th variable.
00032    */
00033   void writePlotFile(const std::string &fname, const std::string &fmt, 
00034                      const std::string &plotTitle, 
00035                      const std::vector<std::string> &names,
00036                      const Array2D& data) {
00037     ofstream f(fname.c_str());
00038     if (!f) {
00039       throw CanteraError("writePlotFile","could not open file "+fname+
00040                          " for writing.");
00041     }
00042     if (fmt == "TEC") {
00043       outputTEC(f, plotTitle, names, data);
00044       f.close();
00045     }
00046     else if (fmt == "XL" || fmt == "CSV") {
00047       outputExcel(f, plotTitle, names, data);
00048       f.close();
00049     }
00050     else {
00051       throw CanteraError("writePlotFile",
00052                          "unsupported plot type:" + fmt);
00053     }
00054   }
00055             
00056 
00057   
00058   //     Write a Tecplot data file.
00059   /*
00060    * @param s        output stream 
00061    * @param title    plot title 
00062    * @param names    vector of variable names 
00063    * @param data      N x M data array. 
00064    *                 data(n,m) is the m^th value of the n^th variable.
00065    */
00066   void outputTEC(std::ostream &s, const std::string &title,
00067                  const std::vector<std::string>& names,  
00068                  const Array2D& data) {
00069     int i,j;
00070     int npts = static_cast<int>(data.nColumns());
00071     int nv = static_cast<int>(data.nRows());
00072     s << "TITLE     = \"" + title + "\"" << endl;
00073     s << "VARIABLES = " << endl;
00074     for (i = 0; i < nv; i++) {
00075       s << "\"" << names[i] << "\"" << endl;
00076     }
00077     s << "ZONE T=\"zone1\"" << endl;
00078     s << " I=" << npts << ",J=1,K=1,F=POINT" << endl;
00079     s << "DT=( ";
00080     for (i = 0; i < nv; i++) s << " SINGLE";
00081     s << " )" << endl;
00082     for (i = 0; i < npts; i++) {
00083       for (j = 0; j < nv; j++) {
00084         s << data(j,i) << " ";
00085       }
00086       s << endl;
00087     }
00088   }
00089 
00090 
00091  
00092   // Write an Excel spreadsheet in 'csv' form.  
00093   /*
00094    * @param s          output stream 
00095    * @param title      plot title 
00096    * @param names      vector of variable names
00097    * @param data       N x M data array.
00098    *                        data(n,m) is the m^th value of the n^th variable.
00099    */
00100   void outputExcel(std::ostream &s, const std::string &title, 
00101                    const std::vector<std::string>& names,  
00102                    const Array2D& data) {
00103     int i,j;
00104     int npts = static_cast<int>(data.nColumns());
00105     int nv = static_cast<int>(data.nRows());
00106     s << title + "," << endl;
00107     for (i = 0; i < nv; i++) {
00108       s << names[i] << ",";
00109     }
00110     s << endl;
00111     for (i = 0; i < npts; i++) {
00112       for (j = 0; j < nv; j++) {
00113         s << data(j,i) << ",";
00114       }
00115       s << endl;
00116     }
00117   }
00118 
00119 
00120 }
Generated by  doxygen 1.6.3