Weewx_METAR_Report

Including METAR reports in WeeWX

METAR_Detail

Introduction

I find METAR weather reports the most useful format to get the current meteorological situation in a few lines. Thanks to weeWx architecture, it is very easy to extend your meteo station capabilities to include and import new information. In this post you will see how to extend the Cheetah Generator in order to include METAR reports in weeWX for a list of airports. You can find the code of this extension in my Github account.

The WeeWX Service Engine

At a high level, weeWx architecture consists of an engine, called StdEngine, managing a set of services. A service consists of a Python class which binds its member functions to various events. The engine arranges to have the bound member function called when a specific event happens, such as a new LOOP packet arrives. If you want to know more about the services being run by default in a weeWx installation you may read this post.

In this topic I will create a new service, subclassing off the SearchList class. This class is a list of Python objects used by the Cheetah template engine. The Cheetah engine reads each template and when a tag starting with $ is found, it scans down the search list and tries to replace it by its value. For instance, if the tag $example is found, it will first try to use example as an attribute. This means that it will try to evaluate obj.example. If that raises an AttributeError exception, then it will try example as a key. It is obj[example]. If that raises a KeyError exception, then it moves on to the next item on the list. The first match that does not raise an exception is then used. If no match is found, Cheetah raises a NameMapper.NotFound exception.

Our aim is to create an extension of the search list that encodes the functionality of obtaining the last available METAR report for the airports configured.

Extending the Search List

First of all create a new Python class by extending the Search List. Then place the file in the executables folder (BIN_ROOT). In my installation it is located in /usr/share/weewx/user/. In the overrided initialization method (init) it reads the configuration parameters contained in the subsection MetarReport of the Skin where the extension is used. I have defined two parameters. The first one is the refresh_interval. It regulates how often the METAR report should be updated. The second one will be commented in next section. It is a list of the airports for which the METAR report will be generated. The airport should be included according to the ICAO airport code.

Then override the method get_extension_list. This function should return a list of objects whose attributes or keys will be searched by the Cheetah Engine. It will iterate over the airports defined in the MetarReport subsection. For each airport, it will try to obtain the last available report. If this function fails, it will read the METAR backup if it exists.

Obtaining the METAR Report

The METAR reports are obtained from Aviation Weather Center. This is a service of the National Weather Service of the USA. I modified the url https://www.aviationweather.gov/adds/metars?station_ids=OACI_CODE+&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submitmet=Get+Weather to request the most recent report translated for an specific airport. It is a free service and its reliability is not 100%. Due to this reason I decided to save the METAR obtained in a backup file. By doing this, if the METAR HTTP request fails, the backup file can therefore be processed and included in my web. The METAR reports are issued actually every 30 minutes.

In order to run this extension, you should include it in the Cheetah Generator. To do so, open the skin.conf file of the skin where the METAR information will be used. Edit the CheetahGenerator section to include your new search list.

Finally, define a new section called MetarReport to include the two parameters required by the extension. These are the refresh interval and the OACI code of the airports.

To print the METAR report in a weeWX report just include the tag $OACI_CODE + _metar wherever you want to show it.

Remember that the report should be generated by the skin where the search list extension has been included. Otherwise the tag will not be found. For instance:

4 thoughts on “Including METAR reports in WeeWX

  1. Thanks very much for this, would love to include on my website.
    I’ve installed it on my pi2b running Rasbian – uname -a > Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l GNU/Linux

    However I am getting the following error when running, perhaps you might have a pointer to fix? :

    May 4 16:10:22 raspberrypi weewx[399]: cheetahgenerator: Generate failed with exception ”
    May 4 16:10:22 raspberrypi weewx[399]: cheetahgenerator: **** Ignoring template /etc/weewx/skins/niculskin/index.html.tmpl
    May 4 16:10:22 raspberrypi weewx[399]: cheetahgenerator: **** Reason: cannot find ‘eidw_metar’
    May 4 16:10:22 raspberrypi weewx[399]: **** Traceback (most recent call last):
    May 4 16:10:22 raspberrypi weewx[399]: **** File “/usr/share/weewx/weewx/cheetahgenerator.py”, line 330, in generate
    May 4 16:10:22 raspberrypi weewx[399]: **** print >> _file, compiled_template
    May 4 16:10:22 raspberrypi weewx[399]: **** File “/usr/lib/python2.7/dist-packages/Cheetah/Template.py”, line 1005, in __str__
    May 4 16:10:22 raspberrypi weewx[399]: **** rc = getattr(self, mainMethName)()
    May 4 16:10:22 raspberrypi weewx[399]: **** File “cheetah__etc_weewx_skins_niculskin_index_html_tmpl_1525444823_3_60304.py”, line 916, in respond
    May 4 16:10:22 raspberrypi weewx[399]: **** NotFound: cannot find ‘eidw_metar’
    May 4 16:10:22 raspberrypi weewx[399]: cheetahgenerator: Generated 7 files for report StandardReport in 3.83 seconds
    May 4 16:10:24 raspberrypi weewx[399]: imagegenerator: Generated 11 images for StandardReport in 1.88 seconds
    May 4 16:10:24 raspberrypi weewx[399]: copygenerator: copied 0 files to /var/weewx/reports
    May 4 16:10:24 raspberrypi weewx[399]: reportengine: Unable to instantiate generator user.metargenerator.MyMetarSearch
    May 4 16:10:24 raspberrypi weewx[399]: **** __init__() takes exactly 2 arguments (7 given)
    May 4 16:10:24 raspberrypi weewx[399]: **** Traceback (most recent call last):
    May 4 16:10:24 raspberrypi weewx[399]: **** File “/usr/share/weewx/weewx/reportengine.py”, line 226, in run
    May 4 16:10:24 raspberrypi weewx[399]: **** self.record)
    May 4 16:10:24 raspberrypi weewx[399]: **** TypeError: __init__() takes exactly 2 arguments (7 given)
    May 4 16:10:24 raspberrypi weewx[399]: **** Generator ignored

  2. Hello,

    It looks like that the report engine is unable to instantiate the new python class created:
    “reportengine: Unable to instantiate generator user.metargenerator.MyMetarSearch”
    “**** __init__() takes exactly 2 arguments (7 given)”.

    Maybe the configuration in skin.conf file have some kind of error? I don’t really know where these 7 arguments come from … Have you made any change in the python class?

  3. Thanks Dani, I changed skin to test this and at least its not getting errors of this nature any longer. However it is now giving the following:

    May 6 16:30:23 raspberrypi weewx[22898]: metargenerator.pyc: error: Cannot get Metar Report. Recovering the last file saved.
    May 6 16:30:23 raspberrypi weewx[22898]: metargenerator.pyc: error: There could not be found an older Metar Report. Skipping!

    The url it should be using – https://www.aviationweather.gov/adds/metars?station_ids=eidw+&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submitmet=Get+Weather is working currently so I’m not sure whats wrong.

    • Hello,

      Can you paste the skin.conf section where the extension is configured?

      It should be something like this:

      [MetarReport]
      refresh_interval = 5
      [[eidw]]

Leave a Comment