public void TenEmptyColumns()
    {
      DataColumnCollection d = new DataColumnCollection();

      DataColumn[] cols = new DataColumn[10];
      for(int i=0;i<10;i++)
      {
        cols[i] = new DoubleColumn();
        d.Add(cols[i]);
      }

      Assert.AreEqual(10, d.ColumnCount);
      Assert.AreEqual(0, d.RowCount);
      Assert.AreEqual(false, d.IsDirty);
      Assert.AreEqual(false, d.IsSuspended);

      Assert.AreEqual("A", d.GetColumnName(0));
      Assert.AreEqual("A", d[0].Name);

      Assert.AreEqual("J", d.GetColumnName(9));
      Assert.AreEqual("J", d[9].Name);


      // Test index to column resolution
      for(int i=0;i<10;i++)
        Assert.AreEqual(cols[i], d[i]);

      // test name to column resolution

      for(int i=0;i<10;i++)
      {
        char name = (char)('A'+i);
        Assert.AreEqual(cols[i], d[name.ToString()],"Column to name resolution of col " + name.ToString());
      }
      // test column to number resolution
      for(int i=0;i<10;i++)
        Assert.AreEqual(i, d.GetColumnNumber(cols[i]));

    }
示例#2
0
        /// <summary>
        /// This function searches for patterns like aaa=bbb in the provided string. If it finds such a item, it creates a column named aaa
        /// and stores the value bbb at the same position in it as in the text column.
        /// </summary>
        /// <param name="strg">The string where to search for the patterns described above.</param>
        /// <param name="store">The column collection where to store the newly created columns of properties.</param>
        /// <param name="index">The index into the column where to store the property value.</param>
        public static void ExtractPropertiesFromString(string strg, Altaxo.Data.DataColumnCollection store, int index)
        {
            string pat;

            pat = @"(\S+)=(\S+)";

            var r = new Regex(pat, RegexOptions.Compiled | RegexOptions.IgnoreCase);

            for (Match m = r.Match(strg); m.Success; m = m.NextMatch())
            {
                string propname  = m.Groups[1].ToString();
                string propvalue = m.Groups[2].ToString();

                // System.Diagnostics.Trace.WriteLine("Found the pair " + propname + " : " + propvalue);

                if (!store.ContainsColumn(propname))
                {
                    Altaxo.Data.DataColumn col;
                    if (Altaxo.Serialization.DateTimeParsing.IsDateTime(propvalue))
                    {
                        col = new Altaxo.Data.DateTimeColumn();
                    }
                    else if (Altaxo.Serialization.NumberConversion.IsNumeric(propvalue))
                    {
                        col = new Altaxo.Data.DoubleColumn();
                    }
                    else
                    {
                        col = new Altaxo.Data.TextColumn();
                    }

                    store.Add(col, propname); // add the column to the collection
                }

                // now the column is present we can store the value in it.
                store[propname][index] = new Altaxo.Data.AltaxoVariant(propvalue);
            }
        }
示例#3
0
		/// <summary>
		/// Imports an Ascii stream into a table. The import options have to be known already.
		/// </summary>
		/// <param name="dataTable">The table into which to import.</param>
		/// <param name="stream">The stream to read from.</param>
		/// <param name="streamOriginHint">Stream origin hint. If the stream was opened from a file, you should prepend <see cref=" FileUrlStart"/> to the file name.</param>
		/// <param name="importOptions">The Ascii import options. This parameter can be null, or the options can be not fully specified. In this case the method tries to determine the import options by analyzing the stream.</param>
		/// <exception cref="System.ArgumentNullException">
		/// Argument importOptions is null
		/// or
		/// Argument table is null
		/// </exception>
		/// <exception cref="System.ArgumentException">Argument importOptions: importOptions must be fully specified, i.e. all elements of importOptions must be valid. Please run a document analysis in-before to get appropriate values.</exception>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// Unconsidered AsciiColumnType:  + impopt.RecognizedStructure[i].ToString()
		/// or
		/// Unknown switch case:  + impopt.HeaderLinesDestination.ToString()
		/// </exception>
		private static void InternalImportFromAsciiStream(this DataTable dataTable, Stream stream, string streamOriginHint, ref AsciiImportOptions importOptions)
		{
			if (null == importOptions || !importOptions.IsFullySpecified)
			{
				var analysisOptions = GetDefaultAsciiDocumentAnalysisOptions(dataTable);
				importOptions = AsciiDocumentAnalysis.Analyze(importOptions ?? new AsciiImportOptions(), stream, analysisOptions);
			}

			if (null == importOptions)
				throw new InvalidDataException("Import options could not be determined from the data stream. Possibly, the data stream is empty or it is not an Ascii data stream");
			if (!importOptions.IsFullySpecified)
				throw new InvalidDataException("Import options could not be fully determined from the data stream. Possibly, the data stream is empty or it is not an Ascii data stream");

			string sLine;
			stream.Position = 0; // rewind the stream to the beginning
			StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default, true);
			DataColumnCollection newcols = new DataColumnCollection();

			DataColumnCollection newpropcols = new DataColumnCollection();

			// in case a structure is provided, allocate already the columsn

			if (null != importOptions.RecognizedStructure)
			{
				for (int i = 0; i < importOptions.RecognizedStructure.Count; i++)
				{
					switch (importOptions.RecognizedStructure[i].ColumnType)
					{
						case AsciiColumnType.Double:
							newcols.Add(new DoubleColumn());
							break;

						case AsciiColumnType.Int64:
							newcols.Add(new DoubleColumn());
							break;

						case AsciiColumnType.DateTime:
							newcols.Add(new DateTimeColumn());
							break;

						case AsciiColumnType.Text:
							newcols.Add(new TextColumn());
							break;

						case AsciiColumnType.DBNull:
							newcols.Add(new DBNullColumn());
							break;

						default:
							throw new ArgumentOutOfRangeException("Unconsidered AsciiColumnType: " + importOptions.RecognizedStructure[i].ToString());
					}
				}
			}

			// add also additional property columns if not enough there
			if (importOptions.NumberOfMainHeaderLines.HasValue && importOptions.NumberOfMainHeaderLines.Value > 0) // if there are more than one header line, allocate also property columns
			{
				int toAdd = importOptions.NumberOfMainHeaderLines.Value;
				for (int i = 0; i < toAdd; i++)
					newpropcols.Add(new Data.TextColumn());
			}

			// if decimal separator statistics is provided by impopt, create a number format info object
			System.Globalization.NumberFormatInfo numberFormatInfo = importOptions.NumberFormatCulture.NumberFormat;
			System.Globalization.DateTimeFormatInfo dateTimeFormat = importOptions.DateTimeFormatCulture.DateTimeFormat;

			var notesHeader = new System.Text.StringBuilder();
			notesHeader.Append("Imported");
			if (!string.IsNullOrEmpty(streamOriginHint))
				notesHeader.AppendFormat(" from {0}", streamOriginHint);
			notesHeader.AppendFormat(" at {0}", DateTime.Now);
			notesHeader.AppendLine();

			// first of all, read the header if existent
			for (int i = 0; i < importOptions.NumberOfMainHeaderLines; i++)
			{
				sLine = sr.ReadLine();
				if (null == sLine) break;

				var tokens = new List<string>(importOptions.SeparationStrategy.GetTokens(sLine));
				if (i == importOptions.IndexOfCaptionLine) // is it the column name line
				{
					for (int k = 0; k < tokens.Count; ++k)
					{
						var ttoken = tokens[k].Trim();
						if (!string.IsNullOrEmpty(ttoken))
						{
							string newcolname = newcols.FindUniqueColumnName(ttoken);
							newcols.SetColumnName(k, newcolname);
						}
					}
					continue;
				}

				switch (importOptions.HeaderLinesDestination)
				{
					case AsciiHeaderLinesDestination.Ignore:
						break;

					case AsciiHeaderLinesDestination.ImportToNotes:
						AppendLineToTableNotes(notesHeader, sLine);
						break;

					case AsciiHeaderLinesDestination.ImportToProperties:
						FillPropertyColumnWithTokens(newpropcols[i], tokens);
						break;

					case AsciiHeaderLinesDestination.ImportToPropertiesOrNotes:
						if (tokens.Count == importOptions.RecognizedStructure.Count)
							FillPropertyColumnWithTokens(newpropcols[i], tokens);
						else
							AppendLineToTableNotes(notesHeader, sLine);
						break;

					case AsciiHeaderLinesDestination.ImportToPropertiesAndNotes:
						FillPropertyColumnWithTokens(newpropcols[i], tokens);
						AppendLineToTableNotes(notesHeader, sLine);
						break;

					default:
						throw new ArgumentOutOfRangeException("Unknown switch case: " + importOptions.HeaderLinesDestination.ToString());
				}
			}

			// now the data lines
			for (int i = 0; true; i++)
			{
				sLine = sr.ReadLine();
				if (null == sLine)
					break;

				int maxcolumns = newcols.ColumnCount;

				int k = -1;
				foreach (string token in importOptions.SeparationStrategy.GetTokens(sLine))
				{
					k++;
					if (k >= maxcolumns)
						break;

					if (string.IsNullOrEmpty(token))
						continue;

					if (newcols[k] is DoubleColumn)
					{
						double val;
						if (double.TryParse(token, System.Globalization.NumberStyles.Any, numberFormatInfo, out val))
							((DoubleColumn)newcols[k])[i] = val;
					}
					else if (newcols[k] is DateTimeColumn)
					{
						DateTime val;
						if (DateTime.TryParse(token, dateTimeFormat, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out val))
							((DateTimeColumn)newcols[k])[i] = val;
					}
					else if (newcols[k] is TextColumn)
					{
						((TextColumn)newcols[k])[i] = token.Trim();
					}
					else if (null == newcols[k] || newcols[k] is DBNullColumn)
					{
						bool bConverted = false;
						double val = Double.NaN;
						DateTime valDateTime = DateTime.MinValue;

						try
						{
							val = System.Convert.ToDouble(token);
							bConverted = true;
						}
						catch
						{
						}
						if (bConverted)
						{
							DoubleColumn newc = new DoubleColumn();
							newc[i] = val;
							newcols.Replace(k, newc);
						}
						else
						{
							try
							{
								valDateTime = System.Convert.ToDateTime(token);
								bConverted = true;
							}
							catch
							{
							}
							if (bConverted)
							{
								DateTimeColumn newc = new DateTimeColumn();
								newc[i] = valDateTime;

								newcols.Replace(k, newc);
							}
							else
							{
								TextColumn newc = new TextColumn();
								newc[i] = token;
								newcols.Replace(k, newc);
							}
						} // end outer if null==newcol
					}
				} // end of for all cols
			} // end of for all lines

			// insert the new columns or replace the old ones
			using (var suspendToken = dataTable.SuspendGetToken())
			{
				bool tableWasEmptyBefore = dataTable.DataColumns.ColumnCount == 0;
				for (int i = 0; i < newcols.ColumnCount; i++)
				{
					if (newcols[i] is DBNullColumn) // if the type is undefined, use a new DoubleColumn
						dataTable.DataColumns.CopyOrReplaceOrAdd(i, new DoubleColumn(), newcols.GetColumnName(i));
					else
						dataTable.DataColumns.CopyOrReplaceOrAdd(i, newcols[i], newcols.GetColumnName(i));

					// set the first column as x-column if the table was empty before, and there are more than one column
					if (i == 0 && tableWasEmptyBefore && newcols.ColumnCount > 1)
						dataTable.DataColumns.SetColumnKind(0, ColumnKind.X);
				} // end for loop

				// add the property columns
				for (int i = 0, j = 0; i < newpropcols.ColumnCount; i++)
				{
					if (newpropcols[i].Count == 0)
						continue;
					dataTable.PropCols.CopyOrReplaceOrAdd(j, newpropcols[i], newpropcols.GetColumnName(i));
					++j;
				}

				dataTable.Notes.Write(notesHeader.ToString());

				suspendToken.Dispose();
			}
		} // end of function ImportAscii
示例#4
0
		/// <summary>
		/// Retrieves the data columns from an Origin table.
		/// </summary>
		/// <param name="wks">The Origin worksheet to retrieve the data from.</param>
		/// <returns>The data columns with the data from the Origin worksheet.</returns>
		public static Altaxo.Data.DataColumnCollection GetDataColumns(this Origin.Worksheet wks)
		{
			var culture = Current.PropertyService.GetValue(Altaxo.Settings.CultureSettings.PropertyKeyDocumentCulture, Altaxo.Main.Services.RuntimePropertyKind.UserAndApplicationAndBuiltin).Culture;

			int nCols = wks.Cols;

			Altaxo.Data.DataColumnCollection result = new Altaxo.Data.DataColumnCollection();

			for (int c = 0; c < nCols; c++)
			{
				var srcCol = wks.Columns[c];

				Altaxo.Data.DataColumn destCol = GetAltaxoColumnFromOriginDataFormat(srcCol.DataFormat);

				int groupNumber = -1;

				var altaxoColumnKind = OriginToAltaxoColumnKind(srcCol.Type);

				if (altaxoColumnKind == Altaxo.Data.ColumnKind.X)
					groupNumber++;

				if (destCol is DoubleColumn)
				{
					var data = srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_NUMERIC, 0, -1, 0);
					(destCol as DoubleColumn).Array = (double[])data;
				}
				else if (destCol is DateTimeColumn)
				{
					var data = (double[])srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_NUMERIC, 0, -1, 0);
					const double refDateAsDouble = 2451910; // this is the number of days in julian calendar belonging to the date below...
					DateTime refDate = DateTime.Parse("2001-01-01", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal);
					for (int i = data.Length - 1; i >= 0; --i)
					{
						destCol[i] = refDate.AddDays(data[i] - refDateAsDouble);
					}
				}
				else if (destCol is TextColumn && srcCol.DataFormat == COLDATAFORMAT.DF_TEXT_NUMERIC)
				{
					var data = srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_VARIANT, 0, -1, 0);

					var destColNum = new Altaxo.Data.DoubleColumn();

					object[] oarr;
					if (null != (oarr = data as object[]))
					{
						int numberOfNums = 0;
						int numberOfObjects = 0;
						for (int i = 0; i < oarr.Length; ++i)
						{
							if (oarr[i] is double)
							{
								destColNum[i] = (double)oarr[i];
								++numberOfNums;
							}
							if (oarr[i] != null)
							{
								destCol[i] = string.Format(culture, "{0}", oarr[i]);
								++numberOfObjects;
							}
						}
						// if the column consist mostly of numerics, then exchange it with destcol
						if (numberOfNums >= 0.99 * numberOfObjects || numberOfNums >= numberOfObjects - 2)
						{
							destCol = destColNum;
						}
					}
				}
				else if (destCol is TextColumn)
				{
					var data = srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_TEXT, 0, -1, 0);
					string[] sarr;
					object[] oarr;

					if (null != (sarr = data as string[]))
					{
						(destCol as TextColumn).Array = sarr;
					}
					else if (null != (oarr = data as object[]))
					{
						for (int i = 0; i < oarr.Length; ++i)
						{
							if (null != oarr[i])
							{
								destCol[i] = string.Format(culture, "{0}", oarr[i]);
							}
							else
							{
								destCol[i] = null;
							}
						}
					}
				}

				result.Add(destCol, (!string.IsNullOrEmpty(srcCol.LongName) ? srcCol.LongName : srcCol.Name), altaxoColumnKind, Math.Max(0, groupNumber));
			}

			return result;
		}
示例#5
0
        /// <summary>
        /// Retrieves the data columns from an Origin table.
        /// </summary>
        /// <param name="wks">The Origin worksheet to retrieve the data from.</param>
        /// <returns>The data columns with the data from the Origin worksheet.</returns>
        public static Altaxo.Data.DataColumnCollection GetDataColumns(this Origin.Worksheet wks)
        {
            var culture = Current.PropertyService.GetValue(Altaxo.Settings.CultureSettings.PropertyKeyDocumentCulture, Altaxo.Main.Services.RuntimePropertyKind.UserAndApplicationAndBuiltin).Culture;

            int nCols = wks.Cols;

            var result = new Altaxo.Data.DataColumnCollection();

            for (int c = 0; c < nCols; c++)
            {
                var srcCol = wks.Columns[c];

                Altaxo.Data.DataColumn destCol = GetAltaxoColumnFromOriginDataFormat(srcCol.DataFormat);

                int groupNumber = -1;

                var altaxoColumnKind = OriginToAltaxoColumnKind(srcCol.Type);

                if (altaxoColumnKind == Altaxo.Data.ColumnKind.X)
                {
                    groupNumber++;
                }

                if (destCol is DoubleColumn)
                {
                    var data = srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_NUMERIC, 0, -1, 0);
                    (destCol as DoubleColumn).Array = (double[])data;
                }
                else if (destCol is DateTimeColumn)
                {
                    var          data            = (double[])srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_NUMERIC, 0, -1, 0);
                    const double refDateAsDouble = 2451910; // this is the number of days in julian calendar belonging to the date below...
                    var          refDate         = DateTime.Parse("2001-01-01", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal);
                    for (int i = data.Length - 1; i >= 0; --i)
                    {
                        destCol[i] = refDate.AddDays(data[i] - refDateAsDouble);
                    }
                }
                else if (destCol is TextColumn && srcCol.DataFormat == COLDATAFORMAT.DF_TEXT_NUMERIC)
                {
                    var data = srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_VARIANT, 0, -1, 0);

                    var destColNum = new Altaxo.Data.DoubleColumn();

                    object[] oarr;
                    if (null != (oarr = data as object[]))
                    {
                        int numberOfNums    = 0;
                        int numberOfObjects = 0;
                        for (int i = 0; i < oarr.Length; ++i)
                        {
                            if (oarr[i] is double)
                            {
                                destColNum[i] = (double)oarr[i];
                                ++numberOfNums;
                            }
                            if (oarr[i] != null)
                            {
                                destCol[i] = string.Format(culture, "{0}", oarr[i]);
                                ++numberOfObjects;
                            }
                        }
                        // if the column consist mostly of numerics, then exchange it with destcol
                        if (numberOfNums >= 0.99 * numberOfObjects || numberOfNums >= numberOfObjects - 2)
                        {
                            destCol = destColNum;
                        }
                    }
                }
                else if (destCol is TextColumn)
                {
                    var      data = srcCol.GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_TEXT, 0, -1, 0);
                    string[] sarr;
                    object[] oarr;

                    if (null != (sarr = data as string[]))
                    {
                        (destCol as TextColumn).Array = sarr;
                    }
                    else if (null != (oarr = data as object[]))
                    {
                        for (int i = 0; i < oarr.Length; ++i)
                        {
                            if (null != oarr[i])
                            {
                                destCol[i] = string.Format(culture, "{0}", oarr[i]);
                            }
                            else
                            {
                                destCol[i] = null;
                            }
                        }
                    }
                }

                result.Add(destCol, (!string.IsNullOrEmpty(srcCol.LongName) ? srcCol.LongName : srcCol.Name), altaxoColumnKind, Math.Max(0, groupNumber));
            }

            return(result);
        }
      } // end serialization

      public ClipboardMemento(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
      {
        _collection = new DataColumnCollection();

        int numberOfColumns = info.GetInt32("ColumnCount");
        for(int nCol=0;nCol<numberOfColumns;nCol++)
        {
          string name = info.GetString("ColumnName_"+nCol.ToString());
          int    group = info.GetInt32("ColumnGroup_"+nCol.ToString());
          ColumnKind kind = (ColumnKind)info.GetValue("ColumnKind_"+nCol.ToString(),typeof(ColumnKind));

          DataColumn column = (DataColumn)info.GetValue("Column_"+nCol.ToString(),typeof(DataColumn));
        
        
          _collection.Add(column,name,kind,group);
        }
      }