001    /*
002     * This file is part of McIDAS-V
003     *
004     * Copyright 2007-2013
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    
029    package edu.wisc.ssec.mcidasv.chooser.adde;
030    
031    
032    import java.util.ArrayList;
033    import java.util.Date;
034    import java.util.List;
035    
036    import javax.swing.ListSelectionModel;
037    
038    import org.w3c.dom.Element;
039    
040    import ucar.unidata.data.AddeUtil;
041    import ucar.unidata.idv.chooser.IdvChooserManager;
042    import ucar.unidata.util.TwoFacedObject;
043    import ucar.visad.UtcDate;
044    import visad.DateTime;
045    
046    import edu.wisc.ssec.mcidas.McIDASUtil;
047    
048    /**
049     * Selection widget for ADDE point data
050     *
051     * @author MetApps Development Team
052     * @version $Revision$ $Date$
053     */
054    public class AddeLightningDataChooser extends AddePointDataChooser {
055    
056    
057        /**
058         * Create a new <code>AddeLightningDataChooser</code> with the preferred
059         * list of ADDE servers.
060         *
061         *
062         * @param mgr The chooser manager
063         * @param root The chooser.xml node
064         */
065        public AddeLightningDataChooser(IdvChooserManager mgr, Element root) {
066            super(mgr, root);
067        }
068    
069    
070        /**
071         * Get the default station model for this chooser.
072         * @return name of default station model
073         */
074        public String getDefaultStationModel() {
075            return "flash";
076        }
077    
078        /**
079         * This allows derived classes to provide their own name for labeling, etc.
080         *
081         * @return  the dataset name
082         */
083        public String getDataName() {
084            return "Lightning Data";
085        }
086    
087        /**
088         * Get the request string for times particular to this chooser
089         *
090         * @return request string
091         * protected String getTimesRequest() {
092         *   StringBuffer buf = getGroupUrl(REQ_POINTDATA, getGroup());
093         *   appendKeyValue(buf, PROP_DESCR, getDescriptor());
094         *   // this is hokey, but take a smattering of stations.
095         *   //appendKeyValue(buf, PROP_SELECT, "'CO US'");
096         *   appendKeyValue(buf, PROP_POS, "0");
097         *   appendKeyValue(buf, PROP_NUM, "ALL");
098         *   appendKeyValue(buf, PROP_PARAM, "DAY TIME");
099         *   return buf.toString();
100         * }
101         */
102    
103        /**
104         * Get the default datasets for the chooser.  The objects are
105         * a descriptive name and the ADDE group/descriptor
106         *
107         * @return  default datasets.
108         */
109        protected TwoFacedObject[] getDefaultDatasets() {
110            return new TwoFacedObject[] { new TwoFacedObject("NLDN", "LGT/NLDN"),
111                                          new TwoFacedObject("USPLN",
112                                          "LGT/USPLN") };
113        }
114    
115        /**
116         * Get the increment between times for relative time requests
117         *
118         * @return time increment (hours)
119         */
120        public float getRelativeTimeIncrement() {
121            return .5f;
122        }
123    
124        /**
125         * Create the date time selection string for the "select" clause
126         * of the ADDE URL.
127         *
128         * @return the select day and time strings
129         */
130        protected String getDayTimeSelectString() {
131            StringBuffer buf = new StringBuffer();
132            if (getDoAbsoluteTimes()) {
133                buf.append("time ");
134                List     times = getSelectedAbsoluteTimes();
135                DateTime dt    = (DateTime) times.get(0);
136                buf.append(UtcDate.getHMS(dt));
137                buf.append(" ");
138                dt = (DateTime) times.get(times.size() - 1);
139                buf.append(UtcDate.getHMS(dt));
140            } else {
141                buf.append(getRelativeTimeId());
142            }
143            return buf.toString();
144        }
145    
146        /**
147         * Get the identifier for relative time.  Subclasses can override.
148         * @return the identifier
149         */
150        protected String getRelativeTimeId() {
151            return AddeUtil.RELATIVE_TIME_RANGE;
152        }
153    
154        /**
155         * Get the selection mode for the absolute times panel. Subclasses
156         * can override.
157         *
158         * @return the list selection mode
159         */
160        protected int getAbsoluteTimeSelectMode() {
161            return ListSelectionModel.SINGLE_INTERVAL_SELECTION;
162        }
163    
164    
165        /**
166         * Set the list of available times.
167         */
168        public void readTimes() {
169            clearTimesList();
170            ArrayList uniqueTimes = new ArrayList();
171    
172            setState(STATE_CONNECTING);
173            try {
174                float    hours      = getRelativeTimeIncrement();
175                int      numTimes   = (int) (24f / hours);
176                DateTime currentDay = new DateTime(new Date());
177                int day = Integer.parseInt(UtcDate.formatUtcDate(currentDay,
178                              "yyyyMMdd"));
179                for (int i = 0; i < numTimes; i++) {
180                    int hour = McIDASUtil.mcDoubleToPackedInteger(i * hours);
181                    try {
182                        DateTime dt =
183                            new DateTime(McIDASUtil.mcDayTimeToSecs(day, hour));
184                        uniqueTimes.add(dt);
185                    } catch (Exception e) {}
186                }
187                setState(STATE_CONNECTED);
188                //System.out.println(
189                //       "found " + uniqueTimes.size() + " unique times");
190            } catch (Exception excp) {
191                handleConnectionError(excp);
192                return;
193            }
194            if (getDoAbsoluteTimes()) {
195                if ( !uniqueTimes.isEmpty()) {
196                    setAbsoluteTimes(new ArrayList(uniqueTimes));
197                }
198                int selectedIndex = getAbsoluteTimes().size() - 1;
199                setSelectedAbsoluteTime(selectedIndex);
200            }
201        }
202    
203    
204    }
205