Ejemplo n.º 1
0
        public void GetValuesTest()
        {
            var s = new StringBuilder();

            s.AppendLine("Boolean,Byte,Bytes,Char,Chars,DateTime,Decimal,Double,Float,Guid,Short,Int,Long,Null");
            s.AppendLine("true,1,0x0102,a,ab,1/1/2019,1.23,4.56,7.89,eca0c8c6-9a2a-4e6c-8599-3561abda13f1,1,2,3,null");
            using (var reader = new StringReader(s.ToString()))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.Delimiter = ",";
                    csv.Configuration.TypeConverterOptionsCache.GetOptions <string>().NullValues.Add("null");
                    var dataReader = new CsvDataReader(csv);
                    dataReader.Read();

                    Assert.AreEqual(true, dataReader.GetBoolean(0));
                    Assert.AreEqual(1, dataReader.GetByte(1));

                    byte[] byteBuffer = new byte[2];
                    dataReader.GetBytes(2, 0, byteBuffer, 0, byteBuffer.Length);
                    Assert.AreEqual(0x1, byteBuffer[0]);
                    Assert.AreEqual(0x2, byteBuffer[1]);

                    Assert.AreEqual('a', dataReader.GetChar(3));

                    char[] charBuffer = new char[2];
                    dataReader.GetChars(4, 0, charBuffer, 0, charBuffer.Length);
                    Assert.AreEqual('a', charBuffer[0]);
                    Assert.AreEqual('b', charBuffer[1]);

                    Assert.IsNull(dataReader.GetData(0));
                    Assert.AreEqual(DateTime.Parse("1/1/2019"), dataReader.GetDateTime(5));
                    Assert.AreEqual(typeof(string).Name, dataReader.GetDataTypeName(0));
                    Assert.AreEqual(1.23m, dataReader.GetDecimal(6));
                    Assert.AreEqual(4.56d, dataReader.GetDouble(7));
                    Assert.AreEqual(typeof(string), dataReader.GetFieldType(0));
                    Assert.AreEqual(7.89f, dataReader.GetFloat(8));
                    Assert.AreEqual(Guid.Parse("eca0c8c6-9a2a-4e6c-8599-3561abda13f1"), dataReader.GetGuid(9));
                    Assert.AreEqual(1, dataReader.GetInt16(10));
                    Assert.AreEqual(2, dataReader.GetInt32(11));
                    Assert.AreEqual(3, dataReader.GetInt64(12));
                    Assert.AreEqual("Boolean", dataReader.GetName(0));
                    Assert.AreEqual(0, dataReader.GetOrdinal("Boolean"));

                    Assert.AreEqual("true", dataReader.GetString(0));
                    Assert.AreEqual("true", dataReader.GetValue(0));

                    var objectBuffer = new object[14];
                    dataReader.GetValues(objectBuffer);
                    Assert.AreEqual("true", objectBuffer[0]);
                    Assert.AreEqual(DBNull.Value, objectBuffer[13]);
                    Assert.IsTrue(dataReader.IsDBNull(13));
                }
        }
Ejemplo n.º 2
0
        private string AppendWeatherData(CsvDataReader csv, WeatherHist result)
        {
            var rowData = new string[csv.FieldCount + 4];

            csv.GetValues(rowData);
            if (result != null)
            {
                var invariantCulture = CultureInfo.InvariantCulture;
                rowData[rowData.Length - 1] = string.Format(invariantCulture, "{0:0.##}", result.Dd);
                rowData[rowData.Length - 2] = string.Format(invariantCulture, "{0:0.##}", result.Ff);
                rowData[rowData.Length - 3] = string.Format(invariantCulture, "{0:0.##}", result.Thq);
                rowData[rowData.Length - 4] = string.Format(invariantCulture, "{0:0.##}", result.Hs);
            }
            return(string.Join(Settings.Delimiter, rowData));
        }
Ejemplo n.º 3
0
        public void DbNullTest()
        {
            var s = new StringBuilder();

            s.AppendLine(",null");
            using (var reader = new StringReader(s.ToString()))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.HasHeaderRecord = false;
                    csv.Configuration.TypeConverterOptionsCache.GetOptions <string>().NullValues.Add("null");

                    var dataReader = new CsvDataReader(csv);
                    Assert.AreEqual(string.Empty, dataReader.GetValue(0));
                    Assert.AreEqual(DBNull.Value, dataReader.GetValue(1));

                    var values = new object[2];
                    dataReader.GetValues(values);
                    Assert.AreEqual(string.Empty, values[0]);
                    Assert.AreEqual(DBNull.Value, values[1]);
                }
        }
Ejemplo n.º 4
0
        public StreamLinesSample()
        {
            InitializeComponent();


            // We will set the tube path position color based on the Density
            string colorColumnName  = "Density"; // "AngularVelocity"
            bool   invertColorValue = false;     // This needs to be set to true when using AngularVelocity


            // First create a gradient legend and texture
            var gradientStopCollection = new GradientStopCollection();

            gradientStopCollection.Add(new GradientStop(Colors.Red, 1));
            gradientStopCollection.Add(new GradientStop(Colors.Yellow, 0.75));
            gradientStopCollection.Add(new GradientStop(Colors.Lime, 0.5));
            gradientStopCollection.Add(new GradientStop(Colors.Aqua, 0.25));
            gradientStopCollection.Add(new GradientStop(Colors.Blue, 0));

            var linearGradientBrush = new LinearGradientBrush(gradientStopCollection,
                                                              new System.Windows.Point(0, 1),  // startPoint (offset == 0) - note that y axis is down (so 1 is bottom)
                                                              new System.Windows.Point(0, 0)); // endPoint (offset == 1)

            // Create Legend control
            var gradientColorLegend = new GradientColorLegend()
            {
                Width  = 70,
                Height = 200,
                Margin = new Thickness(5, 5, 5, 5)
            };

            gradientColorLegend.GradientBrush = linearGradientBrush;

            var gradientTexture = gradientColorLegend.RenderToTexture(size: 256, isHorizontal: true);

            var imageBrush = new ImageBrush(gradientTexture);

            // IMPORTANT:
            // When texture coordinates have one components (for example y) always set to 0,
            // we need to change the ViewportUnits from the default RelativeToBoundingBox to Absolute.
            imageBrush.ViewportUnits = BrushMappingMode.Absolute;

            var gradientMaterial = new DiffuseMaterial(imageBrush);


            // Now load sample data
            // Sample data was created by using ParaView application and exporting the streamlines into csv file.
            string sampleDataFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\Streamlines.csv");


            // Create csv file reader that can read data from a csv file
            var csvDataReader = new CsvDataReader();

            csvDataReader.ReadFile(sampleDataFileName);


            float minValue, maxValue;

            csvDataReader.GetValuesRange(colorColumnName, out minValue, out maxValue);

            float dataRange = maxValue - minValue;


            var streamlineIndexes = csvDataReader.IndividualObjectIndexes;


            // Create the streamlines
            var allStreamlineBounds = new Rect3D();

            for (var i = 0; i < csvDataReader.IndividualObjectIndexes.Length - 1; i++)
            {
                Rect3D bounds;

                int startIndex = streamlineIndexes[i];
                int endIndex   = streamlineIndexes[i + 1] - 1;

                int dataCount = endIndex - startIndex;

                if (dataCount < 2) // Skip streamlines without any positions or with less then 2 positions
                {
                    continue;
                }

                var positions = csvDataReader.GetPositions(startIndex, dataCount, out bounds);

                allStreamlineBounds.Union(bounds);

                var pathPositions = new Point3DCollection(positions);


                float[] dataValues = csvDataReader.GetValues(colorColumnName, startIndex, dataCount);

                // Generate texture coordinates for each path position
                // Because our texture is one dimensional gradient image (size 256 x 1)
                // we set the x coordinate in range from 0 to 1 (0 = first gradient color; 1 = last gradient color).
                //
                // Note:
                // If we would only set x texture coordinate and preserve y at 0,
                // WPF would not render the texture because the y size would be 0
                // and because by default the ViewportUnits is set to RelativeToBoundingBox,
                // WPF "thinks" the texture is empty.
                // Therefore we need to set the imageBrush.ViewportUnits to Absolute.
                var positionsCount     = pathPositions.Count;
                var textureCoordinates = new PointCollection(positionsCount);
                for (int j = 0; j < dataCount; j++)
                {
                    float relativeDataValue = (dataValues[j] - minValue) / dataRange;

                    if (invertColorValue)
                    {
                        relativeDataValue = 1.0f - relativeDataValue;
                    }

                    textureCoordinates.Add(new Point(relativeDataValue, 0));
                }


                var tubePathMesh3D = new Ab3d.Meshes.TubePathMesh3D(
                    pathPositions: pathPositions,
                    pathPositionTextureCoordinates: textureCoordinates,
                    radius: 0.03,
                    isTubeClosed: true,
                    isPathClosed: false,
                    segments: 8);


                var geometryModel3D = new GeometryModel3D(tubePathMesh3D.Geometry, gradientMaterial);
                //var geometryModel3D = new GeometryModel3D(tubePathMesh3D.Geometry, new DiffuseMaterial(Brushes.Red));
                geometryModel3D.BackMaterial = new DiffuseMaterial(Brushes.DimGray);

                var modelVisual3D = new ModelVisual3D()
                {
                    Content = geometryModel3D
                };

                MainViewport.Children.Add(modelVisual3D);
            }

            Camera1.TargetPosition = allStreamlineBounds.GetCenterPosition();
            Camera1.Distance       = allStreamlineBounds.GetDiagonalLength();


            // Add legend control:
            int legendValuesCount = 5;

            for (int i = 0; i < legendValuesCount; i++)
            {
                float t        = (float)i / (float)(legendValuesCount - 1);
                float oneValue = minValue + t * (maxValue - minValue);

                string valueLegendText = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0.00}", oneValue);
                gradientColorLegend.LegendLabels.Add(new GradientColorLegend.LegendLabel(t, valueLegendText));
            }


            var legendTitleTextBlock = new TextBlock()
            {
                Text                = colorColumnName,
                FontSize            = 14,
                FontWeight          = FontWeights.Bold,
                Foreground          = Brushes.Black,
                HorizontalAlignment = HorizontalAlignment.Center
            };


            var stackPanel = new StackPanel()
            {
                Orientation         = Orientation.Vertical,
                VerticalAlignment   = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Right,
                Margin = new Thickness(0, 0, 5, 5)
            };

            stackPanel.Children.Add(legendTitleTextBlock);
            stackPanel.Children.Add(gradientColorLegend);

            RootGrid.Children.Add(stackPanel);
        }
Ejemplo n.º 5
0
        // Read in rows from a .csv file in a stream
        static async Task <long> importCSV(string src, IMongoCollection <BsonDocument> collection, int import_chunk_size)
        {
            long count     = 0;
            var  documents = new List <BsonDocument>();

            using (var fin = new StreamReader(src))
                using (var csv = new CsvReader(fin))
                    using (var cr = new CsvDataReader(csv))
                    {
                        // Create utility arrays that can hold all elements of the header row and any given data row
                        int amt_cols = cr.FieldCount;
                        Debug.Assert(amt_cols > 0);
                        var headers = new object[amt_cols];
                        var records = new object[amt_cols];
                        logger.Debug($"Parsed {amt_cols} columns in file {src}");
                        var ret = cr.GetValues(headers);
                        Debug.Assert(ret > 0);
                        logger.Debug($"Returned {ret} : " + string.Join(',', headers));

                        // Keep reading until EOF
                        while (cr.Read())
                        {
                            // Read record line
                            ret = cr.GetValues(records);
                            Debug.Assert(ret > 0);

                            // Create a dictionary mapping each header element to its respective record element
                            // Weed out any empty string elements
                            var zipped = headers.Zip(records, (h, r) => new { h, r })
                                         .Where(item => item.r.ToString() != "")
                                         .ToDictionary(item => item.h, item => {
                                int i;
                                double d;
                                string r = item.r.ToString();
                                if (r.StartsWith('$'))
                                {
                                    r = r.Substring(1);
                                }
                                if (int.TryParse(r, out i))
                                {
                                    return(i);
                                }
                                if (double.TryParse(r, out d))
                                {
                                    return(d);
                                }
                                return(item.r);
                            });

                            // Add dictionary to import buffer
                            documents.Add(zipped.ToBsonDocument());
                            ++count;

                            // Add documents in batches to the db
                            if (count % import_chunk_size == 0)
                            {
                                await collection.InsertManyAsync(documents);

                                logger.Debug($"Uploded {import_chunk_size} records to {collection.CollectionNamespace.CollectionName}...");
                                documents.Clear();
                            }
                        }

                        // Add any remaining docs to the db
                        if (documents.Count != 0)
                        {
                            await collection.InsertManyAsync(documents);
                        }
                    }
            return(count);
        }
Ejemplo n.º 6
0
        protected override void BeginProcessing()
        {
            var csvHelperConfig = Configuration ?? new Configuration();

            csvHelperConfig.IncludePrivateMembers = true;
            csvHelperConfig.MemberTypes           = MemberTypes.Fields | MemberTypes.Properties;


            csvHelperConfig.AllowComments    = AllowComments;
            csvHelperConfig.BufferSize       = BufferSize;
            csvHelperConfig.Comment          = CommentChar;
            csvHelperConfig.HasHeaderRecord  = !NoHeaderRecord;
            csvHelperConfig.IgnoreBlankLines = !KeepBlankLines;
            csvHelperConfig.IgnoreQuotes     = IgnoreQuote;
            csvHelperConfig.TrimOptions      = TrimOption;

            var p = MyInvocation.BoundParameters;

            if (p.ContainsKey("Delimiter"))
            {
                csvHelperConfig.Delimiter = Delimiter;
            }

            if (p.ContainsKey("EscapeChar"))
            {
                csvHelperConfig.Escape = EscapeChar;
            }

            if (p.ContainsKey("QuoteChar"))
            {
                csvHelperConfig.Quote = QuoteChar;
            }

            _config = new Config()
            {
                CsvHelperConfiguration = csvHelperConfig,
                InitialCapacity        = InitialCapacity,
                ColumnNames            = ColumnNames,
                ColumnNameMap          = ColumnNameMap,
                ColumnTypes            = ColumnTypes,
                Strict  = Strict,
                Culture = Culture
            };

            if (AsDataTable)
            {
                // The current version does not support combination of -AsDataTable and input stream.

                if (string.IsNullOrEmpty(Path))
                {
                    WriteError(new ErrorRecord(new ArgumentException("-Path is required when -AsDataTable is set"), "", ErrorCategory.InvalidArgument, null));
                    return;
                }

                using (var reader = new StreamReader(Path, Encoding))
                    using (var csvReader = new CsvReader(reader, _config.CsvHelperConfiguration))
                        using (var csvDataReader = new CsvDataReader(csvReader))
                        {
                            var dt = new DataTable();

                            if (ColumnTypes != null)
                            {
                                foreach (DictionaryEntry entry in ColumnTypes)
                                {
                                    dt.Columns.Add((string)entry.Key, (Type)entry.Value);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < csvDataReader.FieldCount; i++)
                                {
                                    dt.Columns.Add(csvDataReader.GetName(i), csvDataReader.GetFieldType(i));
                                }
                            }

                            if (ReadCount > 0)
                            {
                                int rowCount = 0;
                                while (csvDataReader.Read())
                                {
                                    if (rowCount % ReadCount == 0)
                                    {
                                        WriteVerbose($"Starting batch of {ReadCount}  ({rowCount} records processed)");
                                    }

                                    string[] row = new String[csvDataReader.FieldCount];

                                    csvDataReader.GetValues(row);
                                    dt.LoadDataRow(row, true);

                                    rowCount++;

                                    if (rowCount % ReadCount == 0)
                                    {
                                        WriteObject(dt.Copy());
                                        dt.Clear();
                                    }
                                }
                                // Write out remaining rows, if any.
                                if (dt.Rows.Count > 0)
                                {
                                    WriteObject(dt);
                                }
                            }
                            else
                            {
                                dt.Load(csvDataReader);
                                WriteObject(dt);
                            }
                        }

                return;
            }

            if (!string.IsNullOrEmpty(Path))
            {
                using (var reader = new StreamReader(Path, Encoding))
                    using (var loader = new CsvLoader(reader, _config))
                    {
                        LoadFile(loader);
                    }

                return;
            }

            _output        = new BlockingCollection <object>();
            _completeEvent = new ManualResetEvent(false);

            _loader = new CsvLoader(null, _config);

            var thread = new Thread(() => {
                try
                {
                    LoadFile(_loader);
                }
                catch (Exception e)
                {
                    _exception = e;
                }
                finally
                {
                    _completeEvent.Set();
                }
            });

            thread.Name = "Import-Csv2 loader thread";
            thread.Start();
        }