Author Topic: Some help possibly? Records for MY airport, finally can retrieve them.  (Read 3289 times)

0 Members and 1 Guest are viewing this topic.

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
This has been a pain in the butt for me since the beginning of my site.

Using Weather Display and the Weather Underground almanac. Unfortunately, my airport
is not listed for the "local" record high, low or the average high low. Mine on WU is KRME (Rome)
and the records and normals come from Syracuse (KSYR) which is about 50 miles away
and a world of difference as far as those temps.

Using the Weather Underground api and getting a api key from them,
I'm able to get the KRME records for this date.

http://api.wunderground.com/api/{mykeygoeshere}/almanac/q/NY/Rome.json

It returns:

HTTP/1.1 200 OK
X-CreationTime:
0.065
Date:
Fri, 04 May 2012 11:59:52 GMT
Content-Length:
470
Expires:
Fri, 01 Jan 1990 00:00:00 GMT
Set-Cookie:
DT=1336132792:8265:365-v3; path=/; expires=Fri, 01-Jan-2020 00:00:00 GMT; domain=.wunderground.com
Content-Type:
application/json; charset=UTF-8
Connection:
close
Server:
Apache/1.3.33 (Unix) PHP/4.4.0
Pragma:
no-cache
Cache-Control:
no-cache, must-revalidate, no-cache="Set-Cookie", private

{
  "response":  {
    "version": "0.1",
    "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
    "features":  {
      "almanac": 1
    }
  },
  "almanac":  {
    "airport_code": "KRME",
    "temp_high":  {
      "normal":  {
        "F": "63",
        "C": "17"
      },
      "record":  {
        "F": "77",
        "C": "25"
      },
      "recordyear": "1981"
    },
    "temp_low":  {
      "normal":  {
        "F": "35",
        "C": "1"
      },
      "record":  {
        "F": "29",
        "C": "-1"
      },
      "recordyear": "1985"
    }
  }
}

So, the $64,000 question is, is it possible, and how can I parse a JSON file
so that I can display it on my site within my dashboard like I used to?
Took me a while to figure out that I can actually get this data, and the API
they provide is actually really good (if you know what you're doing  :-P )

Here's the link to that too: http://www.wunderground.com/weather/api/d/documentation.html

Thanks!
Tony




Offline jgillett

  • Forecaster
  • *****
  • Posts: 1187
  • Boltek, Win7 Pro, ToA
    • TiggrWeather Phoenix
Hey Tony,

In Settings-weather.php you can set $SITE['conditionsMETAR'] = 'KDVT'; to the METAR of your choice. I think this will do what you want, but best to check with Ken.

Also see http://saratoga-weather.org/wxtemplates/find-metar.php for a nifty script to get METAR info in your area.

HTH (I may be having another one of my 'senior moments' here...).
John
W7JKG

Online saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9296
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
That WU API is really nifty stuff .. the only downside is they force you to register your app as a developer in order to get a 'key' to use, and then it's usage is limited (for the free developer key).

I'd done some playing around with the WU API, so you might be able to use some code like this to put the JSON return into a PHP array and then process it.  It does need PHP5 (and have the built-in json_decode() function available)
Code: [Select]
$html = file_get_contents("http://api.wunderground.com/api/{mykeygoeshere}/almanac/q/NY/Rome.json");

$JSON = json_decode($html,true); // get as associative array

print_r($JSON,true);   // print the array for debugging


The variables can be accessed like

$recordHigh = $JSON['almanac']['temp_high']['record']['F"'];
$recordHighYear = $JSON['almanac']['temp_high']['recordyear'];
$recordLow = $JSON['almanac']['temp_low']['record']['F"'];
$recordLowYear = $JSON['almanac']['temp_low']['recordyear'];

etc...

Hope this helps...

Best regards,
Ken

BTW, because they don't offer a generic key (one that works for anyone), I'd decided not to finish up a WU-forecast version that used the API :(
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
Cool Ken. I've got a key for another project already.

I will see how this goes. THANK YOU ONCE AGAIN!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

John, the records and normals shown on my dashboard are what I'm looking for.
The ones in this screenshot are not from my airport (Rome) but from Syracuse.
It's just the nature of the beast and what they make available on their pages for Brian
to scrape with Weather Display.

Thanks!
Tony


EDIT: Worked like a charm Ken. Just had to remove the " after the F for the temps.
Only issue i'll have is every time the page gets viewed, it'll be another hit to the API, right?


You can see the difference between Syracuse (right) and Rome (on the left)
Thanks again!!
« Last Edit: May 04, 2012, 07:52:08 PM by CNYWeather »
Tony




Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
I've been scouring the net looking for ways to cache this file so there arent as many calls
on my API key. Can someone point me in the right direction?
Tony




Online saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9296
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Thought you might ask...

Code: [Select]
<?php
// Settings:
$APIkey '<your-api-key-here>';

$WU_URL 'http://api.wunderground.com/api/'.$APIkey.'/almanac/q/NY/Rome.json';

$cacheFileDir './cache/';                     // default cache file directory
$cacheName "WU-almanac-Rome.txt";           // locally cached page from WU
$refetchSeconds 1800;                    // cache lifetime (1800sec = 30 minutes)

// end of settings
// overrides from Settings.php if available
global $SITE;
if(isset(
$SITE['cacheFileDir']))     {$cacheFileDir $SITE['cacheFileDir']; }
// end of overrides from Settings.php
//
// -------------------begin code ------------------------------------------
$Status "<!-- begin WU-record via API -->\n";
$Force false;
if (isset(
$_REQUEST['force']) and  $_REQUEST['force']=="1" ) {
  
$Force true;
}

$doDebug false;
if (isset(
$_REQUEST['debug']) and strtolower($_REQUEST['debug'])=='y' ) {
  
$doDebug true;
}

$fileName $WU_URL;
if (
$doDebug) {
  
$Status .= "<!-- WU URL: $fileName -->\n";
}

$cacheName $cacheFileDir $cacheName;

// The number 1800 below is the number of seconds the cache will be used instead of pulling a new file
// 1800 = 60s x 30m so it retreives every 30 minutes. 

if (! $Force and file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds time()) {
      
$html implode(''file($cacheName)); 
      
$Status .= "<!-- loading from $cacheName (" strlen($html) . " bytes) -->\n"
  } else { 
      
$Status .= "<!-- loading from $fileName. -->\n"
      
$html fetchUrlWithoutHangingWUR($fileName,true); 
      
$fp fopen($cacheName"w"); 
  if (!$fp) { 
    $Status .= "<!-- unable to open $cacheName for writing. -->\n"
  } else {
        
$write fputs($fp$html); 
        
fclose($fp);  
$Status .= "<!-- saved cache to $cacheName ("strlen($html) . " bytes) -->\n";
  } 


  if(
$doDebug) { print $Status; }

  
$JSON json_decode($html,true); // get as associative array
  
$recordHigh $JSON['almanac']['temp_high']['record']['F'];
  
$recordHighYear $JSON['almanac']['temp_high']['recordyear'];
  
$recordLow $JSON['almanac']['temp_low']['record']['F'];
  
$recordLowYear $JSON['almanac']['temp_low']['recordyear'];

// put rest of your processing here

// Functions --------------------------------------------------------------------------------

function WURmicrotime_float()
{
   list(
$usec$sec) = explode(" "microtime());
   return ((float)
$usec + (float)$sec);
}
// ------------------------------------------------------------------

// get contents from one URL and return as string 
 
function fetchUrlWithoutHangingWUR($url,$useFopen) {

  global 
$Status$needCookie,$timeStamp,$TOTALtime;
  
$overall_start time();
 
//   print "<!-- using file function -->\n";
   
$T_start WURmicrotime_float();

   
$xml implode('',file($url));
   
$T_close WURmicrotime_float();
   
$ms_total sprintf("%01.3f",round($T_close $T_start,3)); 
   
$Status .= "<!-- file() stats: total=$ms_total secs -->\n";
   
$TOTALtime+= ($T_close $T_start);
   
$overall_end time();
   
$overall_elapsed =   $overall_end $overall_start;
   
$Status .= "<!-- fetch function elapsed= $overall_elapsed secs. -->\n"
   return(
$xml);

   }    
// end fetchUrlWithoutHangingWUR
// ------------------------------------------------------------------
?>

Note: since it used file('http://.....') to access the API, you'll need to have a php.ini with

allow_url_fopen = on;

enabled.

Best regards,
Ken
« Last Edit: May 06, 2012, 08:45:11 PM by saratogaWX »
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
Thank you Ken.

http://www.cnyweather.com/cache/WU-almanac-Rome.txt

So you probably can guess what my next question will be then, right?

How do I get the variables like $normalHigh cached text file into the right format to display within my page.

Would I do a php include of WU-almanac?



« Last Edit: May 07, 2012, 08:59:41 AM by CNYWeather »
Tony




Online saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9296
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Hi Tony,

You just do an include("WU-almanac-script.php"); on the page you want it to be used,

then print $recordHigh; (or any of the other variables you've done like

  $recordHigh = $JSON['almanac']['temp_high']['record']['F'];
  $recordHighYear = $JSON['almanac']['temp_high']['recordyear'];
  $recordLow = $JSON['almanac']['temp_low']['record']['F'];
  $recordLowYear = $JSON['almanac']['temp_low']['recordyear'];

in the script to display them on the including page.  You don't have to mess around with the cached JSON text file .. the script will ensure it's kept 'fresh', and it's the script that loads it, puts it into the $JSON array, then simple statements like those above put the values into ordinary PHP variables for easy use/printing.

You could also just use the $JSON array directly (like the right-side of each assignment statement above).

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
Thank you very much sir.

Curly has been egging me along help me figure it out on my own lol  :-P

I was confused with including the WU-Almanac.php script and that it
would keep the cache fresh. I was thinking i'd have to get the data out of the cache text file.

I can make a hell of a plaque, but when it comes to php, I tend to fill the scrap
box up with junk kinda like I do at work from time to time.


THANKS!!  :grin:

Tony
Tony




Online saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9296
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Hi Tony,

The thing to keep in mind is how/when is PHP code executed.

When the script is loaded (either as a .php page, or via include() or require() ), the PHP code in the page is parsed, and when the parsing is done (for the original and any included() pages), the parsed code is executed in a top-down manner until all the code is run.  Then the resulting page is returned to the browser.

So, by including() the script, you cause it to run, and any variables in the top-level of the code are available for printing. That would include anything in the $JSON array, or the $record... variables from $JSON assignments.

Variables within a function are not available to the top-level code, unless specifically listed in global statements.

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
I've seen some code within the ajax-dashboard, some state

include or include_once

Is there a difference?
Tony




Online saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9296
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Yes, a big difference.

include() causes the file to be read-in and parsed.

include_once() causes the file to be read-in and parsed only if it has not already been processed.

A similar thing is with require() and require_once().

I use include_once() throughtout the template sets so that standalone scripts can run equally well inside the template or by themselves.

Using include()/require() means you have to be much more careful coding, as a second include()/require() may cause a fatal PHP error from trying to redefine currently defined function names (from the first include()/require).  include_once()/require_once() avoids that error by skipping processing of the second (and subsequent) include_once()/require_once() calls).
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline CNYWeather

  • Forecaster
  • *****
  • Posts: 2297
    • CNYWeather
I'm all set now. Thanks Ken & Curly.

Not being able to display my area's normals and records has bugged me for a long long time!
Glad I finally can display them without jumping through a lot of hoops.

Tony  8-)
Tony




Offline Gordon

  • Senior Member
  • **
  • Posts: 79
Re: Some help possibly? Records for MY airport, finally can retrieve them.
« Reply #13 on: August 03, 2012, 01:42:21 PM »
The trailing zero on the WeatherDisplay WU Record & Normal tags has always bugged me. Using the Almanac feature of the WU API, along with Ken's script above, solved it.

Thanks for your help with getting this up and running Tony...as well as your supplied script Ken.

http://www.oldlineweather.com
« Last Edit: August 03, 2012, 01:53:18 PM by gclarkso »
Gordon

<iframe src='https://www.weatherlink.com/embeddablePage/show/3512bdb6075043fba2e0fbf66996d8f0/signature' width='760' height='200' frameborder='0'></iframe>