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;
030
031import java.io.BufferedReader;
032import java.io.IOException;
033import java.io.InputStream;
034import java.io.InputStreamReader;
035import java.util.Map;
036import java.util.StringTokenizer;
037
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041import visad.FlatField;
042import visad.OffsetUnit;
043import visad.RealType;
044import visad.data.units.Parser;
045
046public class Calipso2D extends ProfileAlongTrack {
047
048          private static final Logger logger = LoggerFactory.getLogger(Calipso2D.class);
049      double start_time;
050
051      public Calipso2D() {
052      }
053
054      public Calipso2D(MultiDimensionReader reader, Map<String, Object> metadata, boolean isVertTypeAlt) {
055        super(reader, metadata, isVertTypeAlt);
056      }
057
058      public Calipso2D(MultiDimensionReader reader, Map<String, Object> metadata) {
059        super(reader, metadata);
060        Map<String, Object> table = ProfileAlongTrack.getEmptyMetadataTable();
061        table.put(ProfileAlongTrack.array_name, "Surface_Elevation");
062      }
063
064      public float[] getVertBinAltitude() {
065        String propertyFileName = null;
066        float[] altitude = new float[VertLen];
067        int line_cnt = 0;
068        try {
069                propertyFileName = (String) metadata.get(ancillary_file_name);
070                InputStream ios = getClass().getResourceAsStream(propertyFileName);
071                BufferedReader ancillaryReader = new BufferedReader(new InputStreamReader(ios));
072
073                while (true) {
074                        String line = ancillaryReader.readLine();
075                        if (line == null) break;
076                        if (line.startsWith("!")) continue;
077                        StringTokenizer strTok = new StringTokenizer(line);
078                        String[] tokens = new String[strTok.countTokens()];
079                        int tokCnt = 0;
080                        while (strTok.hasMoreElements()) {
081                                tokens[tokCnt++] = strTok.nextToken();
082                        }
083                        altitude[line_cnt] = (Float.valueOf(tokens[0])) * 1000f;
084                        line_cnt++;
085                }
086                ios.close();
087        }
088        catch (IOException ioe) {
089                logger.error("Failed on ancillary file read: " + propertyFileName, ioe);
090        }
091        return altitude;
092      }
093
094      public float[] getTrackTimes() throws Exception {
095        int[] start = new int[] {0, 0};
096        int[] count = new int[] {TrackLen / 10, 1};
097        int[] stride = new int[] {10, 1};
098        double[] times = reader.getDoubleArray((String) metadata.get(profileTime_name), start, count, stride);
099        start_time = times[0];
100        double time_inc = (times[times.length-1] - times[0]) / times.length;
101        float[] new_times = new float[TrackLen];
102        for (int t = 0; t < TrackLen; t++) {
103          new_times[t] = (float) times[0] + (float) (t * time_inc);
104        }
105        return new_times;
106      }
107
108      public float[] getTrackLongitude() throws Exception {
109        int[] start = new int[] {0, 0};
110        int[] count = new int[] {TrackLen, 1};
111        int[] stride = new int[] {1, 1};
112        float[] vals = reader.getFloatArray((String) metadata.get(longitude_name), start, count, stride);
113        return vals;
114      }
115
116      public float[] getTrackLatitude() throws Exception {
117        int[] start = new int[] {0, 0};
118        int[] count = new int[] {TrackLen, 1};
119        int[] stride = new int[] {1, 1};
120        float[] vals = reader.getFloatArray((String) metadata.get(latitude_name), start, count, stride);
121        return vals;
122      }
123
124      public RealType makeVertLocType() throws Exception {
125        return RealType.Altitude;
126      }
127
128      public RealType makeTrackTimeType() throws Exception {
129        OffsetUnit unit = (OffsetUnit) Parser.parse("seconds since 1993-01-01 00:00:00Z");
130        OffsetUnit new_unit = new OffsetUnit(start_time, unit);
131        RealType timeType = RealType.getRealType("Track_Time", new_unit);
132        return timeType;
133      }
134
135      public FlatField getData(Map<String, double[]> subset) throws Exception {
136        FlatField field = super.getData(subset);
137        return field;
138      }
139}