Writing a Twitter Bot to post temperature updates

To the Bot on Twitter.

It all started with a family discussion over Easter 2021 where my parents could not decide if 2021 has been really cold or really warm in Austria so far. So I did the only reasonable thing and started making plots. I compared mean temperature from the beginning of the year until Easter to the same period in past years using data from ERA5. As seen in the figure below Austrian mean temperatures in 2021 until beginning of April where below the mean of the last 11 years but still 1.5°C above the 1950-1999 baseline.

Then I generalised it to any end date (i.e., mean temperature from January 1st to XX) in what I started calling year-to-date temperature. I also added to option to calculate the metric for different regions. As a test bed I used the regions defined for the latest sixth IPCC assessment report (AR6). The animation below shows the example of global temperature until end of April.

Next I started to automate things using the very convenient era5cli and cron to download ERA5 (using the ERA5 API). My experience from this is that ERA5 is about 2 weeks behind real time which I find pretty impressive for a global reanalysis. Here is an example to download all available days from the current month and year in hourly resolution and then resample them to daily using cdo:

year=$(date +%Y)  # current year
month=$(date +%m)  # current month
era5cli "hourly" --variables "2m_temperature" --startyear $year --months $month --outputprefix era5-${month})
# define fn & fn_new
cdo daymean $fn $fn_new

I use a similar approach to update the plots in a directory. I’ve recently updated the layout of the plots a bit but haven’t managed to clean to code up to a point where I think it is clean and documented enough to publish it. But one of these days… Here is mean temperature in the first half of 2021 for the Mediterranean in the new layout:

Finally, I started to play around with the Twitter API and applied for a developer account. I used the Python Tweepy package, which makes automated tweeting pretty easy. At the core it is really only a couple of lines:

import tweepy

# ...

def tweet(fn, text):
    auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    media = api.media_upload(fn)
    api.update_status(status=text, media_ids=[media.media_id])