|
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DatumsDataGetter {
public static void main(String[] args) {
String sosUrl = "https://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/SOS";
String baseUrl = sosUrl + "?service=SOS&request=GetObservation&version=1.0.0&observedProperty=datums";
String saveToYourLocalDir = "C:\\temp\\sosdata\\";
String stationsIds[] = {"8454049", "8637689", "9754228", "9759394"};
String epoch = "Current";
String unit = "Meters";
String responseFormat = "csv";
String url;
URL dataUrl;
String fileName;
try {
String responseMIMEType = getResponseMIMEType(responseFormat);
for (int i = 0; i < stationsIds.length; i++) {
if (!isValidNwlonStationId(stationsIds[i])) {
throw new IOException(" Wrong Station ID: " + stationsIds[i] + ", please enter a valid station ID.");
}
url = baseUrl + "&offering=urn:ioos:station:NOAA.NOS.CO-OPS:" + stationsIds[i]
+ "&responseFormat=" + urlEncode(responseMIMEType);
if (epoch != null && epoch.length() != 0) {
if (epoch.equalsIgnoreCase("Current") || epoch.equalsIgnoreCase("Superseded")) {
url += "&epoch=" + epoch;
} else {
throw new IOException(" Wrong Epoch: please enter a valid epoch string.");
}
}
if (unit != null && unit.length() != 0) {
if (unit.equalsIgnoreCase("Meters") || unit.equalsIgnoreCase("Feet")) {
url += "&unit=" + unit;
} else {
throw new IOException(" Wrong Unit: please enter a valid unit string.");
}
}
dataUrl = new URL(url);
fileName = getFileName(stationsIds[i], responseFormat, saveToYourLocalDir);
getResource(dataUrl, fileName);
}
} catch (Exception e) {
System.err.println("ERROR: ******* " + e.getMessage());
}
}
public static boolean isValidNwlonStationId(String stationId) {
Pattern regexWL = Pattern.compile("^[1-9][0-9]{6}$");
if (stationId == null) {
return false;
} else {
stationId = stationId.trim();
Matcher mWL = regexWL.matcher(stationId);
if (mWL.matches()) {
return true;
} else {
return false;
}
}
}
private static String getResponseMIMEType(String formatOption) throws IOException {
String responseMIMEType = "text/csv";
if (formatOption.equalsIgnoreCase("csv")) {
responseMIMEType = "text/csv";
} else if (formatOption.equalsIgnoreCase("tsv")) {
responseMIMEType = "text/tab-separated-values";
} else if (formatOption.equalsIgnoreCase("kml")) {
responseMIMEType = "application/vnd.google-earth.kml+xml";
} else if (formatOption.equalsIgnoreCase("xml")) {
responseMIMEType = "text/xml;schema=\"ioos/0.6.1\"";
} else {
throw new IOException(" Wrong Response Format: please enter a valid response format string.");
}
return responseMIMEType;
}
private static String urlEncode(String inString) {
if (inString != null) {
inString = inString.replace("/", "%2F");
inString = inString.replace("+", "%2B");
}
return inString;
}
public static String getFileName(String stationId, String responseFormat, String saveToYourLocalDir) {
String fileName = saveToYourLocalDir + "datums_" + stationId + "." + responseFormat.toLowerCase();
return fileName;
}
public static void getResource(URL url, String fileName) {
HttpURLConnection huc = null;
BufferedInputStream in = null;
FileOutputStream file;
BufferedOutputStream out = null;
try {
huc = (HttpURLConnection) url.openConnection();
huc.setAllowUserInteraction(false);
huc.setDoInput(true);
huc.setDoOutput(false);
huc.setUseCaches(false);
huc.setReadTimeout(30000);
huc.connect();
in = new BufferedInputStream(huc.getInputStream());
file = new FileOutputStream(fileName);
out = new BufferedOutputStream(file);
int len;
byte[] data = new byte[1024];
while ((len = in.read(data, 0, 1024)) >= 0) {
out.write(data, 0, len);
}
out.flush();
out.close();
in.close();
huc.disconnect();
Thread.sleep(4000);
} catch (InterruptedException iex) {
System.err.println("ERROR: ******* " + iex.toString());
} catch (IOException ex) {
System.err.println("ERROR: ******* " + ex.toString());
} finally {
try {
in.close();
out.close();
huc.disconnect();
} catch (IOException ex) {
System.err.println("ERROR: ******* " + ex.toString());
}
}
}
}
|