예제 #1
0
        public string ___AddLasFileToFile(LASFile lasFile, string origFilename)
        {

            string res = null;
            // open filestream on append mode

            string path = exportCSVFileName;
            using (StreamWriter sw = File.AppendText(path))
            {

                // get the pre holeID from the filename


                int li = origFilename.LastIndexOf("\\");
                string tempHoleIDa = origFilename.Substring(li);
                li = tempHoleIDa.LastIndexOf(".");
                string tempHoleID = tempHoleIDa.Substring(1, li - 1);
                bool process = true;
                if (tempHoleID.ToLower().StartsWith("wkr"))
                {
                    // do not process - this is a regiuonal file
                    res = "Not processing " + origFilename + ". Reason: REGIONAL FILE";
                    process = false;
                }
                else if (tempHoleID.ToLower().Contains("cal"))
                {
                    // do not process - this is a regiuonal file
                    res = "Not processing " + origFilename + ". Reason: CAL FILE";
                    process = false;
                }


                foreach (string header in lasFile.columnHeaders)
                {

                }
                if (process)
                {
                    foreach (LASDataRow ldr in lasFile.dataRows)
                    {
                        string toWrite = tempHoleID + delimeter + ldr.depth;

                        foreach (double dd in ldr.rowData)
                        {
                            toWrite += delimeter + dd;
                        }
                        // add an extra delimeter to accomodate those files with line speed present
                        if (ldr.records == 1)
                        {
                            toWrite += delimeter;
                        }
                        toWrite += delimeter + lasFile.columnHeaders[0];
                        sw.WriteLine(toWrite);
                    }
                }

            }
            return res;

        }
예제 #2
0
        public List<object> ProcessLASFile(LASFile lasFile, string origFilename, ModelImportStatus mis, Guid currentProjectID, BackgroundWorker backgroundWorker)
        {

            BaseImportTools bit = new BaseImportTools();
            List<object> data = new List<object>();
            data = bit.ImportLasFile(lasFile, origFilename, mis, currentProjectID, backgroundWorker);
            return data;
        
        }
예제 #3
0
        public List <object> ImportLasFile(NKD.Import.LAS.LASFile lasFile, string origFilename, ModelImportStatus mos, Guid currentProjectID, System.ComponentModel.BackgroundWorker backgroundWorker)
        {
            this.currentWorker = backgroundWorker;
            // get the pre holeID from the filename
            List <object>  data = new List <object>();
            LasImportUtils liu  = new LasImportUtils();

            data = liu.ImportLASFile(lasFile, origFilename, mos, currentProjectID, UpdateStatus);

            return(data);
        }
예제 #4
0
        internal List <object> ImportLASFile(NKD.Import.LAS.LASFile lasFile, string origFilename, ModelImportStatus mos, Guid currentProject, Action <string, double> UpdateStatus)
        {
            int    li          = origFilename.LastIndexOf("\\");
            string tempHoleIDa = origFilename.Substring(li);

            li = tempHoleIDa.LastIndexOf(".");
            string tempHoleID = tempHoleIDa.Substring(1, li - 1);
            int    rowCounter = 0;
            // now try and get the hole name from a header item.  Typically the header name might be in
            // WELL in the Well information header section
            string res = lasFile.LookupWellHeaderSection("WELL");

            if (res != null && res.Trim().Length > 0)
            {
                tempHoleID = res;
            }

            List <object> dataList = new List <object>();

            try
            {
                // here we need to create the Geophyiscs data row item
                var entityObj = new NKDC(BaseImportTools.XSTRING, null, false);
                //entityObj.AutoDetectChangesEnabled = false; //TODO: Exhaust this, should be faster now

                var physDataList = new List <Geophysics>();
                //var fDataList = new List<NKD.Module.BusinessObjects.File>();
                var fdDataList = new List <FileData>();

                Geophysics xG = new Geophysics();
                xG.FileName = origFilename;
                Guid gg = Guid.NewGuid();
                xG.GeophysicsID = gg;

                Guid holeGuid = CollarQueries.FindHeaderGuid(tempHoleID, currentProject);
                Guid resHole  = new Guid();
                if (!holeGuid.ToString().Equals(resHole.ToString()))
                {
                    xG.HeaderID = holeGuid;
                }
                Guid           unitGuid = new Guid("2395DE56-8F6F-4B0C-806C-DD2606B9902B"); //FIXME: Magic Number
                UnitQueries    uq       = new UnitQueries();
                DictionaryUnit xu       = uq.FindUnits("m");
                if (xu != null)
                {
                    unitGuid = xu.UnitID;
                }
                xG.DimensionUnitID = unitGuid;
                xG.LasVersion      = string.Format("{0:N1}", lasFile.versionValue);
                xG.LasWrap         = lasFile.versionWrap;
                xG.LasNullValue    = string.Format("{0:N2}", lasFile.nullValue);

                FileStream sr = null;
                try
                {
                    sr = new FileStream(lasFile.filePath, FileMode.Open);
                }
                catch (FileNotFoundException fex)
                {
                    Console.WriteLine("FileNotFoundException:" + fex.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                NKD.Module.BusinessObjects.File F = new NKD.Module.BusinessObjects.File();
                F.LoadFromStream(lasFile.FileName(), sr);
                sr = null;
                Guid fdGUID = Guid.NewGuid();
                var  fD     = new FileData
                {
                    Author       = default(string),
                    FileDataID   = fdGUID,
                    ReferenceID  = xG.GeophysicsID,
                    TableType    = "X_Geophysics",
                    FileName     = F.FileName,
                    FileBytes    = F.FileBytes,
                    FileChecksum = Hash.ComputeHash(F.FileBytes),
                    MimeType     = MimeTypes.MimeTypeHelper.GetMimeTypeByFileName(F.FileName)
                };
                xG.OriginalFileDataID = fD.FileDataID;
                physDataList.Add(xG);
                fdDataList.Add(fD);
                F = null;

                // here we need to add a GeophysicsMetadata item for each column
                Dictionary <string, Guid> metaDataIDLookup = new Dictionary <string, Guid>();
                var unitDataList  = new List <DictionaryUnit>();
                var paramDataList = new List <Parameter>();
                var metaDataList  = new List <GeophysicsMetadata>();

                foreach (string s in lasFile.columnHeaders)
                {
                    Parameter xp = null;
                    xp = GetParameterIDFor(entityObj, "LAS data column", s);

                    //test to see if we already have the unit
                    string splitter = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault().Trim();
                    uq = new UnitQueries();
                    xu = uq.FindUnits(splitter);

                    if (xp != null && xu != null)
                    {
                        xp.UnitID = xu.UnitID;
                        //xp.Unit = xu;
                    }
                    else
                    {
                        bool xpnull = false;
                        if (xp == null)
                        {
                            xpnull = true;
                            xp     = new Parameter();
                            Guid pg = Guid.NewGuid();
                            xp.ParameterID   = pg;
                            xp.ParameterType = "LAS data column";
                            xp.ParameterName = s;
                        }

                        //test to see if we already have added a unit into the list
                        if (unitDataList.Count > 0 && xu == null)
                        {
                            xu = unitDataList.Where(c => c.StandardUnitName == splitter).FirstOrDefault();
                        }
                        if (xu == null)
                        {
                            //create new unit here store it and pass to parameters
                            Guid ug = Guid.NewGuid();
                            xu = new DictionaryUnit
                            {
                                UnitID           = ug,
                                StandardUnitName = splitter,
                                CoalUnitName     = splitter,
                                StrictlySI       = false
                            };

                            unitDataList.Add(xu);
                        }

                        xp.UnitID = xu.UnitID;
                        if (xpnull)
                        {
                            paramDataList.Add(xp);
                        }
                    }

                    Guid gmid = Guid.NewGuid();
                    GeophysicsMetadata xgm = new GeophysicsMetadata
                    {
                        GeophysicsID         = gg,
                        GeophysicsMetadataID = gmid,
                        Unit        = xu.StandardUnitName,
                        Mnemonic    = s,
                        ParameterID = xp.ParameterID
                    };

                    metaDataList.Add(xgm);
                    metaDataIDLookup.Add(s, gmid);
                }

                int insertCounter = 0;
                var geoDataList   = new List <GeophysicsData>();
                foreach (LASDataRow ldr in lasFile.dataRows)
                {
                    double depth = ldr.depth;

                    for (int i = 0; i < ldr.rowData.Count(); i++)
                    {
                        GeophysicsData xd1 = new GeophysicsData();
                        string         s   = lasFile.columnHeaders[i];
                        xd1.GeophysicsDataID = Guid.NewGuid();
                        Guid g     = new Guid();
                        bool found = metaDataIDLookup.TryGetValue(s, out g);
                        if (found)
                        {
                            xd1.GeophysicsMetadataID = g;
                        }
                        xd1.Dimension        = (decimal)depth;
                        xd1.MeasurementValue = (decimal)ldr.rowData[i];

                        geoDataList.Add(xd1);
                    }
                    insertCounter++;
                    rowCounter++;
                }

                lasFile = null;

                dataList.Add(fdDataList);
                dataList.Add(physDataList);
                dataList.Add(unitDataList);
                dataList.Add(paramDataList);
                dataList.Add(metaDataList);
                dataList.Add(geoDataList);

                fdDataList       = null;
                physDataList     = null;
                unitDataList     = null;
                paramDataList    = null;
                metaDataIDLookup = null;
                metaDataList     = null;
                geoDataList      = null;
            }
            catch (Exception ex) {
                mos.errorMessages.Add("Failed to complete import of LAS file: " + origFilename);
                mos.errorMessages.Add("Details: " + ex.Message.ToString());
                if (ex.InnerException != null)
                {
                    mos.errorMessages.Add("Inner Exception: " + ex.InnerException.Message.ToString());
                }
                mos.errorMessages.Add("Row: " + rowCounter);
            }
            mos.recordsAdded = rowCounter;
            return(dataList);
        }
예제 #5
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);
        }
예제 #6
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);
        }