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