Level crossings part 1

It’s a horrible wet day so I’ve got to find something productive to keep me occupied. I’ve decided I’m going to try and create something which alerts me to trains approaching the level crossing near my flat. The level crossing is very near. I live just across the road from West Worthing station and at the end of the station is the crossing. If I don’t have music on I can easily hear the sirens when the barriers go down. In fact I’ve just heard them now.

To be honest, the level crossing is a real pain, I have to cross it every day on my way to and from work. Almost inevitably, the barriers are down either going or coming from work. It’s also a busy road so there are long queues and most British drivers, being who they are, like to sit in the queue with their engines idling. The crossing is so hated that it has it’s own spoof twitter account.

I’d like to create a web page which displays when the barriers are expected to be up or down in the near future. Ideally, I’d like it to have a level of precision such that I can predict how long it will be until the barriers go up if they are down.

To do this I need data. I already have a few pieces of data:

  • The barriers always go up 5 seconds after a train passes through.
  • The barriers are always down when a train passes through (obviously)
  • The barriers go up for at least enough time to get one cycle of traffic through them (the crossing is adjacent to a crossroad which has traffic lights)
  • Most trains stop at the station (but not all do)
  • Almost all the trains are ‘Southern’ passenger trains (I think there’s one freight train per day in the early hours of the morning)

What remaining data do I need:

  • The times of the trains passing through the level crossing
  • The length of time before the train that the barriers are lowered for.
  • The length of time for one cycle of traffic at the crossroad

I’ll come back to the times of the trains, that’s the most crucial piece of data.

The length of time before the train that the barriers are lowered for is not easy to find out. The crossing is controlled by an operator using CCTV so the timings are probably quite variable. I need to collect some data to see if I can pick out any trends. Thankfully I can do that from the comfort of my living room with a timer. It’s a bit tedious but I collected the data.There was a wide variation in time, the shortest time was just 83 seconds whilst the longest was almost 5 minutes. The average was around 2.5 minutes.

The length of time for one cycle of traffic can also be found in the same way. I know that it varies depending on the traffic. From a short time collecting data, I found that the average traffic cycle was 65 seconds in length. The shortest cycle I measured was 40 seconds and the longest 110 seconds.

I’ll make the assumption that the barriers don’t open if they are likely to be open for less than a minute.

Finally the most important piece of data is the train timings. The naive approach here would be to simply find a timetable copy and work out when trains should pass through this stretch of track. That would be a good approach to get rough times but it wouldn’t be very accurate for a few reasons: trains get delayed, they get cancelled, extra trains get put on and timetables change slightly every few months, I don’t want to spend time updating the website every few months.

In summary I need real time data about the trains in the near future. Good news for me, Network Rail provides that data. I signed up for a free account and within a few minutes I potentially had access to lots of lovely data.

Of course it wasn’t that simple, I needed to subscribe to the correct data feeds and set up a way of receiving those feeds and turning them into the data that I actually wanted. This process is not simple but there was a useful wiki to help me.

Network rail provides several data feeds, they provide a daily and weekly schedule of trains, they provide short term updates to the schedules and they provide a live feed of trains passing through timing points on the network. In addition they provide reference data about the timing points and estimated time between them for various types of train.

The real time data from network rail is delivered in messages using the STOMP protocol. STOMP is relatively simple however it isn’t trivial to correctly get messages from the server. I’m used to working with PHP to build my websites so I decided to use PHP for this project. After a short search on the internet, I found this STOMP library.

With all this data I need to decide how to present the data to the end user. I’ve decided that mapping out the barrier closures in the next hour probably provides the best amount of data and accuracy. I’ll attempt to update the model in real time as new events come in on the feeds.

Wireframe
A quick wireframe sketch

One of my favourite parts of a project comes from putting together the user interface. I always like to sketch out ideas on paper first, it helps me put together web pages much quicker.

I then went and put together the actual web page. I’ve tried to use colour and scale to highlight the important information.

So the first half of my project is complete but I still need to work out how to properly populate it with data. This means building a suitable database and updating the web page using Ajax. Stay tuned for part 2.

As an aside I’m thinking of getting a webcam which produces a stream of images I can analyse to see how well my predictions stack up to the real data. It was also give me much more accurate data about the train services to further improve my models.

As a second aside, the photo at the beginning of the post is not my level crossing, It’s another (nicer) level crossing that I happened to take a photo of in the summer.

Memories and airshows

My earliest memory is of an airshow. I remember going to an airshow at White Waltham with my family. I was probably about 4, I remember watching the planes, in fact I was engrossed by the planes. I was standing by the metal barrier watching the planes when my family disappeared off. I don’t remember them go. I must have only been stood for a minute or so on my own but when I turned around I couldn’t see my parents. To a boy of that age, it’s a scary situation, had my parents deserted me for good? But it was drilled into me to trust people in uniform so spotting a man in a fluorescent jacket, I went over to tell him that I’d lost my parents. I think he told his colleague “this young lad’s lost his parents”. They started walking me somewhere and my parents magically reappeared. I had probably been without them for a couple of minutes, I felt a bit silly.

The problem with this memory is that I have no idea if I have remembered it correctly. I think the only reason I remember it is because I remember writing about this incident in secondary school when I was about 12 or 13. I had mentally bookmarked it away as being my earliest memory. There are certain memories that you keep coming back to and each time you remember them, they get distorted slightly until after a few years only the main points of the story remain faithful to the truth.

My most recent memories of an airshow are probably not much more accurate and they are from only two and a half months ago. At about 1.20pm on Saturday the 22nd of August I was sat on the grass on the verge of a dual carriageway. I was about a hundred meters from the end of the runway at the Shoreham airshow. I had somehow lost track of the plane that I was trying to take photos of (my last photo of it is the photo at the top of the post). For some reason, possibly the noise, possibly people moving I turned my head to look along the dual carriageway. What I saw still sticks vividly in my mind. A jet fighter was about 5m above the road. Its nose was pointed towards me but slightly upwards.

The next moments happened within a single second.

I thought about the jet being so low, it was already too large to fit within the frame of my camera. From the planes angle, I reckoned it would pass directly over me only a few metres above where I was sat. I wondered why it was doing a low flypast, planes don’t do low flypasts at airshows. Was the pilot showing off? Then I realised the plane wasn’t going up, it had dropped behind the trees. It was at this moment that I realised something was wrong.

Everything happened too quick. I have some slight memory of seeing the first fireball as the left fuel tank hit the ground but I remember it hitting to the side of the road not in the carriageway itself which is what actually happened. Being sat down, there wasn’t the time to move anywhere. I instinctively turned around so I was facing the opposite direction and lowered my body so I was almost lying on the grass. At the same instant, the first small bits of debris hit my back and leg and an instant later I was engulfed in a fireball.

The heat was immense, it felt hotter than anything I’d ever experienced before. I think I closed my eyes because I don’t remember seeing anything except blocks of bright light followed by darkness. I definitely thought it was the end of me, I was waiting for something sizeable to hit me and cause me to lose conciousness.

Luckily for me that never happened. As soon as I had that thought, the fireball dissipated and I could see the world in front of me. To give you a sense of how fast this all happened, the video below shows the accident as closely as I saw it. I reckon I saw the jet about second 27 in the video.

As soon as I could, only about 10 seconds after the crash, I got to my feet, still facing away from where the plane had first hit. I briefly surveyed the ground and started running. Thankfully, I didn’t see much, just the ground littered by bits of metal and plastic. I didn’t see any bodies or anyone else injured except a man curled up on the pavement. I almost stopped, but I came to the very quick decision that he was far enough way from the crash not to be badly injured and I wanted to be as far away from the crash as possible.

I ran a couple of hundred metres back along a road towards the airfield still holding my camera, I can’t remember even thinking about dropping it. I didn’t know what to do, I just screamed for people to go and help the people who were injured (I assumed at this point that others had been badly injured). It was also at this point that I realised I was probably burnt, my back and legs were stinging and looking down at my hands, my skin had become loose between my thumb and forefinger on both hands.

Airshows, I’ve decided, are good places to get injured at. No sooner had I got back onto the airfield than a bunch of men and women in combat fatigues were sitting me down in a tent and running to find bottles of water to pour on me. I was alive and my long road to recovery was just beginning.

A new blog

This is a new blog and a new challenge. Many years ago, and it really was a long time ago, I kept a somewhat regular blog. It covered technical articles about programming, things going on in my life, my side projects and a mix of other things. With my first blog I tried to post a couple of times a week, that lasted about a month. In the end I managed to blog at least once a month for a whole year and then I stopped.

And now I’ve started again. This time I’m setting myself the challenge of posting something every single day. That’s actually quite a big commitment; I enjoy writing but I’m a slow writer. I can easily take an hour writing just a few paragraphs. To make my challenge a bit easier I’ve decided that my posts each day don’t necessarily have to be writing, they can be photography, recipes, interesting things I’ve found online. It just has to be something interesting.

There’s two purposes to this blog. Firstly, I want to improve my writing. The more I write, the better I’ll get (hopefully).

Secondly, I want to write about interesting things: things that interest me and things that could be of interest to others. A few of the posts on my old blog still get hits, other people still get something out of the stuff I wrote. And the posts which no one reads any more are still interesting to me, they’re a record of my life and what interested me back then. I hope that by forcing myself to write, I’ll go out and find interesting things to write about.

So this is the first of many posts and hopefully one of the least interesting! Feel free to leave civil comments, I appreciate feedback of any kind.

P.S. All the photos used on this blog are my own (unless I say otherwise) and you are welcome to use them in any way you wish!