Home | My New Hugo Project

Getting XML from NOAA in javascript/jquery

2021-01-26

Looking into using one of those “api”.   However, the NOAA provides - well, free data in XML format.

So, next project will be to use something to call every so many hours - store it and then figure out a way to tabulate.  Could be a fun way to use python pandas.

Some important notes

  1. datatype - this is a jquary attribute.
  2. set you time out - other wise you will get a "Timeout violation"

Code to get the data

var weather = "" // global var

$.ajax({url: “https://w1.weather.gov/xml/current_obs/KDCA.xml", timeout: 1000, datatype:“xml”, success: function(result){ weather = result; append_txt( weather );

}});

Code to display the data

var temp = $(weather).find('temp_f').text();
    var location = $(weather).find('location').text();
    var wind_string = $(weather).find('wind_string').text();
    var conditions_gen = $(weather).find('weather').text();
    var wind_kt = $(weather).find('wind_kt').text();
    var wind_dir = $(weather).find('wind_dir').text();
    console.log("Temp:", temp);
    console.log("Wind:", wind_string);
    console.log("Conditions:", conditions_gen);
    console.log("Location:", location);
    console.log("Wind knots: ", wind_kt);
    console.log("Wind Direction: ", wind_dir);

Use Curl to figure out how to parse

curl "https://w1.weather.gov/xml/current_obs/KDCA.xml" <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet href="latest_ob.xsl" type="text/xsl"?> <current_observation version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.weather.gov/view/current_observation.xsd"> <credit>NOAA's National Weather Service</credit> <credit_URL>http://weather.gov/</credit_URL> <image> <url>http://weather.gov/images/xml_logo.gif</url> <title>NOAA's National Weather Service</title> <link>http://weather.gov</link> </image> <suggested_pickup>15 minutes after the hour</suggested_pickup> <suggested_pickup_period>60</suggested_pickup_period> <location>Washington/Reagan National Airport, DC, VA</location> <station_id>KDCA</station_id> <latitude>38.84833</latitude> <longitude>-77.03417</longitude> <observation_time>Last Updated on Jan 26 2021, 3:52 pm EST</observation_time> <observation_time_rfc822>Tue, 26 Jan 2021 15:52:00 -0500</observation_time_rfc822> <weather>Light Drizzle Fog/Mist</weather> <temperature_string>37.0 F (2.8 C)</temperature_string> <temp_f>37.0</temp_f> <temp_c>2.8</temp_c> <relative_humidity>93</relative_humidity> <wind_string>Northeast at 4.6 MPH (4 KT)</wind_string> <wind_dir>Northeast</wind_dir> <wind_degrees>30</wind_degrees> <wind_mph>4.6</wind_mph> <wind_kt>4</wind_kt> <pressure_string>1010.4 mb</pressure_string> <pressure_mb>1010.4</pressure_mb> <pressure_in>29.84</pressure_in> <dewpoint_string>35.1 F (1.7 C)</dewpoint_string> <dewpoint_f>35.1</dewpoint_f> <dewpoint_c>1.7</dewpoint_c> <windchill_string>33 F (1 C)</windchill_string> <windchill_f>33</windchill_f> <windchill_c>1</windchill_c> <visibility_mi>1.25</visibility_mi> <icon_url_base>http://forecast.weather.gov/images/wtf/small/</icon_url_base> <two_day_history_url>http://www.weather.gov/data/obhistory/KDCA.html</two_day_history_url> <icon_url_name>ra.png</icon_url_name> <ob_url>http://www.weather.gov/data/METAR/KDCA.1.txt</ob_url> <disclaimer_url>http://weather.gov/disclaimer.html</disclaimer_url> <copyright_url>http://weather.gov/disclaimer.html</copyright_url> <privacy_policy_url>http://weather.gov/notice.html</privacy_policy_url> </current_observation>