Page 35 of 59

Re: Yet Another Dayfile Reader (PHP)

Posted: Wed 17 Jul 2019 10:22 pm
by Phil23
Hi All,

Really struggling to understand how the php code builds these tables.
My php & html knowledge is near zilch. (Other programming like PIC32 Basic Ok).

Trying to see where they yearly figures are generated so I can change some averages to totals.

Rainfall in particular, Id' like monthly totals not daily averages and probably do the same thing with EVT as it's sort of relevant to rain.
Capture.JPG
I sort of get that the yearly summaries are generated with the 879-1028 line mark in betel_readDayfile.php, but that's about as close as I can get.

Code: Select all

// START year avg table
    $tablelayout .= "                <table id='avgTable' style = 'display:".($show_year_averages ? "table" : "none").";'>\n";
    $tablelayout .= "<tr>\n";
    $tablelayout .= '<th style="width: 80px;">'.($dataSet == 'winddir' ? $translit[$dayfileLang]['dom'] : $translit[$dayfileLang]['avg']).'</th>';
    $tablelayout .= '<th>'.$translit[$dayfileLang]['mn'][0].'</th><th>'.$translit[$dayfileLang]['mn'][1].'</th><th>'.$translit[$dayfileLang]['mn'][2].'</th><th>'.$translit[$dayfileLang]['mn'][3].'</th><th>'.$translit[$dayfileLang]['mn'][4].'</th><th>'.$translit[$dayfileLang]['mn'][5].'</th><th>'.$translit[$dayfileLang]['mn'][6].'</th><th>'.$translit[$dayfileLang]['mn'][7].'</th><th>'.$translit[$dayfileLang]['mn'][8].'</th><th>'.$translit[$dayfileLang]['mn'][9].'</th><th>'.$translit[$dayfileLang]['mn'][10].'</th><th>'.$translit[$dayfileLang]['mn'][11].'</th>';
    $tablelayout .= "</tr>\n";

    $bert = count($type_year) -1;
    $alltime = array(1 => array(), 2 => array(), 3 => array(), 4 => array(), 5 => array(), 6 => array(), 7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array(), 12 => array());

    $bg_alltime = array(1 => array(), 2 => array(), 3 => array(), 4 => array(), 5 => array(), 6 => array(), 7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array(), 12 => array());

    if($dataSet == 'winddir') {
        for($zz = 1; $zz <= 12; $zz++){
            $alltime[$zz] = array( 0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0);
        }
    }
    $shortMon = array();
    for ($d = $bert; $d >= 0; $d--) { // latest year first
        $shortMon[$d] = array();
        $tablelayout .= '<tr>';
        $tablelayout .=  '<th>'.$type_year[$d].'</th>';

        for ($m = 1; $m <= 12; $m++) {
            $shortMon[$d][$m] = false;
            $windd = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0); // twitch fix
            $tablelayout .=  '<td';
            $thisValue = '';
            $part2 = '';

            if(isset($type_data[$type_year[$d]]) && array_key_exists($m, $type_data[$type_year[$d]])) {
                $turd = array();

                $bg_turd = array(); // hmm ... it's not going to be that simple !!!



               foreach($type_data[$type_year[$d]][$m] as $k => $v){

                    if($v <> ''){ // only take account of non null entries
                       if($dataSet == 'winddir'){
                           if($v[0] >0 && $v[1] >0) $windd[round($v[0] / 45)]++ ; // increment wind dir count
                           $v = '';
                        }

                       if(is_array($v)){ // we are dealing with something that needs calculating
                           $thisValue = $v;
                           formatOP(); // only want to use 'native' result
                           $thisValue = ''; // reset
                           $part2 = ''; // reset
                           $turd[] = $native;
                        } else { // 'normal' single piece of data
                           $turd[] = $v; // a 'normal' single data entry

                            if($dataSet == "HoursSun" && $run_sun) {
                                $bg_y = $type_year[$d];
                                $bg_when = mktime(0,0,0, $m, $k, $bg_y);
                                $bg_turd[] = maxsunhours($bg_when);
                            }

                        }
                    }
                }
                if($turd){
                    if($dataSet == 'winddir'){
                        $workMax = max($windd); // find highest occurrence
                        $found = false;
                        foreach($windd as $k => $v){
                            $alltime[$m][$k] += $v;
                            if($found !== true && $v == $workMax){ // see if number of occurrences = max
                                $thisValue = ($k == 0 ? 44 : $k * 45); // sort of put back into a degree range
                                $found = true; // only looking for first (clockwise) event
                            }
                        }
                    } else { // everything that really needs averages
                        $thisValue = array_sum($turd) / count($turd);
                        $thatvalue = "";
                        if($dataSet == 'HoursSun' && $run_sun){
                            $bg_value = round(array_sum($bg_turd) / count($bg_turd), 1);
                            $sunAvailable = $bg_value;
                        }
                    }
                }
                if($thisValue || $thisValue == 0){
                    if($dataSet != 'winddir') $alltime[$m][] = $thisValue;
                    if($dataSet == 'HoursSun' && $run_sun) $bg_alltime[$m][] = $bg_value; // ### NEW bargraph stuff
                    formatOP();
                    $short =  (count($turd) < cal_days_in_month(CAL_GREGORIAN, $m, $type_year[$d]) ? "* " : "");
                    if(count($turd) < cal_days_in_month(CAL_GREGORIAN, $m, $type_year[$d])) $shortMon[$d][$m] = true ;
                    $tablelayout .=  " class=\"datacell2\"".$part2.">".($dataSet != 'HoursSun' ? $short : '').$thisValue."</td>";
                }else {
                    $tablelayout .=  ' class="nondate">&nbsp;</td>';
                }
            }else{
                $tablelayout .=  ' class="nondate">&nbsp;</td>';
            }
        } // END month walk
        $tablelayout .= "</tr>\n";
    } // END year walk
// now do overall month averages
    $tablelayout .= '<tr>';
    $tablelayout .=  '<th class="topborder">'.($dataSet == 'winddir' ? $translit[$dayfileLang]['dom'] : $translit[$dayfileLang]['avg']).'</th>';
    for ($m = 1; $m <= 12; $m++) {
        $tablelayout .=  '<td';
        $thisValue = '';
        $part2 = '';

        if(isset($alltime[$m])) {
            if($dataSet == 'winddir'){
                $workMax = max($alltime[$m]);
                foreach($alltime[$m] as $k => $v){
                    if($v == $workMax){ // see if number of occurences = max
                        $thisValue = ($k == 0 ? 44 : $k * 45); // sort of put back into a degree range
                        break; // only looking for first (clockwise) event
                    }
                }
            } else {
                if(is_array($alltime[$m])) {
                    if(count($alltime[$m]) != 0) $thisValue = array_sum($alltime[$m]) / count($alltime[$m]);
                }
            }
            if($thisValue){

                if($dataSet == 'HoursSun' && $run_sun){
                    if(is_array($bg_alltime[$m])) {
                        if(count($bg_alltime[$m]) != 0) $bg_value = round(array_sum($bg_alltime[$m]) / count($bg_alltime[$m]), 1);
                        $sunAvailable = $bg_value;
                    }
                }

                formatOP();
                $short = "";
                for ($d = $bert; $d >= 0; $d--) { // latest year first (not important)
                    if($shortMon[$d][$m]){
                       $short = "* ";
                       break;
                    }
                }
                $tablelayout .=  " class=\"datacell2 topborder\"".$part2.">".($dataSet != 'HoursSun' ? $short : '').$thisValue."</td>";
            }else {
                $tablelayout .=  ' class="nondate topborder">&nbsp;</td>';
            }
        }else{
            $tablelayout .=  ' class="nondate topborder">&nbsp;</td>';
        }
    } // END month walk
    $tablelayout .= "</tr>\n";
    // END overall year averages
    $tablelayout .= "                </table>\n            </div>\n";
// END year averages table

Can anyone enlighten me a bit on the block of code where I can change these averages to totals?
And how I'd make it apply to only the Rainfall & EVT pages.


Thanks

Phil

Re: Yet Another Dayfile Reader (PHP)

Posted: Wed 17 Jul 2019 10:54 pm
by Mapantz
But then what would be the point in having a drop down to select a year? You can also view the monthly totals by looking at the seasonal data.

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 12:54 am
by beteljuice
No guarantees - the beteljuice is still a bit 'loopy' after being done over in the vets ...

I think I've got the vars right (original code is on my dead XP machine - awaiting new PC and organ donation)

Edit: No
i didn't - rain should be rainfall - fixed.

Edit: No
i didn't - $dataset should be $dataSet - fixed.

Edit:No, No No
$translit[$dayfileLang]['sum'] should be $translit[$dayfileLang]['total'] - fixed


Code has been done 'long-hand' with comments so you have an idea what's been done.

Code changed for rain / evt totals in year table with averages at bottom

Code: Select all


// START year avg table *** MODIFIED FOR RAIN TOTALS ***
    $tablelayout .= "                <table id='avgTable' style = 'display:".($show_year_averages ? "table" : "none").";'>\n";
    $tablelayout .= "<tr>\n";
//    $tablelayout .= '<th style="width: 80px;">'.($dataSet == 'winddir' ? $translit[$dayfileLang]['dom'] : $translit[$dayfileLang]['avg']).'</th>';

// *** NEW for rain / evt totals
if($dataSet == 'winddir') {
	$tablelayout .= '<th style="width: 80px;">'.$translit[$dayfileLang]['dom'].'</th>';
} else {
	if($dataSet == 'rainfall' || $dataSet == 'evt') {
	    $tablelayout .= '<th style="width: 80px;">'.$translit[$dayfileLang]['total'].'</th>';
	} else {
	    $tablelayout .= '<th style="width: 80px;">'.$translit[$dayfileLang]['avg'].'</th>';
	}
}	
//		

    $tablelayout .= '<th>'.$translit[$dayfileLang]['mn'][0].'</th><th>'.$translit[$dayfileLang]['mn'][1].'</th><th>'.$translit[$dayfileLang]['mn'][2].'</th><th>'.$translit[$dayfileLang]['mn'][3].'</th><th>'.$translit[$dayfileLang]['mn'][4].'</th><th>'.$translit[$dayfileLang]['mn'][5].'</th><th>'.$translit[$dayfileLang]['mn'][6].'</th><th>'.$translit[$dayfileLang]['mn'][7].'</th><th>'.$translit[$dayfileLang]['mn'][8].'</th><th>'.$translit[$dayfileLang]['mn'][9].'</th><th>'.$translit[$dayfileLang]['mn'][10].'</th><th>'.$translit[$dayfileLang]['mn'][11].'</th>';
    $tablelayout .= "</tr>\n";

    $bert = count($type_year) -1;
    $alltime = array(1 => array(), 2 => array(), 3 => array(), 4 => array(), 5 => array(), 6 => array(), 7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array(), 12 => array());

    $bg_alltime = array(1 => array(), 2 => array(), 3 => array(), 4 => array(), 5 => array(), 6 => array(), 7 => array(), 8 => array(), 9 => array(), 10 => array(), 11 => array(), 12 => array());

    if($dataSet == 'winddir') {
        for($zz = 1; $zz <= 12; $zz++){
            $alltime[$zz] = array( 0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0);
        }
    }
    $shortMon = array();
    for ($d = $bert; $d >= 0; $d--) { // latest year first
        $shortMon[$d] = array();
        $tablelayout .= '<tr>';
        $tablelayout .=  '<th>'.$type_year[$d].'</th>';

        for ($m = 1; $m <= 12; $m++) {
            $shortMon[$d][$m] = false;
            $windd = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0); // twitch fix
            $tablelayout .=  '<td';
            $thisValue = '';
            $part2 = '';

            if(isset($type_data[$type_year[$d]]) && array_key_exists($m, $type_data[$type_year[$d]])) {
                $turd = array();

                $bg_turd = array(); // hmm ... it's not going to be that simple !!!



               foreach($type_data[$type_year[$d]][$m] as $k => $v){

                    if($v <> ''){ // only take account of non null entries
                       if($dataSet == 'winddir'){
                           if($v[0] >0 && $v[1] >0) $windd[round($v[0] / 45)]++ ; // increment wind dir count
                           $v = '';
                        }

                       if(is_array($v)){ // we are dealing with something that needs calculating
                           $thisValue = $v;
                           formatOP(); // only want to use 'native' result
                           $thisValue = ''; // reset
                           $part2 = ''; // reset
                           $turd[] = $native;
                        } else { // 'normal' single piece of data
                           $turd[] = $v; // a 'normal' single data entry

                            if($dataSet == "HoursSun" && $run_sun) {
                                $bg_y = $type_year[$d];
                                $bg_when = mktime(0,0,0, $m, $k, $bg_y);
                                $bg_turd[] = maxsunhours($bg_when);
                            }

                        }
                    }
                }
                if($turd){
                    if($dataSet == 'winddir'){
                        $workMax = max($windd); // find highest occurrence
                        $found = false;
                        foreach($windd as $k => $v){
                            $alltime[$m][$k] += $v;
                            if($found !== true && $v == $workMax){ // see if number of occurrences = max
                                $thisValue = ($k == 0 ? 44 : $k * 45); // sort of put back into a degree range
                                $found = true; // only looking for first (clockwise) event
                            }
                        }
                    } else { // everything that really needs averages
					
// *** NEW FOR RAIN / EVT TOTALS **
if($dataSet == 'rainfall' || $dataSet == 'evt') {
	$thisValue = array_sum($turd);
} else {
	$thisValue = array_sum($turd) / count($turd);
}
//

//                        $thisValue = array_sum($turd) / count($turd);
                        $thatvalue = "";
                        if($dataSet == 'HoursSun' && $run_sun){
                            $bg_value = round(array_sum($bg_turd) / count($bg_turd), 1);
                            $sunAvailable = $bg_value;
                        }
                    }
                }
                if($thisValue || $thisValue == 0){
                    if($dataSet != 'winddir') $alltime[$m][] = $thisValue;
                    if($dataSet == 'HoursSun' && $run_sun) $bg_alltime[$m][] = $bg_value; // ### NEW bargraph stuff
                    formatOP();
                    $short =  (count($turd) < cal_days_in_month(CAL_GREGORIAN, $m, $type_year[$d]) ? "* " : "");
                    if(count($turd) < cal_days_in_month(CAL_GREGORIAN, $m, $type_year[$d])) $shortMon[$d][$m] = true ;
                    $tablelayout .=  " class=\"datacell2\"".$part2.">".($dataSet != 'HoursSun' ? $short : '').$thisValue."</td>";
                }else {
                    $tablelayout .=  ' class="nondate">&nbsp;</td>';
                }
            }else{
                $tablelayout .=  ' class="nondate">&nbsp;</td>';
            }
        } // END month walk
        $tablelayout .= "</tr>\n";
    } // END year walk
// now do overall month averages
    $tablelayout .= '<tr>';

    $tablelayout .=  '<th class="topborder">'.($dataSet == 'winddir' ? $translit[$dayfileLang]['dom'] : $translit[$dayfileLang]['avg']).'</th>';
    for ($m = 1; $m <= 12; $m++) {
        $tablelayout .=  '<td';
        $thisValue = '';
        $part2 = '';

        if(isset($alltime[$m])) {
            if($dataSet == 'winddir'){
                $workMax = max($alltime[$m]);
                foreach($alltime[$m] as $k => $v){
                    if($v == $workMax){ // see if number of occurences = max
                        $thisValue = ($k == 0 ? 44 : $k * 45); // sort of put back into a degree range
                        break; // only looking for first (clockwise) event
                    }
                }
            } else {
                if(is_array($alltime[$m])) {
	            if(count($alltime[$m]) != 0) $thisValue = array_sum($alltime[$m]) / count($alltime[$m]);
                }
            }
            if($thisValue){

                if($dataSet == 'HoursSun' && $run_sun){
                    if(is_array($bg_alltime[$m])) {
                        if(count($bg_alltime[$m]) != 0) $bg_value = round(array_sum($bg_alltime[$m]) / count($bg_alltime[$m]), 1);
                        $sunAvailable = $bg_value;
                    }
                }

                formatOP();
                $short = "";
                for ($d = $bert; $d >= 0; $d--) { // latest year first (not important)
                    if($shortMon[$d][$m]){
                       $short = "* ";
                       break;
                    }
                }
                $tablelayout .=  " class=\"datacell2 topborder\"".$part2.">".($dataSet != 'HoursSun' ? $short : '').$thisValue."</td>";
            }else {
                $tablelayout .=  ' class="nondate topborder">&nbsp;</td>';
            }
        }else{
            $tablelayout .=  ' class="nondate topborder">&nbsp;</td>';
        }
    } // END month walk
    $tablelayout .= "</tr>\n";
    // END overall year averages
    $tablelayout .= "                </table>\n            </div>\n";
// END year averages table

******************** MAKE SURE YOU BACK-UP YOUR CURRENT VERSION BEFORE MODDING ******************


Edit: Oops .... noticed a missing ) - fixed.

Edit: Oops ....rain should have been )rainfall - fixed.

{b]Edit:[/b] Ooops .... $dataset should be $dataSetl - fixed.

{b]Edit:[/b] Ooops .... $translit[$dayfileLang]['sum'] should be $translit[$dayfileLang]['total'] - fixed

Code changed for rain / evt totals in year table with averages at bottom

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 2:08 am
by Super-T
Gotta watch those Vets mate....might cut the wrong bit off :-)

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 2:28 am
by beteljuice
Worst of it is ..... It's in a place I can't see (but it feels like everyone else in the world has :shock:

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 9:09 am
by beteljuice
Oops .... noticed a missing ) - fixed. :oops:

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 9:49 am
by sutne
I tried your new code, corrected the missing ), but it still shows Average for the monthly summary of Rain (Nedbør):
http://www.rjoandveret.no/datasummary.php

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 8:54 pm
by Phil23
Mapantz wrote: Wed 17 Jul 2019 10:54 pm You can also view the monthly totals by looking at the seasonal data.
Yes, I'm aware of that, but my personal preference for rainfall would be to see monthly totals at the bottom, followed by an average of those.

Average daily rain is not really a meaningly figure to me simply because it's not a value that's readable each day.

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 9:56 pm
by beteljuice
Oops ....rain should have been rainfall - fixed. :groan:

Thank you Sutne

Re: Yet Another Dayfile Reader (PHP)

Posted: Thu 18 Jul 2019 10:06 pm
by beteljuice
@phil23 :bash:
Yes, I'm aware of that, but my personal preference for rainfall would be to see monthly totals at the bottom, followed by an average of those.
The modified code gives overall totals, so just remove the mods in the last section. :roll:

Re: Yet Another Dayfile Reader (PHP)

Posted: Fri 19 Jul 2019 8:40 am
by sutne
I changed rain to rainfall where I thought it should be, but still it does not give total, just average.
http://www.rjoandveret.no/datasummary.php

Re: Yet Another Dayfile Reader (PHP)

Posted: Fri 19 Jul 2019 12:43 pm
by beteljuice
Thank You once again Sutne ...

Told you I was a bit 'loopy' , that and my workhorse HDD not connected ATM = :bash: :bash:

Couple of places - $dataset should be $dataSet - fixed in code block above (I hope !)

Re: Yet Another Dayfile Reader (PHP)

Posted: Fri 19 Jul 2019 9:10 pm
by Phil23
Super-T wrote: Thu 18 Jul 2019 2:08 am Gotta watch those Vets mate....might cut the wrong bit off :-)
That bit would be you bum cheek & wallet?

Up to Vet visit number 3 for the past 2 weeks.

Re: Yet Another Dayfile Reader (PHP)

Posted: Fri 19 Jul 2019 9:20 pm
by Phil23
Thanks,

Not sure I'm game to try it yet, given it's still only 7am here in Aus & my brain has the morning fog.
Just browsing to see where that block of code belongs.

Re: Yet Another Dayfile Reader (PHP)

Posted: Sun 21 Jul 2019 8:39 pm
by Phil23
beteljuice wrote: Thu 18 Jul 2019 12:54 am ******************** MAKE SURE YOU BACK-UP YOUR CURRENT VERSION BEFORE MODDING ******************
Hmmm, been saying that to clients for 30 years. They prefer not to go thru the hassle;
Then request I "Fix It".

Rain totals now done & working just fine.

Have it running on two dayfiles,
my old Fine Offset station that ran from 2011 to 2016, - http://weather.inverellit.com/historic/datasummary.php
and my current live VP/2 data that's carried from 2016 forward - http://weather.inverellit.com/datasummary.php
If you wanted to handle EVT the same way, then ALL lines

if($dataSet == 'rainfall') { ...

become:

if($dataSet == 'rainfall' || $dataSet == 'evt') { ...
With regards to the above comment,

When you say "ALL lines",
do you mean All lines within the New code block,
or All lines within the entire code.

Cheers & thanks for the update.