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