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.rmi.RemoteException;
032import java.util.HashMap;
033import java.util.Map;
034
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037import visad.FlatField;
038import visad.FunctionType;
039import visad.RealTupleType;
040import visad.Set;
041import visad.SetType;
042import visad.VisADException;
043
044public class TrackAdapter extends MultiDimensionAdapter {
045   
046   private static final Logger logger =
047       LoggerFactory.getLogger(TrackAdapter.class);
048   
049   RealTupleType domainType;
050   ArrayAdapter rngAdapter;
051   TrackDomain trackDomain;
052
053   int listIndex = 0;
054
055   String adapterName = null;
056
057   public TrackAdapter() {
058   }
059
060   public TrackAdapter(TrackDomain trackDomain, ArrayAdapter rangeAdapter) throws VisADException {
061     this.trackDomain = trackDomain;
062     this.rngAdapter = rangeAdapter;
063   }
064
065   public Set makeDomain(Map<String, double[]> subset) throws Exception {
066     throw new Exception("Unimplemented");
067   } 
068
069   public FlatField getData(Map<String, double[]> subset) throws VisADException, RemoteException {
070
071     try {
072         float[] rngValues = null;
073         Set set = trackDomain.makeDomain(subset);
074
075         domainType = ((SetType)set.getType()).getDomain();
076
077         rngValues = (rngAdapter.getData(subset).getFloats())[0];
078         FlatField field = new FlatField(new FunctionType(domainType, rngAdapter.getMathType().getRange()), set);
079         field.setSamples(new float[][] {rngValues}, false);
080
081         return field;
082     } catch (Exception e) {
083       logger.error("Problem getting data", e);
084       return null;
085     }
086   }
087
088   public void setName(String name) {
089     adapterName = name;
090   }
091
092   public String getArrayName() {
093     if (adapterName != null) {
094       return adapterName;
095     }
096     else {
097       return rngAdapter.getArrayName();
098     }
099   }
100
101   void setListIndex(int idx) {
102     listIndex = idx;
103   }
104
105   public Map<String, double[]> getDefaultSubset() {
106     Map<String, double[]> subset = rngAdapter.getDefaultSubset();
107     if (subset.containsKey("VertDim")) {
108       double[] coords = subset.get("VertDim");
109       if (coords != null) {
110         coords[0] = listIndex;
111         coords[1] = listIndex;
112         coords[2] = 1;
113       }
114     }
115     return subset;
116   }
117
118   public Map<String, double[]> getSubsetFromLonLatRect(double minLat, double maxLat,
119                                          double minLon, double maxLon) {
120      return trackDomain.getSubsetFromLonLatRect(getDefaultSubset(), minLat, maxLat, minLon, maxLon);
121   }
122
123   public Map<String, double[]> getSubsetFromLonLatRect(double minLat, double maxLat,
124                                          double minLon, double maxLon,
125                                          int xStride, int yStride, int zStride) {
126      return trackDomain.getSubsetFromLonLatRect(getDefaultSubset(), minLat, maxLat, minLon, maxLon,
127                                                 xStride, yStride, zStride);
128   }
129
130}