001    /*
002     * This file is part of McIDAS-V
003     *
004     * Copyright 2007-2013
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    
029    package edu.wisc.ssec.mcidasv.data.hydra;
030    import java.util.HashMap;
031    import visad.Set;
032    import visad.RealTupleType;
033    import visad.RealType;
034    import visad.Gridded3DSet;
035    
036    
037    public class ProfileAlongTrack3D extends MultiDimensionAdapter {
038    
039      public ProfileAlongTrack adapter2D;
040      MultiDimensionReader reader;
041      float[] vertLocs;
042      RealTupleType domain3D;
043    
044    
045      public ProfileAlongTrack3D(ProfileAlongTrack adapter2D) {
046        super(adapter2D.getReader(), (HashMap) adapter2D.getMetadata());
047        this.adapter2D = adapter2D;
048        this.reader = adapter2D.getReader();
049        rangeProcessor = adapter2D.getRangeProcessor();
050        try {
051          init();
052        } catch (Exception e) {
053          System.out.println("init failed");
054        }
055      }
056    
057      void init() throws Exception {
058        vertLocs = adapter2D.getVertBinAltitude();
059        domain3D = RealTupleType.SpatialEarth3DTuple;
060        rangeType = adapter2D.getRangeType();
061      }
062    
063    
064      public Set makeDomain(Object subset) throws Exception {
065        double[] vert_coords = (double[]) ((HashMap)subset).get(ProfileAlongTrack.vertDim_name);
066        double[] track_coords = (double[])  ((HashMap)subset).get(ProfileAlongTrack.trackDim_name);
067    
068        int vert_idx = adapter2D.getVertIdx();
069        int track_idx = adapter2D.getTrackIdx();
070    
071        float[] lonValues = null;
072        float[] latValues = null;
073    
074        String lonArrayName = (String) ((HashMap)getMetadata()).get(ProfileAlongTrack.longitude_name);
075        String latArrayName = (String) ((HashMap)getMetadata()).get(ProfileAlongTrack.latitude_name);
076    
077        int[] start = null;
078        int[] count = null;
079        int[] stride = null;
080    
081        int rank = (reader.getDimensionLengths(lonArrayName)).length;
082    
083        if (rank == 2) {
084          start = new int[2];
085          count = new int[2];
086          stride = new int[2];
087    
088          start[vert_idx] = (int) 0;
089          count[vert_idx] = (int) 1;
090          stride[vert_idx] = (int) vert_coords[2];
091    
092          start[track_idx] = (int) track_coords[0];
093          count[track_idx] = (int) ((track_coords[1] - track_coords[0])/track_coords[2] + 1f);
094          stride[track_idx] = (int) track_coords[2];
095        }
096        else if (rank == 1) {
097          start = new int[1];
098          count = new int[1];
099          stride = new int[1];
100          start[0] = (int) track_coords[0];
101          count[0] = (int) ((track_coords[1] - track_coords[0])/track_coords[2] + 1f);
102          stride[0] = (int) track_coords[2];
103        }
104    
105        if (reader.getArrayType(lonArrayName) == Float.TYPE ) {
106          lonValues = reader.getFloatArray(lonArrayName, start, count, stride);
107          latValues = reader.getFloatArray(latArrayName, start, count, stride);
108        }
109    
110        int vert_len = (int) ((vert_coords[1] - vert_coords[0])/vert_coords[2] + 1f);
111        int track_len = count[track_idx];
112    
113        float[] altitudes = new float[vert_len];
114        for (int k=0; k<vert_len;k++) {
115          altitudes[k] = vertLocs[(int)vert_coords[0] + k*((int)vert_coords[2])];
116        }
117    
118        float[][] alt_lon_lat = new float[3][vert_len*track_len];
119        oneD_threeDfill(lonValues, latValues, track_len, altitudes, vert_len, alt_lon_lat);
120    
121        return new Gridded3DSet(domain3D, alt_lon_lat, vert_len, track_len);
122      }
123      
124    
125      public HashMap getDefaultSubset() {
126        return adapter2D.getDefaultSubset();
127      }
128    
129      public HashMap getSubsetFromLonLatRect(HashMap subset, double minLat, double maxLat, double minLon, double maxLon) {
130        return adapter2D.getSubsetFromLonLatRect(subset, minLat, maxLat, minLon, maxLon);
131      }
132    
133      public HashMap getSubsetFromLonLatRect(double minLat, double maxLat, double minLon, double maxLon) {
134        return adapter2D.getSubsetFromLonLatRect(minLat, maxLat, minLon, maxLon);
135      }
136    
137      public HashMap getSubsetFromLonLatRect(double minLat, double maxLat, double minLon, double maxLon, int xStride, int yStride, int zStride) {
138        return adapter2D.getSubsetFromLonLatRect(minLat, maxLat, minLon, maxLon, xStride, yStride, zStride);
139      }
140    
141      public static void oneD_threeDfill(float[] b, float[] c, int leny, float[] a, int lenx, float[][] abc) {
142        int cnt = 0;
143        for (int i=0; i<leny; i++) {
144          for (int j=0; j<lenx; j++) {
145            abc[0][cnt] = b[i];
146            abc[1][cnt] = c[i];
147            abc[2][cnt] = a[j];
148            cnt++;
149           }
150         }
151       }
152    
153    }