Example #1
0
        static void Main()
        {
            CmdArgs cmdArgs = new CmdArgs(Environment.GetCommandLineArgs());

             if (cmdArgs.Options.Count <= 0 && cmdArgs.Parameters.Count <= 0 && cmdArgs.Arguments.Count <= 1)
             {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
            return;
             }

             AttachConsole(ATTACH_PARENT_PROCESS);

             if (cmdArgs.HasParameter("sc"))
             {
            ScriptCompiler script = new ScriptCompiler();

            if (!script.Run(new FileName(cmdArgs.Parameter("s")), cmdArgs))
               Console.WriteLine("The run failed.");
            else
               Console.WriteLine("The run was ok.");
             }
             else if (cmdArgs.HasParameter("sv"))
             {
            Console.WriteLine("Run VB.NET script is not implemented yet.");
             }
             else if (cmdArgs.HasParameter("l"))
             {
            Console.WriteLine("Run from DLL is not implemented yet.");
             }
             else
             {
            Console.WriteLine("");
            Console.WriteLine("To launch visual interface: MohidToolBox");
            Console.WriteLine("To use command line:        MohidToolbox [[--sc][--sv] scriptfilename] [--l dllfilename]");
            Console.WriteLine("");
            Console.WriteLine("       --sc : Used to indicate a CSharp script file name");
            Console.WriteLine("       --sv : Used to indicate a VB.NET script file name (not implemented)");
            Console.WriteLine("       --l  : Used to indicate a DLL script file name (not implemented)");
            Console.WriteLine("");
            Console.WriteLine("If no options are present, the visual interface will be launched.");
            Console.WriteLine("ATTENTION: Only ONE of the above options can be used at a time");
            Console.WriteLine("");
            Console.WriteLine("Press a key...");
             }

             return;
        }
Example #2
0
        static void Main(string[] args)
        {
            string stepMessage = "";
             FileName outputFileName;

             try
             {
            stepMessage = "command line arguments loading.";
            CmdArgs cmdArgs = new CmdArgs(args);

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

               stepMessage = "configuration file parsing.";
               ConfigNode nodeList = conf.Root.ChildNodes.Find(delegate(ConfigNode node){ return node.Name == "timeseries.to.join"; });
               if (nodeList == null)
                  throw new Exception("Block 'timeseries.to.join' is missing.");
               outputFileName = conf.Root["output.file.name", "output.tsr"].AsFileName();

               TimeUnits timeUnits = (TimeUnits) Enum.Parse(typeof(TimeUnits), conf.Root["time.units", "seconds"].AsString(), true);

               List<FileName> list = new List<FileName>();
               foreach (KeyValuePair<string, KeywordData> item in nodeList.NodeData)
                  list.Add(item.Value.AsFileName());

               if (list.Count <= 1)
                  throw new Exception("Block 'timeseries.to.join' must contain at least 2 entries");

               stepMessage = "loading timeseries.";
               List<TimeSeries> timeSeries = new List<TimeSeries>();

               foreach(FileName ts in list)
               {
                  TimeSeries newTS = new TimeSeries();
                  newTS.Load(ts);
                  timeSeries.Add(newTS);
               }

               DateTime start = timeSeries[0].StartInstant;
               for (int i = 1; i < timeSeries.Count; i++)
               {
                  if (timeSeries[i].StartInstant < start)
                     start = timeSeries[i].StartInstant;
               }

               stepMessage = "creating output timeseries.";
               TimeSeries outTS = new TimeSeries();
               outTS.StartInstant = start;
               outTS.TimeUnits = timeUnits;

               foreach (Column col in timeSeries[0].Columns)
               {
                  Column newCol = new Column(col.ColumnType);
                  newCol.Header = col.Header;
                  outTS.AddColumn(newCol);
               }

               foreach (TimeSeries toJoin in timeSeries)
                  outTS.AddTimeSeries(toJoin);

               stepMessage = "saving output timeseries.";
               outTS.Save(outputFileName);

               Console.WriteLine("Process complete with success.");
            }
            else
            {
               Console.WriteLine("Parameter --cfg is missing.");
               Console.WriteLine("Execution aborted.");
               return;
            }
             }
             catch (Exception ex)
             {
            Console.WriteLine("An exception was raised while {0}", stepMessage);
            Console.WriteLine("Exception message: {0}", ex.Message);
             }
        }
Example #3
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;
        }
Example #4
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();
        }
Example #5
0
        static void Main(string[] args)
        {
            try
             {
            SMSEngine sms = new SMSEngine();
            CmdArgs cmdArgs = new CmdArgs(args);
            string number, message;

            //PORT NUMBER
            if (cmdArgs.HasParameter("port"))
            {
               sms.Port.PortName = "COM" + cmdArgs.Parameters["port"];
            }
            else
               throw new Exception("'port' parameter is missing.");

            //BAUDRATE
            if (cmdArgs.HasParameter("baudrate"))
            {
               sms.Baudrate = int.Parse(cmdArgs.Parameters["baudrate"]);
            }
            else
               throw new Exception("'baudrate' parameter is missing.");

            //SMS CENTER
            if (cmdArgs.HasParameter("sms.center"))
            {
               sms.SMSCenter = cmdArgs.Parameters["sms.center"];
            }
            else
               throw new Exception("'sms.center' parameter is missing.");

            //PIN CODE
            if (cmdArgs.HasParameter("pin"))
            {
               sms.PinCode = cmdArgs.Parameters["pin"];
            }
            else
               throw new Exception("'pin' parameter is missing.");

            //TIMEOUT
            if (cmdArgs.HasParameter("time.out"))
            {
               sms.TimeOut = int.Parse(cmdArgs.Parameters["time.out"]);
            }

            //WAIT TIME
            if (cmdArgs.HasParameter("wait.time"))
            {
               sms.WaitTime = int.Parse(cmdArgs.Parameters["wait.time"]);
            }

            //INTERVAL BETWEEN MESSAGES
            if (cmdArgs.HasParameter("interval.time"))
            {
               sms.IntervalBetweenMessages = int.Parse(cmdArgs.Parameters["interval.time"]);
            }

            //NUMBER
            if (cmdArgs.HasParameter("number") && cmdArgs.HasParameter("message"))
            {
               number = cmdArgs.Parameters["number"];
               message = cmdArgs.Parameters["message"];
               sms.SendSMS(number, message);
            }
            else
               throw new Exception("No NUMBER or MESSAGE parameter found");
             }
             catch (Exception ex)
             {
            Console.WriteLine("An EXCEPTION was raised. The message returned was:");
            Console.WriteLine(ex.Message);
             }
        }
        static void Main(string[] args)
        {
            CmdArgs cmdArgs = new CmdArgs(args);
             Dictionary<String, String> replace_list = new Dictionary<string, string>();
             DateTime end, actual_start, actual_end;

             if (!cmdArgs.HasParameter("cfg"))
             {
            Console.WriteLine("Configuration file was not provided. Use --cfg [file] to provide one.");
            Console.WriteLine("Operation Aborted.");
            return;
             }

             Config cfg = new Config(cmdArgs.Parameter("cfg"));
             if (!cfg.Load())
             {
            Console.WriteLine("Was not possible to load '" + cmdArgs.Parameter("cfg") + "'.");
            if (!string.IsNullOrWhiteSpace(cfg.ExceptionMessage))
            {
               Console.WriteLine("The message returned was:");
               Console.WriteLine(cfg.ExceptionMessage);
               Console.WriteLine("Operation Aborted.");
               return;
            }
             }

             ConfigNode root = cfg.Root;

             int id = root["start.id", 1].AsInt();
             string template = root["template", "FillMatrix.template"].AsString();
             string dateFormat = root["dateFormat.format", "yyyy-MM-dd HH:mm:ss"].AsString();
             actual_start = root["start"].AsDateTime(dateFormat);
             end = root["end"].AsDateTime(dateFormat);
             int interval;
             if (root.NodeData.ContainsKey("interval.days"))
             {
            interval = root["interval.days"].AsInt();
            actual_end = actual_start.AddDays(interval);
             }
             else
             {
            interval = 10;
            actual_end = end;
             }
             string interpolation = root["interpolation.method", "2"].AsString();

             replace_list.Add("<<start>>", actual_start.ToString("yyyy M d H m s"));
             replace_list.Add("<<end>>", actual_end.ToString("yyyy M d H m s"));
             replace_list.Add("<<id>>", id.ToString());
             replace_list.Add("<<interpolation>>", interpolation);

             ExternalApp fillmatrix = new ExternalApp();

             while (actual_end <= end)
             {

            TextFile.Replace(template, "FillMatrix.dat", ref replace_list);

            fillmatrix.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
            fillmatrix.Wait = true;
            fillmatrix.WorkingDirectory = @".\";
            fillmatrix.Executable = @".\fillmatrix.exe";

            try
            {
               fillmatrix.Run();
            }
            catch (Exception ex)
            {
               Console.WriteLine("Erro detectado. Mensagem de erro:");
               Console.WriteLine(ex.Message);
               Console.WriteLine("ID    : {0}", id.ToString());
               Console.WriteLine("Start : {0}", actual_start.ToString());
               Console.WriteLine("End   : {0}", actual_end.ToString());
               Console.WriteLine("");
            }

            id++;
            actual_start = actual_end;
            actual_end = actual_start.AddDays(14);

            if (actual_start < end && actual_end > end)
               actual_end = end;

            replace_list["<<start>>"] = actual_start.ToString("yyyy M d H m s");
            replace_list["<<end>>"] = actual_end.ToString("yyyy M d H m s");
            replace_list["<<id>>"] = id.ToString();
             }
        }
Example #7
0
        static void Main(string[] args)
        {
            Database db = null;
             string query;
             OdbcDataReader dbReader;
             string dateFormat;
             string server = "http://www.meteoagri.com/";

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

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

               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#"].AsString();

               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.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.DBColumn = cn["column"].AsString();
                     nd.NodeID = cn["node.id"].AsString();
                     item.NodesList.Add(nd);
                  }

                  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();
                  }

                  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("arbvs", "arbvs"))
            {
               Console.WriteLine("[OK]");

               int count = 1;
               int nItems = 0;
               Console.WriteLine("DATA READ/WRITE in progress.");
               foreach (DataToRead dr in list)
               {
                  //First, find how many "slots" will be downloaded

                  int slots = (DateTime.Now - dr.Start).Days;
                  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;
                  foreach (NodeData nd in dr.NodesList)
                  {
                     Console.Write("Reading from ARBVS server the node {0}, id = {1}...", nd.DBColumn, nd.NodeID);
                     nd.Data = maEng.GetData(nd.NodeID, dr.Start, slots);

                     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)
                  {
                     Console.Write("Writing info on database...");
                     for (int i = 0; i < nItems; i++)
                     {
                        fields = "Instant";
                        data = dr.Start.AddDays(i).ToString(dateFormat);
                        foreach (NodeData nd in dr.NodesList)
                        {
                           fields += ", " + nd.DBColumn;
                           data += ", " + nd.Data[0].Data[i].Value;
                        }

                        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]", nItems);
                  }

                  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();
        }
            protected IMohidSim LoadUserInterface(CmdArgs args)
            {
                try
                {
                   FileName interfaceName;
                   data.sim.PreProcessing = OnPreProcessing;

                   if (args.HasParameter("script"))
                  interfaceName = new FileName(args.Parameters["script"]);
                   else
                  return null;

                   if (interfaceName.Extension.ToLower() == "dll") //it's a library
                   {
                  if (!args.HasParameter("class"))
                     return null;

                  string class_name = args.Parameter("class");

                  Assembly ass = Assembly.LoadFrom(interfaceName.FullPath);
                  data.userInterface = (IMohidSim)Activator.CreateInstance(ass.GetType("Mohid." + class_name));
                  return data.userInterface;
                   }
                   else //it's a script
                   {
                  ScriptCompiler sc = new ScriptCompiler();
                  Assembly ass = sc.Compile(interfaceName);
                  data.userInterface = (IMohidSim)sc.FindScriptInterface("IMohidSim", ass);
                  return data.userInterface;
                   }
                }
                catch (Exception ex)
                {
                   throw new Exception("MohidRunEngine.LoadUserInterface", ex);
                }
            }
Example #9
0
        static void LoadConfig(CmdArgs args, MohidRunEngineData data)
        {
            data.cfg = new Config();
             Config cfg = data.cfg;

             string configFile;
             if (args.HasParameter("simcfg"))
            configFile = args.Parameter("simcfg");
             else
            configFile = "sim.cfg";

             cfg.ConfigFile.FullPath = configFile;
             if (!cfg.Load())
            throw new Exception("Was not possible to load the configuration file '" + configFile + "'. " + cfg.ExceptionMessage);
        }
Example #10
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();
        }
Example #11
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;
        }
Example #12
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);
              //   }
        }
            protected void LoadConfig(CmdArgs args)
            {
                try
                {
                   data.cfg = new Config();
                   Config cfg = data.cfg;

                   string configFile;
                   if (args.HasParameter("simcfg"))
                  configFile = args.Parameter("simcfg");
                   else
                  configFile = "sim.cfg";

                   if (args.HasParameter("max.iter"))
                  data.maxIterations = int.Parse(args.Parameter("max.iter"));
                   else
                  data.maxIterations = -1;

                   cfg.ConfigFile.FullPath = configFile;
                   if (!cfg.Load())
                  throw new Exception("Was not possible to load the configuration file '" + configFile + "'. " + cfg.ExceptionMessage);

                   ConfigNode root = cfg.Root;

                   data.sim.SimDirectory = root["sim.folder", "sim"].AsFilePath();
                   data.logFileName = root["log.file", data.sim.SimDirectory.Path + "sim.log"].AsFileName();

                   data.RestartFailedRun = root["restart.failed.run", true].AsBool();

                   data.sim.Start = root["sim.start"].AsDateTime(data.dateFormat);
                   data.sim.SimLenght = root["sim.lenght", 14].AsDouble();
                   data.simID = 1;

                   data.sim.CheckRun = root["check.run", true].AsBool();
                   data.sim.Verbose = root["verbose", true].AsBool();
                   data.sim.Wait = root["wait", true].AsBool();
                   data.sim.SuccessString = root["check.this", "successfully terminated"].AsString();

                   data.sim.SetupRunPeriod = root["setup.run.period", false].AsBool();

                   if (data.sim.SetupRunPeriod)
                   {
                  data.sim.EndTAG = root["sim.end.tag", "<<end>>"].AsString();
                  data.sim.StartTAG = root["sim.start.tag", "<<start>>"].AsString();
                   }

                   data.sim.DataDirectory = root["data.folder", data.sim.SimDirectory.Path + "data"].AsFilePath();
                   data.sim.WorkingDirectory = root["working.folder", data.sim.SimDirectory.Path + "exe"].AsFilePath();
                   data.resFolder = root["results.folder", data.sim.SimDirectory.Path + "res"].AsFilePath();
                   data.storeFolder = root["store.folder", data.sim.SimDirectory.Path + "store"].AsFilePath();
                   data.oldFolder = root["old.folder", data.sim.SimDirectory.Path + "old"].AsFilePath();

                   data.sim.SaveOutput = root["save.output", true].AsBool();
                   if (data.sim.SaveOutput)
                   {
                  data.sim.OutputFile = new FileName(data.resFolder.Path + root["output.file", "result.txt"].AsString());
                   }

                   data.sim.Executable = root["mohid.executable", "mohid.exe"].AsFileName();
                   data.sim.CreateInputFiles = root["use.templates", false].AsBool();
                   if (root.NodeData.ContainsKey("sim.end"))
                   {
                  data.useEndOfSimulation = true;
                  data.endOfSimulation = root["sim.end"].AsDateTime(data.dateFormat);
                   }
                   else
                  data.useEndOfSimulation = false;

                   if (!data.useEndOfSimulation && data.maxIterations < 1 && !root["infinite.run", false].AsBool())
                  throw new Exception("'sim.end' keyword and 'max.iter' parameter are missing and 'infinit.run' keyword is missing or set to False.");

                   if (data.sim.SetupRunPeriod && !data.sim.CreateInputFiles)
                  throw new Exception("If 'setup.run.period' is set to True, 'use.templates' also must be set to True.");

                   if (data.sim.CreateInputFiles)
                   {
                  InputFileTemplate newTemplate;
                  List<ConfigNode> itfList = root.ChildNodes.FindAll(FindFirstTemplateInfoBlocks);
                  foreach (ConfigNode ticn in itfList)
                  {
                     newTemplate = new InputFileTemplate(ticn["file"].AsFileName().FullPath,
                                                         (InputFileTemplateType)Enum.Parse(typeof(InputFileTemplateType), ticn["type", "data"].AsString(), true));
                     data.templatesStart.Add(newTemplate);
                  }
                  data.sim.TemplateFilesList = data.templatesStart;

                  data.changeTemplates = root["change.templates", true].AsBool();
                  if (data.changeTemplates)
                  {
                     itfList = root.ChildNodes.FindAll(FindNextTemplateInfoBlocks);
                     foreach (ConfigNode ticn in itfList)
                     {
                        newTemplate = new InputFileTemplate(ticn["file"].AsFileName().FullPath,
                                                            (InputFileTemplateType)Enum.Parse(typeof(InputFileTemplateType), ticn["type", "data"].AsString(), true));
                        data.templatesContinuation.Add(newTemplate);
                     }
                  }
                   }
                }
                catch (Exception ex)
                {
                   throw new Exception("MohidRunEngine.LoadConfig", ex);
                }
            }
        //protected bool FindTemplatesSpecialSetup(ConfigNode toMatch)
        //{
        //   if (toMatch.Name == "template.special.setup")
        //      return true;
        //   return false;
        //}
        protected bool LoadConfig(CmdArgs args)
        {
            Config cfg = new Config();

             string configFile;
             if (args.HasParameter("cfg"))
            configFile = args.Parameter("cfg");
             else
            configFile = "sim.cfg";

             if (args.HasParameter("max.iter"))
            maxIterations = int.Parse(args.Parameter("max.iter"));
             else
            maxIterations = -1;

             cfg.ConfigFile.FullPath = configFile;
             if (!cfg.Load())
            return false;

             ConfigNode root = cfg.Root;

             try
             {
            sim.SimDirectory = root["sim.folder", "sim"].AsFilePath();
            if (!log.Load(root["log.file", sim.SimDirectory.Path + "sim.log"].AsFileName()))
               return false;

            RestartFailedRun = root["restart.failed.run", true].AsBool();
            if (log.Count > 0)
            {
               ConfigNode lastEntry = log[log.Count - 1];
               if (!lastEntry["run.status"].AsBool())
               {
                  if (RestartFailedRun)
                  {
                     sim.Start = lastEntry["sim.start"].AsDateTime(dateFormat);
                     sim.SimLenght = lastEntry["sim.lenght"].AsDouble();
                     simID = lastEntry["sim.id"].AsInt();
                  }
                  else
                     return false;
               }
               else
               {
                  sim.Start = lastEntry["sim.end"].AsDateTime(dateFormat);
                  sim.SimLenght = root["sim.lenght", 14].AsDouble();
                  simID = lastEntry["sim.id"].AsInt() + 1;
               }
            }
            else
            {
               sim.Start = root["sim.start"].AsDateTime(dateFormat);
               sim.SimLenght = root["sim.lenght", 14].AsDouble();
               simID = 1;
            }

            sim.WorkingDirectory = root["working.folder", "."].AsFilePath();
            sim.CheckRun = root["check.run", true].AsBool();
            sim.Verbose = root["verbose", true].AsBool();
            sim.Wait = root["wait", true].AsBool();
            sim.SuccessString = root["check.this", "successfully terminated"].AsString();
            sim.DataDirectory = root["data.folder", sim.SimDirectory.Path + "data"].AsFilePath();
            sim.SetupRunPeriod = root["setup.run.period", false].AsBool();
            if (sim.SetupRunPeriod)
            {
               sim.EndTAG = root["sim.end.tag", "<<end>>"].AsString();
               sim.StartTAG = root["sim.start.tag", "<<start>>"].AsString();
            }
            sim.SetupRunPeriod = root["wait", false].AsBool();
            sim.SaveOutput = true;
            sim.OutputFile = new FileName(sim.SimDirectory.Path + "res" + System.IO.Path.DirectorySeparatorChar + root["output.file", "result.txt"].AsString());
            sim.Executable = new FileName(sim.SimDirectory.Path + "exe" + System.IO.Path.DirectorySeparatorChar + root["mohid.executable", "mohid.exe"].AsString());
            sim.CreateInputFiles = root["use.templates", false].AsBool();
            endOfSimulation = root["sim.end"].AsDateTime(dateFormat);
            resFolder = root["results.folder", sim.SimDirectory.Path + "res"].AsFilePath();
            storeFolder = root["store.folder", sim.SimDirectory.Path + "store"].AsFilePath();
            oldFolder = root["old.folder", sim.SimDirectory.Path + "old"].AsFilePath();

            if (sim.SetupRunPeriod && !sim.CreateInputFiles)
               return false;

            if (sim.CreateInputFiles)
            {
               InputFileTemplate newTemplate;
               List<ConfigNode> itfList = root.ChildNodes.FindAll(FindFirstTemplateInfoBlocks);
               foreach (ConfigNode ticn in itfList)
               {
                  newTemplate = new InputFileTemplate(ticn["file"].AsFileName().FullPath,
                                                      (InputFileTemplateType)Enum.Parse(typeof(InputFileTemplateType), ticn["type", "data"].AsString(), true));
                  first.Add(newTemplate);
               }

               changeTemplates = root["change.templates", true].AsBool();
               if (changeTemplates)
               {
                  itfList = root.ChildNodes.FindAll(FindNextTemplateInfoBlocks);
                  foreach (ConfigNode ticn in itfList)
                  {
                     newTemplate = new InputFileTemplate(ticn["file"].AsFileName().FullPath,
                                                         (InputFileTemplateType)Enum.Parse(typeof(InputFileTemplateType), ticn["type", "data"].AsString(), true));
                     next.Add(newTemplate);
                  }
               }

               //itfList = root.ChildNodes.FindAll(FindTemplatesSpecialSetup);
               //if (itfList.Count > 0)
               //{
               //   templatesSpecialSetup = true;
               //}
               //else
               //   templatesSpecialSetup = false;
            }
             }
             catch
             {
            return false;
             }

             return true;
        }
Example #15
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)
             {
             }
        }
Example #16
0
        static void Main(string[] args)
        {
            string stepMessage = "";
             List<ConfigNode> timeseries, parameters;
             List<string> cfg = new List<string>();
             List<string> blocks = new List<string>();
             DateTime start, end, actual;
             FilePath root;
             bool hasFolders;
             TextFile config, errors;
             string hdfTag;
             FileName hdf5EXE = new FileName();
             FilePath workingFolder = new FilePath();
             FilePath tsPath;
             Dictionary<string, TimeSeries> outTS = new Dictionary<string, TimeSeries>();
             TimeSeries newTS = new TimeSeries();
             List<string> failedPeriods = new List<string>();
             int count_to_save;
             bool use_year_on_path;
             bool join_time_series;
             string startFormat, endFormat, folderFormat;

             errors = new TextFile("errors.log");
             errors.OpenNewToWrite();
             count_to_save = 0;

             try
             {
            stepMessage = "command line arguments loading.";
            CmdArgs cmdArgs = new CmdArgs(args);

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

               stepMessage = "configuration file parsing.";
               ConfigNode tsc = conf.Root.ChildNodes.Find(delegate(ConfigNode node) { return node.Name == "timeseries.to.extract"; });

               if (tsc == null)
                  throw new Exception("block 'timeseries.to.extract' is missing.");

               start = tsc["start.date"].AsDateTime();
               end = tsc["end.date"].AsDateTime();
               hdfTag = tsc["hdf.tag"].AsString();
               hdf5EXE = tsc["exporter.exe"].AsFileName();
               workingFolder = tsc["exporter.working"].AsFilePath();
               tsPath = tsc["timeseries.output.path"].AsFilePath();
               //root = tsc["root", ".\\"].AsFilePath();
               hasFolders = tsc["has.folders", true].AsBool();
               use_year_on_path = tsc["use.year.on.path", true].AsBool();
               join_time_series = tsc["join.timeseries", true].AsBool();
               folderFormat = tsc["folder.format", "{start}_{end}"].AsString();
               startFormat = tsc["start.format", "yyyyMMddHH"].AsString();
               endFormat = tsc["end.format", "yyyyMMddHH"].AsString();

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

               timeseries = tsc.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "timeseries"; });
               if (timeseries.Count < 1)
                  throw new Exception("Block 'timeseries' is missing. There must be at least one.");

               //Creates the blocks of timeseries
               foreach (ConfigNode n in timeseries)
               {
                  blocks.Add("<BeginTimeSerie>");
                  blocks.Add("  NAME    : " + n["name"].AsString());
                  blocks.Add("  COORD_Y : " + n["y"].AsString());
                  blocks.Add("  COORD_X : " + n["x"].AsString());
                  blocks.Add("<EndTimeSerie>");
                  blocks.Add("");

                  outTS[n["name"].AsString()] = new TimeSeries();
                  if (System.IO.File.Exists(tsPath.Path + n["name"].AsString() + ".ets"))
                  {
                     outTS[n["name"].AsString()].Load(new FileName(tsPath.Path + n["name"].AsString() + ".ets"));
                  }
               }

               parameters = tsc.ChildNodes.FindAll(delegate(ConfigNode node) { return node.Name == "parameters"; });
               if (parameters.Count < 1)
                  throw new Exception("Block 'parameters' is missing. There must be at least one.");

               //Creates the blocks of parameters
               foreach (ConfigNode n in parameters)
               {
                  blocks.Add("<BeginParameter>");
                  blocks.Add("  HDF_GROUP : " + n["group"].AsString());
                  blocks.Add("  PROPERTY  : " + n["property"].AsString());
                  blocks.Add("<EndParameter>");
                  blocks.Add("");
               }

               config = new TextFile();
               config.File.FullPath = workingFolder.Path + "HDF5Exporter.dat";

               bool quit = false;
               actual = start;
               FilePath yearFolder = new FilePath();
               ExternalApp hdf5exporter = new ExternalApp();
               hdf5exporter.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
               hdf5exporter.TextToCheck = "Program HDF5Exporter successfully terminated";
               hdf5exporter.Wait = true;
               hdf5exporter.WorkingDirectory = workingFolder.Path;
               hdf5exporter.Executable = hdf5EXE.FullPath;

               FilePath wp = workingFolder;
               List<FileInfo> tsList = new List<FileInfo>();
               bool failed, res;
               do
               {
                  failed = false;
                  if (use_year_on_path)
                     yearFolder.Path = root.Path + actual.Year.ToString();
                  else
                     yearFolder.Path = root.Path;

                  if (FileTools.FolderExists(yearFolder))
                  {
                     if (System.IO.File.Exists(yearFolder.Path + hdfTag + actual.ToString("yyyyMMddHH") + "_" + actual.AddHours(5).ToString("yyyyMMddHH") + ".hdf5"))
                     {
                        config.OpenNewToWrite();

                        cfg.Clear();
                        cfg.Add("");
                        cfg.Add("START_TIME : " + actual.ToString("yyyy M d H m s"));
                        cfg.Add("END_TIME   : " + actual.AddHours(5).ToString("yyyy M d H m s"));
                        cfg.Add("");
                        cfg.Add("<BeginHDF5File>");
                        cfg.Add("  NAME : " + yearFolder.Path + hdfTag + actual.ToString("yyyyMMddHH") + "_" + actual.AddHours(5).ToString("yyyyMMddHH") + ".hdf5");
                        cfg.Add("<EndHDF5File>");
                        cfg.Add("");
                        config.WriteLines(cfg);
                        config.WriteLines(blocks);

                        config.Close();

                        //executes HDF5Exporter
                        try
                        {
                           Console.Write("Running HDF5Exporter to file " + hdfTag + actual.ToString("yyyyMMddHH") + "_" + actual.AddHours(5).ToString("yyyyMMddHH") + ".hdf5 ...");
                           res = hdf5exporter.Run();
                           if (!res)
                           {
                              errors.WriteLine("Unsuccessfull HDF5Exporter run on file '" + hdfTag + actual.ToString("yyyyMMddHH") + "_" + actual.AddHours(5).ToString("yyyyMMddHH") + ".hdf5'");
                              failed = true;
                              Console.WriteLine("[Failed]");
                           }
                           else
                           {
                              Console.WriteLine("[OK]");
                           }
                        }
                        catch(Exception e_run)
                        {
                           errors.WriteLine("HDF5Exporter Run Exception on file '" + hdfTag + actual.ToString("yyyyMMddHH") + "_" + actual.AddHours(5).ToString("yyyyMMddHH") + ".hdf5'");
                           errors.WriteLine("Exception returned this message: " + e_run.Message);
                           failed = true;
                           Console.WriteLine("[Exception]");
                        }

                        if (!failed)
                        {
                           FileTools.FindFiles(ref tsList, wp, "*.ets", true, "", System.IO.SearchOption.TopDirectoryOnly);
                           foreach (FileInfo file in tsList)
                           {
                              if (outTS[file.FileName.Name].NumberOfInstants > 0)
                              {
                                 try
                                 {
                                    newTS.Load(file.FileName);
                                    //outTS.Load(new FileName(tsPath.Path + file.FileName.FullName));
                                    outTS[file.FileName.Name].AddTimeSeries(newTS);
                                 }
                                 catch
                                 {
                                    errors.WriteLine("Was not possible to read timeseries '" + file.FileName + "' from HDF file '" + hdfTag + actual.ToString("yyyyMMddHH") + "_" + actual.AddHours(5).ToString("yyyyMMddHH") + ".hdf5'");
                                 }
                                 //outTS.Save(new FileName(tsPath.Path + file.FileName.FullName));
                                 System.IO.File.Delete(file.FileName.FullPath);
                              }
                              else
                              {
                                 //FileTools.CopyFile(file.FileName, new FileName(tsPath.Path + file.FileName.FullName), CopyOptions.OVERWRIGHT);
                                 //outTS[file.FileName.Name].Load(new FileName(tsPath.Path + file.FileName.FullName));

                                 outTS[file.FileName.Name].Load(file.FileName);
                                 System.IO.File.Delete(file.FileName.FullPath);
                              }
                           }

                           count_to_save++;
                           if (count_to_save >= 60)
                           {
                              count_to_save = 0;
                              foreach (FileInfo file in tsList)
                              {
                                 Console.Write("Saving Timeseries '" + file.FileName.Name + "' ...");
                                 try
                                 {
                                    outTS[file.FileName.Name].Save(new FileName(tsPath.Path + file.FileName.Name + ".ets"));
                                    Console.WriteLine("[OK]");
                                    FileTools.CopyFile(new FileName(tsPath.Path + file.FileName.Name + ".ets"), new FileName((new FilePath(tsPath.Path + "bkp")).Path + file.FileName.Name + ".ets"), CopyOptions.OVERWRIGHT);
                                 }
                                 catch(Exception e_run)
                                 {
                                    Console.WriteLine("[FAILED]");
                                    errors.WriteLine("Was not possible to save timeseries '" + file.FileName.Name + ".ets'");
                                    errors.WriteLine("Exception returned this message: " + e_run.Message);
                                 }
                              }
                           }
                        }
                     }
                  }

                  actual = actual.AddHours(6);
                  if (actual >= end)
                     quit = true;
               }
               while (!quit);

               foreach (KeyValuePair<string, TimeSeries> pair in outTS)
               {
                  pair.Value.Save(new FileName(tsPath.Path + pair.Key + ".ets"));
               }

               //TimeUnits timeUnits = (TimeUnits)Enum.Parse(typeof(TimeUnits), conf.Root["time.units", "seconds"].AsString(), true);

               //   List<FileName> list = new List<FileName>();
               //   foreach (KeyValuePair<string, KeywordData> item in nodeList.NodeData)
               //      list.Add(item.Value.AsFileName());

               //   if (list.Count <= 1)
               //      throw new Exception("Block 'timeseries.to.join' must contain at least 2 entries");

               //   stepMessage = "loading timeseries.";
               //   List<TimeSeries> timeSeries = new List<TimeSeries>();

               //   foreach (FileName ts in list)
               //   {
               //      TimeSeries newTS = new TimeSeries();
               //      newTS.Load(ts);
               //      timeSeries.Add(newTS);
               //   }

               //   start = timeSeries[0].StartInstant;
               //   for (int i = 1; i < timeSeries.Count; i++)
               //   {
               //      if (timeSeries[i].StartInstant < start)
               //         start = timeSeries[i].StartInstant;
               //   }

               //   stepMessage = "creating output timeseries.";
               //   TimeSeries outTS = new TimeSeries();
               //   outTS.StartInstant = start;
               //   outTS.TimeUnits = timeUnits;

               //   foreach (Column col in timeSeries[0].Columns)
               //   {
               //      Column newCol = new Column(col.ColumnType);
               //      newCol.Header = col.Header;
               //      outTS.AddColumn(newCol);
               //   }

               //   foreach (TimeSeries toJoin in timeSeries)
               //      outTS.AddTimeSeries(toJoin);

               //   stepMessage = "saving output timeseries.";
               //   outTS.Save(outputFileName);

               //   Console.WriteLine("Process complete with success.");
               //}
               //else
               //{
               //   Console.WriteLine("Parameter --cfg is missing.");
               //   Console.WriteLine("Execution aborted.");
               //   return;
               //}
            }
             }
             catch (Exception ex)
             {
            Console.WriteLine("An exception was raised while {0}", stepMessage);
            Console.WriteLine("Exception message: {0}", ex.Message);
             }

             errors.Close();
        }
Example #17
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("");
             }
        }