00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <string>
00038 #include <vector>
00039 #include <stdio.h>
00040 #include "StdStringUtils.h"
00041 #include "Plot2D_OptionFile.h"
00042 #include "Plot2D.h"
00043
00044
00045
00046 using namespace namespace_Plot2D;
00047
00048
00049
00050 void PLOT2D_DisplayHelp();
00051
00052
00053
00054 bool PLOT2D_GenerateDefaultOptionFile( std::string filepath );
00055
00056
00057 int main( int argc, char* argv[] )
00058 {
00059 int i = 0;
00060 int j = 0;
00061 int coltmp_x = 1;
00062 int coltmp_y = 2;
00063 int prec = 0;
00064 char label[128];
00065 bool use_stdin = false;
00066
00067 Plot2D_OptionFile opt;
00068 std::string option_filepath;
00069 std::string datapath;
00070 std::string color;
00071 std::vector<std::string> args;
00072 std::vector<std::string> args_numeric;
00073 std::string item;
00074 Plot2D ThePlot;
00075
00076 try
00077 {
00078 if( argc == 1 )
00079 {
00080 printf( "\nUSAGE: Plot2D data.txt // plot 1st vs 2nd column of data.txt\n" );
00081 printf( "USAGE: Plot2D data.txt N M --<opt> <arg> // plot N'th vs M'th column of data.txt\n" );
00082 printf( "USAGE: Plot2D -stdin N M --<opt> <arg> // plot N'th vs M'th col from stdin\n" );
00083 printf( "USAGE: Plot2D -h // display help for all options\n" );
00084 printf( "USAGE: Plot2D -c <option file name> // create a default options file\n" );
00085 printf( "USAGE: Plot2D -f <option file name> // create plot based on this file\n" );
00086 return 0;
00087 }
00088
00089
00090 for( i = 1; i < argc; i++ )
00091 args.push_back( argv[i] );
00092
00093
00094 for( i = 0; i < (int)args.size(); i++ )
00095 {
00096 item = args[i];
00097 StdStringUtils::MakeLower( item );
00098
00099 if( item.compare( "-h" ) == 0 || item.compare( "--help" ) == 0 || item.compare( "help" ) == 0 )
00100 {
00101
00102 PLOT2D_DisplayHelp();
00103 return 0;
00104 }
00105 }
00106
00107
00108 for( i = 0; i < (int)args.size(); i++ )
00109 {
00110 if( args[i].compare( "-c" ) == 0 || args[i].compare( "--create" ) == 0 )
00111 {
00112 if( i+1 >= (int)args.size() )
00113 {
00114 printf( "Invalid options, -c\n" );
00115 return -1;
00116 }
00117
00118 std::string filepath = args[i+1];
00119 if( !PLOT2D_GenerateDefaultOptionFile( filepath ) )
00120 {
00121 printf( "Unable to create the file specified\n" );
00122 }
00123 return 0;
00124 }
00125 }
00126
00127
00128 for( i = 0; i < (int)args.size(); i++ )
00129 {
00130 if( args[i].compare( "-f" ) == 0 || args[i].compare( "--optfile" ) == 0 )
00131 {
00132 if( i+1 >= (int)args.size() )
00133 {
00134 printf( "Invalid arguments, -f\n" );
00135 return -1;
00136 }
00137 else
00138 {
00139 option_filepath = args[i+1];
00140 if( !opt.ReadAndInterpretOptions(option_filepath) )
00141 {
00142 printf( "Problem with the option file specified.\n" );
00143 return -1;
00144 }
00145 }
00146 break;
00147 }
00148 }
00149
00150
00151
00152 for( i = 0; i < (int)args.size(); i++ )
00153 {
00154 if( args[i].compare( "--stdin" ) == 0 || args[i].compare( "-i" ) == 0 )
00155 {
00156 use_stdin = true;
00157 args.erase(args.begin()+i,args.begin()+i+1);
00158 break;
00159 }
00160 }
00161
00162 if( option_filepath.empty() )
00163 {
00164
00165 if( !use_stdin )
00166 {
00167 datapath = args[0];
00168 if( datapath.length() < 1 )
00169 {
00170 printf( "Invalid arguments\n");
00171 return -1;
00172 }
00173
00174
00175 {
00176 FILE* fid = NULL;
00177 fid = fopen( datapath.c_str(), "r" );
00178 if( fid == NULL )
00179 {
00180 printf( "Invalid arguments - input file can not be opened.\n");
00181 return -1;
00182 }
00183 fclose(fid);
00184 }
00185 opt.m_Series[0].DataPath = datapath;
00186 opt.m_Title = datapath;
00187
00188
00189 args.erase(args.begin(),args.begin()+1);
00190 }
00191 else
00192 {
00193 datapath = "stdin";
00194 opt.m_Series[0].DataPath = "stdin";
00195 opt.m_Title = "standard input";
00196 }
00197
00198
00199 i = 0;
00200 while( i < (int)args.size() )
00201 {
00202 item = args[i];
00203
00204 if( item.length() > 2 )
00205 StdStringUtils::MakeLower( item );
00206
00207 if( item.compare( "--out" ) == 0 || item.compare( "-o" ) == 0 )
00208 {
00209 if( i+1 >= (int)args.size() )
00210 {
00211 printf( "Invalid arguments, %s.\n", item.c_str() );
00212 return -1;
00213 }
00214 opt.m_OutputFileName = args[i+1];
00215
00216
00217 args.erase(args.begin()+i,args.begin()+i+1);
00218 args.erase(args.begin()+i,args.begin()+i+1);
00219
00220
00221 i = 0;
00222 }
00223 else if( item.compare( "--ymin" ) == 0 )
00224 {
00225 if( i+1 >= (int)args.size() )
00226 {
00227 printf( "Invalid arguments, %s.\n", item.c_str() );
00228 return -1;
00229 }
00230
00231 if( !StdStringUtils::GetDouble( args[i+1], opt.m_yAxis.lower_limit.val ) )
00232 {
00233 printf( "Invalid arguments, %s.\n", item.c_str() );
00234 return -1;
00235 }
00236 opt.m_yAxis.lower_limit.useDefault = false;
00237
00238
00239 args.erase(args.begin()+i,args.begin()+i+1);
00240 args.erase(args.begin()+i,args.begin()+i+1);
00241
00242
00243 i = 0;
00244 }
00245 else if( item.compare( "--ymax" ) == 0 )
00246 {
00247 if( i+1 >= (int)args.size() )
00248 {
00249 printf( "Invalid arguments, %s.\n", item.c_str() );
00250 return -1;
00251 }
00252
00253 if( !StdStringUtils::GetDouble( args[i+1], opt.m_yAxis.upper_limit.val ) )
00254 {
00255 printf( "Invalid arguments, %s.\n", item.c_str() );
00256 return -1;
00257 }
00258 opt.m_yAxis.upper_limit.useDefault = false;
00259
00260
00261 args.erase(args.begin()+i,args.begin()+i+1);
00262 args.erase(args.begin()+i,args.begin()+i+1);
00263
00264
00265 i = 0;
00266 }
00267 else if( item.compare( "--xmin" ) == 0 )
00268 {
00269 if( i+1 >= (int)args.size() )
00270 {
00271 printf( "Invalid arguments, %s.\n", item.c_str() );
00272 return -1;
00273 }
00274 if( !StdStringUtils::GetDouble( args[i+1], opt.m_xAxis.lower_limit.val ) )
00275 {
00276 printf( "Invalid arguments, %s.\n", item.c_str() );
00277 return -1;
00278 }
00279 opt.m_xAxis.lower_limit.useDefault = false;
00280
00281
00282 args.erase(args.begin()+i,args.begin()+i+1);
00283 args.erase(args.begin()+i,args.begin()+i+1);
00284
00285
00286 i = 0;
00287 }
00288 else if( item.compare( "--xmax" ) == 0 )
00289 {
00290 if( i+1 >= (int)args.size() )
00291 {
00292 printf( "Invalid arguments, %s.\n", item.c_str() );
00293 return -1;
00294 }
00295 if( !StdStringUtils::GetDouble( args[i+1], opt.m_xAxis.upper_limit.val ) )
00296 {
00297 printf( "Invalid arguments, %s.\n", item.c_str() );
00298 return -1;
00299 }
00300 opt.m_xAxis.upper_limit.useDefault = false;
00301
00302
00303 args.erase(args.begin()+i,args.begin()+i+1);
00304 args.erase(args.begin()+i,args.begin()+i+1);
00305
00306
00307 i = 0;
00308 }
00309 else if( item.compare( "--xtick" ) == 0 || item.compare( "-X" ) == 0 )
00310 {
00311 if( i+3 >= (int)args.size() )
00312 {
00313 printf( "Invalid arguments, %s.\n", item.c_str() );
00314 return -1;
00315 }
00316 if( !StdStringUtils::GetDouble( args[i+1], opt.m_xAxis.tick_start.val ) )
00317 {
00318 printf( "Invalid arguments, %s.\n", item.c_str() );
00319 return -1;
00320 }
00321 opt.m_xAxis.tick_start.useDefault = false;
00322 if( !StdStringUtils::GetDouble( args[i+2], opt.m_xAxis.tick_size.val ) )
00323 {
00324 printf( "Invalid arguments, %s.\n", item.c_str() );
00325 return -1;
00326 }
00327 opt.m_xAxis.tick_size.useDefault = false;
00328 if( !StdStringUtils::GetDouble( args[i+3], opt.m_xAxis.tick_end.val ) )
00329 {
00330 printf( "Invalid arguments, %s.\n", item.c_str() );
00331 return -1;
00332 }
00333 opt.m_xAxis.tick_end.useDefault = false;
00334
00335 if( opt.m_xAxis.tick_start.val >= opt.m_xAxis.tick_end.val )
00336 {
00337 printf( "Invalid arguments, %s.\n", item.c_str() );
00338 return -1;
00339 }
00340 if( opt.m_xAxis.tick_size.val <= 0 )
00341 {
00342 printf( "Invalid arguments, %s.\n", item.c_str() );
00343 return -1;
00344 }
00345
00346
00347 args.erase(args.begin()+i,args.begin()+i+1);
00348 args.erase(args.begin()+i,args.begin()+i+1);
00349 args.erase(args.begin()+i,args.begin()+i+1);
00350 args.erase(args.begin()+i,args.begin()+i+1);
00351
00352
00353 i = 0;
00354 }
00355 else if( item.compare( "--xlim" ) == 0 || item.compare( "-x" ) == 0 )
00356 {
00357 if( i+2 >= (int)args.size() )
00358 {
00359 printf( "Invalid arguments, %s.\n", item.c_str() );
00360 return -1;
00361 }
00362 if( !StdStringUtils::GetDouble( args[i+1], opt.m_xAxis.lower_limit.val ) )
00363 {
00364 printf( "Invalid arguments, %s.\n", item.c_str() );
00365 return -1;
00366 }
00367 opt.m_xAxis.lower_limit.useDefault = false;
00368 if( !StdStringUtils::GetDouble( args[i+2], opt.m_xAxis.upper_limit.val ) )
00369 {
00370 printf( "Invalid arguments, %s.\n", item.c_str() );
00371 return -1;
00372 }
00373 opt.m_xAxis.upper_limit.useDefault = false;
00374
00375 if( opt.m_xAxis.lower_limit.val >= opt.m_xAxis.upper_limit.val )
00376 {
00377 printf( "Invalid arguments, %s.\n", item.c_str() );
00378 return -1;
00379 }
00380
00381
00382 args.erase(args.begin()+i,args.begin()+i+1);
00383 args.erase(args.begin()+i,args.begin()+i+1);
00384 args.erase(args.begin()+i,args.begin()+i+1);
00385
00386
00387 i = 0;
00388 }
00389 else if( item.compare( "--ytick" ) == 0 || item.compare( "-Y" ) == 0 )
00390 {
00391 if( i+3 >= (int)args.size() )
00392 {
00393 printf( "Invalid arguments, %s.\n", item.c_str() );
00394 return -1;
00395 }
00396 if( !StdStringUtils::GetDouble( args[i+1], opt.m_yAxis.tick_start.val ) )
00397 {
00398 printf( "Invalid arguments, %s.\n", item.c_str() );
00399 return -1;
00400 }
00401 opt.m_yAxis.tick_start.useDefault = false;
00402 if( !StdStringUtils::GetDouble( args[i+2], opt.m_yAxis.tick_size.val ) )
00403 {
00404 printf( "Invalid arguments, %s.\n", item.c_str() );
00405 return -1;
00406 }
00407 opt.m_yAxis.tick_size.useDefault = false;
00408 if( !StdStringUtils::GetDouble( args[i+3], opt.m_yAxis.tick_end.val ) )
00409 {
00410 printf( "Invalid arguments, %s.\n", item.c_str() );
00411 return -1;
00412 }
00413 opt.m_yAxis.tick_end.useDefault = false;
00414
00415 if( opt.m_yAxis.tick_start.val >= opt.m_yAxis.tick_end.val )
00416 {
00417 printf( "Invalid arguments, %s.\n", item.c_str() );
00418 return -1;
00419 }
00420 if( opt.m_yAxis.tick_size.val <= 0 )
00421 {
00422 printf( "Invalid arguments, %s.\n", item.c_str() );
00423 printf( "Invalid arguments, --ytick.\n" );
00424 return -1;
00425 }
00426
00427 args.erase(args.begin()+i,args.begin()+i+1);
00428 args.erase(args.begin()+i,args.begin()+i+1);
00429 args.erase(args.begin()+i,args.begin()+i+1);
00430 args.erase(args.begin()+i,args.begin()+i+1);
00431
00432
00433 i = 0;
00434 }
00435 else if( item.compare( "--ylim" ) == 0 || item.compare( "-y" ) == 0 )
00436 {
00437 if( i+2 >= (int)args.size() )
00438 {
00439 printf( "Invalid arguments, %s.\n", item.c_str() );
00440 return -1;
00441 }
00442 if( !StdStringUtils::GetDouble( args[i+1], opt.m_yAxis.lower_limit.val ) )
00443 {
00444 printf( "Invalid arguments, %s.\n", item.c_str() );
00445 return -1;
00446 }
00447 opt.m_yAxis.lower_limit.useDefault = false;
00448 if( !StdStringUtils::GetDouble( args[i+2], opt.m_yAxis.upper_limit.val ) )
00449 {
00450 printf( "Invalid arguments, %s.\n", item.c_str() );
00451 return -1;
00452 }
00453 opt.m_yAxis.upper_limit.useDefault = false;
00454
00455 if( opt.m_yAxis.lower_limit.val >= opt.m_yAxis.upper_limit.val )
00456 {
00457 printf( "Invalid arguments, %s.\n", item.c_str() );
00458 return -1;
00459 }
00460
00461
00462 args.erase(args.begin()+i,args.begin()+i+1);
00463 args.erase(args.begin()+i,args.begin()+i+1);
00464 args.erase(args.begin()+i,args.begin()+i+1);
00465
00466
00467 i = 0;
00468 }
00469 else if( item.compare( "--xlabel" ) == 0 || item.compare( "-m" ) == 0 )
00470 {
00471 if( i+1 >= (int)args.size() )
00472 {
00473 printf( "Invalid arguments, %s.\n", item.c_str() );
00474 return -1;
00475 }
00476
00477 opt.m_xAxis.label = args[i+1];
00478 args.erase(args.begin()+i,args.begin()+i+1);
00479 args.erase(args.begin()+i,args.begin()+i+1);
00480 }
00481 else if( item.compare( "--xgrid" ) == 0 || item.compare( "-g" ) == 0 )
00482 {
00483 if( i+1 >= (int)args.size() )
00484 {
00485 printf( "Invalid arguments, %s.\n", item.c_str() );
00486 return -1;
00487 }
00488
00489 if( !StdStringUtils::GetInt( args[i+1], j ) )
00490 {
00491 printf( "Invalid arguments, %s.\n", item.c_str() );
00492 return -1;
00493 }
00494 if( j == 0 )
00495 {
00496 opt.m_xAxis.isGridOn = false;
00497 }
00498 else
00499 {
00500 opt.m_xAxis.isGridOn = true;
00501 }
00502 args.erase(args.begin()+i,args.begin()+i+1);
00503 args.erase(args.begin()+i,args.begin()+i+1);
00504 }
00505 else if( item.compare( "--ylabel" ) == 0 || item.compare( "-r" ) == 0 )
00506 {
00507 if( i+1 >= (int)args.size() )
00508 {
00509 printf( "Invalid arguments, %s.\n", item.c_str() );
00510 return -1;
00511 }
00512
00513 opt.m_yAxis.label = args[i+1];
00514 args.erase(args.begin()+i,args.begin()+i+1);
00515 args.erase(args.begin()+i,args.begin()+i+1);
00516 }
00517 else if( item.compare( "--ygrid" ) == 0 || item.compare( "-v" ) == 0 )
00518 {
00519 if( i+1 >= (int)args.size() )
00520 {
00521 printf( "Invalid arguments, %s.\n", item.c_str() );
00522 return -1;
00523 }
00524
00525 if( !StdStringUtils::GetInt( args[i+1], j ) )
00526 {
00527 printf( "Invalid arguments, %s.\n", item.c_str() );
00528 return -1;
00529 }
00530 if( j == 0 )
00531 {
00532 opt.m_yAxis.isGridOn = false;
00533 }
00534 else
00535 {
00536 opt.m_yAxis.isGridOn = true;
00537 }
00538 args.erase(args.begin()+i,args.begin()+i+1);
00539 args.erase(args.begin()+i,args.begin()+i+1);
00540 }
00541 else if( item.compare( "--title" ) == 0 || item.compare( "-T" ) == 0 )
00542 {
00543 if( i+1 >= (int)args.size() )
00544 {
00545 printf( "Invalid arguments, %s.\n", item.c_str() );
00546 return -1;
00547 }
00548
00549 opt.m_Title = args[i+1];
00550 args.erase(args.begin()+i,args.begin()+i+1);
00551 args.erase(args.begin()+i,args.begin()+i+1);
00552 }
00553 else if( item.compare( "--slabel" ) == 0 || item.compare( "-L" ) == 0 )
00554 {
00555 if( i+2 >= (int)args.size() )
00556 {
00557 printf( "Invalid arguments, %s.\n", item.c_str() );
00558 return -1;
00559 }
00560
00561 if( !StdStringUtils::GetInt( args[i+1], j ) )
00562 {
00563 printf( "Invalid arguments, %s.\n", item.c_str() );
00564 return -1;
00565 }
00566 if( j <= 0 || j > 12 )
00567 {
00568 printf( "Invalid arguments, %s.\n", item.c_str() );
00569 return -1;
00570 }
00571 opt.m_Series[j-1].Label = args[i+2];
00572
00573 args.erase(args.begin()+i,args.begin()+i+1);
00574 args.erase(args.begin()+i,args.begin()+i+1);
00575 args.erase(args.begin()+i,args.begin()+i+1);
00576 }
00577 else if( item.compare( "--sprecision" ) == 0 || item.compare( "-P" ) == 0 )
00578 {
00579 if( i+2 >= (int)args.size() )
00580 {
00581 printf( "Invalid arguments, %s.\n", item.c_str() );
00582 return -1;
00583 }
00584
00585 if( !StdStringUtils::GetInt( args[i+1], j ) )
00586 {
00587 printf( "Invalid arguments, %s.\n", item.c_str() );
00588 return -1;
00589 }
00590 if( j <= 0 || j > 12 )
00591 {
00592 printf( "Invalid arguments, %s.\n", item.c_str() );
00593 return -1;
00594 }
00595 if( !StdStringUtils::GetInt( args[i+2], prec ) )
00596 {
00597 printf( "Invalid arguments, %s.\n", item.c_str() );
00598 return -1;
00599 }
00600 if( prec <= 0 )
00601 {
00602 printf( "Invalid arguments, %s.\n", item.c_str() );
00603 printf( "Invalid arguments: negative precision value.\n" );
00604 return -1;
00605 }
00606 opt.m_Series[j-1].Precision = prec;
00607
00608 args.erase(args.begin()+i,args.begin()+i+1);
00609 args.erase(args.begin()+i,args.begin()+i+1);
00610 args.erase(args.begin()+i,args.begin()+i+1);
00611 }
00612 else if( item.compare( "--scolor" ) == 0 || item.compare( "-C" ) == 0 )
00613 {
00614 if( i+2 >= (int)args.size() )
00615 {
00616 printf( "Invalid arguments, %s.\n", item.c_str() );
00617 return -1;
00618 }
00619
00620 if( !StdStringUtils::GetInt( args[i+1], j ) )
00621 {
00622 printf( "Invalid arguments, %s.\n", item.c_str() );
00623 return -1;
00624 }
00625 if( j <= 0 || j > 12 )
00626 {
00627 printf( "Invalid arguments, %s.\n", item.c_str() );
00628 return -1;
00629 }
00630 color = args[i+2];
00631 StdStringUtils::MakeLower( color );
00632 if( !opt.DetermineColorFromString( opt.m_Series[j-1].Color, color ) )
00633 {
00634 printf( "Invalid arguments, %s.\n", item.c_str() );
00635 return -1;
00636 }
00637
00638 args.erase(args.begin()+i,args.begin()+i+1);
00639 args.erase(args.begin()+i,args.begin()+i+1);
00640 args.erase(args.begin()+i,args.begin()+i+1);
00641 }
00642 else if( item.compare( "--stats" ) == 0 || item.compare( "-s" ) == 0 )
00643 {
00644 if( i+1 >= (int)args.size() )
00645 {
00646 printf( "Invalid arguments, %s.\n", item.c_str() );
00647 return -1;
00648 }
00649
00650 if( !StdStringUtils::GetInt( args[i+1], j ) )
00651 {
00652 printf( "Invalid arguments, %s.\n", item.c_str() );
00653 return -1;
00654 }
00655 if( j == 0 )
00656 {
00657 opt.m_PlotStatistics = false;
00658 }
00659 else
00660 {
00661 opt.m_PlotStatistics = true;
00662 }
00663
00664 args.erase(args.begin()+i,args.begin()+i+1);
00665 args.erase(args.begin()+i,args.begin()+i+1);
00666 }
00667 else if( item.compare( "--gpslabel" ) == 0 || item.compare( "-G" ) == 0 )
00668 {
00669 if( i+2 >= (int)args.size() )
00670 {
00671 printf( "Invalid arguments, %s.\n", item.c_str() );
00672 printf( "Invalid arguments: <on=1/off=0> <utc_offset, always +ve>.\n" );
00673 return -1;
00674 }
00675
00676 if( !StdStringUtils::GetInt( args[i+1], j ) )
00677 {
00678 printf( "Invalid arguments, %s.\n", item.c_str() );
00679 printf( "Invalid arguments: <on=1/off=0> <utc_offset, always +ve>.\n" );
00680 return -1;
00681 }
00682 if( j == 0 )
00683 {
00684 opt.m_UseGPSLabel = false;
00685 }
00686 else
00687 {
00688 opt.m_UseGPSLabel = true;
00689 }
00690
00691 if( !StdStringUtils::GetInt( args[i+2], j ) )
00692 {
00693 printf( "Invalid arguments, %s.\n", item.c_str() );
00694 printf( "Invalid arguments: <on=1/off=0> <utc_offset, always +ve>.\n" );
00695 return -1;
00696 }
00697 if( j < 0 )
00698 {
00699 printf( "Invalid arguments, %s.\n", item.c_str() );
00700 printf( "Invalid arguments: <on=1/off=0> <utc_offset, always +ve>.\n" );
00701 return -1;
00702 }
00703 opt.m_UTCOffset = j;
00704
00705 args.erase(args.begin()+i,args.begin()+i+1);
00706 args.erase(args.begin()+i,args.begin()+i+1);
00707 args.erase(args.begin()+i,args.begin()+i+1);
00708 }
00709 else if( item.compare( "--bcolor" ) == 0 || item.compare( "-B" ) == 0 )
00710 {
00711 if( i+1 >= (int)args.size() )
00712 {
00713 printf( "Invalid arguments, %s.\n", item.c_str() );
00714 return -1;
00715 }
00716
00717 color = args[i+1];
00718 StdStringUtils::MakeLower( color );
00719 if( !opt.DetermineColorFromString( opt.m_FigureBackgroundColor, color ) )
00720 {
00721 printf( "Invalid arguments, %s.\n", item.c_str() );
00722 return -1;
00723 }
00724
00725 args.erase(args.begin()+i,args.begin()+i+1);
00726 args.erase(args.begin()+i,args.begin()+i+1);
00727 }
00728 else
00729 {
00730 i++;
00731 }
00732 }
00733
00734 if( args.size() > 0 )
00735 {
00736
00737 if( args.size()%2 != 0 )
00738 {
00739
00740 printf( "Invalid arguments, plot series columns must be paried.\ne.g. \"plot2d data.txt 1 2\"\n" );
00741 return -1;
00742 }
00743
00744 j = 0;
00745 for( i = 0; i < (int)args.size(); i+=2 )
00746 {
00747 coltmp_x = atoi(args[i].c_str());
00748 coltmp_y = atoi(args[i+1].c_str());
00749 if( coltmp_x < 0 )
00750 {
00751 opt.m_Series[j].negate_X = true;
00752 coltmp_x *= -1;
00753 }
00754 if( coltmp_y < 0 )
00755 {
00756 opt.m_Series[j].negate_Y = true;
00757 coltmp_y *= -1;
00758 }
00759 opt.m_Series[j].DataPath = datapath;
00760 opt.m_Series[j].X_Column = coltmp_x-1;
00761 opt.m_Series[j].Y_Column = coltmp_y-1;
00762
00763 if( opt.m_Series[j].negate_X )
00764 coltmp_x *= -1;
00765 if( opt.m_Series[j].negate_Y )
00766 coltmp_y *= -1;
00767
00768 if( opt.m_Series[j].Label.empty() )
00769 {
00770 sprintf( label, "Col %d vs %d", coltmp_x, coltmp_y );
00771 opt.m_Series[j].Label = label;
00772 }
00773 j++;
00774 printf( "plotting column %d vs column %d of %s\n", coltmp_x, coltmp_y, datapath.c_str() );
00775 }
00776 opt.m_NumberOfSeries = j;
00777 }
00778 else
00779 {
00780 coltmp_x = 1;
00781 coltmp_y = 2;
00782 printf( "plotting either:\n" );
00783 printf( "column %d vs column %d of %s\n", coltmp_x, coltmp_y, datapath.c_str() );
00784 printf( "or:\n" );
00785 printf( "column 1 vs its index of %s\n", datapath.c_str() );
00786 opt.m_NumberOfSeries = 1;
00787 }
00788 }
00789
00790 if( !opt.m_Title.empty() )
00791 {
00792 ThePlot.SetTitle( opt.m_Title );
00793 }
00794
00795 if( !ThePlot.SetNrSeries( opt.m_NumberOfSeries ) )
00796 {
00797 printf( "Error - Check Options: NumberOfSeries.\n" );
00798 return -1;
00799 }
00800
00801 if( !ThePlot.SetPlotSize( opt.m_PlotSize_Width, opt.m_PlotSize_Height ) )
00802 {
00803 printf( "Error - Check Options: PlotSize_Width, PlotSize_Height.\n" );
00804 return -1;
00805 }
00806
00807 ThePlot.Set_X_Label( opt.m_xAxis.label );
00808 ThePlot.Set_X_Axis_isGridOn( opt.m_xAxis.isGridOn );
00809
00810 if( !opt.m_xAxis.lower_limit.useDefault )
00811 {
00812 if( !ThePlot.Set_X_Axis_LowerLimit( opt.m_xAxis.lower_limit.val ) )
00813 {
00814 printf( "Error - Check Options: X_LowerLimit.\n" );
00815 return -1;
00816 }
00817 }
00818 if( !opt.m_xAxis.upper_limit.useDefault )
00819 {
00820 if( !ThePlot.Set_X_Axis_UpperLimit( opt.m_xAxis.upper_limit.val ) )
00821 {
00822 printf( "Error - Check Options: X_UpperLimit.\n" );
00823 return -1;
00824 }
00825 }
00826 if( !opt.m_xAxis.tick_start.useDefault )
00827 {
00828 if( !ThePlot.Set_X_Axis_TickStart( opt.m_xAxis.tick_start.val ) )
00829 {
00830 printf( "Error - Check Options: X_TickStart\n" );
00831 return -1;
00832 }
00833 }
00834 if( !opt.m_xAxis.tick_size.useDefault )
00835 {
00836 if( !ThePlot.Set_X_Axis_TickSize( opt.m_xAxis.tick_size.val ) )
00837 {
00838 printf( "Error - Check Options: X_TickSize.\n" );
00839 return -1;
00840 }
00841 }
00842 if( !opt.m_xAxis.tick_end.useDefault )
00843 {
00844 if( !ThePlot.Set_X_Axis_TickEnd( opt.m_xAxis.tick_end.val ) )
00845 {
00846 printf( "Error - Check Options: X_TickEnd.\n" );
00847 return -1;
00848 }
00849 }
00850
00851
00852
00853 ThePlot.Set_Y_Label( opt.m_yAxis.label );
00854 ThePlot.Set_Y_Axis_isGridOn( opt.m_yAxis.isGridOn );
00855
00856
00857 if( !opt.m_yAxis.lower_limit.useDefault )
00858 {
00859 if( !ThePlot.Set_Y_Axis_LowerLimit( opt.m_yAxis.lower_limit.val ) )
00860 {
00861 printf( "Error - Check Options: Y_LowerLimit.\n" );
00862 return -1;
00863 }
00864 }
00865 if( !opt.m_yAxis.upper_limit.useDefault )
00866 {
00867 if( !ThePlot.Set_Y_Axis_UpperLimit( opt.m_yAxis.upper_limit.val ) )
00868 {
00869 printf( "Error - Check Options: Y_UpperLimit.\n" );
00870 return -1;
00871 }
00872 }
00873 if( !opt.m_yAxis.tick_start.useDefault )
00874 {
00875 if( !ThePlot.Set_Y_Axis_TickStart( opt.m_yAxis.tick_start.val ) )
00876 {
00877 printf( "Error - Check Options: Y_TickStart\n" );
00878 return -1;
00879 }
00880 }
00881 if( !opt.m_yAxis.tick_size.useDefault )
00882 {
00883 if( !ThePlot.Set_Y_Axis_TickSize( opt.m_yAxis.tick_size.val ) )
00884 {
00885 printf( "Error - Check Options: Y_TickSize.\n" );
00886 return -1;
00887 }
00888 }
00889 if( !opt.m_yAxis.tick_end.useDefault )
00890 {
00891 if( !ThePlot.Set_Y_Axis_TickEnd( opt.m_yAxis.tick_end.val ) )
00892 {
00893 printf( "Error - Check Options: Y_TickEnd.\n" );
00894 return -1;
00895 }
00896 }
00897
00898 if( !opt.m_rightY_label.empty() )
00899 {
00900 if( !ThePlot.SetRightYLabel( opt.m_rightY_label, opt.m_rightY_scale_factor, opt.m_rightY_offset, CPLOT_BLACK ) )
00901 {
00902 printf( "Error - Check Options: Y_RightYLabel, Y_RightYScaleFactor, Y_RightYOffset\n" );
00903 return -1;
00904 }
00905 }
00906
00907 if( opt.m_UseGPSLabel )
00908 {
00909 if( !ThePlot.SetGPSLabel( opt.m_UTCOffset ) )
00910 {
00911 printf( "Error - Check Options: X_UseGPSLabel.\n" );
00912 return -1;
00913 }
00914 }
00915
00916 if( opt.m_PlotStatistics )
00917 {
00918 ThePlot.EnablePlotStatistics( true );
00919 }
00920 else
00921 {
00922 ThePlot.EnablePlotStatistics( false );
00923 }
00924
00925 if( opt.m_PlaceLabelBelowThePlot )
00926 {
00927 ThePlot.SetTheSeriesLabelsBelowThePlot( true );
00928 }
00929 else
00930 {
00931 ThePlot.SetTheSeriesLabelsBelowThePlot( false );
00932 }
00933
00934 ThePlot.SetBackgroundColor( opt.m_FigureBackgroundColor );
00935
00936
00937 for( i = 0; i < opt.m_NumberOfSeries; i++ )
00938 {
00939 if( !ThePlot.SetSeriesInfo( i, opt.m_Series[i] ) )
00940 {
00941 printf( "Problem in attempting to obtain series %d.\n", i+1 );
00942 return -1;
00943 }
00944 }
00945 if( !ThePlot.PlotAll() )
00946 {
00947 printf( "Problem with at least on plot series.\n" );
00948 return -1;
00949 }
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965 if( !ThePlot.SavePlotToBitmapFile( opt.m_OutputFileName ) )
00966 {
00967 printf( "Unable to save to %s", opt.m_OutputFileName.c_str() );
00968 return -1;
00969 }
00970 printf( "%s generated.\n", opt.m_OutputFileName.c_str() );
00971
00972 }
00973 catch ( ... )
00974 {
00975 printf( "\nCaught unknown exception\n" );
00976 }
00977
00978 return 0;
00979 }
00980
00981
00982 void PLOT2D_DisplayHelp()
00983 {
00984 printf( "\nPLOT2D HELP\n" );
00985 printf( "Since 2007-12-19\n" );
00986 printf( "Author Glenn D. MacGougan (GDM)\n" );
00987 printf( "Copyright 2008, all rights reserved\n" );
00988 printf( "License: 'BSD license'\n" );
00989 printf( "THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS\n\
00990 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED \n\
00991 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n\
00992 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\n\
00993 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n\
00994 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n\
00995 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n\
00996 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n\
00997 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n\
00998 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n\
00999 SUCH DAMAGE.\n\n" );
01000
01001 printf( "USAGE: Plot2D <ascii data.txt>\n" );
01002 printf( " plot 1st vs 2nd column of data.txt\n\n" );
01003
01004 printf( "USAGE: Plot2D <ascii data.txt> N M\n" );
01005 printf( " plot N'th vs M'th column of data.txt\n\n" );
01006
01007 printf( "USAGE: Plot2D data.txt --option <args> A B C D ...\n" );
01008 printf( " plot A'th vs B'th and C'th vs D'th columsn of data.txt with options.\n\n" );
01009
01010 printf( "USAGE: Plot2D --stdin --option <args> A B C D ...\n" );
01011 printf( " plot A'th vs B'th and C'th vs D'th columsn from standard input data with options.\n\n" );
01012
01013 printf( "USAGE: Plot2D -f <option file name>\n" );
01014 printf( "-f specifies that the name of the ASCII option file\n" );
01015 printf( " is to follow.\n" );
01016
01017 printf( "USAGE: Plot2D -c <option file name>\n" );
01018 printf( "-c specifies that a default option file is to be created with\n" );
01019 printf( " the name specified.\n\n" );
01020
01021 printf( "ALL OPTIONS:\n" );
01022 printf( "-short version, --long version, <parameter(s)>, description\n");
01023 printf( "-f --optfile <option file to use>, all other options ignored\n" );
01024 printf( "-c --create <filename>, create opt file, all other options ignored\n" );
01025 printf( "-i --stdin <N> <M>, plot N vs M columns from the standard input\n" );
01026 printf( " (N & M are optional 1 vs 2 or 1 vs index are default)\n" );
01027 printf( " reads data line by line columnwise,\n" );
01028 printf( " empty line or end of file will stop input\n" );
01029 printf( "-o --out <the output bitmap file path>\n" );
01030 printf( "-T --title \"the title in quotes\"\n" );
01031 printf( "-x --xlim <lower_limit> <upper_limit>\n" );
01032 printf( " --xmin <lower_limit>\n" );
01033 printf( " --xmax <upper_limit>\n" );
01034 printf( "-X --xtick <tick_start> <tick_size> <tick_end>\n" );
01035 printf( "-m --xlabel \"the label in quotes\"\n" );
01036 printf( "-g --xgrid <0 is off, 1 is on>\n" );
01037 printf( "-y --ylim <lower_limit> <upper_limit>\n" );
01038 printf( " --ymin <lower_limit>\n" );
01039 printf( " --ymax <upper_limit>\n" );
01040 printf( "-Y --ytick <tick_start> <tick_size> <tick_end>\n" );
01041 printf( "-r --ylabel \"the label in quotes\"\n" );
01042 printf( "-v --ygrid <0 is off, 1 is on>\n" );
01043 printf( "-s --stats <0 is off, 1 is on>\n" );
01044 printf( "-G --gpslabel <0 is off, 1 is on> <utc_offset_always_positive>\n" );
01045 printf( "-B --bcolor <color> // the figure background color, default is white\n" );
01046 printf( "-L --slabel <series_index_1_to_N> \"the label in quotes\"\n" );
01047 printf( "-P --sprecision <series_index_1_to_N> precision (0 integer, 1 tenths, etc)\n" );
01048 printf( "-C --scolor <series_index_1_to_N> color\n" );
01049 printf( "\ncolor is one of: white, black, blue, green, purple, magenta, darkblue,\n" );
01050 printf( "indianred, babyblue, paislyblue, lightpurple, darkpurple, greypurple, brown\n" );
01051 printf( "red, pink, yellow, orange, cyan, limegreen, grey, lightgrey\n" );
01052 }
01053
01054
01055 bool PLOT2D_GenerateDefaultOptionFile( std::string filepath )
01056 {
01057 FILE *fid = NULL;
01058
01059 if( filepath.empty() )
01060 return false;
01061
01062 fid = fopen( filepath.c_str(), "w" );
01063 if( !fid )
01064 return FALSE;
01065
01066 fprintf( fid, "\
01067 ; PLOT2D OPTIONS FILE\n\
01068 ;______________________________________________________________________________\n\
01069 ; FORMAT\n\
01070 ; \n\
01071 ; OPTIONFIELD, COMMENT = VALUE ; POST_VALUE_COMMENT\n\
01072 ; OPTIONFIELD - a valid identifier, no spaces\n\
01073 ; COMMENT - a good place to put the units\n\
01074 ; VALUE - the option value, a number, a string, an \n\
01075 ; array, a boolean (yes/no, 1/0)\n\
01076 ; POST_VALUE_COMMENT - a comment following the value\n\
01077 ; \n\
01078 ; Anything after a ';' is a comment.\n\
01079 ;______________________________________________________________________________\n\
01080 ; PLOT2D COLOR CHOICES\n\
01081 ;______________________________________________________________________________\n\
01082 ; White; Black; Blue; Green; Purple; Magenta; DarkBlue; IndianRed; BabyBlue\n\
01083 ; PaislyBlue; LightPurple; DarkPurple; GreyPurple; Brown; Red; Pink; Yellow\n\
01084 ; Orange; Cyan; LimeGreen; Grey; LightGrey\n\
01085 ;______________________________________________________________________________\n\
01086 \n\
01087 \n\
01088 \n\
01089 ; GENERAL PLOT2D OPTIONS\n\
01090 ;______________________________________________________________________________\n\
01091 \n\
01092 OutputFileName, (A compressed bitmap file, e.g. test.bmp) = three_waves.bmp\n\
01093 NumberOfSeries, (number of active series as specified below) = 3 ; up to 12\n\
01094 PlotSize_Width, (cm) = 15 ; 15 is a good value\n\
01095 PlotSize_Height, (cm) = 13 ; 13 is a good value\n\
01096 Title, (a string, or empty if none) = Test Plot\n\
01097 \n\
01098 X_IsGridOn, (enable/disable horizontal gridlines) = yes\n\
01099 X_Label, (a string, or empty if none) = Time (s)\n\
01100 ; LowerLimit, UpperLimit, TickStart, TickSize, and Tickend\n\
01101 ; values can be determined automatically, by the data values\n\
01102 ; in the first series, by specifying 'default'.\n\
01103 X_LowerLimit, (lower plotting limit or 'default') = default\n\
01104 X_UpperLimit, (upper plotting limit or 'default') = default\n\
01105 X_TickStart, (start xtick label value or 'default') = default\n\
01106 X_TickSize, (xtick size value or 'default') = default\n\
01107 X_TickEnd, (end xtick label value or 'default') = default\n\
01108 \n\
01109 Y_IsGridOn, (enable/disable horizontal gridlines) = yes\n\
01110 Y_Label, (a string, or empty if none) = Voltage (V)\n\
01111 ; LowerLimit, UpperLimit, TickStart, TickSize, and Tickend\n\
01112 ; values can be determined automatically, by the data values\n\
01113 ; in the first series, by specifying 'default'.\n\
01114 Y_LowerLimit, (lower plotting limit or 'default') = default\n\
01115 Y_UpperLimit, (upper plotting limit or 'default') = default\n\
01116 Y_TickStart, (start xtick label value or 'default') = default\n\
01117 Y_TickSize, (xtick size value or 'default') = default\n\
01118 Y_TickEnd, (end xtick label value or 'default') = default\n\
01119 \n\
01120 ; ADVANCED PLOT2D OPTIONS\n\
01121 ;______________________________________________________________________________\n\
01122 ; Enabling PlotStatistics add a small table to the right of \n\
01123 ; plot including min, max, mean, rms, and standard deviation.\n\
01124 PlotStatistics, (include statistics) = yes ;yes/no\n\
01125 PlaceLabelBelowThePlot, (usually on the right) = no ;yes/no\n\
01126 FigureBackgroundColor, (see above) = White\n\
01127 \n\
01128 ; Enabling X_UseGPSLabel assumes that the X part of the \n\
01129 ; series is a GPS time of week, (0-604800 s), and will\n\
01130 ; compute the corresponding UTC time and include it \n\
01131 ; below each xtick.\n\
01132 X_UseGPSLabel, (include UTC time below GPS time of week) = no ;yes/no\n\
01133 X_GPSUTCOffset, (specify the GPS/UTC Offset, always +ve) = 14 ;ignored if no above \n\
01134 \n\
01135 ; Data can be plotted with respect to y ticks on the right \n\
01136 ; hand side of the plot window. \n\
01137 ; For example, plot elevation angle (0-90, left side y ticks) \n\
01138 ; and plot azimuth angle (-180-180, right side y ticks). \n\
01139 ;\n\
01140 ; rhs_ytick = lhs_ytick * scale_factor + offset\n\
01141 ; e.g.\n\
01142 ; Y_Label, = Elevation (deg)\n\
01143 ; Y_RightYLabel, = Azimuth (deg)\n\
01144 ; Y_RightYOffset, = -180\n\
01145 ; Y_RightYScaleFactor, = 4\n\
01146 Y_RightYLabel, (enable the right Y label if not empty) = \n\
01147 Y_RightYScaleFactor, (scale the lhs y ticks by) = 1\n\
01148 Y_RightYOffset, (offset the lhs y ticks after scaling) = 0\n\
01149 \n\
01150 \n\
01151 ; SERIES INFORMATION (Up to 12 Series)\n\
01152 ; Data for a series is contained within one file and must be column based.\n\
01153 ; Data must be an ASCII data file.\n\
01154 ;______________________________________________________________________________\n\
01155 \n\
01156 Series1_DataPath, (full/relative path to the data file) = \n\
01157 Series1_X_Column, (1 based index of the X data) = 1 ;must be>0\n\
01158 Series1_Y_Column, (1 based index of the Y data) = 2 ;must be>0\n\
01159 Series1_IsConnected, (connected with a line, yes/no) = yes ;yes/no\n\
01160 Series1_Color, (see above) = Blue\n\
01161 Series1_Label, (a string, or empty if none) = \n\
01162 Series1_Units, (a string, or empty if none) = \n\
01163 ; The following option applies if PlotStatistics is enabled.\n\
01164 Series1_Precision, (max nr of significant digits) = 3 \n\
01165 ; Data outside the plot window can be indicated (in Cyan) \n\
01166 ; along the plot borders by enabling the following option.\n\
01167 Series1_MarkOutlierData, (data out of plot range is marked) = yes ;yes/no\n\
01168 Series1_UsesRightYLabel, (pertains to the right ylabel) = no ;yes/no\n\
01169 \n\
01170 \n\
01171 Series2_DataPath, (full/relative path to the data file) = \n\
01172 Series2_X_Column, (1 based index of the X data) = 1 ;must be>0\n\
01173 Series2_Y_Column, (1 based index of the Y data) = 2 ;must be>0\n\
01174 Series2_IsConnected, (connected with a line, yes/no) = yes ;yes/no\n\
01175 Series2_Color, (see above) = LimeGreen\n\
01176 Series2_Label, (a string, or empty if none) = \n\
01177 Series2_Units, (a string, or empty if none) = \n\
01178 ; The following option applies if PlotStatistics is enabled.\n\
01179 Series2_Precision, (max nr of significant digits) = 3 \n\
01180 ; Data outside the plot window can be indicated (in Cyan) \n\
01181 ; along the plot borders by enabling the following option.\n\
01182 Series2_MarkOutlierData, (data out of plot range is marked) = yes ;yes/no\n\
01183 Series2_UsesRightYLabel, (pertains to the right ylabel) = no ;yes/no\n\
01184 \n\
01185 \n\
01186 Series3_DataPath, (full/relative path to the data file) = \n\
01187 Series3_X_Column, (1 based index of the X data) = 1 ;must be>0\n\
01188 Series3_Y_Column, (1 based index of the Y data) = 2 ;must be>0\n\
01189 Series3_IsConnected, (connected with a line, yes/no) = yes ;yes/no\n\
01190 Series3_Color, (see above) = Red\n\
01191 Series3_Label, (a string, or empty if none) = \n\
01192 Series3_Units, (a string, or empty if none) = \n\
01193 ; The following option applies if PlotStatistics is enabled.\n\
01194 Series3_Precision, (max nr of significant digits) = 3 \n\
01195 ; Data outside the plot window can be indicated (in Cyan) \n\
01196 ; along the plot borders by enabling the following option.\n\
01197 Series3_MarkOutlierData, (data out of plot range is marked) = yes ;yes/no\n\
01198 Series3_UsesRightYLabel, (pertains to the right ylabel) = no ;yes/no\n\
01199 \n\
01200 ; and so on ...\n" );
01201
01202 fclose(fid);
01203 return true;
01204 }
01205