001/*
002 * $Id: AxformInfo.java,v 1.7 2011/03/24 16:06:32 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
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 */
030package edu.wisc.ssec.mcidasv.data;
031
032import java.io.BufferedReader;
033import java.io.File;
034import java.io.FileReader;
035import java.util.ArrayList;
036import java.util.Hashtable;
037import java.util.List;
038
039public class AxformInfo extends HeaderInfo {
040
041        /** The url */
042        private String dataFile = "";
043        private boolean isAxform = false;
044
045        /**
046         * Ctor for xml encoding
047         */
048        public AxformInfo() {}
049
050        /**
051         * CTOR
052         *
053         * @param filename The filename
054         */
055        public AxformInfo(File thisFile) {
056                this(thisFile.getAbsolutePath());
057        }
058
059        /**
060         * CTOR
061         *
062         * @param filename The filename
063         */
064        public AxformInfo(String filename) {
065                super(filename);
066        }
067
068        /**
069         * Is the file an AXFORM header file?
070         */
071        public boolean isAxformInfoHeader() {
072                parseHeader();
073                return isAxform;
074        }
075        
076        /**
077         * Which band/file is latitude?
078         */
079        public int getLatBandNum() {
080                return 1;
081        }
082        public String getLatBandFile() {
083                parseHeader();
084                List bandFiles = (List)getParameter(NAVFILES, new ArrayList());
085                if (bandFiles.size()!=2) return "";
086                return (String)(bandFiles.get(0));
087        }
088
089        /**
090         * Which band/file is longitude?
091         */
092        public int getLonBandNum() {
093                return 1;
094        }
095        public String getLonBandFile() {
096                parseHeader();
097                List bandFiles = (List)getParameter(NAVFILES, new ArrayList());
098                if (bandFiles.size()!=2) return "";
099                return (String)(bandFiles.get(1));
100        }
101
102        /**
103         * Parse a potential AXFORM header file
104         */
105        protected void parseHeader() {
106                if (haveParsed()) return;
107                if (!doesExist()) {
108                        isAxform = false;
109                        return;
110                }
111
112                try {
113                        BufferedReader br = new BufferedReader(new FileReader(getFilename()));
114                        int lineNum = 0;
115                        String line;
116                        String description = "";
117                        boolean gotFiles = false;
118                        
119                        List bandNames = new ArrayList();
120                        List bandFiles = new ArrayList();
121                        
122                        String latFile = "";
123                        String lonFile = "";
124                        File thisFile = new File(getFilename());
125                        String parent = thisFile.getParent();
126                        if (parent==null) parent=".";
127
128                        while ((line = br.readLine()) != null) {
129                                lineNum++;
130                                if (line.trim().equals("Space Science & Engineering Center")) {
131                                        isAxform = true;
132                                        continue;
133                                }
134                                if (!isAxform) break;
135
136                                if (line.length() < 80) {
137                                        if (lineNum > 15) gotFiles = true;
138                                        continue;
139                                }
140
141                                // Process the description from lines 5 and 6
142                                if (lineNum==5) {
143                                        description += line.substring(13, 41).trim();
144                                }
145                                else if (lineNum==6) {
146                                        description += " " + line.substring(14, 23).trim() +" " + line.substring(59, 71).trim();
147                                        setParameter(DESCRIPTION, description);
148                                }
149
150                                // Process the file list starting at line 15
151                                else if (lineNum>=15 && !gotFiles) {
152                                        String parameter = line.substring(0, 13).trim();
153                                        if (parameter.equals("Header")) {
154                                                isAxform = true;
155                                        }
156                                        else if (parameter.equals("Latitude")) {
157                                                latFile = parent + "/" + line.substring(66).trim();
158                                        }
159                                        else if (parameter.equals("Longitude")) {
160                                                lonFile = parent + "/" + line.substring(66).trim();
161                                        }
162                                        else {
163                                                setParameter(LINES, Integer.parseInt(line.substring(24, 31).trim()));
164                                                setParameter(ELEMENTS, Integer.parseInt(line.substring(32, 40).trim()));
165                                                setParameter(UNIT, parameter);
166                                                String band = line.substring(19, 23).trim();
167                                                String format = line.substring(41, 59).trim();
168                                                System.out.println("looking at format line: " + format);
169                                                if (format.indexOf("ASCII") >= 0) {
170                                                        setParameter(DATATYPE, kFormatASCII);
171                                                }
172                                                else if (format.indexOf("8 bit") >= 0) {
173                                                        setParameter(DATATYPE, kFormat1ByteUInt);
174                                                }
175                                                else if (format.indexOf("16 bit") >= 0) {
176                                                        setParameter(DATATYPE, kFormat2ByteSInt);
177                                                }
178                                                else if (format.indexOf("32 bit") >= 0) {
179                                                        setParameter(DATATYPE, kFormat4ByteSInt);
180                                                }
181                                                String filename = line.substring(66).trim();
182                                                filename = parent + "/" + filename;
183                                                bandFiles.add(filename);
184                                                bandNames.add("Band " + band);
185                                        }
186                                }
187
188                                // Look for the missing value, bail when you find it
189                                else if (gotFiles) {
190                                        if (line.indexOf("Navigation files missing data value") >= 0) {
191                                                setParameter(MISSINGVALUE, Float.parseFloat(line.substring(44, 80).trim()));                            
192                                                break;
193                                        }
194                                }
195
196                                setParameter(BANDNAMES, bandNames);
197                                setParameter(BANDFILES, bandFiles);
198                                
199                                List latlonFiles = new ArrayList();
200                                latlonFiles.add(latFile);
201                                latlonFiles.add(lonFile);
202                                setParameter(NAVFILES, latlonFiles);
203
204                        }
205                        br.close();
206                }
207                catch (Exception e) {
208                        e.printStackTrace();
209                }
210
211        }
212
213}