public void Open(IGeoModel model) { Debug.Assert(model != null); using (SkippingLineReader lineReader = new SkippingLineReader(uri.LocalPath, commentBlanksPattern)) { int numSurfaces = ReadHeader(lineReader); // Read the body string line; while ((line = lineReader.ReadLine()) != null) { // Split the fields in the line string name = line.Substring(0, 16).Trim(); string shotPoint = line.Substring(16, 10).Trim(); double x = Double.Parse(line.Substring(26, 10).Trim()); double y = Double.Parse(line.Substring(36, 10).Trim()); // Retrieve the next n values, where n is the number of horizons string[] zs = line.Substring(46).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (zs.Length < numSurfaces) { StringBuilder message = new StringBuilder(); message.AppendFormat("Too few z values at line {0}", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), uri); } double[] z = new double[numSurfaces]; for (int i = 0; i < numSurfaces; ++i) { // Charisma Z values may have non-numeric characters // within the field, such as 1234.45F so we use a regex // to extract the substring the matches a float Match match = Patterns.CFloat.Match(zs[i]); if (match.Success) { z[i] = Double.Parse(match.Value); } else { StringBuilder message = new StringBuilder(); message.AppendFormat("Bad value '{0}' at line {1}", zs[i], lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), uri); } } // TODO: Call the builder here } } }
public override void Open(IGeoModel model) { // Read all of the Z values into an redimensionable array. Regex delimiterPattern = new Regex(@"[\s,;]+"); using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentBlanksPattern)) { try { string line; // TODO: This redimensionable array needs to be a new type // containing a 1-D array, but which we can view as // a 2-D array with different sizes. For now, we use // a List List <double> redimensionableArray = new List <double>(); while ((line = lineReader.ReadLine()) != null) { string[] fields = delimiterPattern.Split(line); foreach (string field in fields) { double value = Double.Parse(field); redimensionableArray.Add(value); } } } catch (FormatException formatException) { StringBuilder message = new StringBuilder(); message.AppendFormat("Invalid field value at line {0}", lineReader.PhysicalLineNumber); throw new OpenException(message.ToString(), Location, formatException); } } // TODO: Write AsciiScanlineZ heuristics // Try to guess the null value using statistics and by testing // commonly used values. Null value could be the most common. // Factorize the length of the z array into a set of factors // Try each pair of common factors to determine which produces // the smoothest grid: Test each node against those to its north // and east - sum the differences. The grid with the smallest // total difference is the smoothest. // TODO: Write AsciiScanlineZ loader }
public void Open(IGeoModel model) { Debug.Assert(uri != null); Debug.Assert(model != null); builder = model.CreateGridBuilder(); int postHeaderLine = ParseHeader(); // Read the body of the grid, skipping comment and blank lines using (SkippingLineReader lineReader = new SkippingLineReader(uri.LocalPath, Cps3.commentBlanksPattern)) { // Skip forward over the header while (lineReader.PhysicalLineNumber < postHeaderLine) { lineReader.ReadLineNoSkip(); } // Parse the body of the grid string line; int counter = 0; while ((line = lineReader.ReadLine()) != null) { string[] fields = Regex.Split(line, @"\s+"); foreach (string field in fields) { double z; if (double.TryParse(field, out z)) { int i = counter % parameters.INum.Value; int j = counter / parameters.INum.Value; if (z != parameters.ZNull) { builder[i, j] = z; } ++counter; } else { StringBuilder message = new StringBuilder(); message.AppendFormat("Bad grid data '{0}' at line {1}", field, lineReader.PhysicalLineNumber); throw new OpenException(message.ToString()); } } } } }
public override Recognition Recognize() { // Check for any EarthVision header tokens at the beginning of the file // We count success at being at least three lines. using (ILineReader lineReader = new SkippingLineReader(Location.LocalPath)) { int matching = 0; for (int i = 0; i < searchLimit; ++i) { string line = lineReader.ReadLine(); if (tokensRegex.IsMatch(line)) { ++matching; if (matching >= 3) { return(Recognition.Yes); } } } return(matching == 0 ? Recognition.No : Recognition.Maybe); } }
public override Recognition Recognize() { // Search through the file, if it contains only numbers // and more than three numbers per line, it might be a z file int searchLimit = 50; // Number of lines to test Regex searchPattern = new Regex(@"\s*(" + Patterns.CFloat + @"\s+){4,}"); using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentBlanksPattern)) { do { string line = lineReader.ReadLine(); if (line != null) { break; } if (!searchPattern.IsMatch(line)) { return(Recognition.No); } }while (lineReader.LogicalLineNumber < searchLimit); } return(Recognition.Maybe); }
private string DetermineType() { int searchLimit = 50; // Number of lines to test // TODO: Assemble this list of patterns by querying the Cps3 Entity Loaders Dictionary <string, Regex> recognitionPatterns = new Dictionary <string, Regex>(); recognitionPatterns.Add("Surface", new Regex(@"^(FSASCI|!\s*NCOL)")); recognitionPatterns.Add("Fault", new Regex(@"^(FFASCI|->)")); using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentBlanksPattern)) { do { string line = lineReader.ReadLine().Trim(); foreach (string type in recognitionPatterns.Keys) { if (recognitionPatterns[type].IsMatch(line)) { return(type); } } }while (lineReader.PhysicalLineNumber < searchLimit); } return("Unknown"); }