source: trunk/examples/c-example/plot.cc @ 4503

Last change on this file since 4503 was 3177, checked in by mmc, 12 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

File size: 3.0 KB
Line 
1// ======================================================================
2//  Copyright (c) 2004-2012  HUBzero Foundation, LLC
3//  See the file "license.terms" for information on usage and
4//  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
5// ======================================================================
6
7#include "rappture.h"
8
9#include <stdlib.h>
10#include <math.h>
11#include <iostream>
12#include <sstream>
13
14#ifdef DEBUG
15static int debug = 1;
16#else
17static int debug = 0;
18#endif
19
20int main(int argc, char * argv[])
21{
22
23    RpLibrary* lib = NULL;
24
25    std::string filePath = "";
26    std::string xmltext = "";
27    double fmin, fmax;
28    std::string strFormula = "";
29    int i;
30
31    if (argc < 2) {
32        std::cout << "usage: " << argv[0] << " driver.xml" << std::endl;
33    }
34
35    filePath = std::string(argv[1]);
36
37    if (debug) {
38        std::cout << "filePath: " << filePath << std::endl;
39    }
40
41    // create a rappture library from the file filePath
42    lib = new RpLibrary(filePath);
43
44    if (lib) {
45        if(debug) {
46            std::cout << "created Rappture Library successfully" << std::endl;
47        }
48    }
49    else {
50        // cannot open file or out of memory
51        std::cout << "FAILED creating Rappture Library" << std::endl;
52        exit(1);
53    }
54
55    // get the xml that is stored in the rappture library lib
56    xmltext = lib->xml();
57
58    if(! (xmltext.empty()) ) {
59        if(debug) {
60        //printf("XML file content:\n");
61        //printf("%s\n", xmltext);
62    }
63    }
64    else {
65        printf("lib->xml() failed\n");
66        delete lib;
67        exit(1);
68    }
69
70    // get the min
71    xmltext = lib->getString("input.number(min).current");
72
73    if ( xmltext.empty() ) {
74        std::cout << "lib->getString(input.number(xmin).current) returns null" << std::endl;
75        delete lib;
76        exit(1);
77    }
78
79    if(debug) {
80        std::cout << "xml min :" << xmltext << ": len=" << xmltext.size() << std::endl;
81    }
82
83    // grab a double value from the xml
84    fmin = lib->getDouble("input.number(min).current");
85
86    if(debug) {
87        std::cout << "min: " << fmin << std::endl;
88    }
89
90    // get the max
91    fmax = lib->getDouble("input.(max).current");
92    if(debug) {
93        std::cout << "max: " << fmax << std::endl;
94    }
95
96    // label the graph with a title
97    lib->put("output.curve(result).about.label",
98        "Formula: Y Vs X","",RPLIB_OVERWRITE);
99
100    // evaluate formula and generate results
101    // science begains here
102    double fx, fy;
103    int npts = 100;
104    std::stringstream myStr;
105
106    for (i = 0; i<npts; i++) {
107        fx = i* (fmax - fmin)/npts + fmin;
108        fy = sin(fx);
109        myStr << fx << " " << fy << std::endl;
110        lib->put("output.curve(result).component.xy",
111            myStr.str(),"",RPLIB_APPEND);
112        myStr.str("");
113    }
114
115
116    // write output to run file and signal
117    lib->result();
118
119    delete lib;
120
121    return 0;
122}
Note: See TracBrowser for help on using the repository browser.