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 java.util.HashMap;
032import java.util.Map;
033
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036import visad.FunctionType;
037import visad.GriddedSet;
038import visad.IntegerNDSet;
039import visad.Linear1DSet;
040import visad.Linear2DSet;
041import visad.Linear3DSet;
042import visad.LinearNDSet;
043import visad.LinearSet;
044import visad.RealTupleType;
045import visad.RealType;
046
047public class ArrayAdapter extends MultiDimensionAdapter {
048    
049    private static final Logger logger = LoggerFactory.getLogger(ArrayAdapter.class);
050    
051   RealTupleType domainType;
052   FunctionType ftype;
053   GriddedSet domain;
054   RealType[] realTypes;
055
056   public ArrayAdapter() {
057   }
058
059   public ArrayAdapter(MultiDimensionReader reader, Map<String, Object> metadata) {
060     super(reader, metadata);
061     init();
062   }
063
064   private void init() {
065     try {
066     realTypes = new RealType[array_rank];
067     int[] lengths = new int[array_rank];
068     for (int i=0; i<array_rank; i++) {
069       realTypes[i] = RealType.getRealType(array_dim_names[i]);
070       lengths[i] = array_dim_lengths[i];
071     }
072
073     domainType = new RealTupleType(realTypes);
074
075     String rangeName = null;
076     if (metadata.get("range_name") != null) {
077        rangeName = (String)metadata.get("range_name");
078     } 
079     else {
080        rangeName = (String)metadata.get("array_name");
081     }
082     rangeType = RealType.getRealType(rangeName);
083     ftype = new FunctionType(domainType, rangeType);
084     domain = IntegerNDSet.create(domainType, lengths);
085
086     RangeProcessor rangeProcessor = RangeProcessor.createRangeProcessor(reader, metadata);
087     if ( !(reader instanceof GranuleAggregation)) {
088        setRangeProcessor(rangeProcessor);
089     }
090
091     } catch (Exception e) {
092       logger.error("problem initializing array adapter", e);
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(Map<String, double[]> 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 = 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 Map<String, double[]> getDefaultSubset() {
143     Map<String, double[]> map = getEmptySubset();
144     for (int i=0; i<array_rank; i++) {
145       double[] coords = 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 Map<String, double[]> getEmptySubset() {
154     Map<String, double[]> subset = new HashMap<>();
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}