Category Archives: ubuntu

Checking river pumping restrictions using python

I have a licence from Melbourne Water to pump water from the Yarra River. Water pumped from the river is stored in a large concrete tank (20,000 liters), and used for toilet flushing, watering the garden and as a source of water for fire fighting.

For environmental reasons, pumping from the river is restricted during periods of low flow. Melbourne water publishes flow and pumping restriction data on their website, so I can check if restrictions apply before starting the pump to top up the tank. I hacked together a quick python script to get the information I need, so I can display the information on my computer’s desktop using conky.

The code makes use of the Beautiful Soup html parser for python to extract the data from Melbourne Water’s web page.

#!/usr/bin/env python
#Python script to get current pumping restrictions for Upper Yarra River.
import mechanize
import HTMLParser
from BeautifulSoup import BeautifulSoup
BASE_URL = "http://www.melbournewater.com.au/content/rivers_and_creeks/waterway_diverters/yarra_upper.asp"
br = mechanize.Browser()
data = br.open(BASE_URL).get_data()
soup = BeautifulSoup(data)
table=soup.find("table",title="Table showing current waterway diversion status")
for row in table.findAll('tr')[1:]:
    col = row.findAll('td')
    restrict = col[1]
    ban = col[2].string
    flow = col[3].string
    avflow = col[4].string
    date = col[5].string
#Function to strip html tags from table cells (allows for episodic coloring/bolding of cell contents when bans apply!)
def stripper(data):
   data = str(data)
   count = data.count('<')
   while count:
       start = data.find('<')
       end = data.find('>')
       rem = data[start:end+1]
       data = data.replace(rem,'',1)
       count-=1
   out = data
   return out
#apply the stripper function to current restriction and ban statuses
restrict = stripper(restrict)
ban = stripper(ban)
print 'Restricted?  %s   Banned? %s' % (restrict, ban) 
print 'Flow: %s ML/d  AvFlow: %s ML/d' % (flow, avflow)

Bushfire danger forecast in conky

The Australian Bureau of Meteorology publishes daily forecasts of bush fire danger on its web and ftp sites. I live in an area with very high fire danger during the summer months, and I need to be aware of the forecast bushfire risk. I devised this simple bash script (called fire.sh), which grabs the local weather forecast, strips out the required information and returns it.

#! /bin/bash
#BOM weather forecast for Yarra Glen
  URL=ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10729.txt
#Get the data from the web
  DATA=$(curl -s $URL)
#strip out the fire danger rating line from the raw text
  FIREDANGER=$(echo "$DATA" | grep -A1 'Fire Danger')
#print the fire danger forecast
  echo $(echo "$FIREDANGER")

Displaying this information on my computer’s desktop using conky,  is accomplished by adding the following line to my .conkyrc file:

${execi 4000 /home/michael/Scripts/fire.sh}

Using dropbox with R

I like to use dropbox for online storage and syncing of R code and data that I am using at home and at work. Storing the code in the cloud means I always have access wherever I am, and that I don’t have to remember to bring my USB flashdrive to and from work.

My work computer (Windows XP) and my home computer (Ubuntu Linux) store their dropbox folders in different locations, meaning that calls to the R function setwd() will work on one of my computers, but not the other.

This chunk of R code checks which operating system R is being run under, and sets the working directory appropriately. I’m sure there are more elegant ways to fix this problem, but this approach works for me.

if(Sys.info()[1]=="Linux") {setwd("/home/michael/Dropbox")} else
                           {setwd("D:/Dropbox/My Dropbox")}