001    /*
002     * $Id: MultiDimensionSubset.java,v 1.12 2012/02/19 17:35:41 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    
031    package edu.wisc.ssec.mcidasv.data.hydra;
032    
033    import ucar.unidata.data.DataSelection;
034    import java.util.HashMap;
035    import java.util.Hashtable;
036    import java.util.Iterator;
037    import java.util.Set;
038    
039    
040    public class MultiDimensionSubset extends DataSelection {
041    
042      public static final MultiDimensionSubset key = new MultiDimensionSubset();
043    
044      private double[][] coords = null;
045      private String[] keys = null;
046    
047      public MultiDimensionSubset() {
048        super();
049      }
050    
051      public MultiDimensionSubset(HashMap subset) {
052        super();
053        coords = new double[subset.size()][];
054        keys = new String[subset.size()];
055        Iterator iter = subset.keySet().iterator();
056        int cnt =0;
057        while (iter.hasNext()) {
058           String key = (String) iter.next();
059           keys[cnt] = key;
060           coords[cnt] = (double[]) subset.get(key);
061           cnt++;
062        }
063      }
064    
065      public MultiDimensionSubset(double[][] coords, String[] keys) {
066        super();
067        /**
068        int num = keys.length;
069        this.keys = new String[num];
070        this.coords = new double[num][];
071        for (int i=0; i<num; i++) {
072          this.keys[i] = keys[i];
073          this.coords[i] = new double[coords[i].length];
074          for (int j=0; j<coords[i].length; j++) {
075            this.coords[i][j] = coords[i][j];
076          }
077        }
078        **/
079        this.coords = coords;
080        this.keys = keys;
081      }
082    
083      public HashMap getSubset() {
084        HashMap hmap = new HashMap();
085        for (int k=0; k<keys.length; k++) {
086          double[] new_coords = new double[coords[k].length];
087          System.arraycopy(coords[k],0,new_coords,0,new_coords.length);
088          hmap.put(keys[k], new_coords);
089        }
090        return hmap;
091      }
092    
093      public double[][] getCoords() {
094        return coords;
095      }
096    
097      public void setCoords(double[][] coords) {
098        this.coords = coords;
099      }
100    
101      public String[] getKeys() {
102        return keys;
103      }
104    
105      public void setKeys(String[] keys) {
106        this.keys = keys;
107      }
108    
109      public MultiDimensionSubset clone() {
110        MultiDimensionSubset subset = new MultiDimensionSubset(coords, keys);
111        Hashtable props = new Hashtable();
112        props.put(MultiDimensionSubset.key, subset);
113        subset.setProperties(props);
114        return subset;
115      }
116    
117      public String toString() {
118              StringBuffer sb = new StringBuffer();
119              if (keys != null) {
120                      for (int i = 0; i < keys.length; i++) {
121                              sb.append(new String(keys[i] + ": " + coords[i][0] + ", " + coords[i][1] + ", " + coords[i][2] + "\n"));
122                      }
123              }
124              return sb.toString();
125      }
126    
127      /***
128      public boolean equals(Object obj) {
129        if (!(obj instanceof MultiDimensionSubset)) return false;
130        if ((keys == null) || (coords == null)) return false;
131    
132        String[] keys_in = ((MultiDimensionSubset)obj).getKeys();
133        if ((keys_in == null) || (keys_in.length != keys.length)) return false;
134    
135        for (int k=0; k<keys.length; k++) {
136          if (keys_in[k] != keys[k]) return false;
137        } 
138    
139        double[][] coords_in = (double[][]) ((MultiDimensionSubset)obj).getCoords();
140        if ((coords_in == null) || (coords.length != coords_in.length)) return false;
141        for (int k=0; k<coords.length; k++) {
142          for (int t=0; t<coords[k].length; t++) {
143            if (coords[k][t] != coords_in[k][t]) return false;
144          }
145        }
146        return true;
147      }
148      ***/
149    }