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