Beispiel #1
0
            public void GetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
            {
                Altaxo.Data.TextColumn s = (Altaxo.Data.TextColumn)obj;
                System.Runtime.Serialization.ISurrogateSelector ss = AltaxoStreamingContext.GetSurrogateSelector(context);
                if (null != ss)
                {
                    System.Runtime.Serialization.ISerializationSurrogate surr =
                        ss.GetSurrogate(typeof(Altaxo.Data.DataColumn), context, out ss);

                    surr.GetObjectData(obj, info, context); // stream the data of the base object
                }
                else
                {
                    ((DataColumn)s).GetObjectData(info, context);
                }

                if (s.m_Count != s.m_Capacity)
                {
                    // instead of the data array itself, stream only the first m_Count
                    // array elements, since only they contain data
                    string[] streamarray = new string[s.m_Count];
                    System.Array.Copy(s.m_Array, streamarray, s.m_Count);
                    info.AddValue("Data", streamarray);
                }
                else // if the array is fully filled, we don't need to save a shrinked copy
                {
                    info.AddValue("Data", s.m_Array);
                }
            }
        public void EhView_CopyParameterNVV()
        {
            System.Windows.Forms.DataObject dao = new System.Windows.Forms.DataObject();
            Altaxo.Data.TextColumn          txt = new Altaxo.Data.TextColumn();
            Altaxo.Data.DoubleColumn        col = new Altaxo.Data.DoubleColumn();
            Altaxo.Data.DoubleColumn        var = new Altaxo.Data.DoubleColumn();

            for (int i = 0; i < _doc.CurrentParameters.Count; i++)
            {
                txt[i] = _doc.CurrentParameters[i].Name;
                col[i] = _doc.CurrentParameters[i].Parameter;
                var[i] = _doc.CurrentParameters[i].Variance;
            }

            Altaxo.Data.DataTable tb = new Altaxo.Data.DataTable();
            tb.DataColumns.Add(txt, "Name", Altaxo.Data.ColumnKind.V, 0);
            tb.DataColumns.Add(col, "Value", Altaxo.Data.ColumnKind.V, 0);
            tb.DataColumns.Add(var, "Variance", Altaxo.Data.ColumnKind.V, 0);
            Altaxo.Worksheet.Commands.EditCommands.WriteAsciiToClipBoardIfDataCellsSelected(
                tb, new Altaxo.Collections.AscendingIntegerCollection(),
                new Altaxo.Collections.AscendingIntegerCollection(),
                new Altaxo.Collections.AscendingIntegerCollection(),
                dao);
            System.Windows.Forms.Clipboard.SetDataObject(dao, true);
        }
Beispiel #3
0
        public override void CopyDataFrom(Altaxo.Data.DataColumn v)
        {
            if (v.GetType() != typeof(Altaxo.Data.TextColumn))
            {
                throw new ArgumentException("Try to copy " + v.GetType() + " to " + this.GetType(), "v"); // throw exception
            }
            Altaxo.Data.TextColumn vd = (Altaxo.Data.TextColumn)v;

            // suggestion, but __not__ implemented:
            // if v is a standalone column, then simply take the dataarray
            // otherwise: copy the data by value
            int oldCount = this.m_Count;

            if (null == vd.m_Array || vd.m_Count == 0)
            {
                m_Array    = null;
                m_Capacity = 0;
                m_Count    = 0;
            }
            else
            {
                m_Array    = (string[])vd.m_Array.Clone();
                m_Capacity = m_Array.Length;
                m_Count    = ((Altaxo.Data.TextColumn)v).m_Count;
            }
            if (oldCount > 0 || m_Count > 0) // message only if really was a change
            {
                NotifyDataChanged(0, oldCount > m_Count? (oldCount):(m_Count), m_Count < oldCount);
            }
        }
Beispiel #4
0
        // -----------------------------------------------------------------------------
        //
        //                        Operators
        //
        // -----------------------------------------------------------------------------

        // ----------------------- Addition operator -----------------------------------
        public static Altaxo.Data.TextColumn operator +(Altaxo.Data.TextColumn c1, Altaxo.Data.TextColumn c2)
        {
            int len = c1.Count < c2.Count ? c1.Count : c2.Count;
            var c3  = new Altaxo.Data.TextColumn(len);

            for (int i = 0; i < len; i++)
            {
                c3._data[i] = c1._data[i] + c2._data[i];
            }
            c3._count = len;
            return(c3);
        }
Beispiel #5
0
        public static Altaxo.Data.TextColumn operator +(Altaxo.Data.TextColumn c1, Altaxo.Data.DateTimeColumn c2)
        {
            int len = c1.Count < c2.Count ? c1.Count : c2.Count;
            var c3  = new Altaxo.Data.TextColumn(len);

            for (int i = 0; i < len; i++)
            {
                c3._data[i] = c1._data[i] + c2.GetValueDirect(i).ToString();
            }

            c3._count = len;

            return(c3);
        }
Beispiel #6
0
            public void Serialize(object obj, Altaxo.Serialization.Xml.IXmlSerializationInfo info)
            {
                Altaxo.Data.TextColumn s = (Altaxo.Data.TextColumn)obj;
                // serialize the base class
                info.AddBaseValueEmbedded(s, typeof(Altaxo.Data.DataColumn));

                if (null == info.GetProperty("Altaxo.Data.DataColumn.SaveAsTemplate"))
                {
                    info.AddArray("Data", s.m_Array, s.m_Count);
                }
                else
                {
                    info.AddArray("Data", s.m_Array, 0);
                }
            }
Beispiel #7
0
            public object Deserialize(object o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object parent)
            {
                Altaxo.Data.TextColumn s = null != o ? (Altaxo.Data.TextColumn)o : new Altaxo.Data.TextColumn();

                // deserialize the base class
                info.GetBaseValueEmbedded(s, typeof(Altaxo.Data.DataColumn), parent);

                int count = info.GetInt32Attribute("Count");

                s._data = new string[count];
                info.GetArray(s._data, count);
                s._capacity = null == s._data ? 0 : s._data.Length;
                s._count    = s._capacity;

                return(s);
            }
Beispiel #8
0
        /// <summary>
        /// This will create a property column as text column with the names of the data columns.
        /// </summary>
        /// <param name="table">The data table.</param>
        public static void CreatePropertyColumnOfColumnNames(Altaxo.Data.DataTable table)
        {
            const string NameColumnName = "ColumnName";

            Altaxo.Data.TextColumn col = table.PropertyColumns[NameColumnName] as Altaxo.Data.TextColumn;

            if (col == null)
            {
                col = new Altaxo.Data.TextColumn();
                table.PropertyColumns.Add(col, NameColumnName, Altaxo.Data.ColumnKind.Label, 0);
            }

            col.Suspend();
            for (int i = table.DataColumnCount - 1; i >= 0; i--)
            {
                col[i] = table.DataColumns.GetColumnName(i);
            }
            col.Resume();
        }
Beispiel #9
0
            public object SetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.ISurrogateSelector selector)
            {
                Altaxo.Data.TextColumn s = (Altaxo.Data.TextColumn)obj;
                System.Runtime.Serialization.ISurrogateSelector ss = AltaxoStreamingContext.GetSurrogateSelector(context);
                if (null != ss)
                {
                    System.Runtime.Serialization.ISerializationSurrogate surr =
                        ss.GetSurrogate(typeof(Altaxo.Data.DataColumn), context, out ss);
                    surr.SetObjectData(obj, info, context, selector);
                }
                else
                {
                    ((DataColumn)s).SetObjectData(obj, info, context, selector);
                }
                s.m_Array    = (string[])(info.GetValue("Data", typeof(string[])));
                s.m_Capacity = null == s.m_Array ? 0 : s.m_Array.Length;
                s.m_Count    = s.m_Capacity;

                return(s);
            }
Beispiel #10
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+)";

            Regex 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);
            }
        }
    public void ImportAscii(AsciiImportOptions impopt, Altaxo.Data.DataTable table)
    {
      string sLine;
      stream.Position=0; // rewind the stream to the beginning
      System.IO.StreamReader sr = new System.IO.StreamReader(stream,System.Text.Encoding.Default,true);
      Altaxo.Data.DataColumnCollection newcols = new Altaxo.Data.DataColumnCollection();
    
      Altaxo.Data.DataColumnCollection newpropcols = new Altaxo.Data.DataColumnCollection();

      // in case a structure is provided, allocate already the columsn
      
      if(null!=impopt.recognizedStructure)
      {
        for(int i=0;i<impopt.recognizedStructure.Count;i++)
        {
          if(impopt.recognizedStructure[i]==typeof(Double))
            newcols.Add(new Altaxo.Data.DoubleColumn());
          else if(impopt.recognizedStructure[i]==typeof(DateTime))
            newcols.Add(new Altaxo.Data.DateTimeColumn());
          else if(impopt.recognizedStructure[i]==typeof(string))
            newcols.Add(new Altaxo.Data.TextColumn());
          else
            newcols.Add(new Altaxo.Data.DBNullColumn());;
        }
      }

      // add also additional property columns if not enough there
      if(impopt.nMainHeaderLines>1) // if there are more than one header line, allocate also property columns
      {
        int toAdd = impopt.nMainHeaderLines-1;
        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=null;
      if(impopt.m_DecimalSeparatorCommaCount>0 || impopt.m_DecimalSeparatorDotCount>0)
      {
        numberFormatInfo = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.CurrentInfo.Clone();

        // analyse the statistics
        if(impopt.m_DecimalSeparatorCommaCount>impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
        {
          numberFormatInfo.NumberDecimalSeparator=",";
          if(numberFormatInfo.NumberGroupSeparator==numberFormatInfo.NumberDecimalSeparator)
            numberFormatInfo.NumberGroupSeparator=""; // in case now the group separator is also comma, remove the group separator
        }
        else if(impopt.m_DecimalSeparatorCommaCount<impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
        {
          numberFormatInfo.NumberDecimalSeparator=".";
          if(numberFormatInfo.NumberGroupSeparator==numberFormatInfo.NumberDecimalSeparator)
            numberFormatInfo.NumberGroupSeparator=""; // in case now the group separator is also comma, remove the group separator
        }
      }
      else // no decimal separator statistics is provided, so retrieve the numberFormatInfo object from the program options or from the current thread
      {
        numberFormatInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
      }


      char [] splitchar = new char[]{impopt.cDelimiter};

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

        string[] substr = sLine.Split(splitchar);
        int cnt = substr.Length;
        for(int k=0;k<cnt;k++)
        {
          if(substr[k].Length==0)
            continue;

          if(k>=newcols.ColumnCount)
            continue;
        
          if(i==0) // is it the column name line
          {
            newcols.SetColumnName(k, substr[k]);
          }
          else // this are threated as additional properties
          {
            ((Data.DataColumn)newpropcols[i-1])[k] = substr[k]; // set the properties
          }
        }
      }
      
      for(int i=0;true;i++)
      {
        sLine = sr.ReadLine();
        if(null==sLine) break;

        string[] substr = sLine.Split(splitchar);
        int cnt = Math.Min(substr.Length,newcols.ColumnCount);
        for(int k=0;k<cnt;k++)
        {
          if(substr[k].Length==0)
            continue;

          if(newcols[k] is Altaxo.Data.DoubleColumn)
          {
            try { ((Altaxo.Data.DoubleColumn)newcols[k])[i] = System.Convert.ToDouble(substr[k],numberFormatInfo); }
            catch {}
          }
          else if( newcols[k] is Altaxo.Data.DateTimeColumn)
          {
            try { ((Altaxo.Data.DateTimeColumn)newcols[k])[i] = System.Convert.ToDateTime(substr[k]); }
            catch {}
          }
          else if( newcols[k] is Altaxo.Data.TextColumn)
          {
            ((Altaxo.Data.TextColumn)newcols[k])[i] = substr[k];
          }
          else if(null==newcols[k] || newcols[k] is Altaxo.Data.DBNullColumn)
          {
            bool bConverted = false;
            double val=Double.NaN;
            DateTime valDateTime=DateTime.MinValue;

            try
            { 
              val = System.Convert.ToDouble(substr[k]);
              bConverted=true;
            }
            catch
            {
            }
            if(bConverted)
            {
              Altaxo.Data.DoubleColumn newc = new Altaxo.Data.DoubleColumn();
              newc[i]=val;
              newcols.Replace(k,newc);
            }
            else
            {
              try
              { 
                valDateTime = System.Convert.ToDateTime(substr[k]);
                bConverted=true;
              }
              catch
              {
              }
              if(bConverted)
              {
                Altaxo.Data.DateTimeColumn newc = new Altaxo.Data.DateTimeColumn();
                newc[i]=valDateTime;
                
                newcols.Replace(k, newc);
              }
              else
              {
                Altaxo.Data.TextColumn newc = new Altaxo.Data.TextColumn();
                newc[i]=substr[k];
                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
      table.Suspend();
      bool tableWasEmptyBefore = table.DataColumns.ColumnCount==0;
      for(int i=0;i<newcols.ColumnCount;i++)
      {
        if(newcols[i] is Altaxo.Data.DBNullColumn) // if the type is undefined, use a new DoubleColumn
          table.DataColumns.CopyOrReplaceOrAdd(i,new Altaxo.Data.DoubleColumn(), newcols.GetColumnName(i));
        else
          table.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)
          table.DataColumns.SetColumnKind(0,Altaxo.Data.ColumnKind.X);

      } // end for loop

      // add the property columns
      for(int i=0;i<newpropcols.ColumnCount;i++)
      {
        table.PropCols.CopyOrReplaceOrAdd(i,newpropcols[i], newpropcols.GetColumnName(i));
      }
      table.Resume();
    } // end of function ImportAscii
Beispiel #12
0
        /// <summary>
        /// Imports a couple of ASCII files into one (!) table. The first column of each file is considered to be the x-column, and if they match another x-column, the newly imported columns will get the same column group.
        /// </summary>
        /// <param name="filenames">An array of filenames to import.</param>
        /// <param name="table">The table the data should be imported to.</param>
        /// <returns>Null if no error occurs, or an error description.</returns>
        public static string ImportMultipleAscii(string[] filenames, Altaxo.Data.DataTable table)
        {
            Altaxo.Data.DataColumn    xcol = null;
            Altaxo.Data.DataColumn    xvalues;
            System.Text.StringBuilder errorList = new System.Text.StringBuilder();
            int lastColumnGroup = 0;

            if (table.DataColumns.ColumnCount > 0)
            {
                lastColumnGroup = table.DataColumns.GetColumnGroup(table.DataColumns.ColumnCount - 1);
                Altaxo.Data.DataColumn xColumnOfRightMost = table.DataColumns.FindXColumnOfGroup(lastColumnGroup);
                xcol = (Altaxo.Data.DoubleColumn)xColumnOfRightMost;
            }

            // add also a property column named "FilePath" if not existing so far
            Altaxo.Data.TextColumn filePathPropCol = (Altaxo.Data.TextColumn)table.PropCols.EnsureExistence("FilePath", typeof(Altaxo.Data.TextColumn), Altaxo.Data.ColumnKind.Label, 0);

            foreach (string filename in filenames)
            {
                Altaxo.Data.DataTable newtable = null;
                using (System.IO.Stream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
                {
                    newtable = null;
                    newtable = Import(stream);
                    stream.Close();
                }

                if (newtable.DataColumns.ColumnCount == 0)
                {
                    continue;
                }


                xvalues = newtable.DataColumns[0];
                bool bMatchsXColumn = false;

                // first look if our default xcolumn matches the xvalues
                if (null != xcol)
                {
                    bMatchsXColumn = ValuesMatch(xvalues, xcol);
                }

                // if no match, then consider all xcolumns from right to left, maybe some fits
                if (!bMatchsXColumn)
                {
                    for (int ncol = table.DataColumns.ColumnCount - 1; ncol >= 0; ncol--)
                    {
                        if ((Altaxo.Data.ColumnKind.X == table.DataColumns.GetColumnKind(ncol)) &&
                            (ValuesMatch(xvalues, table.DataColumns[ncol]))
                            )
                        {
                            xcol            = table.DataColumns[ncol];
                            lastColumnGroup = table.DataColumns.GetColumnGroup(xcol);
                            bMatchsXColumn  = true;
                            break;
                        }
                    }
                }

                // create a new x column if the last one does not match
                if (!bMatchsXColumn)
                {
                    xcol            = (Altaxo.Data.DataColumn)xvalues.Clone();
                    lastColumnGroup = table.DataColumns.GetUnusedColumnGroupNumber();
                    table.DataColumns.Add(xcol, newtable.DataColumns.GetColumnName(0), Altaxo.Data.ColumnKind.X, lastColumnGroup);
                }

                for (int i = 1; i < newtable.DataColumns.ColumnCount; i++)
                {
                    // now add the y-values
                    Altaxo.Data.DataColumn ycol = (Altaxo.Data.DataColumn)newtable.DataColumns[i].Clone();
                    table.DataColumns.Add(ycol,
                                          table.DataColumns.FindUniqueColumnName(newtable.DataColumns.GetColumnName(i)),
                                          Altaxo.Data.ColumnKind.V,
                                          lastColumnGroup);


                    // now set the file name property cell
                    int destcolnumber = table.DataColumns.GetColumnNumber(ycol);
                    filePathPropCol[destcolnumber] = filename;

                    // now set the imported property cells
                    for (int s = 0; s < newtable.PropCols.ColumnCount; s++)
                    {
                        Altaxo.Data.DataColumn dest = table.PropCols.EnsureExistence(newtable.PropCols.GetColumnName(i), newtable.PropCols[i].GetType(), Altaxo.Data.ColumnKind.V, 0);
                        dest.SetValueAt(destcolnumber, table.PropCols[s][i]);
                    }
                }
            } // foreache file

            return(errorList.Length == 0 ? null : errorList.ToString());
        }
    /// <summary>
    /// This will create a property column as text column with the names of the data columns.
    /// </summary>
    /// <param name="table">The data table.</param>
    public static void CreatePropertyColumnOfColumnNames(Altaxo.Data.DataTable table)
    {
      const string NameColumnName="ColumnName";
      
      Altaxo.Data.TextColumn col = table.PropertyColumns[NameColumnName] as Altaxo.Data.TextColumn;

      if(col==null)
      {
        col = new Altaxo.Data.TextColumn();
        table.PropertyColumns.Add(col,NameColumnName,Altaxo.Data.ColumnKind.Label,0);
      }

      col.Suspend();
      for(int i=table.DataColumnCount-1;i>=0;i--)
      {
        col[i] = table.DataColumns.GetColumnName(i);
      }
      col.Resume();

    }
    public void EhView_CopyParameterNVV()
    {
      System.Windows.Forms.DataObject dao = new System.Windows.Forms.DataObject();
      Altaxo.Data.TextColumn txt = new Altaxo.Data.TextColumn();
      Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
      Altaxo.Data.DoubleColumn var = new Altaxo.Data.DoubleColumn();

      for (int i = 0; i < _doc.CurrentParameters.Count; i++)
      {
        txt[i] = _doc.CurrentParameters[i].Name;
        col[i] = _doc.CurrentParameters[i].Parameter;
        var[i] = _doc.CurrentParameters[i].Variance;
      }

      Altaxo.Data.DataTable tb = new Altaxo.Data.DataTable();
      tb.DataColumns.Add(txt, "Name", Altaxo.Data.ColumnKind.V, 0);
      tb.DataColumns.Add(col, "Value", Altaxo.Data.ColumnKind.V, 0);
      tb.DataColumns.Add(var, "Variance", Altaxo.Data.ColumnKind.V, 0);
      Altaxo.Worksheet.Commands.EditCommands.WriteAsciiToClipBoardIfDataCellsSelected(
        tb, new Altaxo.Collections.AscendingIntegerCollection(),
        new Altaxo.Collections.AscendingIntegerCollection(),
        new Altaxo.Collections.AscendingIntegerCollection(),
        dao);
      System.Windows.Forms.Clipboard.SetDataObject(dao, true);
    }
Beispiel #15
0
 public static Altaxo.Data.TextColumn operator +(Altaxo.Data.TextColumn c1, Altaxo.Data.DateTimeColumn c2)
 {
   int len = c1.Count<c2.Count ? c1.Count : c2.Count;
   Altaxo.Data.TextColumn c3 = new Altaxo.Data.TextColumn(len);
   for(int i=0;i<len;i++)
   {
     c3.m_Array[i] = c1.m_Array[i] + c2.GetValueDirect(i).ToString();
   }
   
   
   c3.m_Count=len;
   
   return c3;  
 }
Beispiel #16
0
    // -----------------------------------------------------------------------------
    //
    //                        Operators
    //
    // -----------------------------------------------------------------------------


    // ----------------------- Addition operator -----------------------------------
    public static Altaxo.Data.TextColumn operator +(Altaxo.Data.TextColumn c1, Altaxo.Data.TextColumn c2)
    {
      int len = c1.Count<c2.Count ? c1.Count : c2.Count;
      Altaxo.Data.TextColumn c3 = new Altaxo.Data.TextColumn(len);
      for(int i=0;i<len;i++)
      {
        c3.m_Array[i] = c1.m_Array[i]+c2.m_Array[i];
      }
      c3.m_Count=len;
      return c3;  
    }
    /// <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+)";

      Regex 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);
      }   
    }
Beispiel #18
0
        public void ImportAscii(AsciiImportOptions impopt, Altaxo.Data.DataTable table)
        {
            string sLine;

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

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

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

            if (null != impopt.recognizedStructure)
            {
                for (int i = 0; i < impopt.recognizedStructure.Count; i++)
                {
                    if (impopt.recognizedStructure[i] == typeof(Double))
                    {
                        newcols.Add(new Altaxo.Data.DoubleColumn());
                    }
                    else if (impopt.recognizedStructure[i] == typeof(DateTime))
                    {
                        newcols.Add(new Altaxo.Data.DateTimeColumn());
                    }
                    else if (impopt.recognizedStructure[i] == typeof(string))
                    {
                        newcols.Add(new Altaxo.Data.TextColumn());
                    }
                    else
                    {
                        newcols.Add(new Altaxo.Data.DBNullColumn());
                    };
                }
            }

            // add also additional property columns if not enough there
            if (impopt.nMainHeaderLines > 1) // if there are more than one header line, allocate also property columns
            {
                int toAdd = impopt.nMainHeaderLines - 1;
                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 = null;
            if (impopt.m_DecimalSeparatorCommaCount > 0 || impopt.m_DecimalSeparatorDotCount > 0)
            {
                numberFormatInfo = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.CurrentInfo.Clone();

                // analyse the statistics
                if (impopt.m_DecimalSeparatorCommaCount > impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
                {
                    numberFormatInfo.NumberDecimalSeparator = ",";
                    if (numberFormatInfo.NumberGroupSeparator == numberFormatInfo.NumberDecimalSeparator)
                    {
                        numberFormatInfo.NumberGroupSeparator = ""; // in case now the group separator is also comma, remove the group separator
                    }
                }
                else if (impopt.m_DecimalSeparatorCommaCount < impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
                {
                    numberFormatInfo.NumberDecimalSeparator = ".";
                    if (numberFormatInfo.NumberGroupSeparator == numberFormatInfo.NumberDecimalSeparator)
                    {
                        numberFormatInfo.NumberGroupSeparator = ""; // in case now the group separator is also comma, remove the group separator
                    }
                }
            }
            else // no decimal separator statistics is provided, so retrieve the numberFormatInfo object from the program options or from the current thread
            {
                numberFormatInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
            }


            char [] splitchar = new char[] { impopt.cDelimiter };

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

                string[] substr = sLine.Split(splitchar);
                int      cnt    = substr.Length;
                for (int k = 0; k < cnt; k++)
                {
                    if (substr[k].Length == 0)
                    {
                        continue;
                    }

                    if (k >= newcols.ColumnCount)
                    {
                        continue;
                    }

                    if (i == 0) // is it the column name line
                    {
                        newcols.SetColumnName(k, substr[k]);
                    }
                    else // this are threated as additional properties
                    {
                        ((Data.DataColumn)newpropcols[i - 1])[k] = substr[k]; // set the properties
                    }
                }
            }

            for (int i = 0; true; i++)
            {
                sLine = sr.ReadLine();
                if (null == sLine)
                {
                    break;
                }

                string[] substr = sLine.Split(splitchar);
                int      cnt    = Math.Min(substr.Length, newcols.ColumnCount);
                for (int k = 0; k < cnt; k++)
                {
                    if (substr[k].Length == 0)
                    {
                        continue;
                    }

                    if (newcols[k] is Altaxo.Data.DoubleColumn)
                    {
                        try { ((Altaxo.Data.DoubleColumn)newcols[k])[i] = System.Convert.ToDouble(substr[k], numberFormatInfo); }
                        catch {}
                    }
                    else if (newcols[k] is Altaxo.Data.DateTimeColumn)
                    {
                        try { ((Altaxo.Data.DateTimeColumn)newcols[k])[i] = System.Convert.ToDateTime(substr[k]); }
                        catch {}
                    }
                    else if (newcols[k] is Altaxo.Data.TextColumn)
                    {
                        ((Altaxo.Data.TextColumn)newcols[k])[i] = substr[k];
                    }
                    else if (null == newcols[k] || newcols[k] is Altaxo.Data.DBNullColumn)
                    {
                        bool     bConverted  = false;
                        double   val         = Double.NaN;
                        DateTime valDateTime = DateTime.MinValue;

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

                                newcols.Replace(k, newc);
                            }
                            else
                            {
                                Altaxo.Data.TextColumn newc = new Altaxo.Data.TextColumn();
                                newc[i] = substr[k];
                                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
            table.Suspend();
            bool tableWasEmptyBefore = table.DataColumns.ColumnCount == 0;

            for (int i = 0; i < newcols.ColumnCount; i++)
            {
                if (newcols[i] is Altaxo.Data.DBNullColumn) // if the type is undefined, use a new DoubleColumn
                {
                    table.DataColumns.CopyOrReplaceOrAdd(i, new Altaxo.Data.DoubleColumn(), newcols.GetColumnName(i));
                }
                else
                {
                    table.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)
                {
                    table.DataColumns.SetColumnKind(0, Altaxo.Data.ColumnKind.X);
                }
            } // end for loop

            // add the property columns
            for (int i = 0; i < newpropcols.ColumnCount; i++)
            {
                table.PropCols.CopyOrReplaceOrAdd(i, newpropcols[i], newpropcols.GetColumnName(i));
            }
            table.Resume();
        } // end of function ImportAscii