/* * DataSetUtil.java * * Created on January 26, 2004, 6:03 PM */ package org.das2.dataset; import org.das2.datum.CacheTag; import org.das2.datum.DatumRange; import org.das2.datum.Units; import org.das2.datum.DatumVector; import org.das2.datum.Datum; import java.util.*; /** * * @author Jeremy */ public class DataSetUtil { public static CacheTag guessCacheTag( DataSet ds ) { if ( ds.getProperty(DataSet.PROPERTY_CACHE_TAG)!=null ) { return (CacheTag)ds.getProperty(DataSet.PROPERTY_CACHE_TAG); } else { Datum start= ds.getXTagDatum(0); Datum end= ds.getXTagDatum( ds.getXLength()-1 ); Datum resolution= ds.getXTagDatum(1).subtract( start ); return new CacheTag( start, end, resolution ); } } /** * returns the xrange of the dataset. This assumes that the xtags * are monotonic. * @param ds * @return */ public static DatumRange xRange( DataSet ds ) { int n=ds.getXLength(); return new DatumRange( ds.getXTagDatum(0), ds.getXTagDatum(n-1) ); } private static DatumRange yRangeTDS( TableDataSet ds ) { DatumRange result=null; if ( ds.tableCount()==0 ) { return new DatumRange(0,10,ds.getYUnits()); } else { for ( int i=0; i1 ) { Units units= table.getXUnits(); double min= Math.abs( table.getXTagDouble(1,units) - table.getXTagDouble( 0,units) ); for ( int i=2; i(-(insertion point) - 1). (See Arrays.binarySearch) */ public static int xTagBinarySearch( DataSet ds, Datum datum, int low, int high ) { Units units= datum.getUnits(); double key= datum.doubleValue(units); while (low <= high) { int mid = (low + high) >> 1; double midVal = ds.getXTagDouble(mid,units); int cmp; if (midVal < key) { cmp = -1; // Neither val is NaN, thisVal is smaller } else if (midVal > key) { cmp = 1; // Neither val is NaN, thisVal is larger } else { long midBits = Double.doubleToLongBits(midVal); long keyBits = Double.doubleToLongBits(key); cmp = (midBits == keyBits ? 0 : // Values are equal (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) 1)); // (0.0, -0.0) or (NaN, !NaN) } if (cmp < 0) low = mid + 1; else if (cmp > 0) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. } public static int closestColumn( DataSet table, Datum datum ) { int result= xTagBinarySearch( table, datum, 0, table.getXLength()-1 ); if (result == -1) { result = 0; //insertion point is 0 } else if (result < 0) { result= ~result; // usually this is the case if ( result >= table.getXLength()-1 ) { result= table.getXLength()-1; } else { double x= datum.doubleValue( datum.getUnits() ); double x0= table.getXTagDouble(result-1, datum.getUnits() ); double x1= table.getXTagDouble(result, datum.getUnits() ); result= ( ( x-x0 ) / ( x1 - x0 ) < 0.5 ? result-1 : result ); } } return result; } public static int closestColumn( DataSet table, double x, Units units ) { return closestColumn( table, units.createDatum(x) ); } /** * finds the dataset column closest to the value, starting the search at guessIndex. * Handles monotonically increasing or decreasing tags. * @param table * @param xdatum * @param guessIndex * @return */ public static int closestColumn( DataSet table, Datum xdatum, int guessIndex ) { int monotonicDir= 1; int result= guessIndex; int len= table.getXLength(); if ( len==1 ) return 0; if ( result>=len-1 ) result=len-2; if ( result<0 ) result=0; Units units= xdatum.getUnits(); if ( table.getXTagDouble(result, units) > table.getXTagDouble(result+1, units ) ) { monotonicDir= -1; } double x= xdatum.doubleValue(units); if ( monotonicDir==1 ) { while ( result<(len-1) && table.getXTagDouble(result,units) < x ) result++; while ( result>0 && table.getXTagDouble(result,units) > x ) result--; } else { while ( result<(len-1) && table.getXTagDouble(result,units) > x ) result++; while ( result>0 && table.getXTagDouble(result,units) < x ) result--; } // result should now point to the guy to the left of x, unless x>the last guy or 0 && ds.getXTagDatum(i).ge(datum) ) { return i-1; } else { return i; } } /** * returns the first column that is after the given datum. Note the * if the datum identifies (==) an xtag, then the previous column is * returned. */ public static int getNextColumn( DataSet ds, Datum datum ) { int i= closestColumn( ds, datum ); // TODO: consider the virtue of le if ( i