I am trying to create a java program that retrieves a METARs from the stations like this:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class metar{
public static void main(String[] args) throws IOException {
URL aURLGlobal=null;
Document doc;
String fichier ="stations.txt";
try {
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
// need http protocol
String ligne;
while ((ligne=br.readLine())!=null){
URL aURL = new URL( "https://www.aviationweather.gov/adds/metars/?station_ids="+ligne+"&std_trans=standard&chk_metars=on&hoursStr=most+recent+only&submitmet=Submit");
// System.out.println("******************city num"+aURL.toString()+"**************************");
doc = Jsoup.connect(aURL.toString()).get();
String element= doc.select("FONT").first().ownText();
System.out.println(element);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
stations.txt :
LSGG
LFMD
LFMN
LFQB
LFMK
LFCR
LFML
LFMI
LFMY
LFRK
result :
LSGG 201620Z 22018KT 7000 -RA FEW014 SCT025 OVC045 13/10 Q1010 TEMPO 4000 RA BR
LFMD 201600Z AUTO 23017G28KT 200V280 CAVOK 18/10 Q1008
LFMN 201600Z 07015KT CAVOK 17/12 Q1007 TEMPO WS ALL RWY
LFQB 201600Z AUTO 32006KT 9999 -RA BKN004/// BKN035/// BKN042/// ///TCU 08/08 Q1005
LFMK 201600Z AUTO 28015KT 240V300 CAVOK 20/03 Q1018
LFCR 201600Z AUTO 25015KT 9999 OVC018 13/10 Q1018
LFML 201530Z 25017KT CAVOK 17/10 Q1016 NOSIG
LFMI 201600Z AUTO 29014KT CAVOK 17/09 Q1016
LFMY 201600Z AUTO 28012KT 250V310 CAVOK 17/09 Q1016
LFRK 201600Z AUTO 29012KT 9999 BKN036 OVC044 09/08 Q1009
I am now looking to parse a json file METARs, I found a metar.js script to parse any metar
http://epeli.github.io/metar.js/
My question is (I do not know too in json): did i can use it to parse all of my METARs and copied in one json file, that is to say call the file parser.js from java.
someone can help me please..
Edit : THANKS A LOT FOR YOUR IDEA
CODE:
package tests;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class test {
public static void main(String[] args) throws IOException, ScriptException, NoSuchMethodException {
URL aURLGlobal=null;
Document doc;
String fichier ="stations.txt";
try {
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
// need http protocol
String ligne;
while ((ligne=br.readLine())!=null){
URL aURL = new URL( "https://www.aviationweather.gov/adds/metars/?station_ids="+ligne+"&std_trans=standard&chk_metars=on&hoursStr=most+recent+only&submitmet=Submit");
// System.out.println("******************city num"+aURL.toString()+"**************************");
doc = Jsoup.connect(aURL.toString()).get();
String element= doc.select("FONT").first().ownText();
System.out.println(element);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// read script file
engine.eval(Files.newBufferedReader(Paths.get("C:\\metar.js"), StandardCharsets.UTF_8));
Invocable inv = (Invocable) engine;
// call function from script file
inv.invokeFunction("parseMETAR", element);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}}
error :
LSGG 201620Z 22018KT 7000 -RA FEW014 SCT025 OVC045 13/10 Q1010 TEMPO 4000 RA BR
Exception in thread "main" java.lang.NoSuchMethodException: No such function parse
at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:197)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:381)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:187)
at tests.test.main(test.java:52)
as I told you, json is a black box for me, it is the parse function to be used in metar.js, and I put my metar parameter recevid from the previous step, Thanks for your help