package org.autoplot.matsupport; import com.jmatio.io.MatFileWriter; import com.jmatio.types.MLArray; import com.jmatio.types.MLDouble; import java.io.File; import java.util.ArrayList; import java.util.List; import org.das2.datum.Units; import org.das2.datum.UnitsConverter; import org.das2.datum.UnitsUtil; import org.das2.util.monitor.ProgressMonitor; import org.das2.qds.QDataSet; import org.das2.qds.SemanticOps; import org.autoplot.datasource.AbstractDataSourceFormat; import org.das2.qds.DataSetUtil; import org.das2.qds.ops.Ops; /** * Export to .mat file for easy import into Matlab. rank 1 datasets, rank 2 datasets, rank 3 datasets, and rank 2 bundles are supported. * jmatio library is used to format the files: https://github.com/gradusnikov/jmatio/ copied on 2018-07-28. * @author jbf */ public class MatDataSourceFormat extends AbstractDataSourceFormat { private MLArray formatRank1( QDataSet data, String guessName ) { String su= getParam( "tunits", "t1970" ); QDataSet wds= Ops.valid(data); double[] dd= new double[data.length()]; for ( int i=0; i formatRank2Bundle( String uri, QDataSet data ) throws Exception { setUri(uri); List stage= new ArrayList<>(); for ( int i=0; i stage= new ArrayList<>(); QDataSet dep0= (QDataSet) data.property(QDataSet.DEPEND_0); if ( dep0!=null ) { stage.add( formatRank1( dep0, "dep0" ) ); } if ( SemanticOps.isBundle(data) ) { stage.addAll( formatRank2Bundle( uri, data ) ); } else { switch (data.rank()) { case 2: stage.add( formatRank2( data, name ) ); break; case 1: stage.add( formatRank1( data, name ) ); break; default: throw new IllegalArgumentException("unsupported rank: "+data.rank()); } } QDataSet dep1= (QDataSet) data.property(QDataSet.DEPEND_1); if ( dep1!=null ) { if ( dep1.rank()==2 ) { stage.add( formatRank2( dep1, "dep1" ) ); } else { stage.add( formatRank1( dep1, "dep1" ) ) ; } } setUri(uri); File f= new File( getResourceURI().toURL().getFile() ); MatFileWriter w= new MatFileWriter(); w.write(f, stage); } @Override public boolean canFormat(QDataSet ds) { return DataSetUtil.isQube(ds) && ( ds.rank()==1 || ds.rank()==2 ); } @Override public String getDescription() { return "Matlab .mat file"; } }