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