@@ -37,22 +37,23 @@ public class BuoyDataReader
3737{
3838 static final String MISSING = "MM" ;
3939 static final Logger logger = LoggerFactory .getLogger (BuoyDataReader .class );
40+ static final String expectedFields = "#STN LAT LON YYYY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES PTDY ATMP WTMP DEWP VIS TIDE" ;
41+ static final String expectedUnits = "#text deg deg yr mo day hr mn degT m/s m/s m sec sec degT hPa hPa degC degC degC nmi ft" ;
4042
4143 public static List <BuoyDataRecord > read (String url ) throws IOException {
4244 List <BuoyDataRecord > recs = new ArrayList <>();
4345 URL dataUrl = new URL (url );
4446
4547 try (BufferedReader reader = new BufferedReader (new InputStreamReader (dataUrl .openStream ()))) {
46- // Skip Header lines
47- String header = reader .readLine ();
48- boolean headerOk = checkHeader (header );
49- if (!headerOk ) {
50- throw new IOException ("Unexpected header format: " + header );
48+ String fields = reader .readLine ();
49+ boolean fieldsOk = checkFields (fields );
50+ if (!fieldsOk ) {
51+ logger .warn ("Fields in header do not match expected fields:\n \t {}" , fields );
5152 }
5253 String units = reader .readLine ();
5354 boolean unitsOk = checkUnits (units );
5455 if (!unitsOk ) {
55- throw new IOException ( "Unexpected units format: " + units );
56+ logger . warn ( "Units in header do not match expected units: \n \t {}" , units );
5657 }
5758 while (true ) {
5859 String inline = reader .readLine ();
@@ -61,8 +62,6 @@ public static List<BuoyDataRecord> read(String url) throws IOException {
6162 if (inline .isBlank ()) continue ;
6263
6364 String [] values = inline .split ("\\ s+" );
64- // #STN LAT LON YYYY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES PTDY ATMP WTMP DEWP VIS TIDE
65- // #text deg deg yr mo day hr mn degT m/s m/s m sec sec degT hPa hPa degC degC degC nmi ft
6665 BuoyDataRecord rec = new BuoyDataRecord ();
6766 rec .id = values [0 ];
6867 rec .lat = parseDouble (values [1 ]);
@@ -103,18 +102,14 @@ public static List<BuoyDataRecord> read(String url) throws IOException {
103102 }
104103 }
105104
106- // #STN LAT LON YYYY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES PTDY ATMP WTMP DEWP VIS TIDE
107- public static boolean checkHeader (String header ) {
108- // TODO check fields are as expected
109- String [] fields = header .split ("\\ s+" );
110- return (fields .length == 22 );
105+ public static boolean checkFields (String fields ) {
106+ fields = fields .trim ();
107+ return fields .equalsIgnoreCase (expectedFields );
111108 }
112109
113- // #text deg deg yr mo day hr mn degT m/s m/s m sec sec degT hPa hPa degC degC degC nmi ft
114110 public static boolean checkUnits (String units ) {
115- String [] fields = units .split ("\\ s+" );
116- // TODO check units are as expected
117- return (fields .length == 22 );
111+ units = units .trim ();
112+ return units .equalsIgnoreCase (expectedUnits );
118113 }
119114
120115 // Default URL is https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt
@@ -138,7 +133,7 @@ public static Double parseDouble(String s) {
138133 try {
139134 return Double .parseDouble (s );
140135 } catch (NumberFormatException e ) {
141- logger .error ("" , e );
136+ logger .error (e . getMessage () , e );
142137 return null ;
143138 }
144139 }
@@ -153,7 +148,7 @@ public static Integer parseInt(String s) {
153148 try {
154149 return Integer .parseInt (s );
155150 } catch (NumberFormatException e ) {
156- logger .error ("" , e );
151+ logger .error (e . getMessage () , e );
157152 return null ;
158153 }
159154 }
0 commit comments