Ejemplo n.º 1
0
            public void Save(FileName file)
            {
                TextFile f = new TextFile(file);

                f.OpenNewToWrite(FileShare.None);

                foreach (string line in Header)
                   f.WriteLine(line);

                int i = 0;

                if (HorizGrid != null)
                {
                   f.WriteLine("<BeginXX>");
                   for (i = 0; i < HorizGrid.XX.Count; i++)
                  f.WriteLine(HorizGrid.XX[i].ToString(DataFormat));
                   f.WriteLine("<EndXX>");
                   f.WriteLine("<BeginYY>");
                   for (i = 0; i < HorizGrid.YY.Count; i++)
                  f.WriteLine(HorizGrid.YY[i].ToString(DataFormat));
                   f.WriteLine("<EndYY>");
                }

                f.WriteLine("<BeginGridData2D>");
                for (i = 0; i < Data.Count; i++)
                   f.WriteLine(Data[i].ToString(DataFormat));
                f.WriteLine("<EndGridData2D>");
                f.Close();
            }
Ejemplo n.º 2
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();
        }
        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
            {
            }
             }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            DateTime start, end;
             FilePath rootResultsPath;
             FileName hdfResultsFile;
             List<FilePath> timeseriesOutputPath = new List<FilePath>();
             FileName exporterEXE;
             FilePath exporterWorkingPath;
             bool joinTimeseries = false;
             bool useDateOnTimeseriesName = true;
             string folderFormat, startFormat, endFormat;
             StringBuilder exporterCFGBase = new StringBuilder();
             StringBuilder exporterCFG = new StringBuilder();
             List<ConfigNode> nodes;
             double runLenght;
             bool verbose;
             List<FileInfo> tsFileInfoList = new List<FileInfo>();
             Dictionary<string, TimeSeries> outTS = new Dictionary<string,TimeSeries>();
             TimeSeries newTS = new TimeSeries();
             TextFile txtFile, errors;
             int timeseriesSaveCounter = 1, saveCounter;
             int timeseriesNameCounter;
             errors = new TextFile("errors.log");
             errors.OpenNewToWrite();
             verbose = true;
             FilePath path;
             bool dateAndTimeInOutputFolder;
             string outputFolderFormat = "", outputFolderStartFormat = "", outputFolderEndFormat = "", outputPath, outputPath2;
             bool useCounter;

             try
             {
            CmdArgs cmdArgs = new CmdArgs(args);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                     exporterCFGBase.AppendLine("<EndTimeSerie>");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                     actual = actual.AddDays(runLenght);
                  }

                  tscCount++;

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

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

             Console.WriteLine("GetTSFromModelResultsHDF finished.");
             errors.Close();
        }
Ejemplo n.º 5
0
            public void Save(FileName file, List<int> columnsToSave, Interval interval)
            {
                //Check to see if the interval exists
                if (interval.Start < 0 || interval.Start >= instants.Count || interval.End >= instants.Count)
                   throw new Exception("Invalid Interval");

                //Check to see if the columns chosen exists
                foreach (int col in columnsToSave)
                   if (col < 0 || col >= dataColumns.Count)
                  throw new Exception("Invalid column index");

                int endRow;
                if (interval.End < 0)
                   endRow = instants.Count - 1;
                else
                   endRow = interval.End;

                TextFile ts = new TextFile(file);
                ts.OpenNewToWrite();

                string toSave;

                //Save TimeSeries Header
                ts.WriteLines(extraLines);
                ts.WriteLine("SERIE_INITIAL_DATA : " + startInstant.ToString("yyyy M d H m s"));
                ts.WriteLine("TIME_UNITS : " + timeUnits.ToString());
                toSave = timeUnits.ToString();
                foreach(int col in columnsToSave)
                   toSave += " " + dataColumns[col].Header;
                ts.WriteLine(toSave);

                //Save Data
                ts.WriteLine("<BeginTimeSerie>");
                for (int row = interval.Start; row <= endRow; row++)
                {
                   toSave = ((double)instants[row]).ToString(instants.DataFormat);
                   //Save the columns to file
                   foreach (int col in columnsToSave)
                  toSave += " " + string.Format("{0" + dataColumns[col].DataFormat + "}", dataColumns[col][row]);
                   ts.WriteLine(toSave);
                }
                ts.WriteLine("<EndTimeSerie>");

                ts.Close();
            }
        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"));
            }
             }
        }
        bool IMohidTask.Run(ConfigNode cfg)
        {
            last_exception = null;
             this.cfg = cfg;
             bool result = true;

             if (!LoadGlobalConfig())
            return false;

             files_to_process.Clear();

             for (; ; )
             {
            if (!CreateFileList())
            {
               if (last_exception == null)
               {
                  result = true;
                  break;
               }
               else
               {
                  result = false;
                  break;
               }
            }

            if (!RunForFileList())
            {
               result = false;
               break;
            }
             }

             if (do_log && log.Length > 0)
             {
            try
            {
               if (add_date_time)
                  log_file.FullName = log_file.Name + "_" + DateTime.Now.ToString(log_dateonfile_format) + "." + log_file.Extension;

               TextFile log_f = new TextFile(log_file);
               log_f.OpenNewToWrite();

               log_f.Write(log);

               log_f.Close();
            }
            catch (Exception ex)
            {
               last_exception = ex;
               result = false;
            }
             }

             return result;
        }
Ejemplo n.º 8
0
            public virtual bool Save()
            {
                TextFile cf = new TextFile(ConfigFile);
                cf.OpenNewToWrite();

                bool result = false;

                try
                {
                   result = SaveToConfigFile(ref cf, Root);
                }
                catch(Exception ex)
                {
                   exceptionMessage = ex.Message;
                   cf.Close();
                   return false;
                }

                cf.Close();
                return result;
            }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            bool verbose = false;

             try
             {

            CmdArgs cmdArgs = new CmdArgs(args);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        if (applyScaleFator)
                           res *= scaleFactor;

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

                     file_o.WriteLine(line_o);
                  }

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

                  bkCount++;
                  if (verbose)
                     Console.WriteLine("[OK]");
               }
            }
             }
             catch (Exception ex)
             {
            if (verbose)
            {
               Console.WriteLine("[FAIL]");
               Console.WriteLine("");
               Console.WriteLine("An EXCEPTION was raised. The message returned was:");
               Console.WriteLine(ex.Message);
            }
             }
             if (verbose)
             {
            Console.WriteLine("Process finished.");
            Console.WriteLine("");
             }
        }
Ejemplo n.º 10
0
            public int Glue()
            {
                if (filesToGlue != null && filesToGlue.Count <= 0)
                   return ThrowException(-3, "No files to glue.");

                if (filesToGlue2 != null && filesToGlue2.Count <= 0)
                   return ThrowException(-3, "No files to glue.");

                try
                {
                   TextFile datafile = new TextFile();

                   if (Verbose)
                  Console.WriteLine("Creating input file: {0}", fWorkingDirectory + GlueInpuFile);

                   datafile.File = new FileName(fWorkingDirectory + GlueInpuFile);

                   datafile.OpenNewToWrite(System.IO.FileShare.None);

                   if (!String.IsNullOrEmpty(fOutwatch) && fOutwatch.Trim() != "")
                  datafile.WriteLine("OUTWATCH         : " + fOutwatch.Trim());
                   datafile.WriteLine("<begin_file>");
                   datafile.WriteLine("  ACTION           : GLUES HDF5 FILES");
                   datafile.WriteLine("  OUTPUTFILENAME   : " + fOutput);
                   if (Is3DFile)
                  datafile.WriteLine("  3D_FILE          : 1");
                   else
                  datafile.WriteLine("  3D_FILE          : 0");
                   datafile.WriteLine("  BASE_GROUP       : " + BaseGroup);
                   datafile.WriteLine("  TIME_GROUP       : " + TimeGroup);
                   datafile.WriteLine("  <<begin_list>>");
                   if (filesToGlue != null)
                   {
                  foreach (string file in filesToGlue)
                     datafile.WriteLine("      " + file);
                   }
                   else if (filesToGlue2 != null)
                   {
                  foreach (FileName file in filesToGlue2)
                     datafile.WriteLine("      " + file.FullPath);
                   }
                   datafile.WriteLine("  <<end_list>>");
                   datafile.WriteLine("<end_file>");
                   datafile.Close();
                }
                catch (Exception ex)
                {
                   return ThrowException(-1, ex.Message);
                }

                TextToCheck = "successfully terminated";
                if (!ExecuteApp())
                   return ThrowException(-2);

                return 0;
            }
Ejemplo n.º 11
0
            public int Extract()
            {
                if (Parameters.Count <= 0)
                   return ThrowException(-1);
                if (string.IsNullOrEmpty(Input))
                   return ThrowException(-2);
                if (string.IsNullOrEmpty(Output))
                   return ThrowException(-3);
                if (string.IsNullOrEmpty(fAppPath))
                   return ThrowException(-4);
                if (string.IsNullOrEmpty(fExtractorInputFileName))
                   return ThrowException(-5);
                if (string.IsNullOrEmpty(fGlobalInputFileName))
                   return ThrowException(-6);
                if (string.IsNullOrEmpty(AppName))
                   return ThrowException(-7);

                if (string.IsNullOrEmpty(fWorkingDirectory))
                   fWorkingDirectory = fAppPath;

                try
                {
                   TextFile nomfich = new TextFile();
                   TextFile datafile = new TextFile();

                   nomfich.File = new FileName(fWorkingDirectory + fGlobalInputFileName);
                   nomfich.OpenNewToWrite(System.IO.FileShare.None);
                   nomfich.WriteLine("IN_MODEL : " + fExtractorInputFileName);
                   nomfich.Close();

                   datafile.File = new FileName(fWorkingDirectory + fExtractorInputFileName);
                   datafile.OpenNewToWrite(System.IO.FileShare.None);
                   datafile.WriteLine("FILENAME         : " + fInput);
                   datafile.WriteLine("OUTPUTFILENAME   : " + fOutput);
                   datafile.WriteLine("START_TIME       : " + Start.ToString("yyyy MM dd HH mm ss"));
                   datafile.WriteLine("END_TIME         : " + End.ToString("yyyy MM dd HH mm ss"));
                   if (ExtractWindow)
                   {
                  datafile.WriteLine("XY_WINDOW_OUTPUT : 1");
                  datafile.WriteLine("XY_WINDOW_LIMITS : " + WindowILB.ToString() + " " + WindowJLB.ToString() + " " + WindowIUB.ToString() + " " + WindowJUB.ToString());
                   }
                   else
                  datafile.WriteLine("XY_WINDOW_OUTPUT : 0");
                   if (ExtractSpecificLayers)
                   {
                  datafile.WriteLine("LAYERS_OUTPUT    : 1");
                  datafile.WriteLine("LAYERS_MIN_MAX   : " + LayerKLB.ToString() + " " + LayerKUB.ToString());
                   }
                   else
                  datafile.WriteLine("LAYERS_OUTPUT    : 0");
                   if (ExtractByInterval)
                   {
                  datafile.WriteLine("INTERVAL         : 1");
                  datafile.WriteLine("DT_INTERVAL      : " + DTInterval.ToString());
                   }
                   else
                  datafile.WriteLine("INTERVAL         : 0");
                   if (ConvertV3ToV4)
                  datafile.WriteLine("CONVERT_V3_TO_V4 : 1");
                   else
                  datafile.WriteLine("CONVERT_V3_TO_V4 : 0");
                   foreach (HDFParameter p in Parameters)
                   {
                  datafile.WriteLine("<BeginParameter>");
                  if (ConvertV3ToV4)
                  {
                     datafile.WriteLine("HDF_GROUP_V3     :" + p.GroupV3);
                     datafile.WriteLine("PROPERTY_V3      :" + p.PropertyV3);
                  }
                  datafile.WriteLine("HDF_GROUP        : " + p.Group);
                  datafile.WriteLine("PROPERTY         : " + p.Property);
                  datafile.WriteLine("<EndParameter>");
                   }
                   datafile.Close();
                }
                catch (Exception ex)
                {
                   ThrowException(-8, ex.Message);
                }

                TextToCheck = "Operation was successfull";
                if (!ExecuteApp())
                   return -9;

                return 0;
            }