Esempio n. 1
0
        static void Main(string[] args)
        {
            DateTime start, end;
             FilePath rootResultsPath;
             FileName hdfResultsFile;
             List<FilePath> timeseriesOutputPath = new List<FilePath>();
             FileName exporterEXE;
             FilePath exporterWorkingPath;
             bool joinTimeseries = false;
             bool useDateOnTimeseriesName = true;
             string folderFormat, startFormat, endFormat;
             StringBuilder exporterCFGBase = new StringBuilder();
             StringBuilder exporterCFG = new StringBuilder();
             List<ConfigNode> nodes;
             double runLenght;
             bool verbose;
             List<FileInfo> tsFileInfoList = new List<FileInfo>();
             Dictionary<string, TimeSeries> outTS = new Dictionary<string,TimeSeries>();
             TimeSeries newTS = new TimeSeries();
             TextFile txtFile, errors;
             int timeseriesSaveCounter = 1, saveCounter;
             int timeseriesNameCounter;
             errors = new TextFile("errors.log");
             errors.OpenNewToWrite();
             verbose = true;
             FilePath path;
             bool dateAndTimeInOutputFolder;
             string outputFolderFormat = "", outputFolderStartFormat = "", outputFolderEndFormat = "", outputPath, outputPath2;
             bool useCounter;

             try
             {
            CmdArgs cmdArgs = new CmdArgs(args);

            if (cmdArgs.HasOption("v"))
               verbose = true;
            else
               verbose = false;

            if (cmdArgs.HasParameter("cfg"))
            {
               if (verbose)
                  Console.Write("Reading configuration file...");

               Config conf = new Config(cmdArgs.Parameters["cfg"]);
               conf.Load();

               if (verbose)
                  Console.WriteLine("[OK]");

               if (verbose)
                  Console.Write("Looking for 'timeseries.to.extract' blocks...");

               List<ConfigNode> tsList = conf.Root.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "timeseries.to.extract"; });

               if (tsList == null || tsList.Count <= 0)
                  throw new Exception("No 'timeseries.to.extract' block found in configuration file.");

               if (verbose)
               {
                  Console.WriteLine("[OK]");
                  Console.WriteLine("{0} 'timeseries.to.extract' block(s) found.", tsList.Count);
               }

               dateAndTimeInOutputFolder = conf.Root["user.def.output.folder", false].AsBool();
               if (dateAndTimeInOutputFolder)
               {
                   outputFolderFormat = conf.Root["user.def.output.folder.format", "start-end"].AsString();
                   outputFolderStartFormat = conf.Root["user.def.output.start.format", "yyyyMMddHH"].AsString();
                   outputFolderEndFormat = conf.Root["user.def.output.end.format", "yyyyMMddHH"].AsString();
               }

               int tscCount = 1;
               foreach (ConfigNode tsc in tsList)
               {
                  timeseriesNameCounter = 1;

                  if (verbose)
                     Console.Write("Processing 'timeseries.to.extract' block {0}...", tscCount);

                  start                   = tsc["start"].AsDateTime();
                  end                     = tsc["end"].AsDateTime();
                  rootResultsPath         = tsc["root.results.path", @".\"].AsFilePath();
                  hdfResultsFile          = tsc["hdf.results.file"].AsFileName();
                  if (tsc.Contains("backup_path"))
                  {
                      path = tsc["backup_path"].AsFilePath();
                      timeseriesOutputPath.Add(path);
                  }
                  if (tsc.Contains("storage_path"))
                  {
                      path = tsc["storage_path"].AsFilePath();
                      timeseriesOutputPath.Add(path);
                  }
                  if (tsc.Contains("publish_path"))
                  {
                      path = tsc["publish_path"].AsFilePath();
                      timeseriesOutputPath.Add(path);
                  }

                  if (timeseriesOutputPath.Count <= 0)
                  {
                      throw new Exception("An error was found in the configuration file. Missing keyword." + "At least ONE of the following keywords must be provided:" +
                                          "BACKUP_PATH, STORAGE_PATH, PUBLISH_PATH");
                  }
                  exporterEXE             = tsc["exporter.exe"].AsFileName();
                  exporterWorkingPath     = tsc["exporter.working.path"].AsFilePath();
                  joinTimeseries          = tsc["join.timeseries", true].AsBool();
                  if (joinTimeseries && dateAndTimeInOutputFolder)
                  {
                      throw new Exception("join.timeseries can't be used with user.def.output.folder");
                  }
                  folderFormat            = tsc["folder.format", "start-end"].AsString();
                  startFormat             = tsc["start.format", "yyyyMMddHH"].AsString();
                  endFormat               = tsc["end.format", "yyyyMMddHH"].AsString();
                  runLenght               = tsc["run.lenght"].AsDouble();
                  useCounter              = tsc["use.counter.on.names", false].AsBool();

                  if (joinTimeseries)
                  {
                     outTS = new Dictionary<string, TimeSeries>();
                     newTS = new TimeSeries();

                     timeseriesSaveCounter = tsc["save.counter"].AsInt();
                  }
                  else
                  {
                     useDateOnTimeseriesName = tsc["use.date.on.timeseries.name", true].AsBool();
                  }

                  exporterCFGBase.Append("EXPORT_TYPE      : "); exporterCFGBase.AppendLine(tsc["export.type", 1].AsString());
                  exporterCFGBase.Append("COMPUTE_RESIDUAL : "); exporterCFGBase.AppendLine(tsc["compute.residual", 0].AsString());
                  exporterCFGBase.Append("VARIABLE_GRID    : "); exporterCFGBase.AppendLine(tsc["variable.grid", 0].AsString());
                  exporterCFGBase.Append("WATERPOINTS_NAME : "); exporterCFGBase.AppendLine(tsc["points.name", "WaterPoints2D"].AsString());
                  exporterCFGBase.Append("GRID_FILENAME    : "); exporterCFGBase.AppendLine(tsc["grid.file.name", "grid.dat"].AsString());

                  nodes = tsc.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "timeseries"; });

                  if (nodes == null || nodes.Count <= 0)
                     throw new Exception("No 'timeseries' block found in configuration file.");

                  foreach (ConfigNode param in nodes)
                  {
                     exporterCFGBase.AppendLine();
                     exporterCFGBase.AppendLine("<BeginTimeSerie>");
                     exporterCFGBase.Append("  NAME            : "); exporterCFGBase.AppendLine(param["name"].AsString());

                     if (param.NodeData.ContainsKey("coord.y"))
                     {
                        exporterCFGBase.Append("  COORD_Y        : ");
                        exporterCFGBase.AppendLine(param["coord.y"].AsString());
                     }
                     if (param.NodeData.ContainsKey("coord.x"))
                     {
                        exporterCFGBase.Append("  COORD_X        : ");
                        exporterCFGBase.AppendLine(param["coord.x"].AsString());
                     }
                     if (param.NodeData.ContainsKey("depth.level"))
                     {
                        exporterCFGBase.Append("  DEPTH_LEVEL    : ");
                        exporterCFGBase.AppendLine(param["depth.level"].AsString());
                     }
                     if (param.NodeData.ContainsKey("localization.i"))
                     {
                        exporterCFGBase.Append("  LOCALIZATION_I : ");
                        exporterCFGBase.AppendLine(param["localization.i"].AsString());
                     }
                     if (param.NodeData.ContainsKey("localization.j"))
                     {
                        exporterCFGBase.Append("  LOCALIZATION_J : ");
                        exporterCFGBase.AppendLine(param["localization.j"].AsString());
                     }
                     if (param.NodeData.ContainsKey("localization.k"))
                     {
                        exporterCFGBase.Append("  LOCALIZATION_K : ");
                        exporterCFGBase.AppendLine(param["localization.k"].AsString());
                     }
                     if (param.NodeData.ContainsKey("latitude"))
                     {
                        exporterCFGBase.Append("  LATITUDE       : ");
                        exporterCFGBase.AppendLine(param["latitude"].AsString());
                     }
                     if (param.NodeData.ContainsKey("longitude"))
                     {
                        exporterCFGBase.Append("  LONGITUDE      : ");
                        exporterCFGBase.AppendLine(param["longitude"].AsString());
                     }

                     exporterCFGBase.AppendLine("<EndTimeSerie>");

                     if (joinTimeseries)
                     {
                        outTS[param["name"].AsString()] = new TimeSeries();
                     }
                  }

                  nodes = tsc.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "parameter"; });

                  if (nodes == null || nodes.Count <= 0)
                     throw new Exception("No 'parameter' block found in configuration file.");

                  foreach (ConfigNode param in nodes)
                  {
                     exporterCFGBase.AppendLine();
                     exporterCFGBase.AppendLine("<BeginParameter>");
                     exporterCFGBase.Append("  HDF_GROUP : "); exporterCFGBase.AppendLine(param["hdf.group"].AsString());
                     exporterCFGBase.Append("  PROPERTY  : "); exporterCFGBase.AppendLine(param["hdf.property"].AsString());
                     exporterCFGBase.AppendLine("<EndParameter>");
                  }

                  txtFile = new TextFile(exporterWorkingPath.Path + "nomfich.dat");
                  txtFile.OpenNewToWrite();
                  txtFile.WriteLine("IN_MODEL : hdfexporter.dat");
                  txtFile.Close();

                  ExternalApp hdf5exporter = new ExternalApp();
                  hdf5exporter.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
                  hdf5exporter.TextToCheck = "Program HDF5Exporter successfully terminated";
                  hdf5exporter.Wait = true;
                  hdf5exporter.WorkingDirectory = exporterWorkingPath.Path;
                  hdf5exporter.Executable = exporterEXE.FullPath;

                  bool fail;
                  DateTime actual = start;
                  FilePath hdfResultsPath = new FilePath();
                  saveCounter = 1;
                  while (actual.AddDays(runLenght) <= end)
                  {
                     fail = false;

                     hdfResultsPath.Path = rootResultsPath.Path +
                           (folderFormat.Replace("start", actual.ToString(startFormat)).Replace("end", actual.AddDays(runLenght).ToString(endFormat)));

                     if (System.IO.Directory.Exists(hdfResultsPath.Path))
                     {

                        exporterCFG.Clear();
                        exporterCFG.Append("START_TIME : "); exporterCFG.AppendLine(actual.ToString("yyyy M d H m s"));
                        exporterCFG.Append("END_TIME : "); exporterCFG.AppendLine(actual.AddDays(runLenght).ToString("yyyy M d H m s"));
                        exporterCFG.AppendLine();
                        exporterCFG.AppendLine("<BeginHDF5File>");
                        exporterCFG.Append("  NAME : "); exporterCFG.AppendLine(hdfResultsPath.Path + hdfResultsFile.FullName);
                        exporterCFG.AppendLine("<EndHDF5File>");

                        txtFile.File.FullPath = exporterWorkingPath.Path + "hdfexporter.dat";
                        txtFile.OpenNewToWrite();
                        txtFile.Write(exporterCFGBase.ToString());
                        txtFile.Write(exporterCFG.ToString());
                        txtFile.Close();

                        try
                        {
                           bool res = hdf5exporter.Run();
                           if (!res)
                           {
                              throw new Exception("Unsuccessfull HDF5Exporter run");
                           }
                        }
                        catch (Exception e_run)
                        {
                           errors.WriteLine("[" + DateTime.Now.ToString() + "] HDF5Exporter Run Exception when processing '" + hdfResultsFile + "' on results folder '" + hdfResultsPath.Path + "'");
                           errors.WriteLine("[" + DateTime.Now.ToString() + "] The exception returned this message: " + e_run.Message);
                           fail = true;
                        }

                        if (!fail)
                        {
                           FileTools.FindFiles(ref tsFileInfoList, exporterWorkingPath, "*.ets", true, "", System.IO.SearchOption.TopDirectoryOnly);

                           if (joinTimeseries)
                           {
                              foreach (FileInfo file in tsFileInfoList)
                              {
                                 if (outTS[file.FileName.Name].NumberOfInstants > 0)
                                 {
                                    saveCounter++;

                                    try
                                    {
                                       newTS.Load(file.FileName);
                                       outTS[file.FileName.Name].AddTimeSeries(newTS);
                                    }
                                    catch
                                    {
                                       errors.WriteLine("[" + DateTime.Now.ToString() + "] Was not possible to read timeseries '" + file.FileName.FullName + "' from HDF file '" + hdfResultsFile + "' on results folder '" + hdfResultsPath.Path + "'");
                                    }

                                    System.IO.File.Delete(file.FileName.FullPath);

                                    if (saveCounter > timeseriesSaveCounter)
                                    {
                                       try
                                       {
                                           if (dateAndTimeInOutputFolder)
                                           {
                                               outputPath = timeseriesOutputPath[0].Path +
                                                   (outputFolderFormat.Replace("start", actual.ToString(outputFolderStartFormat)).Replace("end", actual.AddDays(runLenght).ToString(outputFolderEndFormat))) + System.IO.Path.DirectorySeparatorChar;
                                           }
                                           else
                                           {
                                               outputPath = timeseriesOutputPath[0].Path;
                                           }
                                           if (!System.IO.Directory.Exists(outputPath))
                                               System.IO.Directory.CreateDirectory(outputPath);

                                          outTS[file.FileName.Name].Save(new FileName(outputPath + file.FileName.FullName));
                                          saveCounter = 1;
                                          if (timeseriesOutputPath.Count > 1)
                                          {
                                              for (int i = 1; i < timeseriesOutputPath.Count; i++)
                                              {
                                                  outputPath2 = timeseriesOutputPath[i].Path +
                                                   (outputFolderFormat.Replace("start", actual.ToString(outputFolderStartFormat)).Replace("end", actual.AddDays(runLenght).ToString(outputFolderEndFormat))) + System.IO.Path.DirectorySeparatorChar;
                                                  if (!System.IO.Directory.Exists(outputPath2))
                                                      System.IO.Directory.CreateDirectory(outputPath2);
                                                  FileTools.CopyFile(new FileName(outputPath + file.FileName.FullName),
                                                                     new FileName(outputPath2 + file.FileName.FullName), CopyOptions.OVERWRIGHT);
                                              }
                                          }

                                       }
                                       catch (Exception ex)
                                       {
                                          errors.WriteLine("[" + DateTime.Now.ToString() + "] Was not possible to save joined timeseries '" + file.FileName.FullName + "' from HDF file '" + hdfResultsFile + "' when processing results folder '" + hdfResultsPath.Path + "'");
                                          errors.WriteLine("[" + DateTime.Now.ToString() + "] The exception returned this message: " + ex.Message);
                                       }
                                    }
                                 }
                                 else
                                 {
                                    outTS[file.FileName.Name].Load(file.FileName);
                                    System.IO.File.Delete(file.FileName.FullPath);
                                 }
                              }
                           }
                           else
                           {
                              FileName timeseriesTarget = new FileName();

                              foreach (FileInfo file in tsFileInfoList)
                              {
                                  if (dateAndTimeInOutputFolder)
                                  {
                                      outputPath = timeseriesOutputPath[0].Path +
                                          (outputFolderFormat.Replace("start", actual.ToString(outputFolderStartFormat)).Replace("end", actual.AddDays(runLenght).ToString(outputFolderEndFormat))) + System.IO.Path.DirectorySeparatorChar;
                                  }
                                  else
                                  {
                                      outputPath = timeseriesOutputPath[0].Path;
                                  }
                                  timeseriesTarget.Path = outputPath;
                                  if (!System.IO.Directory.Exists(outputPath))
                                      System.IO.Directory.CreateDirectory(outputPath);
                                  if (useDateOnTimeseriesName)
                                 {

                                    timeseriesTarget.FullName = file.FileName.FullName.Insert(file.FileName.FullName.LastIndexOf('.'), actual.ToString(startFormat) + "_" + actual.AddDays(runLenght).ToString(endFormat));
                                 }
                                  else if (useCounter)
                                  {
                                      timeseriesTarget.FullName = file.FileName.FullName.Insert(file.FileName.FullName.LastIndexOf('.'), timeseriesNameCounter.ToString());
                                      timeseriesNameCounter++;
                                  }
                                  else
                                  {
                                      timeseriesTarget.FullName = file.FileName.FullName;
                                  }
                                 FileTools.CopyFile(file.FileName, timeseriesTarget, CopyOptions.OVERWRIGHT);
                                 if (timeseriesOutputPath.Count > 1)
                                 {
                                     for (int i = 1; i < timeseriesOutputPath.Count; i++)
                                     {
                                         outputPath2 = timeseriesOutputPath[i].Path +
                                          (outputFolderFormat.Replace("start", actual.ToString(outputFolderStartFormat)).Replace("end", actual.AddDays(runLenght).ToString(outputFolderEndFormat))) + System.IO.Path.DirectorySeparatorChar;
                                         if (!System.IO.Directory.Exists(outputPath2))
                                             System.IO.Directory.CreateDirectory(outputPath2);
                                         FileTools.CopyFile(new FileName(outputPath + file.FileName.FullName),
                                                            new FileName(outputPath2 + file.FileName.FullName), CopyOptions.OVERWRIGHT);
                                     }
                                 }
                              }
                           }
                        }
                     }

                     actual = actual.AddDays(runLenght);
                  }

                  tscCount++;

                  if (joinTimeseries)
                  {
                     foreach (KeyValuePair<string, TimeSeries> pair in outTS)
                     {
                         if (dateAndTimeInOutputFolder)
                         {
                             outputPath = timeseriesOutputPath[0].Path +
                                 (outputFolderFormat.Replace("start", actual.ToString(outputFolderStartFormat)).Replace("end", actual.AddDays(runLenght).ToString(outputFolderEndFormat))) + System.IO.Path.DirectorySeparatorChar;
                         }
                         else
                         {
                             outputPath = timeseriesOutputPath[0].Path;
                         }
                         if (!System.IO.Directory.Exists(outputPath))
                             System.IO.Directory.CreateDirectory(outputPath);
                         pair.Value.Save(new FileName(outputPath + pair.Key + ".ets"));
                         for (int i = 1; i < timeseriesOutputPath.Count; i++)
                         {
                             outputPath2 = timeseriesOutputPath[i].Path +
                              (outputFolderFormat.Replace("start", actual.ToString(outputFolderStartFormat)).Replace("end", actual.AddDays(runLenght).ToString(outputFolderEndFormat))) + System.IO.Path.DirectorySeparatorChar;
                             if (!System.IO.Directory.Exists(outputPath2))
                                 System.IO.Directory.CreateDirectory(outputPath2);
                             FileTools.CopyFile(new FileName(outputPath + pair.Key + ".ets"),
                                                new FileName(outputPath2 + pair.Key + ".ets"), CopyOptions.OVERWRIGHT);
                         }
                     }
                  }

               }
            }
            Console.WriteLine("[OK]");
             }
             catch(Exception ex)
             {
            if (verbose)
            {
               Console.WriteLine("[FAIL]");
               Console.WriteLine("");
               Console.WriteLine("An EXCEPTION was raised. The message returned was:");
               Console.WriteLine(ex.Message);
            }
             }

             Console.WriteLine("GetTSFromModelResultsHDF finished.");
             errors.Close();
        }
Esempio n. 2
0
        static int Main(string[] args)
        {
            CmdArgs cmdArgs = null;
             Exception e = null;
             bool sendMail = false;
             bool sendSMS = false;
             bool verbose = true;
             IMohidSim stdScript = null;
             int return_value = 0;

             try
             {
            Setup.StandardSetup();
            cmdArgs = new CmdArgs(args);

            if (cmdArgs.HasOption("v"))
               verbose = true;
            else
               verbose = false;

            if (cmdArgs.HasParameter("m"))
               sendMail = true;

            if (cmdArgs.HasParameter("s"))
               sendSMS = true;

            if (cmdArgs.HasOption("i"))
               stdScript = (IMohidSim)new StandardScript();
             }
             catch (Exception ex)
             {
            if (verbose)
               Console.WriteLine("[{0}] Model Runner: Exception raised during initialization: {0}", DateTime.Now, ex.Message);

            return -1;
             }

             try
             {
            MohidRunEngine mre = new MohidRunEngine();
            mre.Verbose = verbose;
            mre.Run(cmdArgs, stdScript);
             }
             catch (Exception ex)
             {
            e = ex;
            return_value = -1;
             }

             if (sendMail)
             {
            try
            {
               Config cfg = new Config(cmdArgs.Parameter("m"));
               if (!cfg.Load())
               {
                  if (verbose)
                     Console.WriteLine("[{0}] Model Runner: Was not possible to load the mail configuration file '{1}'", DateTime.Now, cmdArgs.Parameter("mailcfg"));
               }
               else
               {
                  MailSender ms = new MailSender();

                  string sendTo,
                         header,
                         message;

                  if (e != null)
                  {
                     header = "[ERROR] " + cfg.Root["header", "MohidRun Report"].AsString();
                     sendTo = "sendto.onerror";
                     message = cfg.Root["message", "Mohid Run Report"].AsString() + Environment.NewLine;
                     message += "An exception happened" + e.Message + Environment.NewLine;

                     while (e != null)
                     {
                        message += "  => " + e.Message + Environment.NewLine;
                        e = e.InnerException;
                     }
                  }
                  else
                  {
                     header = "[SUCCESS] " + cfg.Root["header", "MohidRun Report"].AsString();
                     sendTo = "sendto.onsuccess";
                     message = cfg.Root["message", "Mohid Run Report"].AsString();
                  }

                  ms.SetFrom(cfg.Root["from"].AsString(), cfg.Root["display", cfg.Root["from"].AsString()].AsString());
                  ms.User = cfg.Root["user", "*****@*****.**"].AsString();
                  ms.Password = cfg.Root["pass", "MohidOperationalISTMARETEC2011"].AsString();
                  ms.SetMessage(message, header);
                  ms.Host = cfg.Root["host", "smtp.gmail.com"].AsString();
                  ms.Port = cfg.Root["port", 587].AsInt();
                  ms.EnableSSL = cfg.Root["enable.ssl", true].AsBool();

                  foreach (ConfigNode n in cfg.Root.ChildNodes.FindAll(delegate(ConfigNode node) { return (node.Name == sendTo || node.Name == "sendto"); }))
                  {
                     if (!(n["bcc", ""].AsString() == ""))
                        ms.AddBCC(n["bcc"].AsString(), n["display", n["bcc"].AsString()].AsString());
                     else if (!(n["cc", ""].AsString() == ""))
                        ms.AddCC(n["cc"].AsString(), n["display", n["cc"].AsString()].AsString());
                     else
                        ms.AddTo(n["to"].AsString(), n["display", n["to"].AsString()].AsString());
                  }

                  ms.SendMail();
               }
            }
            catch (Exception ex)
            {
               if (verbose)
               {
                  Console.WriteLine("[{0}] Model Runner: Was not possible to send the mail. An EXCEPTION happened. The message returned was:", DateTime.Now);
                  Console.WriteLine("{0}", ex.Message);
               }

               return_value -= 2;
            }
             }

             if (sendSMS)
             {
             }

             return return_value;
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            OPTamegaScript script = new OPTamegaScript();
             CmdArgs cmdArgs = null;
             Exception e = null;
             bool sendMail = false;
             bool sendSMS = false;
             bool verbose = false;

             try
             {
            Setup.StandartSetup();
            cmdArgs = new CmdArgs(args);

            if (cmdArgs.HasParameter("mailcfg"))
               sendMail = true;

            if (cmdArgs.HasParameter("smscfg"))
               sendSMS = true;

            if (cmdArgs.HasOption("verbose"))
               verbose = true;
             }
             catch (Exception ex)
             {
            Console.WriteLine("Exception raised during initialization: {0}", ex.Message);
            return;
             }

             try
             {
            if (Run(cmdArgs, verbose))
               throw new Exception("Run failed.");
             }
             catch (Exception ex)
             {
            e = ex;
             }

             if (sendMail)
             {
            try
            {
               Config cfg = new Config(cmdArgs.Parameter("mailcfg"));
               if (!cfg.Load())
               {
                  Console.WriteLine("[{0}] Was not possible to load the mail configuration file '{1}'", DateTime.Now, cmdArgs.Parameter("mailcfg"));
               }
               else
               {
                  MailSender ms = new MailSender();

                  string sendTo,
                         header,
                         message;

                  if (e != null)
                  {
                     header = "[ERROR] " + cfg.Root["header", "MohidRun Report"].AsString();
                     sendTo = "sendto.onerror";
                     message = cfg.Root["message", "Mohid Run Report"].AsString() + Environment.NewLine;
                     message += "Exception raised: " + Environment.NewLine;
                     message += e.Message;
                  }
                  else
                  {
                     header = "[SUCCESS] " + cfg.Root["header", "MohidRun Report"].AsString();
                     sendTo = "sendto.onsuccess";
                     message = cfg.Root["message", "Mohid Run Report"].AsString();
                  }

                  ms.SetFrom(cfg.Root["from"].AsString(), cfg.Root["display", cfg.Root["from"].AsString()].AsString());
                  ms.User = cfg.Root["user", "*****@*****.**"].AsString();
                  ms.Password = cfg.Root["pass", "MohidOperationalISTMARETEC2011"].AsString();
                  ms.SetMessage(message, header);
                  ms.Host = cfg.Root["host", "smtp.gmail.com"].AsString();
                  ms.Port = cfg.Root["port", 587].AsInt();
                  ms.EnableSSL = cfg.Root["enable.ssl", true].AsBool();

                  foreach (ConfigNode n in cfg.Root.ChildNodes.FindAll(delegate(ConfigNode node) { return (node.Name == sendTo || node.Name == "sendto"); }))
                  {
                     if (!(n["bcc", ""].AsString() == ""))
                        ms.AddBCC(n["bcc"].AsString(), n["display", n["bcc"].AsString()].AsString());
                     else if (!(n["cc", ""].AsString() == ""))
                        ms.AddCC(n["cc"].AsString(), n["display", n["cc"].AsString()].AsString());
                     else
                        ms.AddTo(n["to"].AsString(), n["display", n["to"].AsString()].AsString());
                  }

                  ms.SendMail();
               }
            }
            catch (Exception ex)
            {
               Console.WriteLine("[{0}] Was not possible to send the mail. An EXCEPTION happened. The message returned was:", DateTime.Now);
               Console.WriteLine("{0}", ex.Message);
            }
             }

             if (sendSMS)
             {
             }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Database db = null;
             string query;
             OdbcDataReader dbReader;
             int interval_seconds_guess;
             string dateFormat;
             string server = "http://www.meteoagri.com/";

             Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

             try
             {
            db = new Database();
            CmdArgs cmdArgs = new CmdArgs(args);
            List<DataToRead> list = new List<DataToRead>();
            int defaultDaysToDownload;

            string user, pass;

            bool save_ts = cmdArgs.HasOption("save_ts");
            bool save_db = cmdArgs.HasOption("save_db");

            if (!cmdArgs.HasParameter("user"))
               throw new Exception("Must provide the user. Uses '--user user_name'.");

            if (!cmdArgs.HasParameter("pass"))
               throw new Exception("Must provide the pass. Uses '--pass password'.");

            user = cmdArgs.Parameters["user"];
            pass = cmdArgs.Parameters["pass"];

            if (cmdArgs.HasParameter("cfg"))
            {
               Config conf = new Config(cmdArgs.Parameters["cfg"]);
               conf.Load();

               if (save_db)
               {
                  ConfigNode dbConf = conf.Root.ChildNodes.Find(delegate(ConfigNode node) { return node.Name == "database.config"; });
                  if (dbConf == null)
                     throw new Exception("No 'database.config' block was found.");

                  if (db.Connect(dbConf["db.conn.string"].AsString()) != Result.TRUE)
                     throw new Exception("Connection to database was not possible. The message returned was: " + db.RaisedException.Message);
               }

               server = conf.Root["server", "http://www.meteoagri.com/"].AsString();
               defaultDaysToDownload = conf.Root["days.to.download", 14].AsInt();
               dateFormat = conf.Root["date.format", "#yyyy-MM-dd HH:mm:ss#"].AsString();
               interval_seconds_guess = conf.Root["interval.guess", 900].AsInt();

               List<ConfigNode> nodeList = conf.Root.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "data.to.read"; });
               if (nodeList == null)
                  throw new Exception("No blocks 'nodes.to.read' were found.");

               DataToRead item;
               NodeData nd;
               int blockNumber = 1;
               foreach (ConfigNode node in nodeList)
               {
                  item = new DataToRead();
                  list.Add(item);

                  item.TimeSeriesFileName = node["time.series.file.name", ""].AsString();
                  item.TableName    = node["table.name"].AsString();
                  item.DBDateColumn = node["db.date.column"].AsString();

                  List<ConfigNode> cfList = node.ChildNodes.FindAll(delegate(ConfigNode n) { return n.Name == "node"; });
                  if (cfList == null)
                     throw new Exception("No blocks 'node' were found on block number " + blockNumber.ToString());

                  foreach (ConfigNode cn in cfList)
                  {
                     nd = new NodeData();
                     nd.DefaultValue = cn["default.value", ""].AsString();
                     nd.DBColumn = cn["column"].AsString();
                     nd.TSHeader = cn["ts.header", nd.DBColumn].AsString();
                     nd.NodeID   = cn["node.id"].AsString();
                     item.NodesList.Add(nd);
                  }

                  if (save_db)
                  {
                     query = string.Format("SELECT {0} FROM {1} ORDER BY {0} DESC",
                                           item.DBDateColumn, item.TableName);

                     dbReader = db.ExecuteQuerySingleRow(query);

                     item.Start = DateTime.Now.AddDays(-defaultDaysToDownload);

                     if (dbReader.HasRows)
                     {
                        while (dbReader.Read())
                           item.Start = dbReader.GetDateTime(dbReader.GetOrdinal(item.DBDateColumn)).AddDays(1);

                        if (!dbReader.IsClosed)
                           dbReader.Close();
                     }
                  }
                  else
                  {
                     item.Start = DateTime.Now.AddDays(-defaultDaysToDownload);
                  }
                  blockNumber++;
               }
            }
            else
               throw new Exception("No valid configuration file provided. use 'cfg' parameter.");

            MeteoAgriEngine maEng = new MeteoAgriEngine(server);
            maEng.TimeOut = 360;

            Console.Write("Connecting to MeteoAgri...");
            if (maEng.Login(user, pass))
            {
               Console.WriteLine("[OK]");

               int count = 1;
               int nItems = 0;
               Console.WriteLine("DATA READ/WRITE in progress.");
               foreach (DataToRead dr in list)
               {
                  TimeSeries ts = new TimeSeries();
                  ts.StartInstant = dr.Start;
                  ts.TimeUnits = TimeUnits.SECONDS;

                  foreach (NodeData nd in dr.NodesList)
                  {
                     Column col = new Column(typeof(string), 0, nd.TSHeader);
                     col.DefaultValue = "0";
                     ts.AddColumn(col);
                  }

                  //First, find how many "slots" will be downloaded

                  int slots = (int)(DateTime.Now - dr.Start).TotalSeconds / interval_seconds_guess;
                  if (slots <= 0)
                  {
                     count++;
                     continue;
                     //throw new Exception("Start date for 'data.to.read' number " + count.ToString() + " (" + dr.Start.ToString("yyyy-MM-dd") + ") is same than today's date");
                  }

                  nItems = 0;
                  int column = -1;
                  foreach (NodeData nd in dr.NodesList)
                  {
                     column++;
                     Console.WriteLine("Reading from ARBVS server the node {0}, id = {1}...", nd.DBColumn, nd.NodeID);
                     maEng.GetData(nd.NodeID, dr.Start, slots, ts, column, dateFormat);

                     //if (nd.Data == null)
                     //{
                     //   Console.WriteLine("[NO DATA]");
                     //   //throw new Exception(maEng.ExceptionMessage);
                     //}
                     //else if (nd.Data.Count < 1)
                     //{
                     //   Console.WriteLine("[NO DATA]");
                     //   //throw new Exception("ARBVS server returned an empty node");
                     //}
                     //else
                     //{
                     //   if (nItems < 1)
                     //      nItems = nd.Data[0].Data.Count;
                     //   else if (nItems != nd.Data[0].Data.Count)
                     //   {
                     //      Console.WriteLine("[ERROR]");
                     //      throw new Exception("ARBVS server returned a different number of items than the previous node");
                     //   }

                     //   Console.WriteLine("[OK]");

                     //   //Console.WriteLine("");
                     //   //Console.WriteLine("The data returned was:");
                     //   //foreach (MeteoAgriNode node in nodes)
                     //   //{
                     //   //   Console.WriteLine("NODE : {0}", node.NodeId);
                     //   //   foreach (MeteoAgriData data in node.Data)
                     //   //      Console.WriteLine(string.Format("    type:'{0}' d:'{1}' s:'{2}' t:'{3}' value:'{4}'",
                     //   //                                      data.Type, data.D, data.S, data.T, data.Value));
                     //   //   Console.WriteLine("------------");
                     //   //}
                     //}
                  }

                  string fields, data;
                  //if (nItems > 0)
                  //{
                     //DateTime start = DateTime.Now;
                     //bool start_defined = false;
                  if (save_db)
                  {
                     Console.Write("Writing info on database...");
                     for (int i = 0; i < ts.NumberOfInstants; i++)
                     {
                        fields = "Instant";
                        data = ts.InstantAsDateTime(i).ToString(dateFormat);

                        //if (dr.NodesList[0].Data[0].Data[i].D == "-1")
                        //{
                        //   start = DateTime.ParseExact(dr.NodesList[0].Data[0].Data[i].T, "yyyyMMddTHH:mm:ss", null);
                        //   data = start.ToString(dateFormat);
                        //   start_defined = true;
                        //}
                        //else
                        //{
                        //   if (!start_defined)
                        //      throw new Exception("The start instant was not found in the imported data.");
                        //   start = start.AddSeconds(double.Parse(dr.NodesList[0].Data[0].Data[i].T));
                        //   data = start.ToString(dateFormat);
                        //}

                        int column_index = -1;
                        foreach (NodeData nd in dr.NodesList)
                        {
                           column_index++;
                           fields += ", " + nd.DBColumn;
                           data += ", " + (ts[column_index, i] as string);
                        }

                        query = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", dr.TableName, fields, data);
                        //if (db.ExecuteCommand(query) != 1)
                        db.ExecuteCommand(query);
                     }
                     Console.WriteLine("[OK][{0} items inserted]", ts.NumberOfInstants.ToString());
                     //}
                  }

                  if (save_ts)
                  {
                      if (ts.NumberOfInstants > 1)
                          ts.Save(new FileName(dr.TimeSeriesFileName));
                      else
                          Console.WriteLine("No data was available for {0}", dr.TimeSeriesFileName);
                  }
                  count++;
               }

               Console.Write("Logging out...");
               if (maEng.Logout())
                  Console.WriteLine("[OK]");
               else
               {
                  Console.WriteLine("[ERROR]");
                  Console.WriteLine("The message returned was:");
                  Console.WriteLine(maEng.ExceptionMessage);
               }

            }
            else
            {
               Console.WriteLine("[ERROR]");
               Console.WriteLine("The message returned was:");
               Console.WriteLine(maEng.ExceptionMessage);
            }

            db.Disconnect();
             }
             catch (Exception ex)
             {
            if (db != null) db.Disconnect();
            Console.WriteLine("");
            Console.WriteLine("An exception has happened. The message returned was:");
            Console.WriteLine(ex.Message);

             }

             //Console.WriteLine("");
             //Console.WriteLine("Press any key...");
             //Console.ReadKey();
        }
Esempio n. 5
0
        static int Main(string[] args)
        {
            TextFile log = null;
             CmdArgs cmd_args = null;
             Engine eng = null;
             int result = 0;
             bool debug = false;

             try
             {
            cmd_args = new CmdArgs(args);
             }
             catch (Exception ex)
             {
            Console.WriteLine("Error when trying to parse the command line arguments.");
            Console.WriteLine("The message returned was:");
            Console.WriteLine(ex.Message);
            result = -1;
             }

             if (result == 0)
            try
            {
               string log_file;
               if (cmd_args.HasParameter("log"))
               {
                  log_file = cmd_args.Parameters["log"];
               }
               else
               {
                  if (cmd_args.HasParameter("logpath"))
                     log_file = (new FilePath(cmd_args.Parameters["logpath"])).Path + "log_";
                  else
                     log_file = "log_";

                  DateTime DateNow = DateTime.Now;

                  log_file += DateNow.Year.ToString("D4") + DateNow.Month.ToString("D2") + DateNow.Day.ToString("D2") + "-" +
                              DateNow.Hour.ToString("D2") + DateNow.Minute.ToString("D2") + DateNow.Second.ToString("D2") + ".dat";

                  //Console.WriteLine("WARNING: Missing 'log' parameter.");
                  //Console.WriteLine("A log file will be created with the name:");
                  //Console.WriteLine(log_file);
               }

               log = new TextFile(log_file);
            }
            catch (Exception ex)
            {
               Console.WriteLine("Error when trying to create log file.");
               Console.WriteLine("The message returned was:");
               Console.WriteLine(ex.Message);
               result = -2;
            }

             bool verbose = false;
             if (cmd_args.HasOption("verbose")) verbose = true;

             if (cmd_args.HasOption("debug")) debug = true;

             if (result == 0)
            try
             {
               DateTime d_and_t = DateTime.Now;

               if (!cmd_args.HasParameter("cfg"))
                  throw new Exception("No configuration file was provided.");
               if (cmd_args.HasParameter("date"))
                  d_and_t = DateTime.ParseExact(cmd_args.Parameters["date"], "yyyy mm dd", CultureInfo.InvariantCulture);

               if (verbose) Console.WriteLine("Starting sms engine.");
               eng = new Engine();
               eng.Debug = debug;

               if (verbose) Console.WriteLine("Loading configuration.");
               if (!eng.LoadConfig(new Mohid.Files.FileName(cmd_args.Parameters["cfg"])))
                  throw new Exception("Error when loading the configuration file.");

               if (verbose) Console.WriteLine("Sending messages.");
               if (!eng.SendMessages(d_and_t))
                  throw new Exception("Error when trying to send the messages.");
             }
             catch (Exception ex)
             {
               Console.WriteLine("Error when trying to send messages.");
               Console.WriteLine("The exception returned was:");
               Console.WriteLine(ex.Message);
               result = -3;
             }

             if (log != null)
             {
            if (eng.HasErrors)
            {
               log.OpenNewToWrite();
               log.Write(eng.Errors);
               log.Close();
            }
             }

             return result;
        }
Esempio n. 6
0
        static int Main(string[] args)
        {
            CmdArgs cmdArgs = null;
             Exception last_exception = null;
             string task_block;

             try
             {
            Setup.StandartSetup();
            cmdArgs = new CmdArgs(args);

            //======================================================================================
            //Load configuration
            //======================================================================================
            Config cfg = new Config(cmdArgs.Parameter("cfg"));
            if (!cfg.Load())
            {
               Console.WriteLine("[{0}] Was not possible to load the configuration file '{1}'", DateTime.Now, cmdArgs.Parameter("cfg"));
               return -1;
            }

            //======================================================================================
            //Check to see if there are a specific name for the task block
            //======================================================================================
            if (cmdArgs.HasParameter("task"))
               task_block = cmdArgs.Parameter("task");
            else
               task_block = "task.config";

            //======================================================================================
            //Execute task
            //======================================================================================
            ConfigNode task_cfg = cfg.Root.ChildNodes.Find(delegate(ConfigNode node) { return node.Name == task_block; });
            if (task_cfg != null)
            {
               TaskEngine te = new TaskEngine();
               if (!te.LoadConfig(task_cfg))
               {
                  if ((last_exception = te.LastException) == null)
                     last_exception = new Exception("Unknow error during process of task configuration.");
               }
               if (!te.CreateNewHDF())
               {
                  if ((last_exception = te.LastException) == null)
                     last_exception = new Exception("Unknow error during process of task execution.");
               }
               te.End();
            }
            else
            {
               last_exception = new Exception("No task.config block found in configuration.");
            }

            if (cmdArgs.HasOption("verbose"))
            {
               if (last_exception != null)
                  Console.WriteLine("MohidHDF5Processor FAILED to complete the process.");
               else
                  Console.WriteLine("MohidHDF5Processor SUCCESSFULLY completed the process.");
            }

            //======================================================================================
            //Send STATUS e-mail if mail.config block exists
            //======================================================================================
            ConfigNode mail_cfg = cfg.Root.ChildNodes.Find(delegate(ConfigNode node) { return node.Name == "mail.config"; });
            if (mail_cfg != null)
            {
               MailEngine mail_engine = new MailEngine();

               if (!mail_engine.SendMail(mail_cfg, last_exception))
               {
                  Console.WriteLine("[{0}] Was not possible to send the status e-mail.", DateTime.Now);
                  if ((last_exception = mail_engine.LastException) != null)
                     Console.WriteLine("The message returned was: {0}", last_exception);
                  return -1;
               }
            }
            else if (last_exception != null)
            {
               throw last_exception;
            }
             }
             catch(Exception ex)
             {
            Console.WriteLine("[{0}] An unexpected exception happened. The message returned was: {1}", DateTime.Now, ex.Message);
            return -1;
             }

             return 0;

              //   HDFEngine engine = new HDFEngine();
              //   Exception last_ex = null;

              //   Console.WriteLine("Starting...");

              //   if (!engine.InitializeLibrary())
              //   {
              //      Console.WriteLine("Library start failed.");
              //      if ((last_ex = engine.LastException) != null)
              //         Console.WriteLine("Message: {0}", last_ex.Message);
              //   }

              //   if (!engine.OpenHDF(new FileName(@"E:\Development\Tests\HDF5\basin.evtp.hdf5")))
              //   {
              //      Console.WriteLine("File Open failed.");
              //      if ((last_ex = engine.LastException) != null)
              //         Console.WriteLine("Message: {0}", last_ex.Message);
              //   }

              //   Console.WriteLine("ROOT: '/'");
              //   List<HDFObjectInfo> list = engine.GetTree(null, engine.FileID, "/");
              //   PrintList(list, 0);

              //   Console.WriteLine("");

              //   Console.WriteLine("ROOT: '/Grid/'");
              //   list.Clear();
              //   list = engine.GetTree(null, engine.FileID, "/Grid/");
              //   PrintList(list, 1);

              //   if (!engine.CloseHDF())
              //   {
              //      Console.WriteLine("File Close failed.");
              //      if ((last_ex = engine.LastException) != null)
              //         Console.WriteLine("Message: {0}", last_ex.Message);
              //   }

              //   if (!engine.CloseLibrary())
              //   {
              //      Console.WriteLine("Library close failed.");
              //      if ((last_ex = engine.LastException) != null)
              //         Console.WriteLine("Message: {0}", last_ex.Message);
              //   }

              //   Console.WriteLine("End. Press any key.");
              //   Console.ReadKey();
              //}

              //public static void PrintList(List<HDFObjectInfo> list, int level)
              //{
              //   foreach (HDFObjectInfo oi in list)
              //   {
              //      Console.WriteLine("{0}: {1}", level, oi.Name);
              //      if (oi.Children != null)
              //         PrintList(oi.Children, level + 1);
              //   }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            bool verbose = false;

             try
             {

            CmdArgs cmdArgs = new CmdArgs(args);

            if (cmdArgs.HasOption("v"))
               verbose = true;
            else
               verbose = false;

            if (cmdArgs.HasParameter("cfg"))
            {
               if (verbose)
               {
                  Console.WriteLine("");
                  Console.Write("Reading configuration file...");
               }

               Config conf = new Config(cmdArgs.Parameters["cfg"]);
               conf.Load();

               if (verbose)
                  Console.WriteLine("[OK]");

               if (verbose)
                  Console.Write("Looking for 'xyz.to.process' blocks...");

               List<ConfigNode> bkList = conf.Root.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "xyz.to.process"; });

               if (bkList == null || bkList.Count <= 0)
                  throw new Exception("No 'xyz.to.process' block found in configuration file.");

               if (verbose)
               {
                  Console.WriteLine("[OK]");
                  Console.WriteLine("{0} 'xyz.to.process' block(s) found.", bkList.Count);
               }

               int bkCount = 1;
               foreach (ConfigNode bk in bkList)
               {
                  if (verbose)
                     Console.Write("Processing 'xyz.to.process' block #{0}...", bkCount);

                  FileName input = bk["input.file"].AsFileName();
                  FileName output = bk["output.file"].AsFileName();
                  bool eraseNoData = bk["erase.no.data", true].AsBool();
                  double noData = -99.0;
                  if (eraseNoData)
                     noData = bk["no.data.value", -99.0].AsDouble();
                  bool applyScaleFator = bk["apply.scale.fator", true].AsBool();
                  double scaleFactor = 1.0;
                  if (applyScaleFator)
                     scaleFactor = bk["scale.factor", 1.0].AsDouble();

                  Dictionary<string, string> lookupTable = new Dictionary<string, string>();
                  List<ConfigNode> lookupTables = bk.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "lookup.table"; });

                  foreach (ConfigNode lt in lookupTables)
                  {
                     foreach (KeyValuePair<string, KeywordData> kp in lt.NodeData)
                        lookupTable[kp.Key] = kp.Value.AsString();
                  }

                  //Start processing XYZ
                  TextFile file_i = new TextFile(input); file_i.OpenToRead();
                  TextFile file_o = new TextFile(output); file_o.OpenNewToWrite();

                  string line_i = null;
                  string line_o = null;
                  string [] tokens;
                  string [] sep = {" "};
                  double res;
                  while ((line_i = file_i.ReadLine()) != null)
                  {
                     tokens = line_i.Split(sep, StringSplitOptions.RemoveEmptyEntries);
                     if (tokens.Length == 3)
                     {
                        foreach (KeyValuePair<string, string> kp in lookupTable)
                           if (double.Parse(tokens[2]) == double.Parse(kp.Key))
                           {
                              tokens[2] = kp.Value;
                              break;
                           }

                        res = double.Parse(tokens[2]);

                        if (eraseNoData && res == noData)
                           continue;

                        if (applyScaleFator)
                           res *= scaleFactor;

                        line_o = string.Format("{0} {1} {2}  ", tokens[0], tokens[1], res);
                     }
                     else
                        line_o = line_i;

                     file_o.WriteLine(line_o);
                  }

                  file_i.Close();
                  file_o.Close();

                  bkCount++;
                  if (verbose)
                     Console.WriteLine("[OK]");
               }
            }
             }
             catch (Exception ex)
             {
            if (verbose)
            {
               Console.WriteLine("[FAIL]");
               Console.WriteLine("");
               Console.WriteLine("An EXCEPTION was raised. The message returned was:");
               Console.WriteLine(ex.Message);
            }
             }
             if (verbose)
             {
            Console.WriteLine("Process finished.");
            Console.WriteLine("");
             }
        }