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