package org.autoplot.csv; import com.csvreader.CsvWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.autoplot.datasource.AbstractDataSourceFormat; import org.das2.datum.Datum; import org.das2.datum.Units; import org.das2.datum.format.DatumFormatter; import org.das2.util.monitor.ProgressMonitor; import org.das2.qds.DataSetUtil; import org.das2.qds.QDataSet; import org.das2.qds.SemanticOps; import org.autoplot.datasource.URISplit; /** * Format data to CSV (comma separated values) file. * @author jbf */ public class CsvDataSourceFormat extends AbstractDataSourceFormat { @Override public void formatData(String uri, QDataSet data, ProgressMonitor mon) throws Exception { super.setUri(uri); URISplit split = URISplit.parse( uri ); Map params= URISplit.parseParams(split.params); char delim= ','; if ( params.containsKey("delim") ) { String sdelimiter= params.get("delim"); if ( sdelimiter.equals("COMMA") ) sdelimiter= ","; if ( sdelimiter.equals("SEMICOLON") ) sdelimiter= ";"; delim= sdelimiter.charAt(0); } super.maybeMkdirs(); mon.setTaskSize( data.length() ); mon.started(); File outFile= new File( split.resourceUri ); FileWriter fw= new FileWriter(outFile); CsvWriter writer= null; try { writer= new CsvWriter( fw, delim ); writer.setForceQualifier(true); writer.setUseTextQualifier(true); // force quotes on header String[] values; String[] labels; int col=0; QDataSet[] dss; QDataSet[] wdss; List ldss= new ArrayList(); List lwdss= new ArrayList(); if ( data.property(QDataSet.DEPEND_0)!=null ) { ldss.add( (QDataSet) data.property(QDataSet.DEPEND_0)); lwdss.add( DataSetUtil.weightsDataSet((QDataSet) data.property(QDataSet.DEPEND_0) ) ); col++; } ldss.add(data); lwdss.add(DataSetUtil.weightsDataSet(data)); switch (data.rank()) { case 1: col++; break; case 2: col+= data.length(0); break; default: throw new IllegalArgumentException("rank limit, data must be rank 1 sequence or a rank 2 table of data"); } dss= ldss.toArray( new QDataSet[ldss.size()] ); wdss= lwdss.toArray( new QDataSet[lwdss.size()] ); values= new String[col]; labels= new String[col]; //set the headers { col= 0; for ( int ids=0; ids