001    /*
002     * $Id: EnviInfo.java,v 1.5 2012/02/19 17:35:44 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
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    package edu.wisc.ssec.mcidasv.data;
031    
032    import java.io.BufferedReader;
033    import java.io.File;
034    import java.io.FileReader;
035    import java.util.ArrayList;
036    import java.util.Hashtable;
037    import java.util.List;
038    
039    public class EnviInfo extends HeaderInfo {
040    
041            /** The url */
042            private String dataFile = "";
043            private boolean isEnvi = false;
044    
045            /**
046             * Ctor for xml encoding
047             */
048            public EnviInfo() {}
049    
050            /**
051             * CTOR
052             *
053             * @param filename The filename
054             */
055            public EnviInfo(File thisFile) {
056                    this(thisFile.getAbsolutePath());
057            }
058            
059            /**
060             * CTOR
061             *
062             * @param filename The filename
063             */
064            public EnviInfo(String filename) {
065                    super(filename);
066                    this.dataFile = filename.replace(".hdr", ".img");
067            }
068    
069            /**
070             * Is the file an ENVI header file?
071             */
072            public boolean isEnviHeader() {
073                    parseHeader();
074                    return isEnvi;
075            }
076    
077            /**
078             * Can we find a matching ENVI data file?
079             */
080            public boolean hasEnviData() {
081                    File testFile = new File(dataFile);
082                    if (testFile.exists()) return true;
083                    else return false;
084            }
085    
086            /**
087             * Is this a navigation header file?
088             */
089            public boolean isNavHeader() {
090                    parseHeader();
091                    List bandNames = (List)getParameter(BANDNAMES, new ArrayList());
092                    if (bandNames == null) return false;
093                    if (bandNames.contains("Latitude") && bandNames.contains("Longitude")) return true;
094                    return false;
095            }
096    
097            /**
098             * Which band/file is latitude?
099             */
100            public int getLatBandNum() {
101                    parseHeader();
102                    List bandNames = (List)getParameter(BANDNAMES, new ArrayList());
103                    for (int i=0; i<bandNames.size(); i++) {
104                            if (bandNames.get(i).equals("Latitude")) return i+1;
105                    }
106                    return -1;
107            }
108            public String getLatBandFile() {
109                    parseHeader();
110                    List bandFiles = (List)getParameter(BANDFILES, new ArrayList());
111                    int bandNum = getLatBandNum();
112                    if (bandNum < 0) return "";
113                    return (String)(bandFiles.get(bandNum-1));
114            }
115    
116            /**
117             * Which band/file is longitude?
118             */
119            public int getLonBandNum() {
120                    parseHeader();
121                    List bandNames = (List)getParameter(BANDNAMES, new ArrayList());
122                    for (int i=0; i<bandNames.size(); i++) {
123                            if (bandNames.get(i).equals("Longitude")) return i+1;
124                    }
125                    return -1;
126            }
127            public String getLonBandFile() {
128                    parseHeader();
129                    List bandFiles = (List)getParameter(BANDFILES, new ArrayList());
130                    int bandNum = getLonBandNum();
131                    if (bandNum < 0) return "";
132                    return (String)(bandFiles.get(bandNum-1));
133            }
134    
135            /**
136             * Return a FlatField representing the data
137             */
138    //      public FlatField getDataField() {
139    //
140    //      }
141    
142            /**
143             * Return a Gridded2DSet representing navigation
144             */
145    //      public Gridded2DSet getNavField() {
146    //
147    //      }
148    
149            /**
150             * Parse a potential ENVI header file
151             */
152            protected void parseHeader() {
153                    if (haveParsed()) return;
154                    if (!doesExist()) {
155                            isEnvi = false;
156                            return;
157                    }
158    
159                    try {
160                            BufferedReader br = new BufferedReader(new FileReader(getFilename()));
161                            String line;
162                            String parameter = "";
163                            String value = "";
164                            boolean inValue = false;
165    
166                            List bandNames = new ArrayList();
167                            List bandFiles = new ArrayList();
168    
169                            while ((line = br.readLine()) != null) {
170                                    if (line.trim().equals("ENVI")) {
171                                            isEnvi = true;
172                                            continue;
173                                    }
174                                    if (!isEnvi) break;
175    
176                                    int indexOfEquals = line.indexOf("=");
177                                    int indexOfOpen = line.indexOf("{");
178                                    int indexOfClose = line.indexOf("}");
179                                    if (indexOfEquals >= 0) {
180                                            parameter = line.substring(0, indexOfEquals).trim();
181                                            value = "";
182                                            inValue = false;
183                                    }
184                                    if (indexOfOpen >= 0) {
185                                            if (indexOfClose >= 0) {
186                                                    value += line.substring(indexOfOpen+1, indexOfClose).trim();
187                                                    inValue = false;
188                                            }
189                                            else {
190                                                    value += line.substring(indexOfOpen+1).trim();
191                                                    inValue = true;
192                                                    continue;
193                                            }
194                                    }
195                                    else if (inValue) {
196                                            if (indexOfClose >= 0) {
197                                                    value += line.substring(0, indexOfClose).trim();
198                                                    inValue = false;
199                                            }
200                                            else {
201                                                    value += line.trim();
202                                                    continue;
203                                            }
204                                    }
205                                    else {
206                                            value += line.substring(indexOfEquals+1).trim();
207                                    }
208    
209                                    if (parameter.equals("")) continue;
210    
211                                    if (parameter.equals("description")) {
212                                            setParameter(DESCRIPTION, value);
213                                    }
214                                    else if (parameter.equals("samples")) {
215                                            setParameter(ELEMENTS, Integer.parseInt(value));
216                                    }
217                                    else if (parameter.equals("lines")) {
218                                            setParameter(LINES, Integer.parseInt(value));
219                                    }
220                                    else if (parameter.equals("header offset")) {
221                                            setParameter(OFFSET, Integer.parseInt(value));
222                                    }
223                                    else if (parameter.equals("data type")) {
224                                            setParameter(DATATYPE, Integer.parseInt(value));
225                                    }
226                                    else if (parameter.equals("data ignore value") ||
227                                                    parameter.equals("bad value")) {
228                                            setParameter(MISSINGVALUE, Float.parseFloat(value));                            
229                                    }
230                                    else if (parameter.equals("interleave")) {
231                                            setParameter(INTERLEAVE, value.toUpperCase());
232                                    }
233                                    else if (parameter.equals("byte order")) {
234                                            boolean bigEndian = false;
235                                            if (value.equals("1")) bigEndian = true;
236                                            setParameter(BIGENDIAN, bigEndian);
237                                    }
238                                    else if (parameter.equals("bands")) {
239                                            if (bandNames.size() <= 0 && bandFiles.size() <= 0) {
240                                                    int bandCount = Integer.parseInt(value);
241                                                    for (int i=0; i<bandCount; i++) {
242                                                            bandNames.add("Band " + i+1);
243                                                            bandFiles.add(dataFile);
244                                                    }
245                                                    setParameter(BANDNAMES, bandNames);
246                                                    setParameter(BANDFILES, bandFiles);
247                                            }
248                                    }
249                                    else if (parameter.equals("band names")) {
250                                            bandNames = new ArrayList();
251                                            bandFiles = new ArrayList();
252                                            String[] bandNamesSplit = value.split(",");
253                                            for (int i=0; i<bandNamesSplit.length; i++) {
254                                                    bandNames.add(bandNamesSplit[i].trim());
255                                                    bandFiles.add(dataFile);
256                                            }
257                                            setParameter(BANDNAMES, bandNames);
258                                            setParameter(BANDFILES, bandFiles);
259                                    }
260    
261                            }
262                            br.close();
263                    }
264                    catch (Exception e) {
265                            e.printStackTrace();
266                    }
267    
268            }
269    
270    }