예제 #1
0
            private void DoWork(object sender, DoWorkEventArgs e)
            {
                ExternalApp app = new ExternalApp();
                Options opts = sender as Options;

                app.Options = sender as Options;

                if (!app.Run())
                {
                   //MessageBox.Show("HDFExporter tool has failed.", "ATTENTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   return;
                }
            }
예제 #2
0
        static void Main(string[] args)
        {
            Config cfg = new Config("corr.cfg");
             cfg.Load();

             FilePath processor_path = cfg.Root["processor.path"].AsFilePath();
             FilePath output_path = cfg.Root["output.path"].AsFilePath();
             FilePath hdfs_path = cfg.Root["hdf.path"].AsFilePath();

             System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(hdfs_path.Path);
             System.IO.FileInfo[] aryFi = di.GetFiles("*.hdf5", System.IO.SearchOption.TopDirectoryOnly);

             Dictionary<string, string> info = new Dictionary<string,string>();

             ExternalApp app = new ExternalApp();
             app.Arguments = "--cfg task.cfg";
             app.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
             app.Executable = processor_path.Path + "MohidHDF5Processor.exe";
             app.UseShell = false;
             app.Verbose = false;
             app.Wait = true;
             app.WorkingDirectory = processor_path.Path;

             int count = 1;

             foreach (System.IO.FileInfo fi in aryFi)
             {
            Console.Write("{1}: Processing {0}...", fi.Name, count);

            info["<<input>>"] = fi.FullName;
            info["<<output>>"] = output_path.Path + fi.Name;

            TextFile.Replace(processor_path.Path + "task.template", processor_path.Path + "task.cfg", ref info);
            if (!app.Run())
            {
               Console.WriteLine("Failure when trying to correct file {0}", fi.Name);
               Console.WriteLine("[FAIL]");
            }
            else
               Console.WriteLine("[ OK ]");

            count++;
             }
        }
        private void ExportByFolderList(ExportHDFToTSOptions opts, DoWorkEventArgs e)
        {
            string config_name = "";
             TextFile cfg = new TextFile();
             ExternalApp app = new ExternalApp();
             List<TimeSeries> outputTS = new List<TimeSeries>();
             bool first = true;

             System.IO.SearchOption so;
             FileName file = new FileName();
             FilePath path = new FilePath();
             List<Mohid.Files.FileInfo> files = new List<Mohid.Files.FileInfo>();

             if (opts.SearchSubFolders)
            so = System.IO.SearchOption.AllDirectories;
             else
            so = System.IO.SearchOption.TopDirectoryOnly;

             foreach (string folder in opts.List)
             {
            path.Path = folder;
            FileTools.FindFiles(ref files, path, Path.GetFileName(opts.File), false, null, so);
             }

             int totalFiles = files.Count;
             int filesProcessed = 0;

             foreach (Mohid.Files.FileInfo fi in files)
             {
            //Find a file name for the configuration file.
            if (this.CancellationPending)
            {
               return;
            }

            bool Found = false;
            int n = 1;
            int tentatives = 0;
            while (!Found)
            {
               config_name = opts.ConfigFile + n + ".cfg";
               if (System.IO.File.Exists(opts.WorkingFolder + config_name))
               {
                  n++;
               }
               else
               {
                  try
                  {
                     cfg.File.FullPath = opts.WorkingFolder + config_name;
                     cfg.OpenNewToWrite();
                     Found = true;
                  }
                  catch
                  {
                     n++;
                     tentatives++;

                     if (tentatives > 20)
                     {
                        MessageBox.Show("Was not possibe to create the configuration file to the HDFExporter tool.", "ATTENTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                     }
                  }
               }
            }

            //Create the Config Data File
            cfg.WriteLine("!File created using Mohid Toolbox");

            file.FullPath = fi.FileName.FullPath;
            cfg.WriteLine("<BeginHDF5File>");
            cfg.WriteLine("  NAME : " + file.FullPath);
            cfg.WriteLine("<EndHDF5File>");

            if (opts.Type == ExportHDFToTSType.ByMask) //by Mask
            {
               cfg.WriteLine("EXPORT_TYPE       : 2");
               cfg.WriteLine("MASK_GRID         : " + opts.MaskFile);
               cfg.WriteLine("AREA_FILL_VALUE   : " + opts.AreaFillValue);
               if (opts.UsePoints)
               {
                  cfg.WriteLine("USE_POINTS        : 1");
                  cfg.WriteLine("WATERPOINTS_NAME  : " + opts.PointsName);
                  cfg.WriteLine("WATERPOINTS_GROUP : " + opts.PointsGroup);
               }
               else
                  cfg.WriteLine("USE_POINTS        : 0");
            }
            else //By coordinates
            {
               cfg.WriteLine("EXPORT_TYPE       : 1");
               cfg.WriteLine("WATERPOINTS_NAME  : " + opts.PointsName);
               cfg.WriteLine("WATERPOINTS_GROUP : " + opts.PointsGroup);
            }

            if (opts.CheckPropertyName)
               cfg.WriteLine("CHECK_PROPERTY    : 1");
            else
               cfg.WriteLine("CHECK_PROPERTY    : 0");

            if (opts.UseStart)
               cfg.WriteLine("START_TIME        : " + opts.Start.ToString("yyyy MM dd HH mm ss"));
            if (opts.UseEnd)
               cfg.WriteLine("END_TIME          : " + opts.End.ToString("yyyy MM dd HH mm ss"));

            if (opts.Variable)
               cfg.WriteLine("VARIABLE_GRID     : 1");
            else
               cfg.WriteLine("VARIABLE_GRID     : 0");

            if (!string.IsNullOrWhiteSpace(opts.GridFile))
               cfg.WriteLine("GRID_FILENAME     : " + opts.GridFile);

            if (!string.IsNullOrWhiteSpace(opts.TimeGroup))
               cfg.WriteLine("TIME_GROUP        : " + opts.TimeGroup);

            if (!string.IsNullOrWhiteSpace(opts.DecimationFactor))
               cfg.WriteLine("DECIMATION_FACTOR : " + opts.DecimationFactor);

            foreach (TimeseriesBlock ts in opts.TimeSeries)
            {
               cfg.WriteLine("<BeginTimeSerie>");

               if (opts.Type == ExportHDFToTSType.ByMask) //by Mask
               {
                  cfg.WriteLine("  NAME              : " + file.Path + ts.Name);
                  if (!string.IsNullOrWhiteSpace(ts.MaskID))
                     cfg.WriteLine("  MASK_ID           : " + ts.MaskID);
                  if (!string.IsNullOrWhiteSpace(ts.Layer))
                     cfg.WriteLine("  LAYER             : " + ts.Layer);
               }
               else
               {
                  cfg.WriteLine("  NAME              : " + file.Path + ts.Name);
                  if (!string.IsNullOrWhiteSpace(ts.I))
                     cfg.WriteLine("  LOCALIZATION_I    : " + ts.I);
                  if (!string.IsNullOrWhiteSpace(ts.J))
                     cfg.WriteLine("  LOCALIZATION_J    : " + ts.J);
                  if (!string.IsNullOrWhiteSpace(ts.K))
                     cfg.WriteLine("  LOCALIZATION_J    : " + ts.K);
                  if (!string.IsNullOrWhiteSpace(ts.Latitude))
                     cfg.WriteLine("  LATITUDE          : " + ts.Latitude);
                  if (!string.IsNullOrWhiteSpace(ts.Longitude))
                     cfg.WriteLine("  LONGITUDE         : " + ts.Longitude);
                  if (!string.IsNullOrWhiteSpace(ts.X))
                     cfg.WriteLine("  COORD_X           : " + ts.X);
                  if (!string.IsNullOrWhiteSpace(ts.Y))
                     cfg.WriteLine("  COORD_Y           : " + ts.Y);
               }
               cfg.WriteLine("<EndTimeSerie>");
            }

            foreach (ParameterBlock p in opts.Parameters)
            {
               cfg.WriteLine("<BeginParameter>");
               cfg.WriteLine("  PROPERTY          : " + p.Name);
               cfg.WriteLine("  HDF_GROUP         : " + p.Group);
               cfg.WriteLine("<EndParameter>");
            }
            cfg.Close();

            //run HDFExporter Tool
            app.Executable = opts.PathToHDFExporter;
            app.UseShell = false;
            app.WorkingDirectory = opts.WorkingFolder;
            app.Arguments = "-c " + config_name;
            app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
            app.TextToCheck = "successfully terminated";
            app.Verbose = false;
            app.Wait = true;
            app.SearchTextOrder = SearchTextOrder.FROMEND;

            AppExitStatus es;

            if ((es = app.Run(this, e)) != AppExitStatus.Finished)
            {
               if (es != AppExitStatus.Canceled)
                  MessageBox.Show("HDFExporter tool has failed.", "ATTENTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
               return;
            }

            if (!opts.KeepConfigFile)
            {
               try
               {
                  System.IO.File.Delete(opts.WorkingFolder + config_name);
               }
               catch
               {
               }
            }

            if (opts.JoinTimeseries)
            {
               if (first)
               {
                  foreach (TimeseriesBlock tsb in opts.TimeSeries)
                  {
                     TimeSeries new_ts = new TimeSeries();
                     new_ts.Load(new FileName(file.Path + tsb.Name + ".ets"));
                     new_ts.Name = tsb.Name + ".ets";
                     outputTS.Add(new_ts);
                  }

                  first = false;
               }
               else
               {
                  int count = 0;
                  foreach (TimeseriesBlock tsb in opts.TimeSeries)
                  {
                     TimeSeries new_ts = new TimeSeries();
                     new_ts.Load(new FileName(file.Path + tsb.Name + ".ets"));
                     TimeSeries this_ts = outputTS[count];
                     this_ts.AddTimeSeries(new_ts);
                     new_ts = null;
                     if (!opts.KeepIntermediateTSFiles)
                     {
                        try
                        {
                           System.IO.File.Delete(file.Path + tsb.Name + ".ets");
                        }
                        catch
                        {
                        }
                     }
                     count++;
                  }
               }
            }

            if (this.WorkerReportsProgress)
            {
               filesProcessed++;
               int percentComplete = (int)((float)filesProcessed / (float)totalFiles * 100);
               this.ReportProgress(percentComplete);
            }
             }

             if (opts.JoinTimeseries)
             {
            foreach (TimeSeries ts in outputTS)
            {
               ts.Save(new FileName(new FilePath(opts.PathToOutputTimeSeries).Path + ts.Name + ".ets"));
            }
             }
        }
        protected bool GenerateMeteoFiles(MohidRunEngineData mre)
        {
            DateTime simStartDate = mre.sim.Start;
             DateTime startsim;
             DateTime endsim;
             string dateStr;
             DateTime simStart = simStartDate.AddSeconds(1);
             int counter;
             string file, outputPath, outputName;
             bool found_file;
             System.IO.FileInfo fi;

             ConfigNode node = mre.cfg.Root.ChildNodes.Find(FindScriptBlocks);
             if (node == null)
             {
            Console.WriteLine("Node 'script.data' was not found in the config file.");
            return false;
             }

             //First is required the glue of the files
             HDFGlue tool = new HDFGlue();
             tool.AppName = node["glue.exe.name", "glue.exe"].AsString();
             tool.AppPath = node["glue.exe.path", @"..\tools\glue\"].AsString();
             tool.WorkingDirectory = node["glue.working.folder", @"..\tools\glue\"].AsString();
             outputPath = node["glue.output.path", @"..\" + mre.sim.SimDirectory.Path + @"local.data\boundary.conditions\"].AsString();
             outputName = node["glue.output.name", "mm5.glued.hdf5"].AsString();
             tool.Output = outputPath + outputName;
             tool.Is3DFile = false;

             //This will ensure that the first hour of simulation is included in the meteo file.
             startsim = simStart.AddHours(-5);
             endsim = startsim.AddHours(5);
             int maxSkips = node["max.skips", 10].AsInt();
             int numberOfSkips = 0;

             Console.WriteLine("Sim start: {0}", simStartDate.ToString("yyyy/MM/dd HH:mm:ss"));
             Console.WriteLine("Generating Meteo file from {0} to {1}", startsim.ToString("yyyy/MM/dd HH:mm:ss"), mre.sim.End.ToString("yyyy/MM/dd HH:mm:ss"));
             Console.WriteLine("Sim lenght: {0}", mre.sim.SimLenght);

             int numberOfFiles = (int)(mre.sim.SimLenght * 4 + 1.0);
             Console.WriteLine("Expected number of 6 hours files to look: {0}", numberOfFiles);
             Console.WriteLine("Folder where meteo files are: {0}", node["meteo.folder", @"H:\"].AsString());
             Console.WriteLine("");
             Console.WriteLine("Looking for files...");
             Console.WriteLine("");
             for (counter = 0; counter < numberOfFiles; counter++)
             {
            found_file = false;

            dateStr = startsim.ToString("yyyyMMddHH") + "_" + endsim.ToString("yyyyMMddHH");
            file = node["meteo.folder", @"H:\"].AsString() + @"D3_" + dateStr + ".hdf5";

            if (System.IO.File.Exists(file))
            {
               fi = new System.IO.FileInfo(file);
               if (fi.Length > 2000000)
               {
                  found_file = true;
                  tool.FilesToGlue.Add(file);
               }
            }

            if (!found_file)
            {
               Console.WriteLine("File '{0}' was not found.", file);

               if (counter + 1 == numberOfFiles)
               {
                  counter--;
                  numberOfSkips++;
                  if (numberOfSkips > maxSkips)
                  {
                     Console.WriteLine("Max number of skips reached during meteo creation file.");
                     return false;
                  }
               }
            }

            startsim = endsim.AddHours(1);
            endsim = startsim.AddHours(5);
             }

             Console.WriteLine("Gluing files...");

             tool.ThrowExceptionOnError = true;
             if (tool.Glue() != 0)
             {
            Console.WriteLine("Glue failed.");
            return false;
             }

             Dictionary<string, string> replace_list = new Dictionary<string, string>();
             replace_list["<<glued>>"] = outputPath;
             replace_list["<<father>>"] = node["father.grid", @"..\..\general.data\digital.terrain\"].AsString();
             replace_list["<<dest>>"] = node["hdf.output.path", @"..\..\general.data\boundary.conditions\"].AsString() + mre.sim.Start.ToString("yyyyMMdd.HHmmss") + "-" + mre.sim.End.ToString("yyyyMMdd.HHmmss") + @"\";
             replace_list["<<model>>"] = node["model.grid", @"..\..\general.data\digital.terrain\"].AsString();

             string template = node["template", mre.sim.SimDirectory.Path + @"templates\tools\convert.to.HDF5.action.template"].AsString();
             string action = node["action.file",  @"..\tools\interpolation\converttoHDF5action.dat"].AsString();

             TextFile.Replace(template, action, ref replace_list);

             ExternalApp app = new ExternalApp();

             app.WorkingDirectory = node["interpolation.working.folder", @"..\tools\interpolation\"].AsString();
             app.Executable = node["interpolation.exe", @"..\tools\interpolation\interpolation.exe"].AsString();
             app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
             app.TextToCheck = "successfully terminated";
             app.SearchTextOrder = SearchTextOrder.FROMEND;
             app.Wait = true;
             Console.WriteLine("Interpolating meteo file to model grid...");
             bool result = app.Run();
             if (!result)
             {
            Console.WriteLine("Interpolation failed.");
            return false;
             }

             //Saves the created meteo file in the folder
             if (!FileTools.CopyFile(new FilePath(replace_list["<<dest>>"]), new FilePath(orig.Path), "meteo.hdf5", Files.CopyOptions.OVERWRIGHT))
             {
            Console.WriteLine("Was not possible to copy the meteo file to the meteo storage folder.");
            return false;
             }

             return true;
        }
        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();
             }
        }
예제 #6
0
            public bool ExecuteApp()
            {
                ExternalApp app;
                bool result;

                try
                {
                   app = new ExternalApp();

                   app.Executable = fAppPath + AppName;
                   if (!string.IsNullOrWhiteSpace(Arguments))
                  app.Arguments = Arguments;
                   app.WorkingDirectory = fWorkingDirectory;
                   app.CheckSuccessMethod = CheckSuccessMethod;
                   app.TextToCheck = TextToCheck;
                   app.SearchTextOrder = TextOrder;
                   app.Verbose = Verbose;
                   app.UseShell = UseShell;
                   app.Wait = true;
                }
                catch (Exception ex)
                {
                   return ThrowException(ex.Message);
                }

                result = app.Run();
                if (!result)
                {
                   //Console.WriteLine(app.Options.SaveDefaultOutput.ToString());
                   //foreach(string line in ExternalApp.DefaultOutput)
                   //   Console.WriteLine(line);
                   return ThrowException("Application '" + app.Executable + "' run Failed.");
                }
                return true;
            }
예제 #7
0
        public override bool OnSimStart(object data)
        {
            string workingfolder = @"..\0.Data\Boundary.Conditions\";
             string outputfolder = @"..\..\Run.1\general.data\boundary.conditions\";
              MohidRunEngineData mre = (MohidRunEngineData)data;

             //Extract date from original 2010 MM5 file
             ExternalApp app = new ExternalApp();

             Dictionary<String, String> replace_list = new Dictionary<string, string>();
             replace_list.Add("<<start>>", mre.sim.Start.ToString("yyyy M d H m s"));
             replace_list.Add("<<end>>", mre.sim.End.AddHours(1).ToString("yyyy M d H m s"));
             replace_list.Add("<<input>>", "MM5-D3-Portugal-2010.hdf5");
             replace_list.Add("<<output>>", "mm5extracted.hdf5");

             TextFile.Replace(workingfolder + "hdf5extractor.template", workingfolder + "hdf5extractor.dat", ref replace_list);

             app.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
             app.Wait = true;
             app.WorkingDirectory = workingfolder;
             app.Executable = workingfolder + "HDF5Extractor.x64.omp.d.exe";

             try
             {
            if (!app.Run()) throw new Exception("Run Failed. Unknown error.");
             }
             catch (Exception ex)
             {
            Console.WriteLine("");
            Console.WriteLine("[OnSimStart-Extract] Erro detectado. Mensagem de erro:");
            Console.WriteLine(ex.Message);
            Console.WriteLine("Start  : {0}", mre.sim.Start.ToString("yyyy M d H m s"));
            Console.WriteLine("End    : {0}", mre.sim.End.AddHours(1).ToString("yyyy M d H m s"));
            Console.WriteLine("");
            return false;
             }

               //Convert extracted HDF to project grid data

             replace_list.Clear();
             replace_list.Add("<<input>>", "mm5extracted.hdf5");
             replace_list.Add("<<input.grid>>", "MM5-D3-Portugal.dat");
             replace_list.Add("<<output>>", outputfolder + "mm5.hdf5");
             replace_list.Add("<<output.grid>>", "project-griddata.dat");

             TextFile.Replace(workingfolder + "converttohdf5action.template", workingfolder + "converttohdf5action.dat", ref replace_list);

             app.Executable = workingfolder + "Convert2HDF.x64.omp.d.exe";
             try
             {
            if (!app.Run()) throw new Exception("Run Failed. Unknown error.");
             }
             catch (Exception ex)
             {
            Console.WriteLine("");
            Console.WriteLine("[OnSimStart-Convert] Erro detectado. Mensagem de erro:");
            Console.WriteLine(ex.Message);
            Console.WriteLine("Start  : {0}", mre.sim.Start.ToString("yyyy M d H m s"));
            Console.WriteLine("End    : {0}", mre.sim.End.AddHours(1).ToString("yyyy M d H m s"));
            Console.WriteLine("");
            return false;
             }

             return true;
        }
예제 #8
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();
        }
        protected void ConvertToMGD(ConfigNode cfg)
        {
            Console.WriteLine("Converting XYZ to MGD...");

             List<FileInfo> list_of_files = new List<FileInfo>();
             Dictionary<string, string> replace_list = new Dictionary<string, string>();
             FileName template = cfg["conv.to.mgd.template"].AsFileName();
             string working_dir = cfg["mgd.working.dir", fWorkingDir.Path].AsFilePath().Path;
             string config = template.FullName.Replace("template", "dat");
             string output;

             FileTools.FindFiles(ref list_of_files, cfg["xyz.path"].AsFilePath(), "*.xyz", true, "", System.IO.SearchOption.TopDirectoryOnly);
             foreach (FileInfo file in list_of_files)
             {
            Console.WriteLine("    -> {0}", file.FileName.FullName);
            output = cfg["mgd.path"].AsFilePath().Path + file.FileName.Name + ".dat"; ;
            if (!cfg["rewrite.mgd", false].AsBool())
            {
               if (System.IO.File.Exists(output))
                  continue;
            }

            replace_list["{{input.file}}"] = file.FileName.FullPath;
            replace_list["{{output.file}}"] = output;
            replace_list["{{no.data.point}}"] = cfg["no.data.point", "-99"].AsString();

            TextFile.Replace(template.FullPath, working_dir + config, ref replace_list, false);

            ExternalApp tool = new ExternalApp();

            tool.Arguments = "";
            tool.WorkingDirectory = working_dir;
            tool.Executable = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cfg["conv.to.mgd.exe"].AsString());
            tool.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
            tool.TextToCheck = "successfully terminated";
            tool.SearchTextOrder = SearchTextOrder.FROMEND;
            tool.Wait = true;

            bool result = tool.Run();
            if (!result)
            {
               if (tool.Exception != null)
               {
                  Console.WriteLine("ConvertToMGD failed: {0}", tool.Exception.Message);
                  throw tool.Exception;
               }
               else
               {
                  Console.WriteLine("ConvertToMGD failed without explanation.");
                  throw new Exception("ConvertToMGD failed.");
               }
            }
             }
        }
예제 #10
0
        public bool GenerateMeteoFilesToForecast(DateTime simStartDate)
        {
            System.Collections.Generic.Dictionary<string, string> toReplace = new Dictionary<string, string>();

             toReplace["<<datetime>>"] = simStartDate.ToString("yyyyMMddHH");
             TextFile.Replace(@"..\templates\tools\convertToHDF5Action.template", @"..\tools\ConvertToHDF\ConvertToHDF5Action.dat", ref toReplace);

             ExternalApp app = new ExternalApp();

             app.Executable = @"..\tools\ConvertToHDF\interpolation.exe";
             app.WorkingDirectory = @"..\tools\ConvertToHDF\";
             app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
             app.TextToCheck = "successfully terminated";
             app.SearchTextOrder = SearchTextOrder.FROMEND;
             app.Wait = true;

             return app.Run();
        }
예제 #11
0
        protected bool SendToFTPOnEnd(MohidRunEngineData mre)
        {
            bool res = true;

             Dictionary<string, string> toReplace = new Dictionary<string, string>();

             try
             {
            ExternalApp app = new ExternalApp();

            FileTools.CopyFile(new FileName(@"..\templates\Tools\ref-evtp-cumulative.template"), new FileName(@"..\tools\ftp\ftp.dat"), CopyOptions.OVERWRIGHT);

            app.Executable = "ftp";
            app.WorkingDirectory = @"..\Cumulative";
            app.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
            app.Wait = true;
            app.Arguments = "-n -s:" + @"..\tools\ftp\ftp.dat ftp.mywater-fp7.eu";

            res = app.Run();

             }
             catch
             {
            return false;
             }

             return res;
        }
예제 #12
0
        //public override bool OnEnd(object data)
        //{
        //   MohidRunEngineData mre = (MohidRunEngineData)data;
        //   return SendToFTPOnEnd(mre);
        //}
        protected bool SendToFTP(MohidRunEngineData mre)
        {
            bool res = true;

             Dictionary<string, string> toReplace = new Dictionary<string, string>();

             string folderName = mre.sim.Start.ToString("yyyyMMdd.HHmmss") + "-" + mre.sim.End.ToString("yyyyMMdd.HHmmss");

             try
             {
            toReplace["<<folder>>"] = folderName;
            TextFile.Replace(@"..\templates\tools\ref-evtp.template", @"..\tools\ftp\ftp.dat", ref toReplace);

            ExternalApp app = new ExternalApp();

            app.Executable = "ftp";
            app.WorkingDirectory = @"..\store\" + folderName + @"\To.FTP";
            app.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
            app.Wait = true;
            app.Arguments = "-n -s:" + @"..\..\..\tools\ftp\ftp.dat ftp.mywater-fp7.eu";

            res = app.Run();
            if (!res)
               return false;
             }
             catch
             {
            return false;
             }

             return res;
        }
예제 #13
0
        protected bool GenerateAlternativeMeteoFilesToForecast(DateTime simStartDate)
        {
            //bool result = true;
             DateTime startsim;
             DateTime endsim;
             string dateStr;
             DateTime simStart = simStartDate.AddSeconds(1);

             char pd = System.IO.Path.DirectorySeparatorChar;

             //First is required the glue of two files
             HDFGlue tool = new HDFGlue();
             tool.AppName = "glue.exe";
             tool.AppPath = System.IO.Path.Combine("..", "tools", "glue");
             tool.WorkingDirectory = tool.AppPath;
             tool.Output = "meteo.glued.hdf5";
             tool.Is3DFile = false;

             startsim = simStart.AddHours(-5);
             endsim = startsim.AddHours(5);
             dateStr = startsim.ToString("yyyyMMddHH") + "_" + endsim.ToString("yyyyMMddHH");
             tool.FilesToGlue.Add("\\\\ftpserver\\ftp.mohid.com\\LocalUser\\meteoIST\\mm5_6h\\D3_" + dateStr + ".hdf5");
             startsim = endsim.AddHours(1);
             endsim = startsim.AddHours(5);
             dateStr = startsim.ToString("yyyyMMddHH") + "_" + endsim.ToString("yyyyMMddHH");
             tool.FilesToGlue.Add("\\\\ftpserver\\ftp.mohid.com\\LocalUser\\meteoIST\\mm5_6h\\D3_" + dateStr + ".hdf5");
             tool.ThrowExceptionOnError = true;
             if (tool.Glue() != 0) return false;

             //Now, do the "extraction"
             if (!FileTools.CopyFile(new FileName(@".."+ pd + "templates" + pd + "tools" + pd + "ConvertToHDF5Action-2.template"), new FileName(@".." + pd + "tools" + pd + "interpolation" + pd + "ConvertToHDF5Action.dat"), CopyOptions.OVERWRIGHT))
            return false;

             ExternalApp app = new ExternalApp();

             app.Executable = @".." + pd + "tools" + pd + "interpolation" + pd + "interpolation.exe";
             app.WorkingDirectory = @".." + pd + "tools" + pd + "interpolation" + pd;
             app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
             app.TextToCheck = "successfully terminated";
             app.SearchTextOrder = SearchTextOrder.FROMEND;
             app.Wait = true;

             return app.Run();
        }
예제 #14
0
        protected bool SendToFTP(FileName file, FileName ftp_template, FilePath working_folder, string ftp_address, string path_on_ftp)
        {
            bool res = true;

             Dictionary<string, string> toReplace = new Dictionary<string, string>();
             toReplace["<<file.to.upload>>"] = file.FullPath;
             toReplace["<<path.on.ftp>>"] = path_on_ftp;

             try
             {
            ExternalApp app = new ExternalApp();

            TextFile.Replace(ftp_template.FullPath, working_folder.Path + "ftp.dat", ref toReplace, false);

            app.Executable = "ftp";
            app.WorkingDirectory = working_folder.Path;
            app.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
            app.Wait = true;
            app.Arguments = "-n -s:ftp.dat " + ftp_address;

            res = app.Run();

             }
             catch
             {
            return false;
             }

             return res;
        }
        private void ExportByFileList(ExportHDFToTSOptions opts, DoWorkEventArgs e)
        {
            string config_name = "";
             TextFile cfg = new TextFile();
             ExternalApp app = new ExternalApp();

             //Find a file name for the configuration file.
             bool Found = false;
             int n = 1;
             int tentatives = 0;
             while (!Found)
             {
            config_name = opts.ConfigFile + n + ".cfg";
            if (System.IO.File.Exists(opts.WorkingFolder + config_name))
            {
               n++;
            }
            else
            {
               try
               {
                  cfg.File.FullPath = opts.WorkingFolder + config_name;
                  cfg.OpenNewToWrite();
                  Found = true;
               }
               catch
               {
                  n++;
                  tentatives++;

                  if (tentatives > 20)
                  {
                     MessageBox.Show("Was not possibe to create the configuration file to the HDFExporter tool.", "ATTENTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return;
                  }
               }
            }
             }

             //Create the Config Data File
             cfg.WriteLine("!File created using Mohid Toolbox");

             foreach (string file in opts.List)
             {
            cfg.WriteLine("<BeginHDF5File>");
            cfg.WriteLine("  NAME : " + file);
            cfg.WriteLine("<EndHDF5File>");
             }

             if (opts.Type == ExportHDFToTSType.ByMask) //by Mask
             {
            cfg.WriteLine("EXPORT_TYPE       : 2");
            cfg.WriteLine("MASK_GRID         : " + opts.MaskFile);
            cfg.WriteLine("AREA_FILL_VALUE   : " + opts.AreaFillValue);
            if (opts.UsePoints)
            {
               cfg.WriteLine("USE_POINTS        : 1");
               cfg.WriteLine("WATERPOINTS_NAME  : " + opts.PointsName);
               cfg.WriteLine("WATERPOINTS_GROUP : " + opts.PointsGroup);
            }
            else
               cfg.WriteLine("USE_POINTS        : 0");
             }
             else //By coordinates
             {
            cfg.WriteLine("EXPORT_TYPE       : 1");
            cfg.WriteLine("WATERPOINTS_NAME  : " + opts.PointsName);
            cfg.WriteLine("WATERPOINTS_GROUP : " + opts.PointsGroup);
             }

             if (opts.CheckPropertyName)
            cfg.WriteLine("CHECK_PROPERTY    : 1");
             else
            cfg.WriteLine("CHECK_PROPERTY    : 0");

             if (opts.UseStart)
            cfg.WriteLine("START_TIME        : " + opts.Start.ToString("yyyy MM dd HH mm ss"));
             if (opts.UseEnd)
            cfg.WriteLine("END_TIME          : " + opts.End.ToString("yyyy MM dd HH mm ss"));

             if (opts.Variable)
            cfg.WriteLine("VARIABLE_GRID     : 1");
             else
            cfg.WriteLine("VARIABLE_GRID     : 0");

             if (!string.IsNullOrWhiteSpace(opts.GridFile))
            cfg.WriteLine("GRID_FILENAME     : " + opts.GridFile);

             if (!string.IsNullOrWhiteSpace(opts.TimeGroup))
            cfg.WriteLine("TIME_GROUP        : " + opts.TimeGroup);

             if (!string.IsNullOrWhiteSpace(opts.DecimationFactor))
            cfg.WriteLine("DECIMATION_FACTOR : " + opts.DecimationFactor);

             foreach (TimeseriesBlock ts in opts.TimeSeries)
             {
            cfg.WriteLine("<BeginTimeSerie>");

            if (opts.Type == ExportHDFToTSType.ByMask) //by Mask
            {
               cfg.WriteLine("  NAME              : " + (new FilePath(opts.PathToOutputTimeSeries)).Path + ts.Name);
               if (!string.IsNullOrWhiteSpace(ts.MaskID))
                  cfg.WriteLine("  MASK_ID           : " + ts.MaskID);
               if (!string.IsNullOrWhiteSpace(ts.Layer))
                  cfg.WriteLine("  LAYER             : " + ts.Layer);
            }
            else
            {
               cfg.WriteLine("  NAME              : " + (new FilePath(opts.PathToOutputTimeSeries)).Path + ts.Name);
               if (!string.IsNullOrWhiteSpace(ts.I))
                  cfg.WriteLine("  LOCALIZATION_I    : " + ts.I);
               if (!string.IsNullOrWhiteSpace(ts.J))
                  cfg.WriteLine("  LOCALIZATION_J    : " + ts.J);
               if (!string.IsNullOrWhiteSpace(ts.K))
                  cfg.WriteLine("  LOCALIZATION_J    : " + ts.K);
               if (!string.IsNullOrWhiteSpace(ts.Latitude))
                  cfg.WriteLine("  LATITUDE          : " + ts.Latitude);
               if (!string.IsNullOrWhiteSpace(ts.Longitude))
                  cfg.WriteLine("  LONGITUDE         : " + ts.Longitude);
               if (!string.IsNullOrWhiteSpace(ts.X))
                  cfg.WriteLine("  COORD_X           : " + ts.X);
               if (!string.IsNullOrWhiteSpace(ts.Y))
                  cfg.WriteLine("  COORD_Y           : " + ts.Y);
            }
            cfg.WriteLine("<EndTimeSerie>");
             }

             foreach (ParameterBlock p in opts.Parameters)
             {
            cfg.WriteLine("<BeginParameter>");
            cfg.WriteLine("  PROPERTY          : " + p.Name);
            cfg.WriteLine("  HDF_GROUP         : " + p.Group);
            cfg.WriteLine("<EndParameter>");
             }
             cfg.Close();

             //run HDFExporter Tool
             app.Executable = opts.PathToHDFExporter;
             app.UseShell = false;
             app.WorkingDirectory = opts.WorkingFolder;
             app.Arguments = "-c " + config_name;
             app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
             app.TextToCheck = "successfully terminated";
             app.Verbose = false;
             app.Wait = false;
             app.SearchTextOrder = SearchTextOrder.FROMEND;

             Software.AppExitStatus es;

             if ((es = app.Run(this, e)) != AppExitStatus.Finished)
             {
            if (es != AppExitStatus.Canceled)
               MessageBox.Show("HDFExporter tool has failed.", "ATTENTION", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
             }

             if (!opts.KeepConfigFile)
             {
            try
            {
               System.IO.File.Delete(opts.WorkingFolder + config_name);
            }
            catch
            {
            }
             }
        }
        protected void ConvertToXYZ(ConfigNode cfg)
        {
            Console.WriteLine("Converting ASC to XYZ...");

             List<FileInfo> list_of_files = new List<FileInfo>();
             Dictionary<string, string> replace_list = new Dictionary<string,string>();
             FileName template = cfg["conv.to.xyz.template"].AsFileName();
             string working_dir = cfg["xyz.working.dir", fWorkingDir.Path].AsFilePath().Path;
             string config = template.FullName.Replace("template", "dat");
             string output;
             ExternalApp tool = new ExternalApp();

             try
             {
            FileTools.FindFiles(ref list_of_files, cfg["asc.path"].AsFilePath(), "*." + cfg["asc.extension", "asc"].AsString(), true, "", System.IO.SearchOption.TopDirectoryOnly);
            foreach (FileInfo file in list_of_files)
            {
               Console.WriteLine("    -> {0}", file.FileName.FullName);
               output = cfg["xyz.path"].AsFilePath().Path + file.FileName.Name + ".xyz";

               if (!cfg["rewrite.xyz", false].AsBool())
               {
                  if (System.IO.File.Exists(output))
                     continue;
               }

               replace_list["{{input.file}}"] = file.FileName.FullPath;
               replace_list["{{output.file}}"] = output;

               TextFile.Replace(template.FullPath, working_dir + config, ref replace_list, false);

               tool.Arguments = "";
               tool.WorkingDirectory = working_dir;
               tool.Executable = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cfg["conv.to.xyz.exe"].AsString());
               tool.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
               tool.TextToCheck = "Conversion terminated successfully.";
               tool.SearchTextOrder = SearchTextOrder.FROMEND;
               tool.Wait = true;

               bool result = tool.Run();
               if (!result)
               {
                  if (tool.Exception != null)
                  {
                     Console.WriteLine("ConvertToXYZ failed: {0}", tool.Exception.Message);
                     throw new CreateHDFFromASCToolException(tool.Exception.Message, tool.Exception);
                  }
                  else
                  {
                     Console.WriteLine("ConvertToXYZ failed.");
                     throw new CreateHDFFromASCToolException("ConvertToXYZ failed.");
                  }
               }
            }
             }
             catch (CreateHDFFromASCToolException)
             {
            throw;
             }
             catch (Exception ex)
             {
            Console.WriteLine(ex.Message);
            throw;
             }
        }
예제 #17
0
        protected bool GenerateMeteoFiles(MohidRunEngineData mre)
        {
            DateTime simStartDate = mre.sim.Start;
             DateTime startsim;
             DateTime endsim;
             string dateStr;
             DateTime simStart = simStartDate.AddSeconds(1);
             int counter;
             string file, outputPath, outputName, tempFolder;
             bool found_file;
             System.IO.FileInfo fi;
             HDFGlue glueTool = new HDFGlue();
             ExternalApp interpolateTool = new ExternalApp();

             ConfigNode node = mre.cfg.Root.ChildNodes.Find(FindScriptBlocks);
             if (node == null)
             {
            Console.WriteLine("Node 'script.data' was not found in the config file.");
            return false;
             }

             //This will ensure that the first hour of simulation is included in the meteo file.
             startsim = simStart.AddHours(-5);
             endsim = startsim.AddHours(5);

             Console.WriteLine("Sim start: {0}", simStartDate.ToString("yyyy/MM/dd HH:mm:ss"));
             Console.WriteLine("Generating Meteo file from {0} to {1}", startsim.ToString("yyyy/MM/dd HH:mm:ss"), mre.sim.End.ToString("yyyy/MM/dd HH:mm:ss"));
             Console.WriteLine("Sim lenght: {0}", mre.sim.SimLenght);

             //Get some important input from the script block in config file
             outputPath = node["glue.output.path", @"..\" + mre.sim.SimDirectory.Path + @"local.data\boundary.conditions\"].AsString();
             outputName = node["glue.output.name", "meteo.hdf5"].AsString();
             tempFolder = node["interpolation.folder", @".\temp\"].AsString();

             //First, find the required files.
             int numberOfFiles = (int)(mre.sim.SimLenght * 4 + 1.0);
             Console.WriteLine("Expected number of 6 hours files to look: {0}", numberOfFiles);
             Console.WriteLine("Folder where meteo files are: {0}", node["meteo.folder", @"H:\"].AsString());
             Console.WriteLine("");
             Console.WriteLine("Looking for files...");
             Console.WriteLine("");

             int maxSkips = node["max.skips", 10].AsInt();
             int numberOfSkips = 0;
             List<string> requiredFiles = new List<string>();

             for (counter = 0; counter < numberOfFiles; counter++)
             {
            found_file = false;

            dateStr = startsim.ToString("yyyyMMddHH") + "_" + endsim.ToString("yyyyMMddHH");
            file = node["meteo.folder", @"H:\"].AsString() + @"D3_" + dateStr + ".hdf5";

            if (System.IO.File.Exists(file))
            {
               fi = new System.IO.FileInfo(file);
               if (fi.Length > 2000000)
               {
                  found_file = true;
                  requiredFiles.Add(file);
               }
            }

            if (!found_file)
            {
               Console.WriteLine("File '{0}' was not found.", file);

               if (counter + 1 == numberOfFiles)
               {
                  counter--;
                  numberOfSkips++;
                  if (numberOfSkips > maxSkips)
                  {
                     Console.WriteLine("Max number of skips reached during meteo creation file.");
                     return false;
                  }
               }
            }

            startsim = endsim.AddHours(1);
            endsim = startsim.AddHours(5);
             }

             //Now the files were found, they will be interpolated to the model's grid
             Console.WriteLine("Interpolating meteo file to model grid...");
             Dictionary<string, string> replace_list = new Dictionary<string, string>();
             FileName f_name = new FileName();
             string path = node["glue.input.folder", @"..\interpolation\temp\"].AsString();
             bool skipInterpolation = node["skip.interpolation", false].AsBool();

             foreach (string f in requiredFiles)
             {
            f_name.FullPath = f;
            glueTool.FilesToGlue.Add(path + f_name.FullName);

            if (!skipInterpolation)
            {

               replace_list["<<input.hdf>>"] = f;
               replace_list["<<father.grid>>"] = node["father.grid", @"..\..\general.data\digital.terrain\mm5.dat"].AsString();
               replace_list["<<output.hdf>>"] = tempFolder + f_name.FullName;
               replace_list["<<model.grid>>"] = node["model.grid", @"..\..\general.data\digital.terrain\grid.dat"].AsString();

               string template = node["template", mre.sim.SimDirectory.Path + @"templates\tools\convert.to.HDF5.action.template"].AsString();
               string action = node["action.file", @"..\tools\interpolation\converttoHDF5action.dat"].AsString();

               TextFile.Replace(template, action, ref replace_list);

               interpolateTool.WorkingDirectory = node["interpolation.working.folder", @"..\tools\interpolation\"].AsString();
               interpolateTool.Executable = node["interpolation.exe", @"..\tools\interpolation\interpolation.exe"].AsString();
               interpolateTool.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
               interpolateTool.TextToCheck = "successfully terminated";
               interpolateTool.SearchTextOrder = SearchTextOrder.FROMEND;
               interpolateTool.Wait = true;

               bool result = interpolateTool.Run();
               if (!result)
               {
                  Console.WriteLine("Interpolation failed.");
                  return false;
               }
            }
             }

             //Now the interpolated files are glued
             glueTool.AppName = node["glue.exe.name", "glue.exe"].AsString();
             glueTool.AppPath = node["glue.exe.path", @"..\tools\glue\"].AsString();
             glueTool.WorkingDirectory = node["glue.working.folder", @"..\tools\glue\"].AsString();
             glueTool.Output = outputPath + outputName;
             glueTool.Is3DFile = false;
             glueTool.ThrowExceptionOnError = true;

             Console.WriteLine("Gluing files...");

             if (glueTool.Glue() != 0)
             {
            Console.WriteLine("Glue failed.");
            return false;
             }

             //Saves the created meteo file in the folder
             if (!FileTools.CopyFile(node["local.boundary.folder", @"..\actual\local.data\boundary.conditions\"].AsFilePath(), new FilePath(orig.Path), "meteo.hdf5", Files.CopyOptions.OVERWRIGHT))
             {
            Console.WriteLine("Was not possible to copy the meteo file to the meteo storage folder.");
            return false;
             }

             //Delete all the files in the temp directory
             try
             {
            System.IO.DirectoryInfo directory = new DirectoryInfo(@"..\tools\interpolation\temp\");
            foreach (System.IO.FileInfo fileToDelete in directory.GetFiles())
               fileToDelete.Delete();
             }
             catch (Exception ex)
             {
            Console.WriteLine("Was not possible to empty the temp directory due to an exception.");
            Console.WriteLine("Message returned was: {0}", ex.Message);
             }

             return true;
        }
예제 #18
0
        protected bool GenerateMeteoFiles(MohidRunEngineData mre)
        {
            //bool result = true;
             DateTime simStartDate = mre.sim.Start;
             DateTime startsim;
             DateTime endsim;
             string dateStr;
             DateTime simStart = simStartDate.AddSeconds(1);
             int counter;
             string file;
             bool found_file;
             System.IO.FileInfo fi;

             //First is required the glue of two files
             HDFGlue tool = new HDFGlue();
             tool.AppName = "glue.exe";
             tool.AppPath = @"..\tools\glue\";
             tool.WorkingDirectory = @"..\tools\glue\";
             tool.Output = @"..\" + mre.sim.SimDirectory.Path + @"local.data\boundary.conditions\mm5.glued.hdf5";
             tool.Is3DFile = false;

             startsim = simStart.AddHours(-5);
             endsim = startsim.AddHours(5);

             Console.WriteLine("Sim start: {0}", simStartDate.ToString("yyyy/MM/dd HH:mm:ss"));
             Console.WriteLine("Generating Meteo file from {0} to {1}", startsim.ToString("yyyy/MM/dd HH:mm:ss"), startsim.AddHours(11).ToString("yyyy/MM/dd HH:mm:ss"));
             Console.WriteLine("Gluing files...");
             for (counter = 0; counter < 57; counter++)
             {
            found_file = false;

            dateStr = startsim.ToString("yyyyMMddHH") + "_" + endsim.ToString("yyyyMMddHH");
            file = @"H:\mm5_6h\" + startsim.ToString("yyyy") + @"\D3_" + dateStr + ".hdf5";
            if (System.IO.File.Exists(file))
            {
               fi = new System.IO.FileInfo(file);
               if (fi.Length > 2000000)
                  found_file = true;
            }

            if (!found_file && startsim.Year == 2008)
            {
               file = @"H:\mm5_6h\2008_ANL\D3_" + dateStr + ".hdf5";
               if (System.IO.File.Exists(file))
               {
                  fi = new System.IO.FileInfo(file);
                  if (fi.Length > 2000000)
                     found_file = true;
               }
            }

            if (!found_file)
            {
               file = @"\\DATACENTER\alexandria3\modelos\Meteo_IST\mm5_6h\D3_" + dateStr + ".hdf5";
               if (System.IO.File.Exists(file))
               {
                  fi = new System.IO.FileInfo(file);
                  if (fi.Length > 2000000)
                     found_file = true;
               }
            }

            if (!found_file)
            {
               Console.WriteLine("The file 'D3_{0}.hdf5' was not found.", dateStr);
               return false;
            }
            else
            {
               tool.FilesToGlue.Add(file);
               //Console.WriteLine("Adding {0} to the list of files to be glued.", @"H:\mm5_6h\" + startsim.ToString("yyyy") + @"\D3_" + dateStr + ".hdf5");
               startsim = endsim.AddHours(1);
               endsim = startsim.AddHours(5);
            }
             }

             tool.ThrowExceptionOnError = true;
             if (tool.Glue() != 0) return false;

             Dictionary<string, string> replace_list = new Dictionary<string, string>();
             replace_list["<<glued>>"] = @"..\" + mre.sim.SimDirectory.Path + @"local.data\boundary.conditions\";
             replace_list["<<father>>"] = @"..\..\general.data\digital.terrain\";
             replace_list["<<dest>>"] = @"..\..\general.data\boundary.conditions\" + mre.sim.Start.ToString("yyyyMMdd.HHmmss") + "-" + mre.sim.End.ToString("yyyyMMdd.HHmmss") + @"\";
             replace_list["<<model>>"] = @"..\..\general.data\digital.terrain\";
             TextFile.Replace(mre.sim.SimDirectory.Path + @"templates\tools\convert.to.HDF5.action.template", @"..\tools\interpolation\converttoHDF5action.dat", ref replace_list);

             //Now, do the "extraction"
             //if (!FileTools.CopyFile(new FileName(mre.sim.SimDirectory.Path + @"templates\tools\convert.to.HDF5.action.template"), new FileName(@"..\tools\interpolation\converttoHDF5action.dat"), CopyOptions.OVERWRIGHT))
             //   return false;

             ExternalApp app = new ExternalApp();

             app.Executable = @"..\tools\interpolation\interpolation.exe";
             app.WorkingDirectory = @"..\tools\interpolation\";
             app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
             app.TextToCheck = "successfully terminated";
             app.SearchTextOrder = SearchTextOrder.FROMEND;
             app.Wait = true;
             Console.WriteLine("Interpolating meteo file to model grid...");
             return app.Run();
        }
예제 #19
0
        public override bool OnSimStart(object data)
        {
            MohidRunEngineData mre = (MohidRunEngineData)data;

             //bool result = true;
             DateTime startsim;
             DateTime endsim;
             string dateStr;
             DateTime simStart = mre.sim.Start;
             int counter;

             //First is required the glue of two files
             HDFGlue tool = new HDFGlue();

             tool.AppName = "ConvertToHdf5_release_single.exe";
             tool.AppPath = @"..\tools\Convert 2 HDF\";
             tool.WorkingDirectory = @"..\tools\Convert 2 HDF\";
             tool.Output = @"mm5.glued.hdf5";
             tool.Is3DFile = false;

             startsim = simStart.AddHours(-5);
             endsim = startsim.AddHours(5);

             string file_to_glue, path;
             int tentatives;
             bool must_find_another;

             for (counter = 0; counter < 5; counter++)
             {
            tentatives = 3;
            must_find_another = true;

            while (must_find_another)
            {
               dateStr = startsim.ToString("yyyyMMddHH") + "_" + endsim.ToString("yyyyMMddHH");
               file_to_glue = @"D3_" + dateStr + ".hdf5";
               path = @"Q:\";

               if (System.IO.File.Exists(path + file_to_glue))
               {
                  must_find_another = false;
                  tool.FilesToGlue.Add(path + file_to_glue);

                  startsim = endsim.AddHours(1);
                  endsim = startsim.AddHours(5);
               }
               else
               {
                  tentatives--;

                  if (tentatives == 0)
                     return false;

                  if (counter == 0)
                  {
                     startsim = startsim.AddHours(-6);
                     endsim = startsim.AddHours(-1);
                  }
                  else
                  {
                     startsim = endsim.AddHours(1);
                     endsim = startsim.AddHours(5);
                  }
               }
            }
             }

             tool.ThrowExceptionOnError = true;
             if (tool.Glue() != 0)
            return false;

             FileName template = new FileName(mre.sim.SimDirectory.Path + @"templates\tools\interpolate.template");
             FileName griddata = new FileName(mre.sim.SimDirectory.Path + @"templates\tools\grid.data.dat");

             //Now, do the "extraction"
             if (!FileTools.CopyFile(template, new FileName(@"..\tools\convert 2 hdf\converttoHDF5action.dat"), CopyOptions.OVERWRIGHT))
            return false;
             if (!FileTools.CopyFile(griddata, new FileName(@"..\tools\convert 2 hdf\grid.data.dat"), CopyOptions.OVERWRIGHT))
            return false;

             ExternalApp app = new ExternalApp();

             app.Executable = @"..\tools\convert 2 hdf\ConvertToHdf5_release_double.exe";
             app.WorkingDirectory = @"..\tools\convert 2 hdf\";
             app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
             app.TextToCheck = "successfully terminated";
             app.SearchTextOrder = SearchTextOrder.FROMEND;
             app.Wait = true;

             if (!app.Run())
            return false;

             if (!FileTools.CopyFile(new FileName(@"..\tools\convert 2 hdf\meteo.mm5.hdf5"), new FileName(mre.sim.SimDirectory.Path + @"local data\meteo.mm5.hdf5"), CopyOptions.OVERWRIGHT))
            return false;

             return true;
        }
예제 #20
0
        protected bool CreatePracticesIDHDFFile()
        {
            practicesFromHDF = false;

             ConfigNode practicesNode = root.ChildNodes.Find(FindPracticesBlock);
             if (practicesNode != null)
             {
            if (practicesNode["search.first", true].AsBool())
            {
               if (File.Exists(generalBoundary.Path + practicesNode["output.hdf"].AsFileName().FullName))
               {
                  if (FileTools.CopyFile(generalBoundary, root["local.boundary.conditions"].AsFilePath(),
                                          practicesNode["output.hdf"].AsFileName().FullName, Files.CopyOptions.OVERWRIGHT))
                  {
                     return true;
                  }
               }
            }

            Dictionary<string, string> replace_list = new Dictionary<string, string>();

            Console.WriteLine("Creating practices HDF file...");
            ExternalApp tool = new ExternalApp();

            replace_list.Clear();
            replace_list["<<grid.data>>"] = practicesNode["grid.data"].AsFileName().FullPath;
            replace_list["<<output.hdf>>"] = practicesNode["output.hdf"].AsFileName().FullPath;
            replace_list["<<start>>"] = mre.sim.Start.ToString("yyyy MM dd HH 00 00");
            replace_list["<<end>>"] = mre.sim.End.ToString("yyyy MM dd HH 00 00");
            replace_list["<<local.data>>"] = @"..\" + root["local.data"].AsFilePath().Path;

            string template = practicesNode["template", mre.sim.SimDirectory.Path + @"templates\tools\fillmatrix.template"].AsString();
            string action = practicesNode["config.file.name", "fillmatrix.dat"].AsString();

            Console.WriteLine("FillMatrix template {0}.", action);

            TextFile.Replace(template, practicesNode["working.folder", @"..\tools\fillmatrix\"].AsFilePath().Path + action, ref replace_list);

            tool.WorkingDirectory = practicesNode["working.folder", @"..\tools\fillmatrix\"].AsFilePath().Path;
            if (practicesNode.NodeData.ContainsKey("arguments"))
               tool.Arguments = practicesNode["arguments"].AsString();
            tool.Executable = root["exe.file", @"..\tools\fillmatrix\fillmatrix.exe"].AsString();
            tool.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
            tool.TextToCheck = "Finished..";
            tool.SearchTextOrder = SearchTextOrder.FROMEND;
            tool.Wait = true;

            bool result = tool.Run();
            if (!result)
            {
               Console.WriteLine("fillmatrix failed.");
               return false;
            }

            //Saves the created practices file in the folder
            if (!FileTools.CopyFile(root["local.boundary.conditions"].AsFilePath(), generalBoundary,
                                    practicesNode["output.hdf"].AsFileName().FullName, Files.CopyOptions.OVERWRIGHT))
            {
               Console.WriteLine("Was not possible to copy the meteo file to the meteo storage folder.");
               return false;
            }

            practicesFromHDF = true;
             }

             return true;
        }
        protected FileName ProcessHDF5(FileName file_to_process)
        {
            try
             {
            if (!replace_processed_files)
            {
               //Not implemented yet
            }

            replace_list.Clear();
            replace_list["<<input>>"] = file_to_process.FullName;
            replace_list["<<output>>"] = file_to_process.Name + output_tag + ".hdf5";

            string process_template_output = working_folder.Path + hdf_processor_template.Name + ".cfg";
            TextFile.Replace(hdf_processor_template.FullPath, process_template_output, ref replace_list);

            ExternalApp tool = new ExternalApp();

            tool.Arguments = "\\verbose --cfg " + process_template_output;
            tool.WorkingDirectory = working_folder.Path;
            tool.Executable = hdf_processor_exe.FullPath;
            tool.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
            tool.TextToCheck = "MohidHDF5Processor SUCCESSFULLY completed the process.";
            tool.SearchTextOrder = SearchTextOrder.FROMEND;
            tool.Wait = true;

            if (!tool.Run())
               throw new Exception(hdf_processor_exe.Name + " failed to convert " + file_to_process.Name);

            if (!System.IO.File.Exists(working_folder.Path + file_to_process.Name + output_tag + ".hdf5"))
               throw new Exception(hdf_processor_exe.Name + " failed to convert " + file_to_process.Name);

            return new FileName(file_to_process.Name + output_tag + ".hdf5");
             }
             catch (Exception ex)
             {
            last_exception = ex;
            return null;
             }
        }
예제 #22
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();
        }
예제 #23
0
            protected virtual bool RunMohid()
            {
                ExternalApp app = new ExternalApp();

                app.Executable = Executable.FullPath;
                app.WorkingDirectory = WorkingDirectory.Path;
                app.Verbose = Verbose;
                if (CheckRun)
                {
                   app.CheckSuccessMethod = CheckSuccessMethod.DEFAULTOUTPUT;
                   app.TextToCheck = SuccessString;
                   app.SearchTextOrder = SearchTextOrder.FROMEND;
                }
                else
                {
                   app.CheckSuccessMethod = CheckSuccessMethod.DONOTCHECK;
                }
                app.Wait = Wait;

                return app.Run();
            }