Esempio n. 1
0
        public LASFile ReadDataLines(string inputFile, out int errorCode)
        {
            //Pass the file path and file name to the StreamReader constructor
            LASFile res = new LASFile();

            errorCode = 0;
            List <string> errorInfo = new List <string>();

            StreamReader sr = null;

            try
            {
                sr           = new StreamReader(inputFile);
                res.filePath = inputFile;
            }
            catch (FileNotFoundException fex)
            {
            }
            catch (Exception ex)
            {
            }

            int               columnCount   = 0;
            List <string>     columnHeaders = new List <string>();
            List <LASDataRow> dataRows      = new List <LASDataRow>();

            if (sr != null)
            {
                //Read the first line of text
                string        line               = null;
                bool          inDataSection      = false;
                bool          inCurveSection     = false;
                bool          inWellInfoSection  = false;
                bool          inVersionSection   = false;
                bool          inParameterSection = false;
                int           lineCount          = 0;
                List <string> curveHeaders       = new List <string>();
                try
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        lineCount++;

                        if (!line.StartsWith("#"))
                        {
                            if (line.StartsWith("~"))
                            {
                                // this is the start/end of a seciton, so turn everything off
                                inDataSection      = false;
                                inCurveSection     = false;
                                inWellInfoSection  = false;
                                inVersionSection   = false;
                                inParameterSection = false;
                            }

                            if (line.ToUpper().StartsWith("~C"))
                            {
                                inCurveSection = true;
                            }
                            if (line.ToUpper().StartsWith("~P"))
                            {
                                inParameterSection = true;
                            }
                            if (line.ToUpper().StartsWith("~W"))
                            {
                                inWellInfoSection = true;
                            }
                            if (line.ToUpper().StartsWith("~V"))
                            {
                                inVersionSection = true;
                            }
                            if (line.ToUpper().StartsWith("~A"))
                            {
                                try
                                {
                                    inDataSection = true;
                                    if (curveHeaders.Count < 1)
                                    {
                                        columnHeaders = ParseHeaderLine(line);
                                    }
                                    else
                                    {
                                        columnHeaders = curveHeaders;
                                    }
                                    columnCount = columnHeaders.Count;
                                }
                                catch (Exception ex)
                                {
                                    errorCode = LASErrorCodes.ERROR_PARSING_LAS_HEADERS;
                                    errorInfo.Add(LASErrorCodes.LookupCode(errorCode));
                                    break;
                                }
                            }
                            else
                            {
                                if (inDataSection)
                                {
                                    if (line.Trim().Length > 0)
                                    {
                                        LASDataRow ldr = null;
                                        int        err = 0;
                                        try
                                        {
                                            ldr = ParseDataLine(line, columnCount, out err, res.nullValue);
                                        }
                                        catch (Exception ex) { }
                                        if (ldr != null)
                                        {
                                            dataRows.Add(ldr);
                                        }
                                        else
                                        {
                                            errorInfo.Add("Unable to parse LAS data row at line: " + lineCount);
                                        }
                                    }
                                }
                            }

                            if (inWellInfoSection)
                            {
                                res.AddWellSectionHeaderLine(line);
                                // attempt to find the NULL value
                                if (line.Trim().StartsWith("NULL"))
                                {
                                    // parse the line to get the null value
                                    try
                                    {
                                        // NULL will appear like this:
                                        //NULL.    -999.25                        :NULL VALUE
                                        int    idx      = line.IndexOf('.');
                                        int    endIdx   = line.IndexOf(':');
                                        string leftOver = line.Substring(idx + 1, endIdx - idx - 1);
                                        double nullVal  = Convert.ToDouble(leftOver.Trim());
                                        res.nullValue = nullVal;
                                    }
                                    catch (Exception ex)
                                    {
                                        errorInfo.Add("Could not find NULL value, assuming default of " + string.Format("{0:0.###}", res.nullValue));
                                    }
                                }
                            }
                            if (inParameterSection)
                            {
                                res.AddParameterSectionHeaderLine(line);
                            }
                            if (inVersionSection)
                            {
                                res.AddVersionSectionHeaderLine(line);

                                if (line.Trim().StartsWith("VERS"))
                                {
                                    // parse the line to get the null value
                                    try
                                    {
                                        int    idx        = line.IndexOf('.');
                                        int    endIdx     = line.IndexOf(':');
                                        string leftOver   = line.Substring(idx + 1, endIdx - idx - 1);
                                        double versionVal = Convert.ToDouble(leftOver.Trim());
                                        res.versionValue = versionVal;
                                        if (versionVal != supportedLasVersion)
                                        {
                                            errorInfo.Add("LAS file version '" + versionVal + "' is unsupported.  This software can only be used to view LAS 2.0 files.");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                                else if (line.Trim().StartsWith("WRAP."))
                                {
                                    // parse the line to get the wrap value
                                    try
                                    {
                                        int    idx      = line.IndexOf('.');
                                        int    endIdx   = line.IndexOf(':');
                                        string leftOver = line.Substring(idx + 1, endIdx - idx - 1);
                                        string wrap     = leftOver.Trim();
                                        res.versionWrap = wrap;
                                        if (wrap.ToUpper().Equals("YES"))
                                        {
                                            errorInfo.Add("The selected LAS file has WRAP set to 'YES'.  This feature is not supported by this software, LAS files must have one depth step per line.");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                            }
                            if (inCurveSection)
                            {
                                res.AddCurveSectionHeaderLine(line);
                                // decode a line and give it to the headers section
                                int endIdx = line.IndexOf(':');
                                if (endIdx > 0)
                                {
                                    string secondPart = line.Substring(0, endIdx).Trim();
                                    int    ptIdx      = secondPart.IndexOf('.');
                                    string units      = "";
                                    string colName    = "";
                                    if (ptIdx > 0)
                                    {
                                        colName = secondPart.Substring(0, ptIdx);
                                        units   = secondPart.Substring(ptIdx, secondPart.Length - ptIdx);
                                        int firstSpaceAfterUnits = units.Trim().IndexOf(' ');
                                        if (firstSpaceAfterUnits > 0)
                                        {
                                            units = units.Substring(0, firstSpaceAfterUnits);
                                        }
                                    }
                                    string hName = colName.Trim() + units;
                                    //string leftOver = line.Substring(0, endIdx).Trim();
                                    if (!hName.ToUpper().StartsWith("DEPT"))
                                    {
                                        curveHeaders.Add(hName);
                                    }
                                }
                            }
                        }
                    }
                    if (dataRows.Count == 0)
                    {
                        errorCode = LASErrorCodes.NO_DATA_ROWS_LOADED;
                        errorInfo.Add(LASErrorCodes.LookupCode(errorCode));
                    }
                }
                catch (Exception ex)
                {
                    errorCode = LASErrorCodes.ERROR_READING_LAS_DATA_SECTION;
                    errorInfo.Add(LASErrorCodes.LookupCode(errorCode));
                }


                res.columnHeaders = columnHeaders;
                res.dataRows      = dataRows;
                res.errorDetails  = errorInfo;
                sr.Dispose();
                sr.Close();
            }
            else
            {
                errorCode = LASErrorCodes.STREAM_OPEN_FAILED;
            }
            return(res);
        }
Esempio n. 2
0
        public LASFile ReadCSVLines(string inputFile, int columnOffset, out int errCode)
        {
            //Pass the file path and file name to the StreamReader constructor
            LASFile      res = new LASFile();
            StreamReader sr  = null;

            List <string> errorInfo = new List <string>();

            errCode = 0;
            try
            {
                sr = new StreamReader(inputFile);
            }
            catch (FileNotFoundException fex)
            {
                errCode = LASErrorCodes.CSV_FILE_NOT_FOUND;
            }
            catch (Exception ex)
            {
                errCode = LASErrorCodes.CSV_STREAM_ERROR;;
            }


            if (errCode != 0)
            {
                errorInfo.Add("Error loading file.  Message: " + LASErrorCodes.LookupCode(errCode));
            }

            int               columnCount   = 0;
            int               maxErrors     = 20;
            List <string>     columnHeaders = new List <string>();
            List <LASDataRow> dataRows      = new List <LASDataRow>();

            if (sr != null)
            {
                string headLine = sr.ReadLine();
                try
                {
                    res.columnHeaders = ParseCSVHeaderLine(columnOffset, headLine);
                }
                catch (Exception ex) {
                    errCode = LASErrorCodes.CSV_HEADER_PARSE_FAILED;
                }
                if (errCode == 0)
                {
                    columnCount = res.columnHeaders.Count;

                    string line;
                    //Read the first line of text

                    Dictionary <int, int> inputErrors = new Dictionary <int, int>();
                    int lineNumber = 1;

                    while ((line = sr.ReadLine()) != null)
                    {
                        lineNumber++;
                        int        lineErr = 0;
                        LASDataRow ldr     = ParseCSVLine(columnOffset, line, columnCount, out lineErr, res.nullValue);
                        if (lineErr == 0)
                        {
                            dataRows.Add(ldr);
                        }
                        else
                        {
                            errorInfo.Add("Line: " + lineNumber + " : " + LASErrorCodes.LookupCode(lineErr));
                            if (errorInfo.Count == maxErrors)
                            {
                                errCode = LASErrorCodes.MAERRORS_ENCOUNTERED;
                                errorInfo.Add(LASErrorCodes.LookupCode(errCode));
                                break;
                            }
                        }
                    }

                    res.dataRows = dataRows;
                }
                sr.Dispose();
                sr.Close();
                res.errorDetails = errorInfo;
            }
            return(res);
        }