Skip to main content.
^^

WD NOAA HTML PHP Parsing Script

The following code snippet, shows how I took the default WD generated NOAA daily report and parsed it into a formatted table. I then intergraded it into my own web pages using my own style sheet settings.

The code below is provided AS-IS and requires that you have PHP on your website, and that you are uploading from your WD workstation the dailynoaareport.htm file to a known location (you need to modify the path in the script to find it).

Download Script Text File of the same below

<?php
/******************************************************************************
* WD NOAA HTML PHP Parsing Script
* Copyright (C) 1997-2006 TNET Services, Inc.
* http://www.tnetweather.com/notebooks.php
* Email: programs@tnet.com
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*******************************************************************************/

/******************************************************************************
* Info about this script
* 
* PLEASE READ ALL COMMENTS IN THIS FILE BEFORE ASKING ANY QUESTIONS
*
* This is a snippet of code that can be used inside another php page or
* called via include from a php page.  PHP is REQUIRED to use this script.
*
* What is does is read the a WD formatted NOAA Daily report file and
* output most of its information is a tabular formatted format which you
* can control with changes to the CSS settings.
*
* You must change the following variable to point to where your WD
* dailynoaareport.htm file is located on your server.  IF this scrip is 
* located and called from the same directory it is in you can leave the current
* setting alone
*******************************************************************************/

$FILETOPROCESS = "dailynoaareport.htm";

/******************************************************************************
*
* Report Settings:
* 
* $TEMP = Temp Values
* $RAIN = Rain Values
* $WIND = Wind Values
*
* Change to match your reports settings
*
*******************************************************************************/

$TEMP = "°F";
$RAIN = "Inches";
$WIND = "MPH";

/******************************************************************************
* Style Sheet Settings
* 
* The following CSS style settings are ONLY provided to allow this script to
* show some formatting.  Use of style settings in a script as it is, is not
* valid code.  Style settings should be nested inside of the <head> section of
* your real pages, or included in an external CSS file which is included 
* as part of your page.
*
* Colors a bit gaudy... done on purpose to make you choose your own.
*
* The script uses the following Tags:
*
*   h1          Main Title
*   h2          Secondary Title
*   h3          Third Title
*   NoaaHead    Header Table (th)
*   NoaaOdd     Odd Row Table
*   NoaaEven    Even Row Table
*
*******************************************************************************/
?>
<style type="text/css" media="screen" >

    body {
            color: #660066;
            background-color: #FFF666;
            font-family: Comic Sans MS, verdana, helvetica, arial, sans-serif;
            font-size: 78%;  /* Enables font size scaling in MSIE */
            margin: 0;
            padding: 0;
    }

    html > body {
            font-size: 9.5pt;
    }

    
    table   {
            width:80%;
            border: px solid #0043CF;
            border-collapse: collapse;
            }

    h1      {
            font-size: 140%;
            font-weight: bold;
            margin: 0.25em 0 0 0;
            padding: 0;
            }

    h2      {
            font-size: 120%;        
            font-weight: bold;
            margin: 0.25em 0 0 0;
            padding: 0;
            }

    h3      {
            font-size: 100%;        
            font-weight: bold;
            margin: 0.25em 0 0 0;
            padding: 0;
            }

    .NoaaHead   {
            font-size: 10px;
            color:#000000;
            font-family: Courier New, sans-serif;
            text-align: center;
            border:1px solid #FFFFFF;
            background-color:#FF33FF;
            text-decoration: none;
            line-height: 1;
            padding: 3px;
            width:160px;
            vertical-align: bottom;
    }
    
    .NoaaEven   { 
            font-size: 12px; 
            color: #000000;
            font-family: Courier New, sans-serif;
            text-align: center;
            border:1px solid #FF00FF;
            background-color:#FF99FF; 
            text-decoration: none;
            padding: 5px; 
            width:160px;
            vertical-align: top;
    }
    
    .NoaaOdd    { 
            font-size: 12px; 
            color:#000000;
            font-family: Courier New, sans-serif;
            text-align: center;

    border:1px solid #FF00FF;
            background-color:#FFFFFF; 
            text-decoration: none;
            padding: 5px; 
            width:160px;
            vertical-align: top;
    }
</style>

<?
/******************************************************************************
*
* END OF MODIFIABLE CODE
*
* You should not need to change anything further unless you want to make
* code changes on your own.
*
*******************************************************************************/

$DATE="";
$NAME="";
$LOCATION="";

// Returns whether needle was found in haystack
function is_substr($haystack, $needle){
       $pos = strpos($haystack, $needle);

       if ($pos === false) {
               return false;
       } else {
               return true;
       }
}

// Used to display info in proper cell
function dost($instring, $switch){
    if ($switch) {
        echo "<td class='NoaaOdd'>${instring}</td>";
    } else {
        echo "<td class='NoaaEven'>${instring}</td>";
    }
}

// Use to display title info cells
function dohd($instring) {
    echo "<th class='NoaaHead'>${instring}</th>";
}


// Start of main routine
$NOAAFILE = fopen("$FILETOPROCESS", "r");
if (!$NOAAFILE) {
    echo "No File Found [$FILETOPROCESS]<br/>\n";
    exit;
}

$START= 0;
$ODD = 0;

while (!feof($NOAAFILE)) {
    $buffer = rtrim( fgets($NOAAFILE, 4096) );

    $PROC_DONE = 0;

    // Found Main Heading so we can get Date and Time values
    if ( is_substr($buffer, "MONTHLY CLIMATOLOGICAL SUMMARY") ) {
        $values = preg_split ("/\s+/", $buffer);
        $DATE = "MONTHLY CLIMATOLOGICAL SUMMARY<br/>for $values[5] $values[6]";
        $PROC_DONE = 1;
    }

    // Found Location information
    if ( is_substr( $buffer, "NAME: ")) {
        $string = preg_replace("/NAME: /","", $buffer);
        $string = preg_replace("/ CITY: /","- ",$string);
        $string = preg_replace("/ STATE: /",", ", $string);
        $NAME = $string;
        $PROC_DONE = 1;
    }

    // Found Elevation and compass settings
    if ( is_substr( $buffer, "ELEV: ")) {
        $LOCATION = $buffer;
        $PROC_DONE = 1;

        // Start of Formatted output
        echo '<center>';
        echo "<h1>${DATE}</h1> <h2>${NAME}</h2> <h3>${LOCATION}</h3><br/>";
        echo "Note: Temp's are listed in ${TEMP}, Rain in ${RAIN}, ";
        echo "and Wind Speed in ${WIND}<br/><br/>";
        echo "<table><tr>\n";
        dohd("DAY");
        dohd("MEAN TEMP");
        dohd("HIGH");
        dohd("TIME");
        dohd("LOW");
        dohd("TIME");
        dohd("HEAT DEG DAYS");
        dohd("COOL DEG DAYS");
        dohd("RAIN");
        dohd("AVG WIND SPEED");
        dohd("HIGH");
        dohd("TIME");
        dohd("DOM DIR");
        echo "</tr>\n";

        $START = 1;
        next;
    }

    // Found line of dashes before real data table.  Turn on parsing
    if (! $PROC_DONE ) {
        if ( $START == 1 && is_substr($buffer, '------------------------') ) {
            $START = 2;
            $PROC_DONE = 1;
        }
    }

    // Found last line of dashed after data table, Turn parsing off
    if ( ! $PROC_DONE ) {
        if ( $START == 2 && is_substr($buffer, '------------------------') ) {
            $START = 3;
            $PROC_DONE = 1;
        }
    }

    // IF Parsing is on (START=2) and not a line of dashes, this must be a valid row
    if ( ! $PROC_DONE) {

        if ( $START == 2 && !is_substr($buffer, "--------") ){
            $value = preg_split ("/\s+/", $buffer);
            $val2 = $value[1];

            // Check for null data.  Line with date, but no data. Turn off parsing
            if (strlen ($val2) == 0 ) {
                $START = 3;
                $PROC_DONE = 1;
            } else {

                if ($ODD != 1) {
                    $ODD = 1;
                } else {
                    $ODD = 0;
                }

                echo "<tr>";
                dost($value[0],$ODD);
                dost($value[1],$ODD);
                dost($value[2],$ODD);
                dost($value[3],$ODD);
                dost($value[4],$ODD);
                dost($value[5],$ODD);
                dost($value[6],$ODD);
                dost($value[7],$ODD);
                dost($value[8],$ODD);
                dost($value[9],$ODD);
                dost($value[10],$ODD);
                dost($value[11],$ODD);
                dost($value[12],$ODD);
                echo "</tr>\n";
            }
        $PROC_DONE = 1;
        }
    }
}
fclose($NOAAFILE);

echo "</table></center>";

/**********************************************************************
* End of Script
***********************************************************************/

  Goto Top Of Page
Web Design by TNET   © 1992-2010 Copyright TNET Services, Inc.
Home | Privacy Statement | Site Map | About Us - TNETWebGen v2.6.1
Some images and designs (SM) TNET Services, Inc.
SWN Member