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