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