Leuven-script colour trend pages/reports using your WU data

Examples

Demo scriptWhat does this script
ExplanationFirst an image copy with all parts visible and some explanations
Stand-aloneAn example running stand-alone
SaratogaAnd an example where the script is used in the Saratoga template
LeuvenAnd here the script is used in the Leuven Template

How it works


Some history

For those website owners of which their weather-program uploads the NOAA reports there are trend scripts written by Murry Conarroe of Wildwood Weather. These scripts are very easy to integrate in a template, so I tested that for some users of my Leuven-Template. Of course other template users than also wanted those scripts although their weather-program are not able to generate these NOAA text files.
So I wrote a set of similar "looking" scripts but using the most used data storage for private weather-websites, the WeatherUnderground data files.

How does it work

There are other scripts using the WeatherUnderground multi-year data storage to retrieve the weather-stations data and to generate graphs or reports. They all work in a similar way

  1. Get the data from the WU data storage using the weatherstations ID
  2. Store the "historical" data in a cache-type folder on the webserver
  3. Filter the data for the needed timeframe
  4. Display the data in the way the users/visitors of the website expects

1. Get the data

The weather-stations ID (example mine is IVLAAMSG47) and the first year of uploading is needed for this. That information is specified in the first lines of the wsreports scripts.
The information is loaded from the WU website in the same way as one can do manually on the stations page of the weather station. Since long there has been a button or link to download the data in a "CSV" file.

http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IVLAAMSG47&month=01&day=01&year=2014&format=1&graphspan=year

The first few lines of the file returned looks like

Date,TemperatureHighC,TemperatureAvgC,TemperatureLowC,DewpointHighC,DewpointAvgC,DewpointLowC,HumidityHigh,HumidityAvg,HumidityLow,PressureMaxhPa,PressureMinhPa,WindSpeedMaxKMH,WindSpeedAvgKMH,GustSpeedMaxKMH,PrecipitationSumCM
2014-1-1,11,8,6,8,6,4,92,86,76,1009,995,13,2,29,0.13
2014-1-2,11,9,7,8,7,6,92,86,79,1004,993,21,3,32,0.13

So the first line contains the fieldnames, the other lines contain the information for one day.
Attention:

  1. The fieldnames also contain the UOM (unit of measurement) for that field.
    • The Temperature field in this example is in C=Celcius
    • All UOM's are either in metric or in english units in the same file
    • Retrieving the same file again (needed for this years data only) does not guarantee that the same UOM's will be used as last time
    • Retrieving multiple years does not guarantee that the same UOM's will be used in every year
    • The units used are not necessarily the ones the user wants to use on the website
  2. There are numerous fields available, but not as much as in the NOAA style reports.
  3. Not all days of the year have to be available in the CSV. Days can be missing because of upload problems for example. Or the station was of-line for a few days.

2. Store the "historical" data

The load on the WU servers is tremendous so sometimes the "waiting" time can be quite long. The current scripts have a wait time of 60 seconds and that has been sufficient even in heavy load situations.

Normally the first run of the scripts give some wait time before the data can be shown but it is most of the times less than 10 seconds.

All data gathered from the WU servers is stored for later re-use. All data from previous years will therefor only loaded once from the "slow" WU storage and later on always loaded from the faster webserver disks.

That is also the case for this years data but that CSV-file has to be retrieved from the WU-site once or twice per day. The first time the script is executed on a day and an optional second time after 14:00 (2:00pm) to get yesterdays data which comes only available during the day.

3. Select only the needed data

The script can display the data in different ways

  • Detailed = one day per entry, one "year" per page
  • Summary = one year per row, all years on a page

The period can either be

    text
  • a calender year from the first of January to the last day of December
  • a seasonal "year" going form the first day of winter to the last day of autumn

The seasons are defined different for the Northern and Southern hemisphere. North of the equator the meteorological winter starts at the first day of December. South of the equator that is the first day of june.

Based on the selection from the drop-down boxes done by the visitor the CSV data is read and the needed data selected. This is based on (weather value) for the needed period (year) and for a detailed or summary page.

4. Display the data

The data is processed, optional the data is converted when the UOM of the input-data is different of the needed UOM

Remarks

Although the Leuven report scripts are similar to the Murry Conarroe of Wildwood Weather scripts, the PHP code is totally different. But I used similar CSS names so that less CSS customizations should be needed.


All reports are created based on the cached data. So if you change your historical data at the WU site please delete the appropriate year file in wsreports/WUdata folder. The scripts will retrieve that missing years file in the next run.


Install


  1. Download from the link HERE
  2. Unzip the downloaded file
  3. Upload (FTP) the wsreports folder to the main folder on your website.
    For Saratoga and Stand-alone users, this is normally the root
    Leuven-template users place the folder in weather2/
  4. Set the permissions for the wsreports/wuData/ folder and its contents at 777
    otherwise an error message: [function.file-put-contents]: failed to open stream: Permission denied will occur
    The downloaded CSV files are stored in that folder.
  5. Test the installation with www.website.xx/ wsreports/startReports.php
    Please replace www.website.xx/ with the name of your website
    This test uses the data and settings for my own weather-station.
  6. Check and adapt the settings as described in the next tab on this page
These files and folders can be found in the download after unzip:
nameWhere is it used for
folders
wuDataThe retrieved CSV files for every year your weather-station is uploading to WU are stored here.
wuLangThe language translate files are store here. If you see an error in a text, please change the appropriate line here. Do not change the texts in the scripts as this can break the scripts.
There is a separate file for each language
If you want to add your own language there is a small readme inside the lang folder.
Start Scripts
startReports.phpscriptTo use the reports script:
  1. without any changes to test the installation first
  2. after adapting to your settings, to run the scripts "stand-alone"
startLeuven.phpscriptTo use the reports script from within the Leuven-template
wxStartreports.phpscriptTo use the reports script from within the Saratoga-template
Other Scripts/files
_read-me.txttexta summary of all this information
wsreports.cssCSSSeparate CSS script for formatting everything
wsReports1part.phpscript First general part for the reports scripts. All definitions are contained in this script.
wsReportsDaily.phpscript To generate the daily reports
wsReportsSum.phpscript To generate the summary reports
wsReportsFunctions.phpscript The functions used by other scripts are stored here

Settings


Not all documentation is already available in English.

The settings are separatly described for the different implementations of these scripts.


Stand-alone - include


  • You find those settings at the start of the scripts.
    # ---------------HERE YOU NEED TO MAKE SOME CHANGES ----------------------
    

  • These are the two most important settings. An error in these settings will prohibit the scripts from displaying your data.
    The date of your first upload to WeatherUnderground. Also the format this date has to be specified is important, dd= day - mm= month - YYYY= year in four digits
    $wustart 	= '31-01-2011';		// your first day of uploading data to WU.    Important the format is:   dd-mm-YYYY   !
    

    Your station ID for WeatherUnderground. Please test it also by going to your stations page at WU.
    $wuid		= 'IVLAAMSG47';		// your WU station name
    

  • The latitude setting is only used to set the correct seasons. They differ in the northern en Southern parts od our world.
    $latitude	= '50';			// for the seasons: a positive number for north of the equator, or a negative number for south
    

  • The UOM's are specified now. Please set a # character (=comment character) for the UOM you do not want to use. And make sure that you selected at least one UOM for every weather-value.
    #				temp values: ='°C', ='°F'
    $uomTemp 	= '°C';		
    #$uomTemp 	= '°F';
    #				baro values: =' hPa'   ' mb'   ' inHg'
    $uomBaro 	= ' hPa';
    #$uomBaro 	= ' mb';
    #$uomBaro 	= ' inHg';
    #				windspeed values: ' km/h'  ' kts'  ' m/s'  ' mph'
    $uomWind 	= ' km/h';
    #$uomWind 	= ' kts';
    #$uomWind 	= ' m/s';
    #$uomWind 	= ' mph';
    #				rain values:  ' mm'  ' in'
    $uomRain	= ' mm';
    #$uomRain	= ' in';
    #				wind run values: ' km'  ' mi'
    $uomWrun	= ' km';
    #$uomWrun	= ' mi';
    

  • These are the more difficult settings
    #		Be carefull changing the following settings
    

  • If running this script stand-alone either directly from the browser with or without a menu system or in an iFrame, you leave this setting as is. The enclosing html is than generated and also the needed CSS file is specified correctly.
    If you want to use your own html code before and adter the wsreports output, set this setting to false.
    $includeHTML	= true; 		//  are loaded, if set to false include the CSS also
    
    But you have than put a reference to the CSS file in your <head> </head > section of your page:
    <link rel="stylesheet" href="./wsreports/wsreports.css" type="text/css">
    

  • Default language can be set to either
    • 'en' for English - as it is set now
    • 'fr' for French
    • 'de' for German
    • 'nl' for Dutch
    • 'es' for spanish
    $lang		= 'en';			// your language. If no language file is found 'en' is used
    

  • If you are running the startReports.php from the same folder as the other reports scripts, leave this setting as is. You only have to modify this if you have made a copy of this script for integrating in another script/page.
    So if you execute this "mystartReports.php" script by using an PHP include
            include "wsreports/mystartReports.php";
    
    you have to modify the $wsreportsDir setting accordingly to
    $wsreportsDir	= './wsreports/';	// folder where the report scripts are located, leave as is if you are running this script from the same folder
    

  • The information in the wsreports script is in UTF-8. When running the startReports.php in an iFrame or directly from the browser this setting can be left as is.
    Only when integrating the script inside your own page by use of an include you have to change this setting to reflect the character set of the enclosing page.
    $charset	= 'UTF-8';		//  'ISO-8859-1'  'UTF-8'
    

  • # --------------- END OF SETTINGS ----------------------------------------
    

Saratoga


  • You find those settings at the start of the wxStartReports.php script.
    # ---------------HERE YOU NEED TO MAKE SOME CHANGES ----------------------
    

  • These are the two most important settings. An error in these settings will prohibit the scripts from displaying your data.
    The date of your first upload to WeatherUnderground. Also the format this date has to be specified is important, dd= day - mm= month - YYYY= year in four digits
    $wustart 	= '31-01-2011';		// your first day of uploading data to WU.    Important the format is:   dd-mm-YYYY   !
    

    Your station ID for WeatherUnderground. Please test it also by going to your stations page at WU.
    $wuid		= 'IVLAAMSG47';		// your WU station name
    

  • The information in the wsreports script is in UTF-8. You have to change this setting to reflect the character set of the other Saratoga-template pages..
    $charset	= 'ISO-8859-1';		//      = 'ISO-8859-1';  ='UTF-8';
    

  • If you
    • installed the wsreports scripts in the default folder
    • and left the name as wsreports
    • and run the wxStartReports script from the mian saratoga folder as all other saratoga scripts
    leave this setting as is. Otherwise adapt to your situation.
    $wsreportsDir	= './wsreports/';	// only change this if you stored the wsreports scripts in another folder
    

  • The EXACT name of the script is needed to call the correct script after the user made a selection from the selection boxes on top of the page and pushed the "go' button.
    $mypage		= 'wxStartReports.php'; // only change this if you renamed this script
    

    # ---------------END OF USER CHANGES - LEAVE REST OF SCRIPT AS IS ------
    

All other settings, such as language and UOM's are copied from your Saratoga $SITE settings as specified in your Settings.php script.

Leuven


  • You find those settings at the start of the startLeuven.php script.
    # ------ HERE YOU NEED TO CHECK AND OR TO MAKE SOME CHANGES --------------------
    

  • These are the two most important settings. This setting is normally already set in your wsSettings.php file
    If you encounter strange problems when retrieving the WeatherUnderground data check the setting in wsSettings.php first.
    The date of your first upload to WeatherUnderground has to be set correctly. The format of this date is important, dd= day - mm= month - YYYY= year in four digits
    $wustart 	= $SITE['wuStart'];     //      '01-01-2011';  your first day of uploading data to WU.    
    

    Your station ID for WeatherUnderground.
    Also normally correctly set at your wsSettings.php file.
    $wuid		= $SITE['wuId'];	//      your WU station name
    

  • If you
    • installed the wsreports scripts in the default folder
    • and left the name as wsreports
    • and run the startLeuven script from the main template folder as all other Leuven scripts
    leave this setting as is. Otherwise adapt to your situation.
    $wsreportsDir	= './wsreports/';	//      only change this if you stored the wsreports scripts in another folder
    

  • # --------------- END OF USER SETTINGS -----------------------------------------
    

All other settings, such as language and UOM's are copied from your $SITE settings as specified in your wsSettings.php script.