001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2015
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 http://www.gnu.org/licenses.
027 */
028
029package edu.wisc.ssec.mcidasv.data.hydra;
030
031import visad.Data;
032import visad.FlatField;
033import visad.VisADException;
034import visad.CoordinateSystem;
035import visad.RealType;
036import visad.Real;
037import visad.MathType;
038import visad.IntegerNDSet;
039import visad.GriddedSet;
040import visad.LinearNDSet;
041import visad.LinearSet;
042import visad.Linear1DSet;
043import visad.Linear2DSet;
044import visad.Linear3DSet;
045import visad.RealTupleType;
046import visad.SetType;
047import visad.FunctionType;
048import visad.Set;
049import java.rmi.RemoteException;
050import java.util.HashMap;
051import java.util.Iterator;
052
053
054public class ArrayAdapter extends MultiDimensionAdapter {
055
056   RealTupleType domainType;
057   FunctionType ftype;
058   GriddedSet domain;
059   RealType[] realTypes;
060
061   public ArrayAdapter() {
062   }
063
064   public ArrayAdapter(MultiDimensionReader reader, HashMap metadata) {
065     super(reader, metadata);
066     init();
067   }
068
069   private void init() {
070     try {
071     realTypes = new RealType[array_rank];
072     int[] lengths = new int[array_rank];
073     for (int i=0; i<array_rank; i++) {
074       realTypes[i] = RealType.getRealType(array_dim_names[i]);
075       lengths[i] = array_dim_lengths[i];
076     }
077
078     domainType = new RealTupleType(realTypes);
079
080     String rangeName = null;
081     if (metadata.get("range_name") != null) {
082        rangeName = (String)metadata.get("range_name");
083     } 
084     else {
085        rangeName = (String)metadata.get("array_name");
086     }
087     rangeType = RealType.getRealType(rangeName);
088     ftype = new FunctionType(domainType, rangeType);
089     domain = IntegerNDSet.create(domainType, lengths);
090     }
091     catch (Exception e) {
092       e.printStackTrace();
093     }
094   }
095
096   public GriddedSet getDomain() {
097     return domain;
098   }
099
100   public FunctionType getMathType() {
101     return ftype;
102   }
103
104   public GriddedSet makeDomain(Object subset) throws Exception {
105     if (subset == null) {
106        subset = getDefaultSubset();
107     }
108
109     double[] first = new double[array_rank];
110     double[] last = new double[array_rank];
111     int[] length = new int[array_rank];
112
113     for (int kk=0; kk<array_rank; kk++) {
114       RealType rtype = realTypes[kk];
115       double[] coords = (double[]) ((HashMap)subset).get(dimNameMap.get(rtype.getName()));
116       if (array_dim_lengths[kk] == 1) {
117         coords[0] = 0;
118         coords[1] = 0;
119         coords[2] = 1;
120       }
121       first[kk] = coords[0];
122       last[kk] = coords[1];
123       length[kk] = (int) ((last[kk] - first[kk])/coords[2] + 1);
124     }
125
126     LinearSet lin_set = LinearNDSet.create(domainType, first, last, length);
127     GriddedSet new_domain = null;
128
129     if (array_rank == 1) {
130          new_domain = (Linear1DSet) lin_set;
131     } else if (array_rank == 2) {
132          new_domain = (Linear2DSet) lin_set;
133     } else if (array_rank == 3) {
134          new_domain = (Linear3DSet) lin_set;
135     } else {
136          new_domain = (LinearNDSet) lin_set;
137     } 
138
139     return new_domain;
140   }
141
142   public HashMap getDefaultSubset() {
143     HashMap map = getEmptySubset();
144     for (int i=0; i<array_rank; i++) {
145       double[] coords = (double[]) map.get(dimNameMap.get(array_dim_names[i]));
146       coords[0] = 0;
147       coords[1] = array_dim_lengths[i] - 1;
148       coords[2] = 1;
149     }
150     return map;
151   }
152
153   public HashMap getEmptySubset() {
154     HashMap<String, double[]> subset = new HashMap<String, double[]>();
155     for (int i=0; i<array_rank; i++) {
156       subset.put(dimNameMap.get(array_dim_names[i]), new double[3]);
157     }
158     return subset;
159   }
160
161}