Including METAR reports in WeeWX



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:

#include “forecast_table.inc”

13 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:

      refresh_interval = 5

  4. Could you show an example of how to specify a METAR code here? For example, for METAR KPMY?

    refresh_interval = 5 # Obtain every X minutes
    [[eddg]] # Metar Airports to generate

  5. Hello,

    I’m trying to set this up to run with the “Seasons” skin on WeeWX v4.1.1. After following your tutorial, I restarted WeeWX. Unfortunately I’m only getting a widget title printed on the page followed by “$kdmw_metar”. The metar itself is not printed. I looked briefly at metargenerator.py and it looks like it should be writing something to syslog saying it generated the metar tables in so many seconds but I’m not seeing that. I did include this for the [CheetahGenerator] in skin.conf:


    # Possible encodings are ‘html_entities’, ‘utf8’, or ‘strict_ascii’
    encoding = html_entities

    search_list_extensions = user.metargenerator.MyMetarSearch

    # Reports that summarize “by month”

    Then at the end of skin.conf I added these lines:

    #The list of airports to generate METAR report:

    refresh_interval = 5

    I also included the following lines in the index.html.tmpl file so the METAR should print just below the plot_group:

    METAR (Meteorological Aerodrome Report)  


    I’m not seeing any errors (or any signs of life from the generator) in syslog so I don’t know what is happening under the hood. Do you have any suggestions on what I can try next to fix this?


  6. Dani,

    I have been playing with this all day trying to get it to work with WeeWx v4.4.0 and I think I finally have it. The biggest issue which I didn’t realize until earlier this morning was that your original extension was written in Python2 while WeeWx has since moved to Python3. I needed to do a little more than run it through the version converter “2to3” to get it to work though. If you like, I’ll upload my changes to github so all can benefit.


  7. Dani,

    A Commit & Pull Request have been submitted. I doubt this new version will be backward compatible with WeeWx v3 but who knows.

    Thanks for the effort you put into the original version of this generator. A ‘very cool’ addition to my weather station.


  8. I still don’t see a proper howto for this. Where do I put the [MetarReport] section in skin.conf?
    Also, where do I put the tag? In your example it’s in some html in skin.conf, but my skin.conf (neowx-material) does not have any.

    • Hi,
      You should insert the [MetarReport] somewhere in your skin.conf. Then, to show the Metar report, just insert the TAG in the HTML page generated by your skin.conf file (index.html?) and that’s all.


Leave a Comment