Things you can do with Meteoplug - HTML Templates with PHP

Discussion about generation of graphs and tables
Post Reply
ironedge
Posts: 56
Joined: Tue Sep 23, 2014 9:32 am
Contact:

Things you can do with Meteoplug - HTML Templates with PHP

Post by ironedge » Fri Sep 04, 2015 9:44 am

Some time ago I showed a way how to calculate the sunshine duration with a Davis VP2 with Solar sensor and meteohub / meteoplug in the meteohub Forum (in German, sorry):

http://forum.meteohub.de/viewtopic.php? ... 947#p15952

While I really like all the graphing you can do with Meteoplug, I also wanted to show statistics about some other interesting weather parameters that are not delivered by the Meteoplug server by default. E.g. I would like to track values like "heat days" (Tmax>=30), "summer days" (Tmax>=25), etc. for a given period, or things like "the day it rained most last year / since ever", etc.

In addition, I wanted to compare values of the current year with the previous year and the all-time average/extreme values.

The only way to start programming your own statistics is via the HTML templates (http://wiki.meteoplug.com/Define_Chart# ... e_Settings). There you need to dynamically generate all the required template variables ("[actual_xxx_yyy_zzz]") of all values required to do the calculations. The Meteoplug server then replaces the variables with the corresponding values. Once you have them, you can start calculating.

The challenge is that you cannot just create a PHP script doing both the template variable definition and the calculation. This simply because obviously the PHP script runs through before the Meteoplug Server replaces the variables with the corresponding values. Means, your PHP variables at runtime still hold the variable string ("[actual...]") and not the value. My solution was to create a PHP script to generate all the required template variables and to output a Javascript which then uses the variable values to do all the calculations (at the runtime of the Javascript, the Meteoplug Server already has replaced the variables with the corresponding values).

The result is my new http://wetter.isenegger.org/langzeit-tabellen/ page, feel free to check it out.

The Meteoplug Server does not provide an API or any effective way to retrieve multiple database values at once, unfortunately. So the only way is to generate hundreds or even thousands of template variables that get replaced. It looks like the Meteoplug Server has a great and efficient process to handle the replacement because it only takes a fraction of a second to handle this large amount of template variables. Still, it may be beneficial if there would be a possibility to get arrays of data in one go (e.g. all monthly temperature average values over a definable timeframe) instead of creating a large number of single template variables for each month.

Bjarne
Posts: 3
Joined: Thu Feb 21, 2013 9:56 am

Re: Things you can do with Meteoplug - HTML Templates with PHP

Post by Bjarne » Tue Jan 05, 2016 10:22 am

I can only say "WOW" to your homepage. This is what I also would like to implement ... Is there a way to provide me more technical details how you set up your solution? Or could I get an example of your templates, php and javascript?

ironedge
Posts: 56
Joined: Tue Sep 23, 2014 9:32 am
Contact:

Re: Things you can do with Meteoplug - HTML Templates with PHP

Post by ironedge » Wed Jan 06, 2016 9:48 pm

Thanks for your feedback, much appreciated.
I hope for your understanding that I'm hesitant to just hand-out the code directly, there was a lot of work involved to develop it. In addition, it's very difficult to maintain such a home-made solution if you didn't develop it yourself. But I'm happy to provide some more details on the "longterm tables" ("Langzeit Tabellen") that may help you to write your own tailored solution.

Generally, with Meteoplug there's no way around generating the required template variables via PHP, so you can then use it for calculation with the JavaScipt you also output via PHP together with the template variables. The good news is that generating all required template variables can be done quite easily for large timescales by simple loops.

As mentioned earlier, you cannot use the defined template variables in PHP for calculation, because during the runtime of the PHP script, the template variables have not yet been replaced by the Meteoplug server with the corresponding database values. Therefore, the PHP script has to output Javascript code that will then use the template variable values for the required calculations and for display during the Javascript runtime in the client browser.

Generating the Template Variables - Examples for monthly values:

To get the average temperature of a month, you need to generate and output the following template variables:
• Current month (Jan 2016): [20160101000000_month1_th*_temp]
• For December 2015: [20151201000000_month1_th*_temp]
• For October 2014: [20141101000000_month1_th*_temp]
• etc.

These template variable strings have to be embedded into the Javascript code output, so their values can be used at Javascript runtime. Means, you need to define Javascript arrays that hold those template variables (and thus then also the database values once the Meteoplug server replaced the variable string with the value).

Example: Creating the Javascript array for "average temp", holding monthly values for each year:

Code: Select all

var tempavg=new Array(MAXYR);
for (i=0;i<(MAXYR);i++) {
    tempavg[i]=[];
}
If your database values start e.g. in 2012, then tempavg[0][1] would hold January 2012, tempavg[0][2] holds February 2012, tempavg[1][5] holds May 2013, etc.
Therefore, in the PHP script where you output the Javascript code, you need to have a loop to create the following output:

Code: Select all

tempavg[0][1]=“[20120101000000_month1_th*_temp]“;  // Jan 2012
tempavg[0][2]=“[20120201000000_month1_th*_temp]“; // Feb 2012
…
tempavg[1][1]= “[20130101000000_month1_th*_temp]“; // Jan 2013
etc.
The Meteoplug Template engine replaces the template variable string with the corresponding database value which will then be available during runtime of the Javascript. In the Javascript code example above, you will then finally see the following in the browser as the executed code: tempavg[0][1]=“-2.8“; (in case the average temp for Jan 2012 was -2.8, of course)

As mentioned before, this part of the Javascript code can be done easily in PHP loops which build the output string based on yearly / monthly loops, like:

Code: Select all

echo "tempavg[".$yearindex."] [".$monthindex."]=[ \"[".$year.$month."01000000_month1_th*_temp]\"];";
All this has to be done for each single value you want to represent / calculate in your table (tempmin, tempmax, tempavg, hummin, hummax, humavg, etc.). If you use functions properly, it can be handled with a reasonable amount of code even for a large number of weather variables.

Once you have all your values in the Javascript arrays, you can start calculating your own values beyond Meteoplug capability, i.e. besides the usual "avg temp", etc. also things like e.g. "# Summer Days per Month" (days with Tmax>25C) or "the day it rained most that month", etc.
And finally, once you have all the values you wanted, you output them in the way you want. I'm sure there are more fancy ways than my simple HTML table format and if you are a good web programmer, you can basically do anything you like with your weather values (and, this said, I'm no web programmer at all ... when I started with my project, I could neither program PHP nor Javascript but was "learning by doing" ... ok, I have some general background in programming which was definitely helpful nevertheless).

I hope this helps a bit for your own project.

Post Reply