www.webdeveloper.com
Results 1 to 7 of 7

Thread: Search Remote URL .TXT File To Determine Variables

Hybrid View

  1. #1
    Join Date
    May 2013
    Posts
    5

    Question Search Remote URL .TXT File To Determine Variables

    I am trying to look for certain data in a small remote .txt file to determine variables. The data in question is determined by METAR weather data which determines the sky condition/cloud cover.

    Like here is a remote METAR report:

    http://weather.noaa.gov/pub/data/obs...tions/CWBO.TXT

    I need the ability to load that text, and search it for the following:

    SKC
    CLR
    NSC
    FEW
    SCT
    BKN
    OVC
    VV

    If one of those are found, I need it to set a variable. Let's say the variable is m1 which determines m2:

    Code:
    if (m1 == "SKC") {
    	var m2 = 1;
    } else if (m1 == "OVC") {
    	var m2 = 4;
    }
    Is there a way to have JavaScript load the remote .txt file so it can be searched for the values I listed above which would set var m1?

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    <script>
    function cbfunc(data) {
    	var m1 = data.query.results.row[1].col0.split(/(SKC|CLR|RMK|NSC|FEW|SCT|BKN|OVC|VV)/)[1];
    	var m2 = {
    		SKC: 0,
    		CLR: 1,
    		RMK: 2,
    		NSC: 3,
    		FEW: 4,
    		SCT: 5,
    		BKN: 6,
    		OVC: 7,
    		VV: 8
    	}[m1];
    
    	alert(m2);
    };
    </script>
    <script src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D'http%3A%2F%2Fweather.noaa.gov%2Fpub%2Fdata%2Fobservations%2Fmetar%2Fstations%2FCWBO.TXT'%20&format=json&callback=cbfunc"></script>
    i added "RMK" for testing purposes, but you should be able to figure out how to customize it from here...

  3. #3
    Join Date
    May 2013
    Posts
    5
    Thanks. I can't seem to get it to work though. I saved the above code as METAR.js and added the the HTML page:

    <script type="text/javascript" src="METAR.js"></script>
    <body onload="cbfunc();">

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by BTJustice View Post
    Thanks. I can't seem to get it to work though. I saved the above code as METAR.js and added the the HTML page:

    <script type="text/javascript" src="METAR.js"></script>
    <body onload="cbfunc();">

    does it work when the code is in the HTML page itself?

    1. you don't call cbfunc yourself, ever.
    2. point to the YQL url in your HTML file
    3. last caveat: do you have "<script>" tags in metar.js? if so, clean it up and just use the code posted in the first <script> tag in metar.js.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    An alert(JSON.stringify(data)) at the beginning of the function cbFunc show the metar.
    Then some regular expressions could be useful to read all parameters...

  6. #6
    Join Date
    May 2013
    Posts
    5
    Thanks for the help with this. I got it to work now in the a separate .js file that gets loaded on a webpage. The only issue I am noticing is sometimes the Yahoo link goes out.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by BTJustice View Post
    Thanks for the help with this. I got it to work now in the a separate .js file that gets loaded on a webpage. The only issue I am noticing is sometimes the Yahoo link goes out.
    it might not be yahoo, the nws html pages go down a lot, i doubt the csv is any better...
    if you don't mind, see if you can hit the nws page the next time it goes out and let me know if it's yahoo or the gov, since i do use the combo on a few sites im responsible for...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles