Example #1
0
        private static int ReadHeader(SkippingLineReader lineReader)
        {
            int numSurfaces = 0;
            // Read the header
            int searchLimit = 50;

            do
            {
                string line = lineReader.ReadLineNoSkip().Trim();
                if (Charisma.surfaceRegex.IsMatch(line))
                {
                    // The header line has been located
                    // TODO Extract the Charisma surface names, and
                    //      and determine how many there are.
                    numSurfaces = 1; // TODO: Fix numSurfaces
                    break;
                }
            }while (lineReader.PhysicalLineNumber < searchLimit);

            if (numSurfaces == 0)
            {
                throw new OpenException("No surfaces found in Charisma file");
            }
            return(numSurfaces);
        }
Example #2
0
        public override void Open(IGeoModel model)
        {
            Debug.Assert(model != null);
            builder = model.CreateGridBuilder();
            Debug.Assert(builder != null);
            using (ILineReader lineReader = new SkippingLineReader(Location.LocalPath))
            {
                ReadHeader(lineReader);
                BuildGridParameters();
                switch (maxFieldCode)
                {
                case 3:
                    // XYZ points not supported
                    // TODO: Signal not supported
                    break;

                case 5:
                    ReadFiveColumnGrid(lineReader);
                    break;

                case 7:
                    ReadFiveColumnGrid(lineReader);     // Ignore the ID and name columns
                    break;

                default:
                    // Unknown
                    // TODO: Inform user.
                    break;
                }
            }
            builder.Build();
        }
Example #3
0
 public override Recognition Recognize()
 {
     using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentHistoryBlanksPattern))
     {
         string type = DetermineType(lineReader);
         if (typeLoaders.ContainsKey(type))
         {
             return(Recognition.Yes);
         }
     }
     return(Recognition.No);
 }
Example #4
0
        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
                }
            }
        }
Example #5
0
        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
        }
Example #6
0
        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());
                        }
                    }
                }
            }
        }
Example #7
0
 private void OpenSurferAscii()
 {
     using (ILineReader reader = new SkippingLineReader(Location.LocalPath, blanksPattern))
     {
         // TODO: Create a wrapper for the StreamReader which
         // skips comment lines and tracks line numbers
         try
         {
             ReadAsciiHeader(reader);
             parameters.VerifyAndCompleteParameters();
             parameters.Build(builder);
             ReadAsciiBody(reader);
         }
         catch (System.FormatException)
         {
             StringBuilder message = new StringBuilder();
             message.AppendFormat("Invalid grid format at line {0}", reader.PhysicalLineNumber);
             throw new OpenException(message.ToString());
         }
     }
 }
Example #8
0
 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);
     }
 }
Example #9
0
        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);
        }
Example #10
0
        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");
        }
Example #11
0
        public override void Open(IGeoModel model)
        {
            using (SkippingLineReader lineReader = new SkippingLineReader(Location.LocalPath, commentHistoryBlanksPattern))
            {
                string type = DetermineType(lineReader);
                if (!typeLoaders.ContainsKey(type))
                {
                    StringBuilder message = new StringBuilder();
                    message.AppendFormat("Unrecognized ZMap object type code '{0}' at line {1}", type, lineReader.PhysicalLineNumber);
                    throw new OpenException(message.ToString(), Location);
                }
                string key = typeLoaders[type];
                if (!Factory <IZMapReader> .Instance.ContainsKey(key))
                {
                    StringBuilder message = new StringBuilder();
                    message.AppendFormat("ZMap object type code '{0}' recognized at line {1}, but no loader has been implemented.", type, lineReader.PhysicalLineNumber);
                    throw new OpenException(message.ToString(), Location);
                }
                lineReader.UnreadLine();
                IZMapReader zMapReader = Factory <IZMapReader> .Instance.Create(key);

                zMapReader.Read(lineReader, model);
            }
        }