Author Topic: Code snippet- making ET useful  (Read 2177 times)

0 Members and 1 Guest are viewing this topic.

Online SLOweather

  • Administrator
  • Forecaster
  • *****
  • Posts: 3403
Code snippet- making ET useful
« on: October 28, 2007, 09:34:16 PM »
This little PHP scrap calculates a 7 day running total of Evapotranspiration (ET) and rainfall. It's utility is in actually accumulating enough ET data to be useful in making irrigation decisions. It doesn't use a database, but does need to write a file back to the server containing updated values.

There's no magic in the 7 day number. This could easily be edited to make the time period as long or short as desired. But 7 days seems right to me for figuring out how much to run my sprinklers.

Disclaimer: This is not ready to directly implement as written. I wrote this as a test/demo before adding it to my master fire weather index script. It should be run one per day just after midnight. I'm posting this in case someone thinks the idea or routines are useful to them.

Also, it requires a station or program that outputs a daily ET value. For a Davis station, this means that it must have a Solar Radiation sensor.

To use it with the stickertags.txt file, yesterday's high ET value from VWS is added to the .htx stickertags that VWS produces and uploads. I think my version also includes yesterday's low dew point and yesterday's high and low total rain so yesterday rain can be calced.

When properly implemented, it uses 2 files, the modified stickertags file, and a separate data file that stores the once-daily calcs. The astute observer will note that there are a few values in this script that are stored and rewritten to the data file that aren't used in this script. Those are calculated in the master fire index script, and work when this scrap is merged back into it.

Once you start this, it takes 7 days for the script to accumulate enough data to be accurate, although you can seed the original index data file with estimated data.

Mainly, I was originally interested in the weekly ET value, but for funnsies, I added a moving 7 day rain value, as well, and a calc that subtracts 7 day rain from 7 day ET to show what the weekly rain deficit is.

Now that I think about it, that's the beginnings of the Keetch-Byram Drought index. The KBDI, though, adds in a couple of other terms (local average rainfall and a limit of 8" maximum rain deficit, I think).

Since I wrote this and integrated this into my master fire weather index script, I've gotten HomeSeer (my home automation software) to import the data. I think that weekly rain deficit calc is going to come in handy. While I haven't programmed an entire irrigation system, (I still use an off-the-shelf controller for daily operation) I have a Homeseer controlled relay that can, on command, keep the sprinklers from running. Between yesterday's rain, and the last 7 days' rain deficit, I should be able to write a script that will decide "weather" or not the sprinklers should run. 

Per my style of programming, there are lots of print statements for checking and troubleshooting. They can be deleted.


// Test script for moving 7 day ET total
// get the previous day's fire index data file

$index_data_file_path  = '';

$indexdataraw = file_get_contents($index_data_file_path);

//  we need put the previous index data into an array
$indexdata = explode(",", $indexdataraw);
     // clean up and define the data points
     (float)$kbold = trim($indexdata[0]);
     (float)$nesterovold = trim($indexdata[1]);
     (float)$loopcounter = trim($indexdata[2]);
     (float)$ettotal = trim($indexdata[3]);
     (float)$et7day = trim($indexdata[4]);
     (float)$etday1 = trim($indexdata[5]);
     (float)$etday2 = trim($indexdata[6]);
     (float)$etday3 = trim($indexdata[7]);
     (float)$etday4 = trim($indexdata[8]);
     (float)$etday5 = trim($indexdata[9]);
     (float)$etday6 = trim($indexdata[10]);
     (float)$etday7 = trim($indexdata[11]);
     (float)$rain7day = trim($indexdata[12]);
     (float)$rainday1 = trim($indexdata[13]);
     (float)$rainday2 = trim($indexdata[14]);
     (float)$rainday3 = trim($indexdata[15]);
     (float)$rainday4 = trim($indexdata[16]);
     (float)$rainday5 = trim($indexdata[17]);
     (float)$rainday6 = trim($indexdata[18]);
     (float)$rainday7 = trim($indexdata[19]);

print "old 7 Day ET = "; print "$et7day <br>";

// These two lines are data for the test script and need to be deleted when this is pasted into the main firecron script.
$ethighyesterday = 0.17;
$rainyesterday = 0.15;

// Adjust the last 7 day's ET values and calc the new 7 day ET value.

$etday7 = $etday6;
$etday6 = $etday5;
$etday5 = $etday4;
$etday4 = $etday3;
$etday3 = $etday2;
$etday2 = $etday1;
$etday1 = $ethighyesterday;
$et7day = $etday1 + $etday2 + $etday3 + $etday4 + $etday5 + $etday6 +  $etday7;

// Adjust the last 7 days' rain values and calc the new 7 day rain value.

$rainday7 = $rainday6;
$rainday6 = $rainday5;
$rainday5 = $rainday4;
$rainday4 = $rainday3;
$rainday3 = $rainday2;
$rainday2 = $rainday1;
$rainday1 = $rainyesterday;
$rain7day = $rainday1 + $rainday2 + $rainday3 + $rainday4 + $rainday5 + $rainday6 +  $rainday7;

// Weekly rain rain deficit

$raindeficit = $et7day - $rain7day;

// Test print statements to see how it works.
print "ET ";
print "Day1 = "; print "$etday1 <br>";
print "Day2 = "; print "$etday2 <br>";
print "Day3 = "; print "$etday3 <br>";
print "Day4 = "; print "$etday4 <br>";
print "Day5 = "; print "$etday5 <br>";
print "Day6 = "; print "$etday6 <br>";
print "Day7 = "; print "$etday7 <br>";
print "Hi ET yest?= "; print "$ethighyesterday <br>";
print "7 Day ET = "; print "$et7day <br>";
print "Rain";
print "Day1 = "; print "$rainday1 <br>";
print "Day2 = "; print "$rainday2 <br>";
print "Day3 = "; print "$rainday3 <br>";
print "Day4 = "; print "$rainday4 <br>";
print "Day5 = "; print "$rainday5 <br>";
print "Day6 = "; print "$rainday6 <br>";
print "Day7 = "; print "$rainday7 <br>";
print "Hi ET yest?= "; print "$rainyesterday <br>";
print "7 Day rain = "; print "$rain7day <br>";

print "Rain Deficit = "; print "$raindeficit <br>";

//Implode the data back into a CSV file...

$newarray = array($kbnew, $nesterovnew, "$loopcounter", "$ettotal", "$et7day","$etday1", "$etday2", "$etday3", "$etday4", "$etday5", "$etday6", "$etday7", "$rain7day","$rainday1", "$rainday2", "$rainday3", "$rainday4", "$rainday5", "$rainday6", "$rainday7","$raindeficit");
$indexcsv = implode(",",$newarray);

// Here's where we rewrite the index data file...
// Create or open the file
 $filename = "dailyindextest.txt";
 $fh = fopen("dailyindextest.txt", 'w+') or die("can't open file");
 fwrite ($fh,$indexcsv);