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.control;
030
031import java.util.ArrayList;
032import java.util.List;
033
034import edu.wisc.ssec.mcidasv.data.QualityFlag;
035import edu.wisc.ssec.mcidasv.data.hydra.SuomiNPPDataSource;
036
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040import ucar.unidata.data.DataSourceImpl;
041import ucar.unidata.idv.control.ImagePlanViewControl;
042
043import ucar.unidata.data.DataChoice;
044import ucar.unidata.data.grid.GridUtil;
045import ucar.unidata.idv.control.ReadoutInfo;
046
047import visad.Real;
048import visad.georef.EarthLocation;
049
050public class SuomiNPPQfControl extends ImagePlanViewControl {
051    
052    private static final Logger logger = LoggerFactory.getLogger(SuomiNPPQfControl.class);
053
054    /**
055     * Get the {@literal "first"} {@link DataSourceImpl} associated with this
056     * control.
057     *
058     * @return Either the first {@code DataSourceImpl} for this control, or
059     * {@code null}.
060     */
061
062    public DataSourceImpl getDataSource() {
063        DataSourceImpl ds = null;
064        List dataSources = getDataSources();
065        if (!dataSources.isEmpty()) {
066            ds = (DataSourceImpl)dataSources.get(0);
067        }
068        return ds;
069    }
070
071    @Override protected List getCursorReadoutInner(EarthLocation el,
072                                                   Real animationValue,
073                                                   int animationStep,
074                                                   List<ReadoutInfo> samples) throws Exception {
075        try {
076
077            // TJJ May 2017 - Current slice getting mucked with?
078            if (currentSlice == null) {
079                logger.debug("TJJ currentSlice null in getCursorReadoutInner()...");
080                return null;
081            }
082
083            List result = new ArrayList();
084            Real r = GridUtil.sampleToReal(
085                         currentSlice, el, animationValue, getSamplingModeValue(NEAREST_NEIGHBOR));
086            if (r != null) {
087                ReadoutInfo readoutInfo = new ReadoutInfo(this, r, el,
088                                              animationValue);
089                readoutInfo.setUnit(getDisplayUnit());
090                readoutInfo.setRange(getRange());
091                samples.add(readoutInfo);
092            }
093    
094            if ((r != null) && !r.isMissing()) {
095                
096                //logger.trace("cursor value: {}", r.getValue());
097                DataChoice dc = getDataChoice();
098                // TODO: why do we have to append All_Data anyway?
099                String prod = ("All_Data/").concat(dc.toString());
100                //logger.trace("prod: {}", prod);
101                QualityFlag qf = ((SuomiNPPDataSource) getDataSource()).getQfMap().get(prod);
102                //logger.trace("qf: {}", qf.toString());
103                Integer intVal = (int) r.getValue();
104                //logger.trace("intVal: {}", intVal.toString());
105                // getNameForValue wants a STRING representation of an INTEGER
106                String str = qf.getNameForValue(intVal.toString());
107                //logger.trace("str: {}", str);
108                
109                result.add("<tr><td>" + getMenuLabel()
110                           + ":</td><td  align=\"right\">"
111                           + str + "</td></tr>");
112    
113                return result;
114            }
115            } catch (Exception exc) {
116                // Just catching the exception here so we can send it to logger,
117                // otherwise it'll get caught in DisplayControlImpl.getCursorReadout,
118                // get lost in LogUtil.consoleMessage (where do those go??...),
119                // and "doCursorReadout" will be mysteriously shut off.
120                logger.warn("Exception caught: {}", exc.getMessage());
121                // re-throw it so DisplayControlImpl can still do its thing.
122                throw exc;
123            }
124        return null;
125
126    }
127}