Author Topic: Enclosed code change to auto-scale thermometer graphic - thermometer.php  (Read 5523 times)

0 Members and 1 Guest are viewing this topic.

Offline pablo

  • Member
  • *
  • Posts: 32
Howdy,

Where I live (QC, Canada), we have throughout the year, some extreme temperatures.  It can get close to -40C and close to 40C.  The `thermometer.php' script requires one to hard code the maximum and minimum temperatures of the graphic.  When I entered the extremes, the graphic didn't look all that great.   8-)

I made a slight change to the .php code so the maximum and minimum thermometer graphic auto-scale based on the current maximum and minimum.  Empirically, I dialed in the amount needed for the font size of the maximum/minimum labels.  For example, if the current maximum is 19, the thermometer's maximum will be 30.  On the other hand, if the current maximum is 15, the thermometer's maximum will be 20.

We also handle the case where the day's maximum is less than the minimum.   #-o

If you'd like to see the thermometer in action, you can go to my web site http://weather.hillsandlakes.com.  As an aside, for those interested, I plan on releasing the RRD code for the graphs I'm generating ... if people are interested, I can post an announcement here, otherwise I'll only be posting the announcement on the WD forum - no sense in cluttering this board if there's no interest.

Here's the code modification to `thermometer.php' ... see new modification below ...
« Last Edit: April 20, 2009, 09:32:41 PM by pablo »

Offline mackbig

  • Forecaster
  • *****
  • Posts: 4128
    • Mackie's Main Street, Unionville, ON Canada Weather
Looks cool pablo.  Will check it out this evening.  And thanks for the reminder, I just checked my site, and noticed I still have my winter hard code. +10 to -20 for my thermometre.   Since it was 20 on Saturday, guess its time to change.  But back to winter/spring today / tomorrow them warming up again at end of the week back into the 20's

I am sure some might be interested in your updates, and since us VWS users (that still use the thermometer.php ) dont visit the WD forums.....

Also, the mod's will probably take care of this, but this thread should probably in the "Web Weather > Weather Website PHP/AJAX scripting" area....

Andrew

Andrew - Davis VP2+ 6163, serial weatherlink, wireless anemometer, running Weather Display.  Boltek PCI Stormtracker, Astrogenic Nexstorm, Strikestar - UNI, CWOP CW8618, GrLevel3, (Station 2 OS WMR968, VWS 13.01p09), Windows 7-64

Offline JOE

  • Forecaster
  • *****
  • Posts: 390
    • Centralia IL Weather
I'm not smart enough to make that work,  :oops: but thanks for the reminder for me to manually change a couple of mine. 8-)
Joe
www.centraliaweather.com
DW1376
KILCENTR2
K9RJN


Offline pablo

  • Member
  • *
  • Posts: 32
Looks cool pablo.  Will check it out this evening. 
Thank you for the kind words.

And thanks for the reminder, I just checked my site, and noticed I still have my winter hard code. +10 to -20 for my thermometre.   Since it was 20 on Saturday, guess its time to change.  But back to winter/spring today / tomorrow them warming up again at end of the week back into the 20's
Well, that's it.  I'm lazy.  I don't want to have to do that .. initially, I thought I could set up a cron job which would change the values periodically but I thought auto-scaling (ala RRD) might be a better way to go.  Well, better for me.  :)

Also, the mod's will probably take care of this, but this thread should probably in the "Web Weather > Weather Website PHP/AJAX scripting" area....

Ooops!  I'm an idiot.  I saw it as a "suggestion" so I thought I'd post it and post how I did it.  Given I know how to code, no sense in suggesting and _not_ providing a solution.  :)

Offline pablo

  • Member
  • *
  • Posts: 32
I'm not smart enough to make that work

Hi Joe,

Nah, it's nothing to do with smarts ... I suspect there are a million things you can do which I cannot.  ;)  I just happened to be a geek.  :p

If you'd like to roll this change in your code, do the following.  Make a backup of "thermometer.php"  Next, look at the source I posted.  Keep it handy.

Using your favorite editor, edit "thermometer.php" and do a string search for any code you see in black.  Next, cut and paste the red code in your file.  Save it and refresh your page.  See if it works.  If not, you have a back up.  ;)
« Last Edit: April 20, 2009, 04:05:29 PM by pablo »

Offline andro700

  • Chuck
  • Forecaster
  • *****
  • Posts: 420
    • Gobles Weather Page
Nice little code you got the pablo. Here is my link.

http://www.chucksweather.com/index.php

Chuck

Offline pablo

  • Member
  • *
  • Posts: 32
Hi Chuck,

Nice site.  Did you plop the code change?  I didn't confirm how it'd look when the scale is Fahrenheit.  It seems the original code would normalize it so perhaps it'll look okay.  If not, I can't tweak it.  :)

Offline andro700

  • Chuck
  • Forecaster
  • *****
  • Posts: 420
    • Gobles Weather Page
Yes I did change my code and left it the way it was. When I changed the $TMax to +6 it rounded it up 1 degree Fahrenheit. It was 69 it rounded it to 70 but when I have it set at +7 as you can see it rounds it up to 80.

$Tmax = round(($maxtemp + 7) / 10) * 10;
 $Tmin = round(($mintemp - 7) / 10) * 10;

Chuck

Offline mackbig

  • Forecaster
  • *****
  • Posts: 4128
    • Mackie's Main Street, Unionville, ON Canada Weather
Just added to mine. works great.  For VWS users you need to add the code to the next section past where Pablo showed, pasted below again, with red section showing difference.

Thanks Pablo, great mod.
Andrew

if ($wxSoftware == 'VWS') { // Get the VWS Weather Flash data files
 
      $filename = "${wflashDir}wflash.txt";
      $file = file($filename);
      $file = implode('',$file);
      $data = explode(",",$file);;
 
     $curtemp = FtoC($data[9],1);
    
      $filename = "${wflashDir}wflash2.txt";
      $file = file($filename);
      $file = implode('',$file);
      $data = explode(",",$file);;
    
     $mintemp = FtoC($data[92],1);
     $maxtemp = FtoC($data[36],1);

// pablo - auto-scale the thermometer.  The font of the degree
     // value needs seven degrees of space.  Also, the legend doesn't
     // appear properly unless the start/end are tens.  With that, we
     // massage our thermometer's max/min.
     $Tmax = round(($maxtemp + 7) / 10) * 10;
     $Tmin = round(($mintemp - 7) / 10) * 10;

     // pablo - if the maximum is less than the minimum, switch-a-roo
     if ($Tmax < $Tmin) {
        $xyzzy = $Tmax;
        $Tmax = $Tmin;
        $Tmin = $zyzzy; // what we just saved ... rogue anyone?
     }

   }

Andrew - Davis VP2+ 6163, serial weatherlink, wireless anemometer, running Weather Display.  Boltek PCI Stormtracker, Astrogenic Nexstorm, Strikestar - UNI, CWOP CW8618, GrLevel3, (Station 2 OS WMR968, VWS 13.01p09), Windows 7-64

Offline pablo

  • Member
  • *
  • Posts: 32
Yes I did change my code and left it the way it was.

Hi Chuck,

Thank you for letting me know and testing it out.

Offline pablo

  • Member
  • *
  • Posts: 32
Just added to mine. works great. 

Super!

For VWS users you need to add the code to the next section past where Pablo showed, pasted below again, with red section showing difference.

Ah yes, thank you for rolling the change in that section.  Now that I know that it works similar for VWS users (whomever they are!  :), I'll post a refactored version shortly.  This way we don't have duplicate code.

Thanks Pablo, great mod.

You're welcome and I appreciate the kind words Andrew.

Cheers!
-pablo

Offline pablo

  • Member
  • *
  • Posts: 32
Howdy,

As Andrew showed how to add the code to the VWS section, I've simplified the modification.  Below is the new code with no vanity in the comments ;)

For those folks who made the original changes, you can see how I've relocated the code.  This means there's a lot of pre-context before the changes in red


     $curtemp = CtoF($data[4],1);
     $mintemp = CtoF($data[47],1);
     $maxtemp = CtoF($data[46],1);
  } // end Weather Display data
 
if ($wxSoftware == 'VWS') { // Get the VWS Weather Flash data files
 
      $filename = "${wflashDir}wflash.txt";
      $file = file($filename);
      $file = implode('',$file);
      $data = explode(",",$file);;
 
     $curtemp = FtoC($data[9],1);
    
      $filename = "${wflashDir}wflash2.txt";
      $file = file($filename);
      $file = implode('',$file);
      $data = explode(",",$file);;
    
     $mintemp = FtoC($data[92],1);
     $maxtemp = FtoC($data[36],1);
   }

// Add auto-scaling to your thermometer.
//
// Emperically, we've found we need to add seven more degrees to our
// value to ensure we have room for the minimum/maximum font.
// Also, the grid lines don't show up nicely unless we end on an
// even multiple of ten.
//
$Tmax = round(($maxtemp + 7) / 10) * 10;
$Tmin = round(($mintemp - 7) / 10) * 10;

// This shouldn't happen but in case some future modification
// records the minimum a different way, we'll be ready.
if ($Tmax < $Tmin) {
   $xyzzy = $Tmax;
   $Tmax = $Tmin;
   $Tmin = $zyzzy; // what we just saved ... rogue anyone?
}
// End:  add auto-scaling to your thermometer.

if (isset($_REQUEST['current'])) { $curtemp = $_REQUEST['current']; } // for testing
if (isset($_REQUEST['min'])) { $mintemp = $_REQUEST['min']; } // for testing
if (isset($_REQUEST['max'])) { $maxtemp = $_REQUEST['max']; } // for testing

« Last Edit: April 20, 2009, 10:14:58 PM by pablo »

Offline Mark / Ohio

  • Live from Mars!
  • Forecaster
  • *****
  • Posts: 2511
    • Fairfield County Weather
Thanks Pablo!   8-)

I just made the modification and it looks good!
Mark 
2002 Davis VP I Wireless, WeatherLink (Serial), GRLevel3, VirtualVP, StartWatch, Weather Display, Windows 10


Offline pablo

  • Member
  • *
  • Posts: 32
You're welcome.

Last night I was working late and saw my thermometer roll across midnight.  And whaddya know, it worked!  :)  In my case, the upper range was 10C and the bottom was set to 0C.  I also looked at Chuck's thermometer (it's in Fahrenheit) and it worked too.  As is said, even a blind squirrel finds a nut.

Offline WeatherBeacon

  • Chief
  • Forecaster
  • *****
  • Posts: 1369
    • http://www.wxbeacon.com

For those of you who liked the bottom and top minor ticks on 5's (as Ken had it) instead of 10's , you can do:

     $Tmax = round(($maxtemp + 7) / 10) * 10 + 5;
     $Tmin = round(($mintemp - 7) / 10) * 10 - 5;


In this case you might want to change the range (scale) further by changing the 7's to, say 5's or 4's, for example. (Play around with it till you get it the way you want it.)

Nice work, Pablo!
Mae govannen!
Kevin  (Member AMS) http://www.wxbeacon.com               Genesee County, Michigan
Hardware:  Davis Vantage Pro Wireless, Midland WR-300
Software: VWS 14.01p43, WeatherFlash, & GRLevel3

Offline pablo

  • Member
  • *
  • Posts: 32
Howdy,

Should we make the change more parameter driven to account for differences in ticks?

(thank you for the kind words!)

Offline Mark / Ohio

  • Live from Mars!
  • Forecaster
  • *****
  • Posts: 2511
    • Fairfield County Weather
Howdy,

Should we make the change more parameter driven to account for differences in ticks?

(thank you for the kind words!)

How do you mean?
Mark 
2002 Davis VP I Wireless, WeatherLink (Serial), GRLevel3, VirtualVP, StartWatch, Weather Display, Windows 10


Offline pablo

  • Member
  • *
  • Posts: 32
Howdy,

Ideally, it's better to use variables in the code and allow people to edit the variables.  Similar to what was previously done to set the high and low thermometer settings.

 

anything