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