Xtype extension : aggregate type with parameter

122 views
Skip to first unread message

jterr...@gmail.com

unread,
Oct 9, 2022, 8:54:19 AM10/9/22
to weewx-development

Hello,

I am writing a Xtype extension providing  a new aggregate type that calculates a rolling average of a parameter over years.  

I started by defining several different aggregate type for different number of years, such as :

rolling_avg_years1

rolling_avg_years5

rolling_avg_years10

rolling_avg_years15

For exemple :  $day.outTemp.rolling_avg_years10 returns the average temperature of the last 3650 days (i.e. 10 years).

And it works.

But, to allow more flexibility,  I would prefer, if possible,  to have the number of years as parameter, such as 

rolling_avg_years(10)

but I don't know how I could retrieve the parameter in the get_aggregate function of my extension.

Any help is appreciated,
Thanks

Tom Keffer

unread,
Oct 9, 2022, 9:08:22 AM10/9/22
to weewx-development
You're on the right track. If you pass in a parameter to an aggregation, it appears in "option_dict" in the corresponding get_aggregate() call. If you don't name that parameter, it shows up as key "val". So, in your example, 

$day.outTemp.rolling_avg_years(10)

would result in a call to get_aggregate() that looks like:

get_aggregate('outTemp', timespan, 'rolling_avg_years', db_manager, val=10)

The value 'val' will contain the appropriate number of years to do the rolling average. 

Hope that helps.

-tk

--
You received this message because you are subscribed to the Google Groups "weewx-development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-developm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-development/c17fbd97-4547-4a36-bfc0-35b10aa3b68bn%40googlegroups.com.

Jacques Terrettaz

unread,
Oct 9, 2022, 9:48:47 AM10/9/22
to Tom Keffer, weewx-development
Thanks Tom, 

I have modified my extension, and when the aggregate type is used within a tag such as $day.outTemp.rolling_avg_years(10) it works.

But it is not working with the ImageGenerator.
In my extension, I am checking that the aggregate type is "rolling_avg_year", and if not it will raise a weewx.UnknownAggregationexception

When using the aggregate type without parameter, with this ImageGenerator definition :

 [[other_images]]
        x_label_format = %Y
        bottom_label_format = %x %X
        time_length = 220752000 # 7 years
        aggregate_interval = day
        show_daynight = false
        [[[trol10]]]
          [[[[rol10]]]]
            label = "Température : moyenne glissante sur 10 ans"
            aggregate_type = rolling_avg_years10
            data_type = outTemp

The image is generated :

But when using the modified extension accepting a parameter :

 [[other_images]]
        x_label_format = %Y
        bottom_label_format = %x %X
        time_length = 220752000 # 7 years
        aggregate_interval = day
        show_daynight = false
        [[[trol10]]]
          [[[[rol10]]]]
            label = "Température : moyenne glissante sur 10 ans"
            aggregate_type = rolling_avg_years(10)
            data_type = outTemp


I have the following error :

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine: Caught unrecoverable exception in generator 'weewx.imagegenerator.ImageGenerator'
2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****  'outTemp' or 'rolling_avg_years(10)'
2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****  Traceback (most recent call last):

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****    File "/usr/share/weewx/weewx/reportengine.py", line 197, in run

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****      obj.start()

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****    File "/usr/share/weewx/weewx/reportengine.py", line 385, in start

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****      self.run()

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****    File "/usr/share/weewx/weewx/imagegenerator.py", line 42, in run

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****      self.gen_images(self.gen_ts)

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****    File "/usr/share/weewx/weewx/imagegenerator.py", line 109, in gen_images

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****      self.image_dict[timespan][plotname])

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****    File "/usr/share/weewx/weewx/imagegenerator.py", line 249, in gen_plot

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****      **option_dict)

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****    File "/usr/share/weewx/weewx/xtypes.py", line 116, in get_series

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****      raise weewx.UnknownType(msg)

2022-10-09 15:30:24  weewx[22095] ERROR weewx.reportengine:         ****  weewx.UnknownType: 'outTemp' or 'rolling_avg_years(10)'


When called by the ImageGenerator, it appears that  the aggregate type parameter of the get_aggregate function is the full value "rolling_avg_years(10)" and not the value " "rolling_avg_years". So in that case it is considered as an unknown aggregation because it is not "rolling_avg_years" and rejected by my extension.

Is there a special syntax to be used when defining an aggregate type with a parameter for the ImageGenerator ?




You received this message because you are subscribed to a topic in the Google Groups "weewx-development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/weewx-development/RyO1T4UFFBk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to weewx-developm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-development/CAPq0zECGbOo6rT7mNdRFvRQAq6TNmWKZO6NZ7evDA6mvUz925g%40mail.gmail.com.

Tom Keffer

unread,
Oct 9, 2022, 11:01:05 AM10/9/22
to Jacques Terrettaz, weewx-development
Try:

 [[other_images]]
        x_label_format = %Y
        bottom_label_format = %x %X
        time_length = 220752000 # 7 years
        aggregate_interval = day
        show_daynight = false
        [[[trol10]]]
          [[[[rol10]]]]
            label = "Température : moyenne glissante sur 10 ans"
            aggregate_type = rolling_avg_years
            val = 10
            data_type = outTemp

Although, to tell you the truth, I've never tried this!

-tk

Jacques Terrettaz

unread,
Oct 9, 2022, 11:20:47 AM10/9/22
to Tom Keffer, weewx-development
:(. it is not working. In that case, in my get_aggregate function,  option_dict.get('val') is None 



<trol10_OK.png>

Tom Keffer

unread,
Oct 9, 2022, 9:47:58 PM10/9/22
to weewx-development
It seems that there is a bug in get_series(). It should pass the option dictionary on to get_aggregate(), but it doesn't. 

Try this version of xtypes.py.

Also, note that the value will be passed in as a string. To get a numeric value, you'll have to do

years = int(option_dict['val'])

-tk


xtypes.py

Jacques Terrettaz

unread,
Oct 10, 2022, 2:26:49 AM10/10/22
to Tom Keffer, weewx-development
Thanks Tom,

It works now with the new version of xtypes.py and the following image definition :

[[other_images]]
        x_label_format = %Y
        bottom_label_format = %x %X
        time_length = 220752000 # 7 years
        aggregate_interval = day
        show_daynight = false
        [[[trol10]]]
          [[[[rol10]]]]
            label = "Température : moyenne glissante sur 10 ans"
            aggregate_type = rolling_avg_years
            val = 10
            data_type = outTemp
Reply all
Reply to author
Forward
0 new messages