Will it rain?

    From time to time I ride my bike to work. Sometimes it rains outside, the weather is “not cycling,” and then the dilemma arises: wait a bit until it ends, or go by car altogether. It also happens that in the morning the weather is good, and in the evening it begins to rain and I want to find a “window” in order to return home dry. It came to a couple of times that it was ridiculous - in the morning the sun was shining, I was leaving, about five minutes later a light rain began, and a few minutes later it ended, the sun was shining again, and I entered the office wet to my underpants.

    Thus, you need a service with a short-term weather forecast - within a couple of hours. Basically, weather services offer a forecast for several days (while the quality of the forecast is so-so), but I have not seen a convenient and simple short-term forecast. Although it would seem - what could be simpler - precipitation maps are available at every moment of time, and after analyzing the history over the last couple of hours, you can fairly reliably calculate what will happen in the next hour or two.

    Under the cut, we will do the following - programmatically download rain maps from one of the services and see what will happen in the vicinity of a certain point, saving the results in the dropbox. A simple Friday exercise is performed, of course, in LabVIEW.

    Initial data

    First, let's see what we have. I am located in the north of Germany, so the presentation will be applied to this country, but the general principle, of course, applies to other countries - you just need to find a suitable source of information. In Russia, the situation is a little worse - I have not seen good detailed precipitation maps - this, by the way, is an idea for a good startup. The first service I came across is www.niederschlagsradar.de . There is almost everything that is needed to evaluate whether it will rain or not, but there is no convenient presentation of information for a particular point. Second service www.rain-alarm.com. It’s almost exactly what you need - you can set the location there, but I need not so much a warning as a small sign - after all, I’m not only interested when the rain will come, it’s also important for me to know when it will rain to find the window in 20-30 minutes, during which I will get to work dry. In addition, the free version has a bunch of restrictions (for example, analysis is only within 75 km, which is a lot of advertising). It’s not a pity for the paid version of money, it’s just not quite what you need. maps.google.comit can show clouds, but not precipitation, and it’s easy to get a ban there for constantly polling data. There are also several android apps that show an animated rain map, but again, the animation has to be analyzed in the head. Well, roll up our sleeves and make a simple bike. Along the way, I will show how to get a picture from the Internet in LabVIEW - actually the post is about that.

    We will parasitize on the website www.niederschlagsradar.de. I don’t plan to post the application online, and I don’t see any problems in writing the robot “for myself”. My first thought was to analyze the location and movement of precipitation in the vicinity of the point of interest, then calculate the cloud motion vectors and build a forecast for the next hour. However, I did not have to reinvent the wheel - there is already a forecast (however, I lost the most interesting part of the puzzle). The server gives the forecast pictures in this format, for example, for 13:00 on January 4, 2013:


    It is easy to notice that the time is in UTC.

    Retrieving Images from the Internet in LabVIEW

    To get pictures from the Internet, we use the following simple method:


    In this example, the NI website logo is loaded. If you are reading this article in Internet Explorer, and you have LabVIEW version 2010 or higher, you can drag this snippet directly onto the block diagram. A slightly non-trivial part is adding the string [text] to the URL. Why so - can be found in the document Retrieving an Unformatted Text File via FTP or HTTP Using DataSocket .
    Thus, we got the contents of the gif in the string. LabVIEW "out of the box" does not know how to work with gif files, so we will use the library of the good Samaritan MikeS81to convert gif to picture. I added the ability to read contents from a line there, otherwise I would have to save the resulting image to a file, and so we can perform the conversion "on the fly" in memory:


    URL generation

    Now we need to create a line of the form
    www.niederschlagsradar.de/images.aspx?j=-4&file=201301041200 The
    time here is used in increments of five minutes - 12:00, 12:05, etc. So we get the current time, round it up to five minutes, (then in the cycle we will add 300 seconds to get all the forecast files):


    Line generation is primitive:



    Now let's start the analysis. Analysis is, of course, loudly said, but nonetheless. The server gives us GIF images of 550x512 pixels in size with a transparent background. The blue component is always equal to 255, and the red and green are reduced in the rain areas. Say R = 100; G = 100; B = 255 - it is quite heavy rain. The areas with rain are shown in blue:


    Let's do a simple analysis: take a point with the given coordinates (my city is approximately at a point with coordinates x = 302, y = 79), cut a square from the image array (I took 13x13 pixels - this is about 10 km) and calculate the minimum. It is theoretically more correct to calculate the average, but with a minimum I got better results (it happens that a cloud only touches the edge of the square, but at the same time there is quite heavy rain outside the window, so the minimum gives a more realistic result):


    Technically, RGBVIEW in LabVIEW is a one-dimensional byte array in which every three bytes are RGBRGBRGB color components, etc. Accordingly, Decimate 1D Array with three outputs will decompose the array into components. In order not to fool around with the calculation of indices, a one-dimensional array is distilled into a two-dimensional one, and then a square piece is cut out from there.


    It remains only to save the report in an HTML file:


    At the input we get a two-dimensional array with the results. The first column is the forecast time, and the second is the result. We will save the result in% USERPROFILE% \ Dropbox (good tone - do not use hard-coded paths):


    That's almost it. It remains only to put a map of Germany under the clouds in order to control the position and size of the region of interest. To do this, use MaskColor - this SubVI will create a mask through which the map will be visible. The map is loaded from the same folder in which the main VI lies. Put it all together and run in a loop:


    Here, the While loop is triggered every 5 minutes (a pause of 300,000 ms is not kosher, but I don’t want to complicate the diagram), and for the loop loads the data in two hours (22 iterations - exactly so many pictures with the forecast are available on the server). I intentionally did not hide the logic under SubVI, so that it was more visual. On the front panel we will have this picture:


    Here is such a Gambur winter. Well, such a file will be in the dropbox, for example this morning (dashes mean no rain, pluses mean presence and strength): It’s


    immediately clear that at eight in the morning it’s better not to leave the house, but to wait until half past eight.
    Since I have a dropbox client on my tablet and phone, I can always see before leaving home whether I should ride a bicycle or it makes sense to take a car.
    Of course, the program can be hung with whistles to infinity (minimizing to tray, uploading data to ftp, adding temperature, messages to e-mail / SMS, etc.), but even in this simple form this application suits me more than completely. And it goes without saying that such a thing can be assembled in any programming language, this article is just an example - how a small applied problem can be solved using LabVIEW tools.

    Source Code in LabVIEW 2010

    Also popular now: