/// <summary> /// Exports the property columns into Ascii. Each property column is exported into one row (line). /// </summary> /// <param name="strwr">A stream writer to write the ascii data to.</param> /// <param name="columnCollection">The column collection to export.</param> /// <param name="nDataColumns">The number of data columns of the table -> is the number of elements in each property column that must be exported.</param> /// <param name="options">The Ascii export options.</param> protected static void ExportPropertyColumns(StreamWriter strwr, Altaxo.Data.DataColumnCollection columnCollection, int nDataColumns, AsciiExportOptions options) { int nPropColumns = columnCollection.ColumnCount; for (int i = 0; i < nPropColumns; i++) { string columnName = options.ConvertToSaveString(columnCollection.GetColumnName(i)); columnName += "="; bool isTextColumn = columnCollection[i] is Data.TextColumn; for (int j = 0; j < nDataColumns; j++) { if (!columnCollection[i].IsElementEmpty(j)) { string data = options.DataItemToString(columnCollection[i], j); if (options.ExportPropertiesWithName && !isTextColumn && !data.Contains("=")) { strwr.Write(columnName); } strwr.Write(data); } if ((j + 1) < nDataColumns) { strwr.Write(options.SeparatorChar); } else { strwr.WriteLine(); } } } }
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
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