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