Analysis/TimeSeries/timeseries.cpp

Go to the documentation of this file.
00001 #include <QtGui>
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include "timeseries.h"
00006 #include "plotTS.h"
00007 #include "./pihmLIBS/helpDialog/helpdialog.h"
00008 
00009 #include <qwt_plot_canvas.h>
00010 
00011 #define ELEMENT_FEATURE 0
00012 #define RIVER_FEATURE   1
00013 
00014 using namespace std;
00015 
00016 timeSeriesDlg::timeSeriesDlg()
00017 {
00018 
00019         setupUi(this);
00020 
00021         QValidator *validator = new QIntValidator(this);
00022         lineEditEleID->setValidator(validator);
00023         lineEditRivID->setValidator(validator);
00024         lineEditEleTime->setValidator(validator);
00025         lineEditRivTime->setValidator(validator);
00026 
00027         connect( pushButtonBrowse, SIGNAL( clicked() ), this, SLOT( browse() ) );
00028         connect( pushButtonPlot,   SIGNAL( clicked() ), this, SLOT( plot()   ) );
00029         connect( pushButtonHelp,   SIGNAL( clicked() ), this, SLOT( help()   ) );
00030         connect( pushButtonSavePlot, SIGNAL( clicked() ), this, SLOT( savePlot() ) );
00031 }
00032 
00033 void timeSeriesDlg::browse()
00034 {
00035         QString str = QFileDialog::getOpenFileName(this, "Choose Model Output File", "~/","Text File(*.txt *.TXT);; NetCDF File(*.nc *.NC)");
00036         lineEditFileName->setText(str);
00037 }
00038 PlotTS *plotts;
00039 void timeSeriesDlg::plot()
00040 {
00041         double *xVal, *yVal;
00042         int dataCount;
00043         char plotTitle[100], xTitle[100], yTitle[100], legend[100];
00044  
00045         int METHOD =  0;        
00046         int TIME_STEP;
00047         int NUM_STEPS = 0;
00048         int id = 0;
00049         int featureIndex;
00050         int tabIndex;
00051         int variableIndex;
00052         QString fileName;
00053         ifstream inStream;
00054 
00055         int NUM_ELE = 0;
00056         int NUM_RIV = 0;
00057 
00058         tabIndex = tabWidget->currentIndex();   
00059         if(tabIndex == ELEMENT_FEATURE)
00060         {
00061                 featureIndex = comboBoxEleFeature->currentIndex();
00062                 if(featureIndex == 0){
00063                         METHOD = 1;
00064                         QString idString = lineEditEleID->text();
00065                         id = idString.toInt();
00066                 }
00067                 if(featureIndex == 1){
00068                         METHOD = 2;
00069                 }
00070                 if(featureIndex == 2){
00071                         METHOD = 3;
00072                 }
00073 
00074                 TIME_STEP = ( lineEditEleTime->text() ).toInt();
00075                 variableIndex = comboBoxEleVariable->currentIndex();
00076                 fileName = lineEditFileName->text();
00077 
00078                 if( fileName.endsWith("txt", Qt::CaseInsensitive) ){
00079                         inStream.open(qPrintable(fileName));
00080                         if(inStream == NULL){
00081                                 printf("Couldn't open File\n");
00082                                 exit(1);
00083                         }
00084                         string str;
00085                         getline(inStream, str);
00086                         //QString tmpStr = new QString((char *)str);
00087                         //qWarning(qPrintable(str));
00088                         int pos = 0;
00089                         while( (pos = str.find('\t', pos+1)) != -1 ){
00090                                 NUM_ELE++;
00091                         }
00092                         cout<<NUM_ELE<<"\n";
00093                         inStream.seekg(0, ios::beg);
00094                         
00095                         while(getline(inStream, str, '\n'))
00096                                 NUM_STEPS++;
00097 
00098                         //inStream.seekg(0, ios::beg);                  
00099                         inStream.close();
00100                         inStream.open(qPrintable(fileName));
00101                         NUM_STEPS = NUM_STEPS / TIME_STEP + 1;
00102                         
00103                         cout<<"steps= "<<NUM_STEPS<<"\n";
00104                         
00105                         xVal = (double *)malloc((NUM_STEPS) * sizeof(double));
00106                         yVal = (double *)malloc((NUM_STEPS) * sizeof(double));
00107 
00108                         for(int i=0; i<NUM_STEPS; i++)
00109                                 yVal[i]=0.0;                    
00110                         dataCount=0;
00111                         cout<<"Method= "<<METHOD<<"\n";
00112                         int tmpCount=0;
00113                         if(METHOD == 1){
00114                                 double temp;
00115                                 while(inStream){
00116                                         tmpCount++;
00117                                         for(int i=0; i<NUM_ELE; i++){
00118                                                 inStream >> temp;
00119                                                 if(i+1 == id){
00120                                                         yVal[dataCount]+=temp/TIME_STEP;
00121                                                 }
00122                                         }
00123                                         //cout<<tmpCount<<"\t"<<TIME_STEP<<"\t"<<tmpCount%TIME_STEP<<"\n";
00124                                         if(tmpCount%TIME_STEP == 0){
00125                                                 xVal[dataCount]=dataCount;
00126                                                 dataCount++;
00127                                                 //cout<<"dataC= "<<dataCount<<"\n";
00128                                                 tmpCount=0;
00129                                         }
00130                                 }
00131                                 dataCount--;
00132                                 char tempStr[50];
00133                                 sprintf(tempStr, "EleID = %d", id);
00134                                 if(variableIndex == 0){
00135                                         strcpy(plotTitle, "Interception Storage");
00136                                         strcpy(xTitle, "Time");
00137                                         strcpy(yTitle, "Head (meters)");
00138                                         strcpy(legend, tempStr);
00139                                 }
00140                                 if(variableIndex == 1){
00141                                         strcpy(plotTitle, "Saturated Zone");
00142                                         strcpy(xTitle, "Time");
00143                                         strcpy(yTitle, "Head (meters)");
00144                                         strcpy(legend, tempStr);
00145                                 }
00146                                 if(variableIndex == 2){
00147                                         strcpy(plotTitle, "Unsaturated Zone");
00148                                         strcpy(xTitle, "Time");
00149                                         strcpy(yTitle, "Head (meters)");
00150                                         strcpy(legend, tempStr);
00151                                 }
00152                                 if(variableIndex == 3){
00153                                         strcpy(plotTitle, "Surface Water");
00154                                         strcpy(xTitle, "Time");
00155                                         strcpy(yTitle, "Head (meters)");
00156                                         strcpy(legend, tempStr);
00157                                 }
00158                                 if(variableIndex == 4){
00159                                         strcpy(plotTitle, "Evaporation from Canopy");
00160                                         strcpy(xTitle, "Time");
00161                                         strcpy(yTitle, "Rate (meters/day)");
00162                                         strcpy(legend, "Mean ET0");
00163                                 }
00164                                 if(variableIndex == 5){
00165                                         strcpy(plotTitle, "ET1");
00166                                         strcpy(xTitle, "Time");
00167                                         strcpy(yTitle, "Rate (meters/day)");
00168                                         strcpy(legend, tempStr);
00169                                 }
00170                                 if(variableIndex == 6){
00171                                         strcpy(plotTitle, "ET2");
00172                                         strcpy(xTitle, "Time");
00173                                         strcpy(yTitle, "Rate (meters/day)");
00174                                         strcpy(legend, tempStr);
00175                                 }
00176                                 if(variableIndex == 7){
00177                                         strcpy(plotTitle, "Precipitation");
00178                                         strcpy(xTitle, "Time");
00179                                         strcpy(yTitle, "Rate (meters/day)");
00180                                         strcpy(legend, tempStr);
00181                                 }
00182                                 if(variableIndex == 8){
00183                                         strcpy(plotTitle, "Net Precipitation");
00184                                         strcpy(xTitle, "Time");
00185                                         strcpy(yTitle, "Rate (meters/day)");
00186                                         strcpy(legend, tempStr);
00187                                 }
00188                                 if(variableIndex == 9){
00189                                         strcpy(plotTitle, "Infiltration");
00190                                         strcpy(xTitle, "Time");
00191                                         strcpy(yTitle, "Rate (meters/day)");
00192                                         strcpy(legend, tempStr);
00193                                 }
00194                                 if(variableIndex == 10){
00195                                         strcpy(plotTitle, "Groundwater Recharge");
00196                                         strcpy(xTitle, "Time");
00197                                         strcpy(yTitle, "Rate (meters/day)");
00198                                         strcpy(legend, tempStr);
00199                                 }
00200                         }
00201                         else if(METHOD == 2){
00202                                 double temp;
00203                                 while(inStream){
00204                                         tmpCount++;
00205                                         for(int i=0; i<NUM_ELE; i++){
00206                                                 inStream >> temp;
00207                                                 yVal[dataCount]+=temp/(TIME_STEP*NUM_ELE);
00208                                         }
00209                                         if(tmpCount%TIME_STEP ==0){
00210                                                 xVal[dataCount] = dataCount;
00211                                                 dataCount++;
00212                                                 tmpCount=0;
00213                                         }
00214                                 }
00215                                 dataCount--;
00216                                 if(variableIndex == 0){
00217                                         strcpy(plotTitle, "Interception Storage");
00218                                         strcpy(xTitle, "Time");
00219                                         strcpy(yTitle, "Head (meters)");
00220                                         strcpy(legend, "Mean Interception");
00221                                 }
00222                                 if(variableIndex == 1){
00223                                         strcpy(plotTitle, "Saturated Zone");
00224                                         strcpy(xTitle, "Time");
00225                                         strcpy(yTitle, "Head (meters)");
00226                                         strcpy(legend, "Mean");
00227                                 }
00228                                 if(variableIndex == 2){
00229                                         strcpy(plotTitle, "Unsaturated Zone");
00230                                         strcpy(xTitle, "Time");
00231                                         strcpy(yTitle, "Head (meters)");
00232                                         strcpy(legend, "Mean");
00233                                 }
00234                                 if(variableIndex == 3){
00235                                         strcpy(plotTitle, "Surface Water");
00236                                         strcpy(xTitle, "Time");
00237                                         strcpy(yTitle, "Head (meters)");
00238                                         strcpy(legend, "Mean");
00239                                 }
00240                                 if(variableIndex == 4){
00241                                         strcpy(plotTitle, "Evaporation from Canopy");
00242                                         strcpy(xTitle, "Time");
00243                                         strcpy(yTitle, "Rate (meters/day)");
00244                                         strcpy(legend, "Mean ET0");
00245                                 }
00246                                 if(variableIndex == 5){
00247                                         strcpy(plotTitle, "ET1");
00248                                         strcpy(xTitle, "Time");
00249                                         strcpy(yTitle, "Rate (meters/day)");
00250                                         strcpy(legend, "Mean ET1");
00251                                 }
00252                                 if(variableIndex == 6){
00253                                         strcpy(plotTitle, "ET2");
00254                                         strcpy(xTitle, "Time");
00255                                         strcpy(yTitle, "Rate (meters/day)");
00256                                         strcpy(legend, "Mean ET2");
00257                                 }
00258                                 if(variableIndex == 7){
00259                                         strcpy(plotTitle, "Precipitation");
00260                                         strcpy(xTitle, "Time");
00261                                         strcpy(yTitle, "Rate (meters/day)");
00262                                         strcpy(legend, "Mean Precip");
00263                                 }
00264                                 if(variableIndex == 8){
00265                                         strcpy(plotTitle, "Net Precipitation");
00266                                         strcpy(xTitle, "Time");
00267                                         strcpy(yTitle, "Rate (meters/day)");
00268                                         strcpy(legend, "Mean Net Precip");
00269                                 }
00270                                 if(variableIndex == 9){
00271                                         strcpy(plotTitle, "Infiltration");
00272                                         strcpy(xTitle, "Time");
00273                                         strcpy(yTitle, "Rate (meters/day)");
00274                                         strcpy(legend, "Mean Infil");
00275                                 }
00276                                 if(variableIndex == 10){
00277                                         strcpy(plotTitle, "Groundwater Recharge");
00278                                         strcpy(xTitle, "Time");
00279                                         strcpy(yTitle, "Rate (meters/day)");
00280                                         strcpy(legend, "Mean Recharge");
00281                                 }
00282                         }
00283                         else{
00284                         
00285                         }
00286                 }
00287                 else if( fileName.endsWith("nc", Qt::CaseInsensitive) ){
00288 
00289                 }
00290                 else{
00291                         qWarning("File Type NOT Recognised\n");
00292                 }
00293         }
00294         if(tabIndex == RIVER_FEATURE)
00295         {
00296                 /*
00297                 featureIndex = comboBoxRivFeature->currentIndex();
00298                 if(featureIndex == 0){
00299                         METHOD = 1;
00300                         QString idString = lineEditRivID->text();
00301                         id = idString.toInt();
00302                 }
00303                 if(featureIndex == 1){
00304                         METHOD = 2;
00305 
00306                 }
00307                 if(featureIndex == 2){
00308                         METHOD = 3;
00309 
00310                 }
00311                 */
00312                 featureIndex = comboBoxRivFeature->currentIndex();
00313                 if(featureIndex == 0){
00314                         METHOD = 1;
00315                         QString idString = lineEditRivID->text();
00316                         id = idString.toInt();
00317                 }
00318                 if(featureIndex == 1){
00319                         METHOD = 2;
00320                 }
00321                 if(featureIndex == 2){
00322                         METHOD = 3;
00323                 }
00324 
00325                 TIME_STEP = ( lineEditRivTime->text() ).toInt();
00326                 variableIndex = comboBoxRivVariable->currentIndex();
00327                 fileName = lineEditFileName->text();
00328 
00329                 if( fileName.endsWith("txt", Qt::CaseInsensitive) ){
00330                         inStream.open(qPrintable(fileName));
00331                         if(inStream == NULL){
00332                                 printf("Couldn't open File\n");
00333                                 exit(1);
00334                         }
00335                         string str;
00336                         getline(inStream, str);
00337                         //QString tmpStr = new QString((char *)str);
00338                         //qWarning(qPrintable(str));
00339                         int pos = 0;
00340                         while( (pos = str.find('\t', pos+1)) != -1 ){
00341                                 NUM_RIV++;
00342                         }
00343                         cout<<"NumRiv= "<<NUM_RIV<<"\n";
00344                         inStream.seekg(0, ios::beg);
00345                         
00346                         while(getline(inStream, str, '\n'))
00347                                 NUM_STEPS++;
00348 
00349                         //inStream.seekg(0, ios::beg);                  
00350                         inStream.close();
00351                         inStream.open(qPrintable(fileName));
00352                         NUM_STEPS = NUM_STEPS / TIME_STEP + 1;
00353                         
00354                         cout<<"steps= "<<NUM_STEPS<<"\n";
00355                         
00356                         xVal = (double *)malloc((NUM_STEPS) * sizeof(double));
00357                         yVal = (double *)malloc((NUM_STEPS) * sizeof(double));
00358 
00359                         for(int i=0; i<NUM_STEPS; i++)
00360                                 yVal[i]=0.0;                    
00361                         dataCount=0;
00362                         cout<<"Method= "<<METHOD<<"\n";
00363                         int tmpCount=0;
00364                         if(METHOD == 1){
00365                                 double temp;
00366                                 while(inStream){
00367                                         tmpCount++;
00368                                         for(int i=0; i<NUM_RIV; i++){
00369                                                 inStream >> temp;
00370                                                 if(i+1 == id){
00371                                                         yVal[dataCount]+=temp/TIME_STEP;
00372                                                 }
00373                                         }
00374                                         //cout<<tmpCount<<"\t"<<TIME_STEP<<"\t"<<tmpCount%TIME_STEP<<"\n";
00375                                         if(tmpCount%TIME_STEP == 0){
00376                                                 xVal[dataCount]=dataCount;
00377                                                 dataCount++;
00378                                                 //cout<<"dataC= "<<dataCount<<"\n";
00379                                                 tmpCount=0;
00380                                         }
00381                                 }
00382                                 dataCount--;
00383                                 char tempStr[50];
00384                                 sprintf(tempStr, "RivID = %d", id);
00385                                 if(variableIndex == 0){
00386                                         strcpy(plotTitle, "Water Table");
00387                                         strcpy(xTitle, "Time");
00388                                         strcpy(yTitle, "Head (meters)");
00389                                         strcpy(legend, tempStr);
00390                                 }
00391                                 if(variableIndex == 1){
00392                                         strcpy(plotTitle, "Stream Inflow");
00393                                         strcpy(xTitle, "Time");
00394                                         strcpy(yTitle, "Rate (meters^3/day)");
00395                                         strcpy(legend, tempStr);
00396                                 }
00397                                 if(variableIndex == 2){
00398                                         strcpy(plotTitle, "Stream Outflow");
00399                                         strcpy(xTitle, "Time");
00400                                         strcpy(yTitle, "Rate (meters^3/day)");
00401                                         strcpy(legend, tempStr);
00402                                 }
00403                                 if(variableIndex == 3){
00404                                         strcpy(plotTitle, "Stream Baseflow");
00405                                         strcpy(xTitle, "Time");
00406                                         strcpy(yTitle, "Rate (meters^3/day)");
00407                                         strcpy(legend, tempStr);
00408                                 }
00409                                 if(variableIndex == 4){
00410                                         strcpy(plotTitle, "Stream Surface flux");
00411                                         strcpy(xTitle, "Time");
00412                                         strcpy(yTitle, "Rate (meters^3/day)");
00413                                         strcpy(legend, tempStr);
00414                                 }
00415                                 if(variableIndex == 5){
00416                                         strcpy(plotTitle, "Stream Baseflow Left");
00417                                         strcpy(xTitle, "Time");
00418                                         strcpy(yTitle, "Rate (meters^3/day)");
00419                                         strcpy(legend, tempStr);
00420                                 }
00421                                 if(variableIndex == 6){
00422                                         strcpy(plotTitle, "Stream Baseflow Right");
00423                                         strcpy(xTitle, "Time");
00424                                         strcpy(yTitle, "Rate (meters^3/day)");
00425                                         strcpy(legend, tempStr);
00426                                 }
00427                                 if(variableIndex == 7){
00428                                         strcpy(plotTitle, "Stream Surface flux Left");
00429                                         strcpy(xTitle, "Time");
00430                                         strcpy(yTitle, "Rate (meters^3/day)");
00431                                         strcpy(legend, tempStr);
00432                                 }
00433                                 if(variableIndex == 8){
00434                                         strcpy(plotTitle, "Stream Surface flux Right");
00435                                         strcpy(xTitle, "Time");
00436                                         strcpy(yTitle, "Rate (meters^3/day)");
00437                                         strcpy(legend, tempStr);
00438                                 }
00439                         }
00440                         else if(METHOD == 2){
00441                                 double temp;
00442                                 while(inStream){
00443                                         tmpCount++;
00444                                         for(int i=0; i<NUM_RIV; i++){
00445                                                 inStream >> temp;
00446                                                 yVal[dataCount]+=temp/(TIME_STEP*NUM_RIV);
00447                                         }
00448                                         if(tmpCount%TIME_STEP ==0){
00449                                                 xVal[dataCount] = dataCount;
00450                                                 dataCount++;
00451                                                 tmpCount=0;
00452                                         }
00453                                 }
00454                                 dataCount--;
00455                                 if(variableIndex == 0){
00456                                         strcpy(plotTitle, "Water Table");
00457                                         strcpy(xTitle, "Time");
00458                                         strcpy(yTitle, "Head (meters)");
00459                                         strcpy(legend, "Mean Head");
00460                                 }
00461                                 if(variableIndex == 1){
00462                                         strcpy(plotTitle, "Stream Inflow");
00463                                         strcpy(xTitle, "Time");
00464                                         strcpy(yTitle, "Rate (meters^3/day)");
00465                                         strcpy(legend, "Mean Inflow");
00466                                 }
00467                                 if(variableIndex == 2){
00468                                         strcpy(plotTitle, "Stream Outflow");
00469                                         strcpy(xTitle, "Time");
00470                                         strcpy(yTitle, "Rate (meters^3/day)");
00471                                         strcpy(legend, "Mean Outflow");
00472                                 }
00473                                 if(variableIndex == 3){
00474                                         strcpy(plotTitle, "Stream Baseflow");
00475                                         strcpy(xTitle, "Time");
00476                                         strcpy(yTitle, "Rate (meters^3/day)");
00477                                         strcpy(legend, "Mean Baseflow");
00478                                 }
00479                                 if(variableIndex == 4){
00480                                         strcpy(plotTitle, "Stream Surface flux");
00481                                         strcpy(xTitle, "Time");
00482                                         strcpy(yTitle, "Rate (meters^3/day)");
00483                                         strcpy(legend, "Mean Surface flux");
00484                                 }
00485                                 if(variableIndex == 5){
00486                                         strcpy(plotTitle, "Stream Baseflow Left");
00487                                         strcpy(xTitle, "Time");
00488                                         strcpy(yTitle, "Rate (meters^3/day)");
00489                                         strcpy(legend, "Mean flow");
00490                                 }
00491                                 if(variableIndex == 6){
00492                                         strcpy(plotTitle, "Stream Baseflow Right");
00493                                         strcpy(xTitle, "Time");
00494                                         strcpy(yTitle, "Rate (meters^3/day)");
00495                                         strcpy(legend, "Mean flow");
00496                                 }
00497                                 if(variableIndex == 7){
00498                                         strcpy(plotTitle, "Stream Surface flux Left");
00499                                         strcpy(xTitle, "Time");
00500                                         strcpy(yTitle, "Rate (meters^3/day)");
00501                                         strcpy(legend, "Mean flow");
00502                                 }
00503                                 if(variableIndex == 8){
00504                                         strcpy(plotTitle, "Stream Surface flux Right");
00505                                         strcpy(xTitle, "Time");
00506                                         strcpy(yTitle, "Rate (meters^3/day)");
00507                                         strcpy(legend, "Mean flow");
00508                                 }
00509                         }
00510                         else{
00511                         
00512                         }
00513                 }
00514                 else if( fileName.endsWith("nc", Qt::CaseInsensitive) ){
00515 
00516                 }
00517                 else{
00518                         qWarning("File Type NOT Recognised\n");
00519                 }
00520         }
00521 /***********************************************************************************/
00522         //cout<<"dataCount= "<<dataCount<<"\n";
00523         plotts = new PlotTS(plotTitle, xTitle, yTitle, legend, xVal, yVal, dataCount);  
00524         QPalette palette;
00525         palette.setColor(QPalette::Background, Qt::white);
00526         plotts->setPalette(palette);
00527 
00528         plotts->resize(600, 400);
00529         plotts->show();
00530 /***********************************************************************************/
00531 }
00532 
00533 void timeSeriesDlg::savePlot()
00534 {
00535         //QPalette palette;
00536         //palette.setColor(QPalette::Background, Qt::blue);
00537         //plotts->setPalette(palette);
00538         QPixmap *image;
00539         QwtPlotCanvas *canvas;
00540         canvas = (QwtPlotCanvas *) plotts->canvas();
00541         image = canvas->paintCache();
00542         image->save("test.jpg", "JPG");
00543         
00544 
00545 }
00546 
00547 void timeSeriesDlg::help()
00548 {
00549         helpDialog* hlpDlg = new helpDialog(this);
00550         hlpDlg->show();
00551 }

Generated on Sun Aug 5 17:33:57 2007 for PIHMgis by  doxygen 1.5.2