Coding: NOAA Climate Reports and WordPress – php

After setting up a few pages to show graphic information from our weather station I found myself wondering there were some other things that can be done with the data, and whether I can make it easier to display the data on other websites I manage without having to copy vast tracts of text/code between sites.

National Oceanic and Atmospheric Administration Climatological Summary

One of the features of WeatherLink (WL) software is automatic output in a standard NOAA format, allowing it to be submitted and processed as part of international climate monitoring programmes. As well as providing for automatic submission to the Citizen Weather Observer Program (CWOP) WL can be set to upload the same data to an FTP site with the “climatological summary” per day and per month.

The problem is that WL doesn’t archive the data, it only keeps the previous month or year, so unless you are uploading to CWOP or making backups manually there is no long term record. Granted, WL archives the data in a very detailed format and can also upload detailed files with data at 30-minute intervals, but it’s not as easy to follow as the NOAA style.

Luckily, after some searching, I was able to find a script to automatically archive and rename the WL files according to month and year, and then create an index with links to include those files in the displayed page. The only problem for me was that the script is, as far as I can tell, intended for inclusion in Saratoga-Weather’s weather website templates. So, the task was to workout what needs changing in order to include the information on a WordPress (WP) page or post.

The changes (line references refer to the original (archived version) – copy and paste the content into Notepad or other text editor and work with modified and original side-by-side):

Two Notepad windows open side by side.
Two Notepad windows open side by side while editing php code.
  • Lines 43-53: Commented out the html header and page title as these are generated by WP.
  • Line 80: Because the weather information folder is in the webroot rather than the websites’ own sub-directory absolute folder paths are needed.
    • Set $NOAAdir = realpath($_SERVER["DOCUMENT_ROOT"]).'/WeatherLink/Reports';
    • /WeatherLink is the folder that NOAA-reports.php is in and /Reports is where the report files are saved – check your WL ftp settings.
    • Line 81-84: Add the $NOAAdir parameter in front of the WL standard filenames.
  • Line 86: I have not made any changes here because I am not using VWS.
  • Line 92: The original code uses GET-variables (“mo” and “yr”) to call the individual reports, and uses the php file-name (“$PHP_SELF = $_SERVER['PHP_SELF']") as the URI base when building the links (Line 171 onwards) . When this runs in WP the query-strings repeat and break the index links. After playing with a complicated method I found a simple one at stackoverflow and replace the line with $PHP_SELF = strtok($_SERVER['REQUEST_URI'], '?').
  • Lines 166, 167: Commented out the html footer and changed the error message to a paragraph tab rather than header.
  • Lines 257, 258: Commented out html footer closures as these are generated by WP.
  • The final code is here .

When you’re done editing then upload the php file to the WeatherLink folder on your server.

Running A php Script In WordPress

To execute php code within a WordPress page or post you have to use a plugin like Insert PHP Code Snippet. Once you have it installed and enabled then create a new snippet and enter:

$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/WeatherLink/NOAA-reports.php";

Where “/WeatherLink/” is the path to your NOAA file folder (see the mods to Line 80 above).

In the post or page where you want to display the NOAA data just click the “Insert PHP Code Snippet button” in the page/post formatting bar and select the snippet..

Preview the page to see if it works… if it doesn’t then you’ll have to go trouble-shooting: I found missing semi-colons, bad syntax and lots of other little gremlins that had to be exorcised before it would work.

I find the echo command to be useful in identifying problems, e.g. echo = "<p>$NOAAdir</p>"; on line 94 of my modified file should display the server path to the NOAA directory.

What Next?

  • Having awakened my Inner Coder with this exercise I knocked up some php pages to display all the WL generated charts (current, monthly and annual) – at the moment these are just static html content called up and included with a php snippet, but with a bit of effort I should be able to make them a bit more dynamic. I’ll share the code when it’s ready…
  • There’s a time-zone related function $ourTZ = 'PST8PDT'; on Line 12 of the original and I need to see whether this should relate to local time at the weather station (UTC-2) or the webserver (UTC+6, I think).
  • The more complicated part of the NOAA script is the way it archives and indexes the WL text files, Phase 2 is likely to see me working out a way to archive the WL charts (gif files) on a weekly, monthly and annual basis.
    • Or if somebody has a method to dynamically generate charts from tab delimited text files then I’m all ears, or automatically transfer the data into a mysql table and generating the charts from there…

Author: Michael

Parent, husband and civil engineer born and raised in Britain before emigrating to Botswana. Interests in construction, information technology, fitness, mechanics and mapping, among others.

2 thoughts on “Coding: NOAA Climate Reports and WordPress – php”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.