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…

Installed Programmes

Start_ButtonSome time ago I bought a new desktop computer for work (mainly) and spent a while setting it up just the way I like it.  My laptop is set up pretty much the same way, some software is on both computers and some is on only one or the other.

This is an aide-mémoire for me as and when I may need to reinstall or setup a new machine, and maybe useful for you in finding a piece of software that does something useful for you.

For what it’s worth, I’m a civil engineer/project manager by day with interests in geography/mapping, DIY, fitness, computing and listening to funky music when I’m off-duty.

I’ll discuss the hardware in another post on another day, this is for software only. Continue reading “Installed Programmes”

Entry into the blogosphere

After a few months working on Joomla and starting up a few websites I thought it was time to broaden my horizons and try WordPress.

Rather than rolling it out on a commercial site and having to ride the learning curve whilst keeping a client happy I have decided to use it for my own blog.  When we get connected at home the other family members may join the fray.

I have set it up on my own webspace which is hosted at Brinkster on a Windows server with php and MySQL (more on that in another post, another day).

WordPress was easy to upload and configure, and I have already added a Google AdSense plugin, Easy AdSense, and the Atahualpa theme which has a ridiculous number of options – so you can expect this blog to do some shapeshifting in the coming days, weeks and months.

I also added my Google Analytics tracking code to the site so I can monitor comings and goings on the site.