public void Load(FileName file, bool loadHG) { TextFile f = new TextFile(file); string [] seps = new string[] {":", " "}; string [] tokens; int i = 0, j = 0, elements = 0, pos = 0; string line, line_u; if (loadHG && HorizGrid == null) HorizGrid = new MohidHorizontalGrid(); f.OpenToRead(FileShare.Read); Header.Clear(); for(;;) { line = f.ReadLine(); if (line == null) break; line = line.Trim(); if (string.IsNullOrWhiteSpace(line)) continue; line_u = line.ToUpper(); if (line_u == "<BEGINXX>") { if (loadHG) { pos = 0; for(;;) { line = f.ReadLine(); if (line == null) throw new Exception("Invalid GridData file."); line = line.Trim(); if (line.ToUpper() == "<ENDXX>") break; HorizGrid.XX.Add(double.Parse(line, CultureInfo.InvariantCulture)); pos = pos + 1; } } } else if (line_u == "<BEGINYY>") { if (loadHG) { pos = 0; for(;;) { line = f.ReadLine(); if (line == null) throw new Exception("Invalid GridData file"); line = line.Trim(); if (line.ToUpper() == "<ENDYY>") break; HorizGrid.YY.Add(double.Parse(line, CultureInfo.InvariantCulture)); pos = pos + 1; } } } else if (line_u == "<BEGINGRIDDATA2D>") { pos = 0; for(;;) { line = f.ReadLine(); if (line == null) throw new Exception("Invalid GridData file."); line = line.Trim(); if (line.ToUpper() == "<ENDGRIDDATA2D>") break; Data.Add(double.Parse(line, CultureInfo.InvariantCulture)); pos = pos + 1; } } else { Header.Add(line); tokens = line.ToUpper().Split(seps, StringSplitOptions.RemoveEmptyEntries); switch (tokens[0]) { case "ILB_IUB": i = int.Parse(tokens[2]) - int.Parse(tokens[1]) + 1; elements = i * j; if (loadHG) { HorizGrid.ILB = int.Parse(tokens[1]); HorizGrid.IUB = int.Parse(tokens[2]); HorizGrid.Elements = elements; } break; case "JLB_JUB": j = int.Parse(tokens[2]) - int.Parse(tokens[1]) + 1; elements = i * j; if (loadHG) { HorizGrid.JLB = int.Parse(tokens[1]); HorizGrid.JUB = int.Parse(tokens[2]); HorizGrid.Elements = elements; } break; } } } f.Close(); }
public void Load(FileName file) { string line; string[] seps = new string[1]; string[] tokens; string[] header; TextFile ts = new TextFile(file); ts.OpenToRead(); try { dataColumns.Clear(); extraLines.Clear(); instants.Clear(); //Find Header and columns while ((line = ts.ReadLine()) != null) { line = line.Trim(); if (line == "") continue; if (line[0] == '!') { extraLines.Add(line); continue; } seps[0] = ":"; tokens = line.Split(seps, 2, StringSplitOptions.RemoveEmptyEntries); if (tokens[0].Trim() == "SERIE_INITIAL_DATA") { string[] tseps = { ".", " " }; string[] ttokens = tokens[1].Trim().Split(tseps, StringSplitOptions.RemoveEmptyEntries); startInstant = new DateTime((int)float.Parse(ttokens[0]), (int)float.Parse(ttokens[1]), (int)float.Parse(ttokens[2]), (int)float.Parse(ttokens[3]), (int)float.Parse(ttokens[4]), (int)float.Parse(ttokens[5])); //DateTime.ParseExact(temp, "yyyy M d H m s", null); } else if (tokens[0].Trim() == "TIME_UNITS") { timeUnits = (TimeUnits)Enum.Parse(typeof(TimeUnits), tokens[1].Trim(), true); } else { seps[0] = " "; tokens = line.Trim().Split(seps, StringSplitOptions.RemoveEmptyEntries); if (tokens[0].Trim() == "<BeginTimeSerie>") { if (extraLines.Count < 1) { ts.Close(); throw new Exception("The '" + file.FullName + "' is not a valid time series"); } header = extraLines.Last().Trim().Split(seps, StringSplitOptions.RemoveEmptyEntries); instants.Header = header[0]; for (int i = 1; i < header.Length; i++) { Column column = new Column(); dataColumns.Add(column); column.Header = header[i]; } extraLines.Remove(extraLines.Last()); break; } else if (tokens[0].Trim() == "<BeginResidual>") { while ((line = ts.ReadLine()) != null) { if (line.Trim() == "<EndResidual>") break; } if (line == null) throw new Exception("Invalid TimeSeries file."); } else { extraLines.Add(line); } } } int index; seps[0] = " "; line = ts.ReadLine(); if (line == null) throw new Exception ("Invalid TimeSeries file."); tokens = line.Trim().Split(seps, StringSplitOptions.RemoveEmptyEntries); if (tokens == null) throw new Exception("Invalid TimeSeries file."); index = AddInstant(double.Parse(tokens[0], CultureInfo.InvariantCulture)); for (int i = 1; i < tokens.Length; i++) { dataColumns[i - 1].ColumnType = GetColumnType(tokens[i]); if (dataColumns[i - 1].ColumnType == typeof(double)) dataColumns[i - 1][index] = double.Parse(tokens[i], CultureInfo.InvariantCulture); else dataColumns[i - 1][index] = tokens[i]; } while ((line = ts.ReadLine()) != null) { //line = ts.ReadLine(); if (line == null) throw new Exception("Invalid TimeSeries file."); tokens = line.Trim().Split(seps, StringSplitOptions.RemoveEmptyEntries); if (tokens == null) throw new Exception("Invalid TimeSeries file."); if (tokens[0][0] == '<') break; else { index = AddInstant(double.Parse(tokens[0], CultureInfo.InvariantCulture)); for (int i = 1; i < tokens.Length; i++) if (dataColumns[i - 1].ColumnType == typeof(double)) dataColumns[i - 1][index] = double.Parse(tokens[i], CultureInfo.InvariantCulture); else dataColumns[i - 1][index] = tokens[i]; } } while ((line = ts.ReadLine()) != null) extraLines.Add(line); ts.Close(); } catch { ts.Close(); throw; } }
private string LoadCSharpScript(FileName scriptFileName) { TextFile scriptFile; try { scriptFile = new TextFile(scriptFileName); } catch (Exception ex) { throw new Exception ("Error when creating script text object. " + ex.Message); } try { scriptFile.OpenToRead(); } catch (Exception ex) { throw new Exception("Error when opening script file. " + ex.Message); } List<string> script = scriptFile.ReadLines(); int lineNumber = 0; string line; string[] dllName; string[] seps = { ":" }; for (lineNumber = 0; lineNumber < script.Count; lineNumber++) { line = script[lineNumber].Trim(); if (line == "") continue; if (line.StartsWith("//DLLNAME:")) { dllName = line.Split(seps, StringSplitOptions.RemoveEmptyEntries); if (!dllName[1].Trim().EndsWith(".dll")) dllName[1] = dllName[1].Trim() + ".dll"; dllList.Add(dllName[1].Trim()); } } StringBuilder temp = new StringBuilder(); for (lineNumber = 0; lineNumber < script.Count; lineNumber++) temp.AppendLine(script[lineNumber]); scriptFile.Close(); Console.WriteLine("Finished reading script file."); return temp.ToString(); }
public virtual bool Load() { ConfigNode n = Root; Dictionary<string, string> defines = new Dictionary<string, string>(); string [] defSeps = { "=" }; string value; TextFile cf = new TextFile(ConfigFile); cf.OpenToRead(); bool newBlockFound = false; bool newArrayBlockFound = false; int line = 0; try { List<string> lines = cf.ReadLines(); string toRead = ""; string temp; string t; foreach (string t_l in lines) { line++; if ((temp = t_l.Trim()) == "") continue; else if (temp.EndsWith(" _")) { toRead += temp.Substring(0, temp.Length - 2); continue; } else if(temp == "{") { if (newBlockFound) { newBlockFound = false; continue; } else if (newArrayBlockFound) { newArrayBlockFound = false; continue; } else { throw new Exception("Found unexpected '{'"); } } else toRead += temp; if (toRead[0] == '.') //it's a new block { ConfigNode newNode = new ConfigNode(toRead.Substring(1).Trim().ToLower()); newNode.NodeType = NodeType.COMMON; n.ChildNodes.Add(newNode); newNode.Father = n; n = newNode; newBlockFound = true; //Console.WriteLine("New '.' found on line {0}", line.ToString()); } else if (toRead[0] == '+') { ConfigNode newNode = new ConfigNode(toRead.Substring(1).Trim().ToLower()); newNode.NodeType = NodeType.ARRAY; n.ChildNodes.Add(newNode); newNode.Father = n; n = newNode; newArrayBlockFound = true; //Console.WriteLine("New '+' found on line {0}", line.ToString()); } else if (toRead == "}") //it's the end of the current block { n = n.Father; } else if (toRead[0] == '#') //it's a pre-processing directive { if (toRead.StartsWith("#define ")) { string[] tokens = toRead.Substring(8).Split(defSeps, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 2) { defines[tokens[0].Trim()] = tokens[1].Trim(); } } } else if(toRead[0] != '!') //it's a keyword { if (n.NodeType == NodeType.COMMON) { string[] tokens = toRead.Split(keywordSplit, 2, StringSplitOptions.RemoveEmptyEntries); value = tokens[1].Trim(); foreach (KeyValuePair<string, string> def in defines) { t = value.Replace(def.Key, def.Value); value = t; } n.NodeData[tokens[0].Trim().ToLower()] = new KeywordData(value.Trim()); } else if (n.NodeType == NodeType.ARRAY) { value = toRead.Trim(); foreach (KeyValuePair<string, string> def in defines) { t = value.Replace(def.Key, def.Value); value = t; } n.NodeData[(n.NodeData.Count + 1).ToString()] = new KeywordData(value); } } toRead = ""; } cf.Close(); return true; } catch(Exception ex) { exceptionMessage = "Error on line " + line.ToString() + ": " + ex.Message; cf.Close(); return false; } }
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(""); } }