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