001/*
002 * $Id: GroundStations.java,v 1.3 2011/03/24 16:06:32 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 * 
012 * All Rights Reserved
013 * 
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.  
016 * 
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 * 
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025 * GNU Lesser Public License for more details.
026 * 
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program.  If not, see http://www.gnu.org/licenses.
029 */
030
031package edu.wisc.ssec.mcidasv.data;
032
033import java.awt.Frame;
034import java.io.BufferedReader;
035import java.io.BufferedWriter;
036import java.io.File;
037import java.io.FileReader;
038import java.io.FileWriter;
039import java.io.InputStreamReader;
040import java.net.URL;
041import java.net.URLConnection;
042import java.util.ArrayList;
043import java.util.Collections;
044import java.util.Hashtable;
045import java.util.List;
046import javax.swing.JOptionPane;
047import javax.swing.tree.DefaultMutableTreeNode;
048import javax.swing.tree.DefaultTreeModel;
049import javax.swing.tree.TreeNode;
050import javax.swing.tree.TreePath;
051import javax.swing.tree.TreeSelectionModel;
052
053public class GroundStations
054{
055    private static final String card00 = "KMSN,SSEC,43.1398578,-89.3375136,270.4";
056    private int gsCount = 0; // count of stations loaded
057    public static String groundStationDB = "data/groundstations/groundstations_db.csv";
058
059    private List stations = new ArrayList();
060    private List latitudes = new ArrayList();
061    private List longitudes = new ArrayList();
062    private List altitudes = new ArrayList();
063
064    public GroundStations(String topCard)
065    {
066        // read data files for Ground Stations
067        try
068        {
069            BufferedReader gsReader = null; // initalization of reader 
070            
071            //see if local file exists, if not stream from web
072            
073            // read local file
074            if( new File(groundStationDB).exists())
075            {
076                File gsFile = new File(groundStationDB);
077                FileReader gsFileReader = new FileReader(gsFile);
078                gsReader = new BufferedReader(gsFileReader); // from local file
079            }
080            else
081            {
082                // read from web
083                URL url = new URL("http://www.gano.name/shawn/JSatTrak/" + groundStationDB);
084                URLConnection c = url.openConnection();
085                InputStreamReader isr = new InputStreamReader(c.getInputStream());
086                gsReader = new BufferedReader(isr); // from the web
087            }
088
089            String nextLine = topCard;
090            if (topCard == null) {
091               nextLine = card00;
092               stations.add(" ");
093               latitudes.add(" ");
094               longitudes.add(" ");
095               altitudes.add(" ");
096            }
097            
098            while (nextLine != null)
099            {
100                // split line into parts
101                String[] elements = nextLine.split(",");
102                
103                if (elements.length == 5) // if the row is formatted correctly
104                {
105                    String network = elements[0];
106                    String stationName = elements[1];
107                    stations.add(stationName);
108                    String stationLat = elements[2];
109                    latitudes.add(stationLat);
110                    String stationLon = elements[3];
111                    longitudes.add(stationLon);
112                    String stationAlt = elements[4];
113                    altitudes.add(stationAlt);
114
115//                    System.out.println("" + gsCount + " : " + stationName + ", " + stationLat+ ", " + stationLon + ", " + stationAlt);
116                    gsCount++;
117                }
118                nextLine = gsReader.readLine();
119            }// while there are more lines to read
120            gsReader.close();
121        }
122        catch (Exception e)
123        {
124             System.out.println("ERROR IN GROUND STATION READING POSSIBLE FILE FORMAT OR MISSING FILES:");
125        }
126    } // constructor
127
128    public int getGroundStationCount() {
129        return gsCount;
130    }
131
132    public List getStations() {
133        return stations;
134    }
135
136    public List getLatitudes() {
137        return latitudes;
138    }
139
140    public List getLongitudes() {
141        return longitudes;
142    }
143
144    public List getAltitudes() {
145        return altitudes;
146    }
147}