001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2016
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;
030
031import java.io.BufferedReader;
032import java.io.File;
033import java.io.FileReader;
034import java.io.InputStreamReader;
035
036import java.net.URL;
037import java.net.URLConnection;
038
039import java.util.HashMap;
040
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044import visad.georef.EarthLocationTuple;
045
046public class GroundStations
047{
048        private static final Logger logger = LoggerFactory.getLogger(GroundStations.class);
049        private static final String card00 = "KMSN,SSEC,43.1398578,-89.3375136,270.4";
050    public static String groundStationDB = "data/groundstations/groundstations_db.csv";
051    private HashMap<String, EarthLocationTuple> namedLocs = new HashMap<String, EarthLocationTuple>();
052
053    /**
054         * No-arg constructor for empty list which gets populated on-the-fly later.
055         */
056    
057        public GroundStations() {
058        }
059
060        public GroundStations(String topCard)
061    {
062        // read data files for Ground Stations
063        try {
064            BufferedReader gsReader = null; // initialization of reader 
065            
066            //see if local file exists, if not stream from web
067            
068            // read local file
069            if (new File(groundStationDB).exists())
070            {
071                File gsFile = new File(groundStationDB);
072                FileReader gsFileReader = new FileReader(gsFile);
073                gsReader = new BufferedReader(gsFileReader); // from local file
074            }
075            else
076            {
077                // read from web
078                URL url = new URL("http://www.gano.name/shawn/JSatTrak/" + groundStationDB);
079                URLConnection c = url.openConnection();
080                InputStreamReader isr = new InputStreamReader(c.getInputStream());
081                gsReader = new BufferedReader(isr); // from the web
082            }
083
084            String nextLine = topCard;
085            if (topCard == null) {
086               nextLine = card00;
087            }
088            
089            while (nextLine != null)
090            {
091                // split line into parts
092                String[] elements = nextLine.split(",");
093                
094                if (elements.length == 5) // if the row is formatted correctly
095                {
096                    Double dLat = new Double(elements[2]);
097                    Double dLon = new Double(elements[3]);
098                    Double dAlt = new Double(elements[4]);
099                    
100                    EarthLocationTuple elt = new EarthLocationTuple(dLat, dLon, dAlt);
101                    namedLocs.put(elements[1], elt);
102                }
103                nextLine = gsReader.readLine();
104            } // while there are more lines to read
105
106            gsReader.close();
107        }
108        catch (Exception e)
109        {
110                e.printStackTrace();
111            logger.error("ERROR: Problem reading ground stations, missing file or invalid file format");
112        }
113    } // constructor
114    
115    public HashMap getGroundStations() {
116        return namedLocs;
117    }
118
119}