/* File: VectorDataSetBuilder.java * Copyright (C) 2002-2003 The University of Iowa * * Created on December 23, 2003, 8:58 AM * by Edward West * * This file is part of the das2 library. * * das2 is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.das2.dataset; import org.das2.datum.Units; import org.das2.datum.Datum; import java.util.*; /** * * @author Edward West */ public class VectorDataSetBuilder { private GapListDouble xTags = new GapListDouble(); private List yValues = new ArrayList(); private List planeIDs = new ArrayList(); { planeIDs.add(""); } private Units xUnits = Units.dimensionless; private Map yUnitsMap = new HashMap(); { yUnitsMap.put("", Units.dimensionless); } private Map properties = new HashMap(); /** Creates a new instance of VectorDataSetBuilder */ public VectorDataSetBuilder( Units xUnits, Units yUnits ) { this.xUnits= xUnits; setYUnits(yUnits); } public void setProperty(String name, Object value) { properties.put(name, value); } public Object getProperty(String name) { return properties.get(name); } public void addProperties(Map map) { properties.putAll(map); } public void addPlane(String name, Units yUnits ) { if (!planeIDs.contains(name)) { planeIDs.add(name); yUnitsMap.put(name, yUnits ); } } public void insertY(double x, double y) { insertY(x, y, ""); } public void insertY(double x, double y, String planeID) { if ( !planeIDs.contains(planeID) ) { throw new IllegalArgumentException( "invalid planeID: "+planeID+", have "+planeIDs ); } if ( Double.isInfinite(x) || Double.isNaN(x) ) { throw new IllegalArgumentException( "x is not finite" ); } int insertionIndex = xTags.indexOf(x); if (planeID == null) { planeID = ""; } if (insertionIndex < 0) { insertionIndex = ~insertionIndex; xTags.add(x); MultiY scan = new MultiY(); scan.put(planeID, y); yValues.add(insertionIndex, scan); } else { MultiY scan = (MultiY)yValues.get(insertionIndex); scan.put(planeID, y); } } /** * Insert method favored when there is a default and one additional plane, * because it's less prone to error when the * one forgets the planeId. (And it's slightly more efficient because * the index search need only be done once.) */ public void insertY( double x, double y, String planeId1, double planeValue1 ) { if ( Double.isInfinite(x) || Double.isNaN(x) ) { throw new IllegalArgumentException( "x is not finite" ); } int insertionIndex = xTags.indexOf(x); if (insertionIndex < 0) { insertionIndex = ~insertionIndex; xTags.add(x); MultiY scan = new MultiY(); scan.put( "", y ); scan.put( planeId1, planeValue1 ); yValues.add(insertionIndex, scan); } else { //throw new IllegalArgumentException("already got value at this index"); MultiY scan = (MultiY)yValues.get(insertionIndex); scan.put( "", y ); scan.put( planeId1, planeValue1 ); } } /** * insert a datum for the default plane */ public void insertY(Datum x, Datum y) { insertY(x, y, ""); } /** * insert a datum for the plane. */ public void insertY(Datum x, Datum y, String planeID) { if (!planeIDs.contains(planeID)) { throw new IllegalArgumentException("invalid planeID: "+planeID+", have "+planeIDs); } double xd = x.doubleValue(xUnits); double yd = y.doubleValue((Units)yUnitsMap.get(planeID)); insertY(xd, yd, planeID); } public void append(VectorDataSet vds) { String[] planeIds= DataSetUtil.getAllPlaneIds(vds); for ( int iplane=0; iplane