Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

omgclimatedataprocessor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           dataprocessor.cpp  -  description
00003                              -------------------
00004     begin                : Wed Jan 8 2003
00005     copyright            : (C) 2003 by Tim Sutton
00006     email                : t.sutton@reading.ac.uk
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "omgclimatedataprocessor.h"
00019 #include <iostream>
00020 #include <math.h>
00021 #include <cassert>
00022 
00023 const int QUARTER = 3;
00024 const float FREEZING_POINT=0;
00025 
00026 
00027 OmgClimateDataProcessor::OmgClimateDataProcessor(float theNoDataVal)
00028 {
00029   mNoData=theNoDataVal;
00030 }
00031 
00032 OmgClimateDataProcessor::~OmgClimateDataProcessor()
00033 {}
00034 
00035 float OmgClimateDataProcessor::meanOverLowestQ(QVector <float> theClimateVector)
00036 {
00037 #ifdef QGISDEBUG
00038     std::cout << "OmgClimateDataProcessor::meanOverLowestQ () called" << std::endl;
00039     std::cout << "==========================================================" << std::endl;
00040 #endif
00041 
00042     float myLowestMeanFloat;
00043     float myCurrentMeanFloat;
00044     int myInt;
00045     int mySegmentInt;
00046     bool myFirstTimeFlag;
00047 
00048     myLowestMeanFloat = 0;
00049     myFirstTimeFlag = true;
00050     int myVectorLengthInt = theClimateVector.size();
00051 
00052     //Loop from first quarter - i.e. months 1, 2 and 3 to
00053     //last quarter - i.e. months 12, 1 and 2.
00054     for (myInt=0; myInt < theClimateVector.size(); myInt++)
00055     {
00056         myCurrentMeanFloat = 0;
00057 #ifdef QGISDEBUG
00058         std::cout << "Processing element " <<    myInt << std::endl;
00059 #endif
00060         //Check to see whether you are near end of vector and it is necessary to start reading
00061         //from the beginning again.
00062         if (myInt <= (myVectorLengthInt - (QUARTER)))
00063         {
00064             for (mySegmentInt=myInt ; mySegmentInt<=(myInt + QUARTER -1);mySegmentInt++)
00065             {
00066                 float myFloat = theClimateVector[mySegmentInt];
00067                 if (mNoData==myFloat) { return mNoData; }
00068                 myCurrentMeanFloat = myCurrentMeanFloat + myFloat;
00069 #ifdef QGISDEBUG
00070                 std::cout << "Processing element " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00071 #endif
00072             }
00073             myCurrentMeanFloat = myCurrentMeanFloat / QUARTER;
00074         }
00075         else
00076         {
00077             //Near end of vector so read last month(s)
00078             for (mySegmentInt = myInt; mySegmentInt < myVectorLengthInt ; mySegmentInt++)
00079             {
00080                 float myFloat = theClimateVector[mySegmentInt];
00081                 if (mNoData==myFloat) { return mNoData; }
00082                 myCurrentMeanFloat = myCurrentMeanFloat + myFloat;
00083 #ifdef QGISDEBUG
00084                 std::cout << "Wrap Processing element (from end) " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00085 #endif
00086             }
00087             //Read remainding months from beginning of vector
00088             for (mySegmentInt = 0; mySegmentInt<=(QUARTER - (myVectorLengthInt - myInt + 1));mySegmentInt++)
00089             {
00090                 myCurrentMeanFloat = myCurrentMeanFloat + theClimateVector[mySegmentInt];
00091 #ifdef QGISDEBUG
00092                 std::cout << "Wrap Processing element (from start)" <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00093 #endif
00094             }
00095             myCurrentMeanFloat = myCurrentMeanFloat / QUARTER;
00096         }
00097 
00098         //Check whether value is lowest so far.If this is the first quarter always keep value.
00099         if (myFirstTimeFlag)
00100         {
00101             myLowestMeanFloat = myCurrentMeanFloat;
00102             myFirstTimeFlag = false;
00103         }
00104         else
00105         {
00106             //Test to see whether current value is the lowest so far.
00107             if (myLowestMeanFloat > myCurrentMeanFloat)
00108             {
00109                 myLowestMeanFloat = myCurrentMeanFloat;
00110             }
00111         }
00112 
00113 #ifdef QGISDEBUG
00114         std::cout << "Iteration " << myInt << " - current lowest mean over quarter is : " << myLowestMeanFloat << std::endl;
00115 #endif
00116     }
00117 #ifdef QGISDEBUG
00118     std::cout << "Completed - lowest mean over quarter is : " << myLowestMeanFloat << std::endl;
00119     std::cout << "----------------------------------------------------------------------------------------------" << std::endl;
00120 #endif
00121     //Return lowest value over the quarter
00122     return myLowestMeanFloat;
00123 
00124 }
00125 
00126 float OmgClimateDataProcessor::meanOverHighestQ ( QVector <float> theClimateVector)
00127 {
00128 #ifdef QGISDEBUG
00129     std::cout << "OmgClimateDataProcessor::meanOverHighestQ ( ) called" << std::endl;
00130     std::cout << "==========================================================" << std::endl;
00131 #endif
00132 
00133     float myHighestMeanFloat;
00134     float myCurrentMeanFloat;
00135     int myInt;
00136     int mySegmentInt;
00137     bool myFirstTimeFlag;
00138     int myVectorLengthInt = theClimateVector.size();
00139 #ifdef QGISDEBUG
00140     std::cout << "Vector length : " << myVectorLengthInt << std::endl;
00141 #endif
00142     myHighestMeanFloat = 0;
00143     myFirstTimeFlag = true;
00144 
00145     //Loop from first quarter - i.e. months 1, 2 and 3 to
00146     //last quarter - i.e. months 12, 1 and 2.
00147     for (myInt=0; myInt<myVectorLengthInt; myInt++)
00148     {
00149         myCurrentMeanFloat = 0;
00150 #ifdef QGISDEBUG
00151         std::cout << "Processing element " <<    myInt << std::endl;
00152 #endif
00153         //Check to see whether you are near end of vector and it is necessary to start reading
00154         //from the beginning again.
00155         if (myInt <= (myVectorLengthInt - (QUARTER)))
00156         {
00157             for (mySegmentInt=myInt ; mySegmentInt<=(myInt + QUARTER -1);mySegmentInt++)
00158             {
00159                 float myFloat = theClimateVector[mySegmentInt];
00160                 if (mNoData==myFloat) { return mNoData; }
00161                 myCurrentMeanFloat = myCurrentMeanFloat + myFloat;
00162 #ifdef QGISDEBUG
00163                 std::cout << "Processing element " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00164 #endif
00165             }
00166             myCurrentMeanFloat = myCurrentMeanFloat / QUARTER;
00167         }
00168         else
00169         {
00170             //Near end of vector so read last month(s)
00171             for (mySegmentInt = myInt; mySegmentInt < myVectorLengthInt ; mySegmentInt++)
00172             {
00173                 float myFloat = theClimateVector[mySegmentInt];
00174                 if (mNoData==myFloat) { return mNoData; }
00175                 myCurrentMeanFloat = myCurrentMeanFloat + myFloat;
00176 #ifdef QGISDEBUG
00177                 std::cout << "Wrap Processing element (from end) " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00178 #endif
00179             }
00180             //Read remainding months from beginning of vector
00181             for (mySegmentInt = 0; mySegmentInt<=(QUARTER - (myVectorLengthInt - myInt + 1));mySegmentInt++)
00182             {
00183                 float myFloat = theClimateVector[mySegmentInt];
00184                 if (mNoData==myFloat) { return mNoData; }
00185                 myCurrentMeanFloat = myCurrentMeanFloat + myFloat;
00186 #ifdef QGISDEBUG
00187                 std::cout << "Wrap Processing element (from start)" <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00188 #endif
00189             }
00190             myCurrentMeanFloat = myCurrentMeanFloat / QUARTER;
00191         }
00192 
00193         //Check whether value is highest so far.If this is the first quarter always keep value.
00194         if (myFirstTimeFlag)
00195         {
00196             myHighestMeanFloat = myCurrentMeanFloat;
00197             myFirstTimeFlag = false;
00198         }
00199         else
00200         {
00201             //Test to see whether current value is the Highest so far.
00202             if (myHighestMeanFloat < myCurrentMeanFloat)
00203             {
00204                 myHighestMeanFloat = myCurrentMeanFloat;
00205             }
00206         }
00207 
00208 #ifdef QGISDEBUG
00209         std::cout << "Iteration " << myInt << " - current highest mean over quarter is : " << myHighestMeanFloat << std::endl;
00210 #endif
00211     }
00212 #ifdef QGISDEBUG
00213     std::cout << "Completed - highest mean over quarter is : " << myHighestMeanFloat << std::endl;
00214     std::cout << "----------------------------------------------------------------------------------------------" << std::endl;
00215 #endif
00216     //Return Highest value over the quarter
00217     return myHighestMeanFloat;
00218 }
00219 
00220 int OmgClimateDataProcessor::firstMonthOfLowestQ (QVector <float> theClimateVector)
00221 {
00222 #ifdef QGISDEBUG
00223     std::cout << "OmgClimateDataProcessor::firstMonthOfLowestQ ( float *theClimateVector, int myVectorLengthInt) called" << std::endl;
00224     std::cout << "==========================================================" << std::endl;
00225 #endif
00226     float myLowestFloat=0;
00227     float myCurrentFloat=0;
00228     int myInt=0;
00229     int mySegmentInt=0;
00230     bool myFirstTimeFlag=true;
00231     int myLowestMonthInt=0;
00232     int myVectorLengthInt = theClimateVector.size();
00233     try
00234     {
00235         //Loop from first quarter - i.e. months 1, 2 and 3 to
00236         //last quarter - i.e. months 12, 1 and 2.
00237         for (myInt = 0; myInt < myVectorLengthInt; myInt++)
00238         {
00239             myCurrentFloat = 0;
00240             //Check to see whether you are near end of vector and it is necessary to start reading
00241             //from the beginning again.
00242 
00243             if (myInt <= (myVectorLengthInt - (QUARTER)))
00244             {
00245                 for (mySegmentInt=myInt ; mySegmentInt<=(myInt + QUARTER -1);mySegmentInt++)
00246                 {
00247                     float myFloat = theClimateVector[mySegmentInt];
00248                     if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00249                     myCurrentFloat += myFloat;
00250 #ifdef QGISDEBUG
00251                     std::cout << "Processing element " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00252 #endif
00253                 }
00254             }
00255             else
00256             {
00257                 //Near end of vector so read last month(s)
00258                 for (mySegmentInt = myInt; mySegmentInt < myVectorLengthInt; mySegmentInt++)
00259                 {
00260                     float myFloat = theClimateVector[mySegmentInt];
00261                     if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00262                     myCurrentFloat += myFloat;
00263 #ifdef QGISDEBUG
00264                     std::cout << "Wrap Processing element (from end) " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00265 #endif
00266                 }
00267                 //Read remainding months from beginning of vector
00268                 //for (mySegmentInt = 0; mySegmentInt < (QUARTER - (myVectorLengthInt - myInt) - 1); mySegmentInt++)
00269                 for (mySegmentInt = 0; mySegmentInt <=(QUARTER - (myVectorLengthInt - myInt + 1));mySegmentInt++)
00270                 {
00271                     float myFloat = theClimateVector[mySegmentInt];
00272                     if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00273                     myCurrentFloat += myFloat;
00274 #ifdef QGISDEBUG
00275                     std::cout << "Wrap Processing element (from start)" <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00276 #endif
00277                 }
00278             }
00279 
00280             //Check whether value is lowest so far.  If this is the first quarter always keep value.
00281             if (myFirstTimeFlag == true)
00282             {
00283                 myLowestMonthInt = myInt;
00284                 myLowestFloat = myCurrentFloat;
00285                 myFirstTimeFlag = false;
00286             }
00287             else
00288             {
00289                 //Test to see whether current value is the lowest so far. If so store month number
00290                 if (myCurrentFloat < myLowestFloat)
00291                 {
00292                     myLowestMonthInt = myInt;
00293                     myLowestFloat = myCurrentFloat;
00294                 }
00295             }
00296         }
00297     }
00298     catch (...)
00299     {
00300 #ifdef QGISDEBUG
00301         std::cout << "A fatal error occured in the firstMonthOfLowestQ method " << std::endl;
00302         myLowestMonthInt = mNoData;
00303 #endif
00304     }
00305 
00306 
00307     //Return lowest value over the quarter (add 1 to get it into base 1 instead of base0)
00308     return myLowestMonthInt+1;
00309 
00310 
00311 }
00321 int OmgClimateDataProcessor::firstMonthOfHighestQ (QVector <float> theClimateVector)
00322 {
00323 #ifdef QGISDEBUG
00324     std::cout << "OmgClimateDataProcessor::firstMonthOfHighestQ ( float *theClimateVector, int myVectorLengthInt) called" << std::endl;
00325     std::cout << "==========================================================" << std::endl;
00326 #endif
00327 
00328     float myHighestFloat=0;
00329     float myCurrentFloat=0;
00330     int myInt=0;
00331     int mySegmentInt=0;
00332     bool myFirstTimeFlag=true;
00333     int myHighestMonthInt=0;
00334     int myVectorLengthInt = theClimateVector.size();
00335     try
00336     {
00337         //Loop from first quarter - i.e. months 1, 2 and 3 to
00338         //last quarter - i.e. months 12, 1 and 2.
00339         for (myInt = 0; myInt < myVectorLengthInt; myInt++)
00340         {
00341             myCurrentFloat = 0;
00342             //Check to see whether you are near end of vector and it is necessary to start reading
00343             //from the beginning again.
00344 
00345             if (myInt <= (myVectorLengthInt - (QUARTER)))
00346             {
00347                 for (mySegmentInt=myInt ; mySegmentInt<=(myInt + QUARTER -1);mySegmentInt++)
00348                 {
00349                     float myFloat = theClimateVector[mySegmentInt];
00350                     if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00351                     myCurrentFloat += myFloat;
00352 #ifdef QGISDEBUG
00353                     std::cout << "Processing element " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00354 #endif
00355                 }
00356             }
00357             else
00358             {
00359                 //Near end of vector so read last month(s)
00360                 for (mySegmentInt = myInt; mySegmentInt < myVectorLengthInt; mySegmentInt++)
00361                 {
00362                     float myFloat = theClimateVector[mySegmentInt];
00363                     if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00364                     myCurrentFloat += myFloat;
00365 #ifdef QGISDEBUG
00366                     std::cout << "Wrap Processing element (from end) " <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00367 #endif
00368                 }
00369                 //Read remainding months from beginning of vector
00370                 //for (mySegmentInt = 0; mySegmentInt < (QUARTER - (myVectorLengthInt - myInt) - 1); mySegmentInt++)
00371                 for (mySegmentInt = 0; mySegmentInt <=(QUARTER - (myVectorLengthInt - myInt + 1));mySegmentInt++)
00372                 {
00373                     float myFloat = theClimateVector[mySegmentInt];
00374                     if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00375                     myCurrentFloat += myFloat;
00376 
00377 #ifdef QGISDEBUG
00378                     std::cout << "Wrap Processing element (from start)" <<    myInt << ", value is " << theClimateVector[mySegmentInt] << std::endl;
00379 #endif
00380                 }
00381             }
00382 
00383             //Check whether value is highest so far.  If this is the first quarter always keep value.
00384             if (myFirstTimeFlag == true)
00385             {
00386                 myHighestMonthInt = myInt;
00387                 myHighestFloat = myCurrentFloat;
00388                 myFirstTimeFlag = false;
00389             }
00390             else
00391             {
00392                 //Test to see whether current value is the highest so far. If so store month number
00393                 if (myCurrentFloat > myHighestFloat)
00394                 {
00395                     myHighestMonthInt = myInt;
00396                     myHighestFloat = myCurrentFloat;
00397                 }
00398             }
00399         }
00400     }
00401     catch (...)
00402     {
00403 #ifdef QGISDEBUG
00404         std::cout << "A fatal error occured in the firstMonthOfHighestQ method " << std::endl;
00405 #endif
00406         myHighestMonthInt = static_cast<int>(mNoData);
00407     }
00408 
00409 
00410     //Return highest value over the quarter (add 1 to get it into base 1 instead of base0)
00411     return myHighestMonthInt+1;
00412 }
00415 float OmgClimateDataProcessor::meanOverQuarter (QVector <float> theClimateVector, int theStartMonth)
00416 {
00417 
00418 #ifdef QGISDEBUG
00419     std::cout << "OmgClimateDataProcessor::meanOverQuarter (float *theClimateVector, int myVectorLengthInt, int theStartMonth) called" << std::endl;
00420     std::cout << "==========================================================" << std::endl;
00421 #endif
00422 
00423 
00424     int myInt=0;
00425     float myMeanFloat=0;
00426     int mySegmentInt=0;
00427     int myVectorLengthInt = theClimateVector.size();
00428     try
00429     {
00430 
00431         //Check to see whether months need to wrap
00432         if (theStartMonth + QUARTER < myVectorLengthInt)
00433         {
00434             //No wrapping necessary
00435             for (myInt = theStartMonth-1; myInt < (theStartMonth-1 + (QUARTER)); myInt++)
00436             {
00437                 float myFloat = theClimateVector[myInt];
00438                 if (mNoData==myFloat) { return mNoData; }
00439                 myMeanFloat += myFloat;
00440 #ifdef QGISDEBUG
00441                 std::cout << "Added " << theClimateVector[myInt] << ", total is now : " << myMeanFloat << std::endl;
00442 #endif
00443             }
00444         }
00445         else
00446         {
00447             //Wrapping necessary so read last month(s) as normal
00448             for (mySegmentInt=theStartMonth-1; mySegmentInt < myVectorLengthInt; mySegmentInt++)
00449             {
00450                 float myFloat = theClimateVector[mySegmentInt];
00451                 if (mNoData==myFloat) { return mNoData; }
00452                 myMeanFloat += myFloat;
00453 #ifdef QGISDEBUG
00454                 std::cout << "Wrap Added from end " << theClimateVector[mySegmentInt] << ", total is now : " << myMeanFloat << std::endl;
00455 #endif
00456             }
00457             //Read remainding months from beginning of vector
00458             for (mySegmentInt = 0; mySegmentInt < (QUARTER - (myVectorLengthInt - theStartMonth) - 1); mySegmentInt++)
00459             {
00460                float myFloat = theClimateVector[mySegmentInt];
00461                 if (mNoData==myFloat) { return mNoData; }
00462                 myMeanFloat += myFloat;
00463 #ifdef QGISDEBUG
00464                 std::cout << "Wrap Added from start " << theClimateVector[mySegmentInt] << ", total is now : " << myMeanFloat << std::endl;
00465 #endif
00466             }
00467         }
00468         myMeanFloat = (myMeanFloat / QUARTER);
00469     }
00470     catch (...)
00471     {
00472 #ifdef QGISDEBUG
00473         std::cout << "A fatal error occured in the meanOverQuarter method " << std::endl;
00474 #endif
00475         myMeanFloat = mNoData;
00476     }
00477     return myMeanFloat;
00478 }
00481 float OmgClimateDataProcessor::lowestValue (QVector <float> theClimateVector)
00482 {
00483 
00484 #ifdef QGISDEBUG
00485     std::cout << "OmgClimateDataProcessor::lowestValue ( float *theClimateVector, int myVectorLengthInt) called" << std::endl;
00486     std::cout << "==========================================================" << std::endl;
00487 #endif
00488 
00489 
00490     float myLowestFloat=0;
00491     int myInt=0;
00492     bool myFirstTimeFlag=true;
00493     int myVectorLengthInt = theClimateVector.size();
00494     try
00495     {
00496         for (myInt=0; myInt < myVectorLengthInt; myInt++)
00497         {
00498             //If this is the first run store value as lowest
00499             if (myFirstTimeFlag)
00500             {
00501                 float myFloat = theClimateVector[myInt];
00502                 if (mNoData==myFloat) { return mNoData; }
00503                 myLowestFloat = myFloat;
00504 #ifdef QGISDEBUG
00505                 std::cout << "Lowest value set to " << myLowestFloat << " on first iteration." << std::endl;
00506 #endif
00507                 myFirstTimeFlag = false;
00508 
00509             }
00510             //Test to see whether value is lowest so far
00511             else
00512             {
00513                 if (myLowestFloat > theClimateVector[myInt])
00514                 {
00515                     myLowestFloat = theClimateVector[myInt];
00516                 }
00517             }
00518         }
00519         //std::cout <<  "Lowest value was : " << myLowestFloat << std::endl;
00520     }
00521     catch (...)
00522     {
00523 #ifdef QGISDEBUG
00524         std::cout << "A fatal error occured in the lowestValue method " << std::endl;
00525 #endif
00526         myLowestFloat = mNoData;
00527     }
00528     //Return lowest value
00529     return myLowestFloat;
00530 }
00533 float OmgClimateDataProcessor::highestValue (QVector <float> theClimateVector)
00534 {
00535 
00536 #ifdef QGISDEBUG
00537     std::cout << "OmgClimateDataProcessor::highestValue ( float *theClimateVector, int myVectorLengthInt) called" << std::endl;
00538     std::cout << "==========================================================" << std::endl;
00539 #endif
00540 
00541 
00542     float myHighestFloat=0;
00543     int myInt=0;
00544     bool myFirstTimeFlag=true;
00545     int myVectorLengthInt = theClimateVector.size();
00546     try
00547     {
00548         for (myInt=0; myInt < myVectorLengthInt; myInt++)
00549         {
00550             //If this is the first run store value as lowest
00551             if (myFirstTimeFlag == true)
00552             {
00553                 float myFloat = theClimateVector[myInt];
00554                 if (mNoData==myFloat) { return mNoData; }
00555                 myHighestFloat = myFloat;
00556 #ifdef QGISDEBUG
00557                 std::cout << "Highest value set to " << myHighestFloat << " on first iteration." << std::endl;
00558 #endif
00559                 myFirstTimeFlag = false;
00560 
00561             }
00562             //Test to see whether value is highest so far
00563             else
00564             {
00565                 float myFloat = theClimateVector[myInt];
00566                 if (mNoData==myFloat) { return mNoData; }
00567                 if (myHighestFloat < myFloat)
00568                 {
00569                     myHighestFloat = myFloat;
00570                 }
00571             }
00572         }
00573     }
00574     catch (...)
00575     {
00576 #ifdef QGISDEBUG
00577         std::cout << "A fatal error occured in the highest value method " << std::endl;
00578 #endif
00579         myHighestFloat = mNoData;
00580     }
00581     //Return highest value
00582     return myHighestFloat;
00583 
00584 }
00585 
00592 float OmgClimateDataProcessor::greatestTotalRange (QVector <float> theClimateVector1,
00593         QVector <float> theClimateVector2)
00594 {
00595 
00596 #ifdef QGISDEBUG
00597     std::cout << "OmgClimateDataProcessor::greatestTotalRange (float *theClimateVector1, int theArrayLength1, float *theClimateVector2, int theArrayLength2) called" << std::endl;
00598     std::cout << "==========================================================" << std::endl;
00599 #endif
00600     //dont default to 0 as it may be lower (or higher) than any existing value in each vector
00601     float myHighestFloat=theClimateVector1[0]; //default to a valid value in the set
00602     float myLowestFloat=theClimateVector1[0];  //default to a valid value in the set
00603     float myRangeFloat=mNoData;
00604     int myInt=0;
00605     int myVectorLengthInt1 = theClimateVector1.size();
00606     int myVectorLengthInt2 = theClimateVector2.size();
00607     try
00608     {
00609         //process array1 first
00610         for (myInt = 0; myInt < myVectorLengthInt1; myInt++)
00611         {
00612             float myFloat = theClimateVector1[myInt];
00613             if (mNoData==myFloat) { return mNoData; }
00614             if (myHighestFloat < myFloat)
00615             {
00616                 myHighestFloat = myFloat ;
00617             }
00618             if (myLowestFloat > myFloat)
00619             {
00620                 myLowestFloat = myFloat;
00621             }
00622         }
00623         //now the second vector
00624         for (myInt = 0; myInt < myVectorLengthInt2; myInt++)
00625         {
00626             float myFloat = theClimateVector2[myInt];
00627             if (mNoData==myFloat) { return mNoData; }
00628             if (myHighestFloat < myFloat)
00629             {
00630                 myHighestFloat = myFloat;
00631             }
00632             if (myLowestFloat > myFloat)
00633             {
00634                 myLowestFloat = myFloat;
00635             }
00636         }
00637         myRangeFloat = myHighestFloat - myLowestFloat;
00638     }
00639     catch (...)
00640     {
00641 #ifdef QGISDEBUG
00642         std::cout << "A fatal error occured in the greatestTotalRange method " << std::endl;
00643 #endif
00644         myRangeFloat = mNoData;
00645 
00646     }
00647     return myRangeFloat;
00648 }
00654 float OmgClimateDataProcessor::greatestMonthlyRange (QVector <float> theClimateVector1,
00655         QVector <float> theClimateVector2)
00656 {
00657 
00658   std::cout << "Vector 1 size: " << theClimateVector1.size();
00659   std::cout << "Vector 2 size: " << theClimateVector2.size();
00660   assert("Greatest monthly range is not implemented yet!");
00661     return 0;
00662 
00663 }
00664 
00665 bool OmgClimateDataProcessor::threshold(QVector <float> &theClimateVector, float theThreshold)
00666 {
00667     int myVectorLengthInt = theClimateVector.size();
00668     try
00669     {
00670         for (int myInt = 0; myInt < myVectorLengthInt; myInt++)
00671         {
00672             float myFloat = theClimateVector[myInt];
00673             if (mNoData==myFloat) 
00674             { 
00675               continue; 
00676             }
00677             else if (theThreshold<=myFloat)
00678             {
00679               theClimateVector[myInt]=1;
00680             }
00681             else
00682             {
00683               theClimateVector[myInt]=0;
00684             }
00685         }
00686     }
00687     catch (...)
00688     {
00689 #ifdef QGISDEBUG
00690         std::cout << "A fatal error occured in the threshold method " << std::endl;
00691         return false;
00692 #endif
00693     }
00694     return true;
00695 
00696 }
00697 
00698 float OmgClimateDataProcessor::threshold(float theFloat, float theThreshold)
00699 {
00700     try
00701     {
00702             if (mNoData==theFloat) 
00703             { 
00704               return mNoData; 
00705             }
00706             else if (theThreshold<=theFloat)
00707             {
00708               return 1.0;
00709             }
00710             else
00711             {
00712               return 0.0;
00713             }
00714     }
00715     catch (...)
00716     {
00717 #ifdef QGISDEBUG
00718         std::cout << "A fatal error occured in the threshold method " << std::endl;
00719         return mNoData;
00720 #endif
00721     }
00722     return mNoData;
00723 }
00724 
00725 float OmgClimateDataProcessor::sum(QVector <float> theClimateVector)
00726 {
00727     float myRunningTotFloat=0;
00728     int myVectorLengthInt = theClimateVector.size();
00729     try
00730     {
00731         for (int myInt = 0; myInt <= myVectorLengthInt-1; myInt++)
00732         {
00733             float myFloat = theClimateVector[myInt];
00734             if (mNoData==myFloat) { return mNoData; }
00735             myRunningTotFloat += myFloat;
00736         }
00737     }
00738     catch (...)
00739     {
00740 #ifdef QGISDEBUG
00741         std::cout << "A fatal error occured in the sum method " << std::endl;
00742 #endif
00743         myRunningTotFloat = mNoData;
00744     }
00745     return myRunningTotFloat;
00746 }
00747 
00748 
00751 float OmgClimateDataProcessor::mean (QVector <float> theClimateVector)
00752 {
00753 #ifdef QGISDEBUG
00754     std::cout << "OmgClimateDataProcessor::meanOverYear ( float *theClimateVector, int myVectorLengthInt) called" << std::endl;
00755     std::cout << "==============================================================" << std::endl;
00756 #endif
00757 
00758     float myRunningTotFloat=0;
00759     float myMeanOverYearFloat;
00760     int myVectorLengthInt = theClimateVector.size();
00761     try
00762     {
00763         for (int myInt = 0; myInt <= myVectorLengthInt-1; myInt++)
00764         {
00765             float myFloat = theClimateVector[myInt];
00766             if (mNoData==myFloat) { return mNoData; }
00767             myRunningTotFloat += myFloat;
00768 #ifdef QGISDEBUG
00769             std::cout << "Iteration " << myInt << ", running total is : " << myRunningTotFloat << std::endl;
00770 #endif
00771         }
00772         myMeanOverYearFloat = myRunningTotFloat / myVectorLengthInt;
00773     }
00774     catch (...)
00775     {
00776 #ifdef QGISDEBUG
00777         std::cout << "A fatal error occured in the meanOverYear method " << std::endl;
00778 #endif
00779         myMeanOverYearFloat = mNoData;
00780     }
00781 #ifdef QGISDEBUG
00782     std::cout << "Completed - mean over year is : " << myMeanOverYearFloat << std::endl;
00783     std::cout << "----------------------------------------------------------------------------------------------" <<
00784      std::endl;
00785 #endif
00786     return myMeanOverYearFloat;
00787 
00788 }
00790 float OmgClimateDataProcessor::stddevOverYear (QVector <float> theClimateVector)
00791 {
00792 
00793 #ifdef QGISDEBUG
00794     std::cout << "OmgClimateDataProcessor::stddevOverYear ( float *theClimateVector, int myVectorLengthInt) called" << std::endl;
00795     std::cout << "==============================================================" << std::endl;
00796 #endif
00797     int myInt=0;
00798     float myRunningTotFloat=0;
00799     float myMeanOverYearFloat=0;
00800     float mySumSqdDevFloat=0;
00801     float myStddevOverYearFloat=0;
00802     int myVectorLengthInt = theClimateVector.size();
00803     try
00804     {
00805         for (myInt = 0; myInt <= myVectorLengthInt-1; myInt++)
00806         {
00807           float myFloat = theClimateVector[myInt];
00808           //if any element is null, cell is null!
00809           if (mNoData == myFloat) { return mNoData; }
00810           //assert (myFloat < 0);
00811           myRunningTotFloat += myFloat;
00812 #ifdef QGISDEBUG
00813             std::cout << "Iteration " << myInt << ", running total is : " << myRunningTotFloat << std::endl;
00814 #endif
00815         }
00816         myMeanOverYearFloat = myRunningTotFloat / myVectorLengthInt;
00817 
00818         //Calculate the sum of the squared deviations
00819         for (myInt = 0; myInt <  myVectorLengthInt; myInt++)
00820         {
00821             mySumSqdDevFloat += static_cast<float>(pow((theClimateVector[myInt] - myMeanOverYearFloat),2));
00822         }
00823 
00824         //divide result by sample size - 1 and get square root to get stdev
00825         myStddevOverYearFloat = static_cast<float>(sqrt(mySumSqdDevFloat / (myVectorLengthInt - 1)));
00826     }
00827     catch (...)
00828     {
00829 #ifdef QGISDEBUG
00830         std::cout << "A fatal error occured in the stddevOverYear method " << std::endl;
00831 #endif
00832         myStddevOverYearFloat = mNoData;
00833     }
00834 #ifdef QGISDEBUG
00835     std::cout << "Completed - stddev over year is : " << myMeanOverYearFloat << std::endl;
00836     std::cout << "----------------------------------------------------------------------------------------------" << std::endl;
00837 #endif
00838 
00839     return myStddevOverYearFloat;
00840 }
00843 int OmgClimateDataProcessor::monthWithLowestValue(QVector <float> theClimateVector)
00844 {
00845     int myLowestInt=0;     //assume first month is lowest until proven otherwise
00846     int myVectorLengthInt = theClimateVector.size();
00847     try
00848     {
00849         for (int myInt = 0; myInt < myVectorLengthInt; myInt++)
00850         {
00851             float myFloat = theClimateVector[myInt];
00852             if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00853             //Test to see whether current value is the lowest so far.
00854             if (myFloat < theClimateVector[myLowestInt])
00855             {
00856                 myLowestInt = myInt ;
00857             }
00858         }
00859     }
00860     catch (...)
00861     {
00862 #ifdef QGISDEBUG
00863         std::cout << "A fatal error occured in the monthWithLowestValue method " << std::endl;
00864 #endif
00865         myLowestInt = 0;
00866     }
00867 #ifdef QGISDEBUG
00868     std::cout << "Completed - month with lowest value is : " << myLowestInt << std::endl;
00869     std::cout << "----------------------------------------------------------------------------------------------" << std::endl;
00870 #endif
00871     return myLowestInt+1;
00872 
00873 
00874 }
00877 int OmgClimateDataProcessor::monthWithHighestValue (QVector <float> theClimateVector)
00878 {
00879 
00880     int myHighestInt=0;      //assume first month is highest until proven otherwise
00881     int myVectorLengthInt = theClimateVector.size();
00882     try
00883     {
00884         for (int myInt = 0; myInt < myVectorLengthInt; myInt++)
00885         {
00886             float myFloat = theClimateVector[myInt];
00887             if (mNoData==myFloat) { return static_cast<int>(mNoData); }
00888             //Test to see whether current value is the lowest so far.
00889             if (myFloat > theClimateVector[myHighestInt])
00890             {
00891                 myHighestInt = myInt ;
00892             }
00893         }
00894     }
00895     catch (...)
00896     {
00897 #ifdef QGISDEBUG
00898         std::cout << "A fatal error occured in the monthWithHighestValue method " << std::endl;
00899 #endif
00900         myHighestInt = 0;
00901     }
00902 #ifdef QGISDEBUG
00903     std::cout << "Completed - month with highest value is : " << myHighestInt << std::endl;
00904     std::cout << "----------------------------------------------------------------------------------------------" << std::endl;
00905 #endif
00906     return myHighestInt+1;
00907 
00908 }
00911 float OmgClimateDataProcessor::valueGivenMonth (QVector <float> theClimateVector,int theMonth)
00912 {
00913     float myValueFloat=0;
00914     int myVectorLengthInt = theClimateVector.size();
00915     try
00916     {
00917         if (theMonth < 1 || theMonth > 12 || myVectorLengthInt != 12)
00918             throw "Month out of bounds";
00919         myValueFloat = theClimateVector[theMonth-1];
00920     }
00921     catch (...)
00922     {
00923 #ifdef QGISDEBUG
00924         std::cout << "A fatal error occured in the valueGivenMonth function" << std::endl;
00925 #endif
00926         myValueFloat = mNoData;
00927     }
00928     return myValueFloat;
00929 }
00934 float OmgClimateDataProcessor::meanValueOverFrostFreeMonths (QVector<float> theFrostVector,
00935         QVector <float> theClimateVector)
00936 {
00937 #ifdef QGISDEBUG
00938     std::cout << "OmgClimateDataProcessor::meanValueOverFrostFreeMonths (float *theFrostArray, int theFrostArrayLength, float *theClimateVector, int theClimateVectorLength)" << std::endl;
00939     std::cout << "==============================================================" << std::endl;
00940 #endif
00941 
00942     int myInt =0;
00943     float myRunningTotalFloat =0;
00944     float myMonthCountInt =0;
00945     float myMeanFloat=mNoData;
00946     int myFrostVectorLengthInt = theFrostVector.size();
00947     int myClimateVectorLengthInt = theClimateVector.size();
00948 
00949     try
00950     {
00951         //Check that the frost and climate arrays are the same size
00952         if (myFrostVectorLengthInt != myClimateVectorLengthInt)
00953         {
00954             throw ("Vector sizes must be equal!");
00955         }
00956         for (myInt = 0; myInt < myFrostVectorLengthInt; myInt++)
00957         {
00958             float myFloat = theClimateVector[myInt];
00959             if (mNoData==myFloat) { return mNoData; }
00960             //iterate through frost vector looking for frost free months
00961             if (myFloat == 0)
00962             {
00963                 //If month is frost free add value to myRunningTotalDbl and add month to monthcounter
00964                 myRunningTotalFloat += theClimateVector[myInt];
00965                 myMonthCountInt += 1;
00966             }
00967         }
00968 
00969         //return average value
00970         if (myMonthCountInt > 0)
00971         {
00972             myMeanFloat = myRunningTotalFloat / myMonthCountInt;
00973         }
00974     }
00975     catch (...)
00976     {
00977 #ifdef QGISDEBUG
00978         std::cout << "A fatal error occured in the meanValueOverFrostFreeMonths function" << std::endl;
00979 #endif
00980         myMeanFloat = mNoData;
00981     }
00982 
00983     return myMeanFloat;
00984 }
00985 
00986 
00987 
00988 
00989 int OmgClimateDataProcessor::numberOfMonthsAboveZero (QVector <float> theClimateVector)
00990 {
00991 
00992     int myInt=0;
00993     int myMonthCountInt=0;
00994 
00995     try
00996     {
00997         for (myInt=0; myInt < theClimateVector.size(); myInt++)
00998         {
00999             if (theClimateVector[myInt] == mNoData)
01000             {
01001                 return static_cast<int>(mNoData);
01002             }
01003             if (theClimateVector[myInt] > FREEZING_POINT)
01004                 myMonthCountInt += 1;
01005         }
01006     }
01007     catch (...)
01008     {
01009 #ifdef QGISDEBUG
01010         std::cout << "A fatal error occured in the numberOfMonthsAboveZero function" << std::endl;
01011 #endif
01012         myMonthCountInt = static_cast<int>(mNoData);
01013     }
01014     return myMonthCountInt;
01015 }
01016 

Generated on Mon Apr 28 15:08:06 2008 for openModellerDesktop by  doxygen 1.4.1-20050210