main.cpp

00001 /*
00002 *  main.cpp
00003 *  ReGen 1.0.1
00004 *
00005 *  Created by Martin Köchy on Thu Feb 06 2003.
00006 *
00007 */
00008 
00059 #include <stdlib.h>
00060 #include <fstream>
00061 #include <iostream>
00062 #include <sstream>
00063 #include <stack>
00064 #include "ReGen.h"
00065 
00066 
00067 int main (int argc, char * const argv[]) {
00068   
00069   /*********** declarations ************************/
00070   
00071   int nP = 0; // number of supplied parameters
00072   int y; // length of time series, years
00073   
00074   ReGen* pTS; // pointer to data base of time series
00075   
00076   char answer = '?';
00077   
00078   std::string RFileName("ReGen time series.txt");
00079   std::string PList; 
00080   
00081   std::deque <float> manualP;
00082   RAINPARAMETERS* pP;
00083   
00084   do
00085   {
00086         /*********** set parameters ************************/
00087         if (argc==1)
00088         { 
00089           // ask for values
00090           std::cout << "This program generates stochastic time series of precipitation." << std::endl;
00091           std::cout << "It first determines the seasonal probability that a day is a rainy day." << std::endl;
00092           std::cout << "Then it determines the volume for that day using a negative exponential" << std::endl;
00093           std::cout << "distribution whose mean varies seasonally." << std::endl;
00094           std::cout << "Both seasonal functions have a Gaussian peak shape with three parameters." << std::endl;
00095           std::cout << "The location and width should be in month units starting with August = 0." << std::endl;
00096           std::cout << "These parameters can be supplied directly or the program can calculate" << std::endl;
00097           std::cout << "them if the mean annual precipitation is supplied." << std::endl;
00098           std::cout << "The parameters can be entered interactively after the start of the" << std::endl;
00099           std::cout << "program or added as a space-delimited list after the program" << std::endl;
00100           std::cout << "name when it is launched from the command line.\n" << std::endl;
00101           std::cout << "Parameters must be supplied in the following order:" << std::endl;
00102           std::cout << "1. Number of years (365 d) in the time series" << std::endl;
00103           std::cout << "2. rainy day probability" << std::endl;
00104           std::cout << "2.1. amplitude" << std::endl;
00105           std::cout << "2.2. location of maximum" << std::endl;
00106           std::cout << "2.3. width" << std::endl;
00107           
00108           std::cout << "3. rainy day volume" << std::endl;
00109           std::cout << "3.1. amplitude" << std::endl;
00110           std::cout << "3.2. location of maximum" << std::endl;
00111           std::cout << "3.3. width" << std::endl;
00112           std::cout << "4. optionally: a factor [-0.3,+0.3] to change the amplitude\n while maintaining the annual volume\n" << std::endl;
00113           std::cout << "Alternatively, the number of years, mean annual precipitation [90, 900], \nand the optional factor can be supplied.\n\n" << std::endl;
00114           
00115           float parameter = 0;
00116           char input [60];
00117           std::cout << "Enter parameters:" << std::endl;
00118           std::cin.getline(input,60);
00119           istringstream sPList(input);
00120           while (sPList >> parameter) // input funktioniert nicht!
00121                 manualP.push_back(parameter);
00122           nP=manualP.size()-1;
00123           
00124           /*    std::cout << "For further details about how the time series are generated and how" << std::endl;
00125           std::cout << "internal parameters have been derived refer to the documentation" << std::endl;
00126           std::cout << "or http://martinkoechy.de/research/software/ ." << std::endl;
00127           */
00128         }
00129         else // if no interactive input read from command line arguments
00130         {
00131           for(int s=1; s<argc; s++)
00132                 manualP.push_back(atof(argv[s]));
00133           nP = argc-1;
00134         }
00135         
00136         y = int(manualP[0]);
00137         
00138         if (nP==1)
00139         {
00140           // calculate parameters, standard scenario
00141           pP = new RAINPARAMETERS (int(manualP[1]));
00142         }
00143         else if (nP==2)
00144         {
00145           // calculate parameters, apply change to daily pattern
00146           pP = new RAINPARAMETERS (int(manualP[1]), manualP[2]);
00147         }
00148         else if (nP == 6)
00149         {
00150           // parameters provided, standard scenario
00151           pP = new RAINPARAMETERS (manualP[1], manualP[2], manualP[3], manualP[4], manualP[5], manualP[6]);
00152         }
00153         else if (nP == 7)
00154         {
00155           // parameters provided, apply change to daily pattern
00156           pP = new RAINPARAMETERS (manualP[1], manualP[2], manualP[3], manualP[4], manualP[5], manualP[6], manualP[7]);
00157         }
00158         else
00159         {
00160           if (argc < 2)
00161           { 
00162                 // error message
00163                 std::cerr << "Error: You supplied the wrong number of arguments." << std::endl;
00164                 std::cerr << "Either supply the mean annual precipitation and optionally the" << std::endl;
00165                 std::cerr << "fractional change to the amplitdue of daily rain volume," << std::endl;
00166                 std::cerr << "or all six parameters and optionally the fractional change.\n" << std::endl;
00167                 do
00168                 {
00169                   std::cerr << "Quit [Q] or enter manually [M]?" << std::endl;
00170                   std::cin >> answer;
00171                 }
00172                 while (!(answer == 'Q' || answer == 'M' || answer == 'q' || answer == 'm'));
00173                 if (answer=='q' || answer=='Q') exit(argc);
00174           }
00175         }
00176   } while (answer=='m');
00177   
00178   
00179   /*********** calculations ************************/
00180   
00181   pTS = new ReGen(pP);
00182   
00183   /*********** write output ************************/
00184   
00185   std::ofstream RainDocu;
00186   RainDocu.open(RFileName.c_str(), std::ios::app);
00187   RainDocu << "yr\tday\train" << std::endl;
00188   
00189   for (int j = 0; j<y; j++)
00190   {
00191         pTS->drawYear();
00192         for (int t=0; t<days_in_year; t++)
00193         {
00194           RainDocu << j << "\t" << t << "\t" << pTS->getRain(t) << std::endl;
00195         }
00196   }
00197   
00198   RainDocu.close();
00199   delete(pTS);
00200   delete (pP);
00201   return 0;
00202 }

Generated on Tue May 1 23:39:16 2007 for ReGen by  doxygen 1.5.1