00001 #include <QtGui>
00002 #include "spatialplot.h"
00003 #include "../../pihmLIBS/generateShape.h"
00004 #include "../../pihmLIBS/helpDialog/helpdialog.h"
00005
00006 #include <iostream>
00007 #include <fstream>
00008
00009 #define ELEMENT_FEATURE 0
00010 #define RIVER_FEATURE 1
00011
00012 using namespace std;
00013
00014 spatialPlotDlg::spatialPlotDlg(QDialog *parent)
00015 {
00016 setupUi(this);
00017
00018 connect( pushButtonEleShape, SIGNAL( clicked() ), this, SLOT( browseEleShapeFile() ) );
00019 connect( pushButtonRivShape, SIGNAL( clicked() ), this, SLOT( browseRivShapeFile() ) );
00020 connect( pushButtonFileName, SIGNAL( clicked() ), this, SLOT( browseModelFile() ) );
00021 connect( pushButtonGenerate, SIGNAL( clicked() ), this, SLOT( generate() ) );
00022 connect( pushButtonHelp, SIGNAL( clicked() ), this, SLOT( help() ) );
00023 }
00024
00025 void spatialPlotDlg::browseEleShapeFile()
00026 {
00027 QString str = QFileDialog::getOpenFileName(this, "Choose TIN Shape File", "~/","Shape File(*.shp *.SHP)");
00028 lineEditEleShape->setText(str);
00029 }
00030
00031 void spatialPlotDlg::browseRivShapeFile()
00032 {
00033 QString str = QFileDialog::getOpenFileName(this, "Choose Riv Shape File", "~/","Shape File(*.shp *.SHP)");
00034 lineEditRivShape->setText(str);
00035 }
00036
00037 void spatialPlotDlg::browseModelFile()
00038 {
00039 QString str = QFileDialog::getOpenFileName(this, "Choose Model Output File", "~/","Text File(*.txt *.TXT);; NetCDF File(*.nc *.NC)");
00040 lineEditFileName->setText(str);
00041 }
00042
00043 void spatialPlotDlg::generate()
00044 {
00045 double *avgVal;
00046
00047 int tabIndex;
00048 int variableIndex;
00049 int startTime, finishTime;
00050 int NUM_STEPS;
00051 int dataCount = 0;
00052 char fStr[100];
00053
00054 int runFlag = 1;
00055 QString shapeFileName, outputFileName;
00056 ifstream inStream;
00057
00058 int NUM_ELE = 0;
00059 int NUM_RIV = 0;
00060
00061 tabIndex = tabWidget->currentIndex();
00062 if(tabIndex == ELEMENT_FEATURE)
00063 {
00064 variableIndex = comboBoxEleVariable->currentIndex();
00065 startTime = ( lineEditEleStart->text() ).toInt();
00066 finishTime = ( lineEditEleFinish->text() ).toInt();
00067 shapeFileName = lineEditEleShape->text();
00068 outputFileName = lineEditFileName->text();
00069
00070 NUM_STEPS = finishTime - startTime + 1;
00071 cout<<"NUM_STEPS= "<<NUM_STEPS<<"\n";
00072 if( outputFileName.endsWith("txt", Qt::CaseInsensitive) ){
00073 inStream.open( qPrintable(outputFileName) );
00074 if(inStream == NULL){
00075 cout << "Couldn't Open File\n";
00076 exit(1);
00077 }
00078 string str;
00079 getline(inStream, str);
00080 int pos = 0;
00081 while( (pos = str.find('\t', pos+1) ) != -1 ){
00082 NUM_ELE++;
00083 }
00084 cout << "NUM_ELE= "<<NUM_ELE<<"\n";
00085 inStream.close();
00086 inStream.open(qPrintable(outputFileName));
00087
00088 avgVal = (double *)malloc(NUM_ELE * sizeof(double));
00089 for(int i=0; i<NUM_ELE; i++)
00090 avgVal[i] = 0.0;
00091
00092 for(int i=0; i<startTime; i++)
00093 getline(inStream, str);
00094 if(inStream == NULL){
00095 qWarning("Error: File ended Prematurely!\n Cannot parse \"Start Time\" provided.\n");
00096 runFlag = 0;
00097 }
00098 if(runFlag != 0){
00099 double temp;
00100 for(int i=0; i<NUM_STEPS; i++){
00101 if(inStream){
00102 for(int j=0; j<NUM_ELE; j++){
00103 inStream >> temp;
00104 avgVal[j]+=temp;
00105 }
00106 dataCount++;
00107 }
00108 else{
00109 dataCount--;
00110 break;
00111 }
00112 }
00113
00114 cout<<"Data Count= "<< dataCount <<"\n";
00115 if(dataCount+1 < NUM_STEPS)
00116 qWarning("Warning: Model does NOT have that many timestep information\n");
00117 for(int i=0; i<NUM_ELE; i++)
00118 avgVal[i]=avgVal[i]/dataCount;
00119 }
00120 }
00121 else if( outputFileName.endsWith("nc", Qt::CaseInsensitive) ){
00122
00123 }
00124 else{
00125 runFlag = 0;
00126 qWarning("File Type NOT Recognised\n");
00127 }
00128
00129 if(runFlag == 1){
00130 if(variableIndex == 0)
00131 strcpy(fStr, "IS_head");
00132 if(variableIndex == 1)
00133 strcpy(fStr, "Sat_head");
00134 if(variableIndex == 2)
00135 strcpy(fStr, "Usat_head");
00136 if(variableIndex == 3)
00137 strcpy(fStr, "Surf_head");
00138 if(variableIndex == 4)
00139 strcpy(fStr, "ET0");
00140 if(variableIndex == 5)
00141 strcpy(fStr, "ET1");
00142 if(variableIndex == 6)
00143 strcpy(fStr, "ET2");
00144 if(variableIndex == 7)
00145 strcpy(fStr, "Precip");
00146 if(variableIndex == 8)
00147 strcpy(fStr, "NetPrecip");
00148 if(variableIndex == 9)
00149 strcpy(fStr, "InfilRate");
00150 if(variableIndex == 10)
00151 strcpy(fStr, "Recharge_Rate");
00152
00153 generateShape(shapeFileName, avgVal, NUM_ELE, 3, fStr);
00154
00155 applicationPointer->addLayer(QStringList("tempShape.shp"), NULL);
00156 }
00157 }
00158 if(tabIndex == RIVER_FEATURE)
00159 {
00160 variableIndex = comboBoxRivVariable->currentIndex();
00161 startTime = ( lineEditRivStart->text() ).toInt();
00162 finishTime = ( lineEditRivFinish->text() ).toInt();
00163 shapeFileName = lineEditRivShape->text();
00164 outputFileName = lineEditFileName->text();
00165
00166 NUM_STEPS = finishTime - startTime + 1;
00167 cout<<"NUM_STEPS= "<<NUM_STEPS<<"\n";
00168 if( outputFileName.endsWith("txt", Qt::CaseInsensitive) ){
00169 inStream.open( qPrintable(outputFileName) );
00170 if(inStream == NULL){
00171 cout << "Couldn't Open File\n";
00172 exit(1);
00173 }
00174 string str;
00175 getline(inStream, str);
00176 int pos = 0;
00177 while( (pos = str.find('\t', pos+1) ) != -1 ){
00178 NUM_RIV++;
00179 }
00180 cout << "NUM_RIV= "<<NUM_RIV<<"\n";
00181 inStream.close();
00182 inStream.open(qPrintable(outputFileName));
00183
00184 avgVal = (double *)malloc(NUM_RIV * sizeof(double));
00185 for(int i=0; i<NUM_RIV; i++)
00186 avgVal[i] = 0.0;
00187
00188 for(int i=0; i<startTime; i++)
00189 getline(inStream, str);
00190 if(inStream == NULL){
00191 qWarning("Error: File ended Prematurely!\n Cannot parse \"Start Time\" provided.\n");
00192 runFlag = 0;
00193 }
00194 if(runFlag != 0){
00195 double temp;
00196 for(int i=0; i<NUM_STEPS; i++){
00197 if(inStream){
00198 for(int j=0; j<NUM_RIV; j++){
00199 inStream >> temp;
00200 avgVal[j]+=temp;
00201 }
00202 dataCount++;
00203 }
00204 else{
00205 dataCount--;
00206 break;
00207 }
00208 }
00209
00210 cout<<"Data Count= "<< dataCount <<"\n";
00211 if(dataCount+1 < NUM_STEPS)
00212 qWarning("Warning: Model does NOT have that many timestep information\n");
00213 for(int i=0; i<NUM_RIV; i++)
00214 avgVal[i]=avgVal[i]/dataCount;
00215 }
00216 }
00217 else if( outputFileName.endsWith("nc", Qt::CaseInsensitive) ){
00218
00219 }
00220 else{
00221 runFlag = 0;
00222 qWarning("File Type NOT Recognised\n");
00223 }
00224
00225 if(runFlag == 1){
00226 if(variableIndex == 0)
00227 strcpy(fStr, "River_head");
00228 if(variableIndex == 1)
00229 strcpy(fStr, "Inflow");
00230 if(variableIndex == 2)
00231 strcpy(fStr, "Outflow");
00232 if(variableIndex == 3)
00233 strcpy(fStr, "Baseflow");
00234 if(variableIndex == 4)
00235 strcpy(fStr, "Surfaceflow");
00236 if(variableIndex == 5)
00237 strcpy(fStr, "Baseflow_L");
00238 if(variableIndex == 6)
00239 strcpy(fStr, "BaseFlow_R");
00240 if(variableIndex == 7)
00241 strcpy(fStr, "SurfFlow_L");
00242 if(variableIndex == 8)
00243 strcpy(fStr, "SurfFlow_R");
00244
00245 generateShape(shapeFileName, avgVal, NUM_RIV, 2, fStr);
00246
00247 applicationPointer->addLayer(QStringList(shapeFileName), NULL);
00248 }
00249 }
00250
00251 }
00252
00253 void spatialPlotDlg::help()
00254 {
00255
00256 helpDialog* hlpDlg = new helpDialog(this, "Spatial Plot", 1, "helpFiles/spatialplot.html", "Help :: Spatial Plot");
00257 hlpDlg->show();
00258 }
00259
00260 void spatialPlotDlg::setApplicationPointer(QgisApp* appPtr){
00261 applicationPointer = appPtr;
00262 }