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;
030
031import java.io.BufferedReader;
032import java.io.IOException;
033import java.io.InputStreamReader;
034import java.net.URL;
035import java.util.ArrayList;
036
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040import visad.VisADException;
041import visad.georef.EarthLocationTuple;
042
043public class GroundStations {
044    private static final Logger logger =
045        LoggerFactory.getLogger(GroundStations.class);
046    
047    private static final String card00 = "KMSN,SSEC,43.1398578,-89.3375136,270.4";
048
049    // taken from http://www.gano.name/shawn/JSatTrak/data/groundstations/
050    public static String groundStationDB = "/edu/wisc/ssec/mcidasv/resources/orbittrack_groundstations_db.csv";
051    
052    // shouldn't expose the List implementation to the public, but changing it
053    // to List<...> namedLocs = new ArrayList<>() will break existing bundles :(
054    private ArrayList<GroundStation> namedLocs = new ArrayList<>();
055
056    /**
057     * No-arg constructor for empty list which gets populated on-the-fly later.
058     */
059    
060    public GroundStations() {
061    }
062    
063    public GroundStations(String topCard) {
064        // read data files for Ground Stations
065        URL url = GroundStations.class.getResource(groundStationDB);
066        try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
067            String nextLine = topCard;
068            if (topCard == null) {
069                nextLine = card00;
070            }
071            
072            while (nextLine != null) {
073                // split line into parts
074                String[] elements = nextLine.split(",");
075        
076                if (elements.length == 5) // if the row is formatted correctly
077                {
078                    double dLat = Double.parseDouble(elements[2]);
079                    double dLon = Double.parseDouble(elements[3]);
080                    double dAlt = Double.parseDouble(elements[4]);
081                    
082                    EarthLocationTuple elt = new EarthLocationTuple(dLat, dLon, dAlt);
083                    namedLocs.add(new GroundStation(elements[1], elt));
084                }
085                nextLine = in.readLine();
086            } // while there are more lines to read
087        } catch (IOException e) {
088            logger.error("Problem reading ground stations, missing file or invalid file format", e);
089        } catch (VisADException e) {
090            logger.error("Problem creating earth location", e);
091        }
092    }
093    
094    public ArrayList<GroundStation> getGroundStations() {
095        return namedLocs;
096    }
097
098}