package henfredemars;

import java.util.HashMap;

//Locate stations by their id
public class StationLocator {
	
	private HashMap<String,float[]> lookupTable = null;
	
	public StationLocator(String fileName) {
		lookupTable = new HashMap<String,float[]>();
		String[] f = Util.readTextfile(fileName);
		int tableStart = -1;
		for (int i = 0; i<f.length; i++) {
			if (f[i].startsWith("010010")) {
				tableStart = i;
				break;
			}
		}
		if (tableStart==-1) {
			throw new RuntimeException("StationLocator - table not found");
		}
		for (int i = tableStart; !f[i].startsWith("997000"); i++) {
			String latString = f[i].substring(59,63);
			String lonString = f[i].substring(65,70);
			if (latString.trim().isEmpty()) continue;
			if (lonString.trim().isEmpty()) continue;
			float latitude = Integer.valueOf(latString)/100.0f;
			float longitude = Integer.valueOf(lonString)/100.0f;
			if (f[i].charAt(63)=='S') {
				latitude *= -1;
			}
			if (f[i].charAt(70)=='W') {
				longitude *= -1;
			}
			if (f[i].charAt(63)==' ') continue;
			if (f[i].charAt(70)==' ') continue;
			float[] farray = {latitude,longitude};
			lookupTable.put(f[i].split(" +")[0], farray);
		}
	}
	
	public float[] getLatLon(String stationid) {
		return lookupTable.get(stationid);
	}
	
	public boolean inTargetArea(String stationid) {
		if (!knowsStation(stationid)) return false;
		float[] latlon = getLatLon(stationid);
		boolean latInRange = ((latlon[0] < -73) && (latlon[0] > -135));
		boolean lonInRange = ((latlon[1] < 50) && (latlon[1] > 23));
		return latInRange && lonInRange;
	}
	
	public boolean knowsStation(String stationid) {
		return lookupTable.containsKey(stationid);
	}

}

@@ -1433,11 +1433,10 @@
 %5D %3C 
--73
+49
 ) &&
@@ -1453,11 +1453,9 @@
 %5D %3E 
--13
+2
 5));
@@ -1496,10 +1496,11 @@
 %5D %3C 
+-7
 5
-0
 ) &&
@@ -1517,10 +1517,12 @@
 %5D %3E 
-23
+-125
 ));%0A

Update StationLocator.java