001 /** 002 * 003 */ 004 package org.wdssii.dualpol.icing; 005 006 import java.io.BufferedReader; 007 import java.io.File; 008 import java.io.FileReader; 009 import java.text.SimpleDateFormat; 010 import java.util.ArrayList; 011 import java.util.Collections; 012 import java.util.Comparator; 013 import java.util.Date; 014 import java.util.List; 015 016 import org.apache.commons.logging.Log; 017 import org.apache.commons.logging.LogFactory; 018 import org.wdssii.core.DataUnavailableException; 019 import org.wdssii.core.Location; 020 021 /** 022 * 023 * Reads PIREPS (pilot reports of icing) 024 * 025 * @author lakshman 026 * 027 */ 028 public class PirepsReader { 029 030 private static Log log = LogFactory.getLog(PirepsReader.class); 031 032 public PirepsReader() { 033 } 034 035 private static float FeetToKms(float feet){ 036 return 0.0003048f * feet; 037 } 038 039 /** pass in the file name and obtain an object back. */ 040 public List<PilotReport> createObject(File path) { 041 try { 042 List<PilotReport> reports = new ArrayList<PilotReport>(); 043 BufferedReader reader = new BufferedReader(new FileReader(path)); 044 String line; 045 while ((line = reader.readLine()) != null) { 046 String[] fields = line.split("\t"); 047 if ( fields.length != 12 ){ 048 log.warn("Found line with only " + fields.length + " fields in " + path); 049 continue; 050 } 051 // preprend with zeroes if necessary so that 132 becomes 0132 052 fields[1] = fields[1].trim(); 053 while (fields[1].length() != 4 ){ 054 fields[1] = "0" + fields[1]; 055 } 056 Date time = new SimpleDateFormat("yyyyMMddHHmm") 057 .parse(fields[0] + fields[1]); 058 float lat = Float.parseFloat(fields[6]); 059 float lon = - Float.parseFloat(fields[7]); // they report 97 when they mean -97 060 float lowHt = FeetToKms( Float.parseFloat(fields[8]) ); 061 float topHt = FeetToKms( Float.parseFloat(fields[9]) ); 062 int intensity = Integer.parseInt(fields[10]); 063 PilotReport report = new PilotReport(time, new Location(lat, 064 lon, lowHt), new Location(lat, lon, topHt), intensity); 065 reports.add(report); 066 } 067 if (log.isInfoEnabled()) { 068 log.info("Found " + reports.size() + " pilot reports in " 069 + path); 070 } 071 return reports; 072 } catch (Exception e) { 073 log.warn("Unable to read " + path, e); 074 throw new DataUnavailableException(e); 075 } 076 } 077 078 /** 079 * Reads all the reports in a given directory. The filename should match the 080 * given pattern 081 */ 082 public PilotReport[] readReports(String dir, String pattern) { 083 try { 084 List<PilotReport> reports = new ArrayList<PilotReport>(); 085 File[] files = new File(dir).listFiles(); 086 for (File f : files) { 087 if ( f.getName().contains(pattern) ){ 088 List<PilotReport> r = this.createObject(f); 089 reports.addAll(r); 090 } 091 } 092 Collections.sort(reports, new Comparator<PilotReport>() { 093 @Override 094 public int compare(PilotReport o1, PilotReport o2) { 095 return o1.getTime().compareTo(o2.getTime()); 096 } 097 }); 098 return reports.toArray(new PilotReport[0]); 099 } catch (Exception e) { 100 log.warn(e); 101 throw new DataUnavailableException(e); 102 } 103 } 104 105 }