001    /*
002     * $Id: HeaderInfo.java,v 1.6 2012/02/19 17:35:45 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.File;
033    import java.util.ArrayList;
034    import java.util.Hashtable;
035    import java.util.List;
036    
037    public class HeaderInfo {
038    
039            // Enumerate some constants
040            public static final int kFormatUnknown = -1;
041            public static final int kFormatASCII = 0;
042            public static final int kFormat1ByteUInt = 1;
043            public static final int kFormat2ByteSInt = 2;
044            public static final int kFormat4ByteSInt = 3;
045            public static final int kFormat4ByteFloat = 4;
046            public static final int kFormat8ByteDouble = 5;
047            public static final int kFormat2x8Byte = 9;
048            public static final int kFormat2ByteUInt = 12;
049            public static final int kFormatImage = 8080;
050            
051            public static final int kNavigationUnknown = -1;
052            public static final int kNavigationBounds = 1;
053            public static final int kNavigationFiles = 2;
054            
055            public static final String kInterleaveSequential = "BSQ";
056            public static final String kInterleaveByLine = "BIL";
057            public static final String kInterleaveByPixel = "BIP";
058    
059            public static final String DESCRIPTION = "description";
060            public static final String ELEMENTS = "elements";
061            public static final String LINES = "lines";
062            public static final String UNIT = "unit";
063            public static final String OFFSET = "offset";
064            public static final String DATATYPE = "dataType";
065            public static final String MISSINGVALUE = "missingValue";
066            public static final String BANDNAMES = "bandNames";
067            public static final String BANDFILES = "bandFiles";
068            public static final String INTERLEAVE = "interleave";
069            public static final String BYTEORDER = "byteOrder";
070            public static final String BIGENDIAN = "bigEndian";
071            public static final String NAVBOUNDS = "navBounds";
072            public static final String NAVFILES = "navFiles";
073    
074            /** The url */
075            private String headerFile = "";
076            private Hashtable parameters = new Hashtable();
077    
078            /**
079             * Ctor for xml encoding
080             */
081            public HeaderInfo() {}
082    
083            /**
084             * CTOR
085             *
086             * @param filename The filename
087             */
088            public HeaderInfo(File thisFile) {
089                    this(thisFile.getAbsolutePath());
090            }
091            
092            /**
093             * CTOR
094             *
095             * @param filename The filename
096             */
097            public HeaderInfo(String filename) {
098                    setFilename(filename);
099            }
100            
101            /**
102             * Set the filename we are working with
103             */
104            public void setFilename(String filename) {
105                    parameters = new Hashtable();
106                    headerFile = filename;
107            }
108            
109            /**
110             * Get the filename we are working with
111             */
112            public String getFilename() {
113                    return headerFile;
114            }
115            
116            /**
117             * Get the number of bands this header knows about
118             */
119            public int getBandCount() {
120                    if (!haveParsed()) {
121                            parseHeader();
122                    }
123                    List bandNames = getParameter(BANDNAMES, new ArrayList());
124                    return bandNames.size();
125            }
126                    
127            /**
128             * Return the matching header parameter if available, default value if not available
129             */
130            public String getParameter(String parameter, String defaultValue) {
131                    parseHeader();
132                    Object hashedValue = parameters.get(parameter);
133                    if (hashedValue == null || !(hashedValue instanceof String)) return defaultValue;
134                    return (String)hashedValue;
135            }
136            public Integer getParameter(String parameter, Integer defaultValue) {
137                    parseHeader();
138                    Object hashedValue = parameters.get(parameter);
139                    if (hashedValue == null || !(hashedValue instanceof Integer)) return defaultValue;
140                    return (Integer)hashedValue;
141            }
142            public Float getParameter(String parameter, Float defaultValue) {
143                    parseHeader();
144                    Object hashedValue = parameters.get(parameter);
145                    if (hashedValue == null || !(hashedValue instanceof Float)) return defaultValue;
146                    return (Float)hashedValue;
147            }
148            public Boolean getParameter(String parameter, Boolean defaultValue) {
149                    parseHeader();
150                    Object hashedValue = parameters.get(parameter);
151                    if (hashedValue == null || !(hashedValue instanceof Boolean)) return defaultValue;
152                    return (Boolean)hashedValue;
153            }
154            public List getParameter(String parameter, List defaultValue) {
155                    parseHeader();
156                    Object hashedValue = parameters.get(parameter);
157                    if (hashedValue == null || !(hashedValue instanceof List)) return defaultValue;
158                    return (List)hashedValue;
159            }
160            
161            /**
162             * Set a parsed parameter value
163             */
164            public void setParameter(String parameter, Object value) {
165                    parameters.put(parameter, value);
166            }
167            
168            /**
169             * Have we already parsed once?
170             */
171            public boolean haveParsed() {
172                    return parameters.size() > 0;
173            }
174            
175            /**
176             * Does the file we are pointing to even exist?
177             */
178            public boolean doesExist() {
179                    File checkFile = new File(headerFile);
180                    return checkFile.exists();
181            }
182            
183            /**
184             * Override this when extending for a specific header type
185             */
186            protected void parseHeader() {}
187            
188    }