Finding the City Nearest to Thousands of Weather Stations

Remember when I did a post about weather? I told myself then that I would be doing another post concerning WeatherData[] “soon,” because there were so many options to explore concerning this function.

Welp. Here we are.

I got side-tracked to easily because there’s so much to do in Mathematica, especially because I updated my version not too long ago. Wolfram sure does a good job of adding new features (and data :))))))))))) with each new version. (However, I don’t know why multiple undo took so long to implement… Even Notepad and MS Paint have that). Mathematica’s WeatherData[] provides us with weather stations that give us a wealth of data, but the locations of these stations are only given in coordinates. Today, we will convert these coordinates into the region each weather station is located in.

Nearest[] is Our Friend

First, assign the basic data to easy-to-read variables.

Note: I closed Mathematica before I took the screenshots. This code took several hours to run, so I do not want to run the code again.

notebook1

Unfortunately, these variables are full of weather stations that are no longer used, as well as cities that do not have their coordinates recorded. These must be removed. The easiest way is to use FreeQ[]. I typed out the function makes it easier to read, but the # and & symbols are not needed. Simply type FreeQ[Missing[“NotAvailable”]] in Select[].

notebook2

Now we get to the “meat” of this operation. Use the Nearest[] function to find which city each station is closest to. Remember to save all of the Nearest[] variable (which take billions upon billions of years to get)!

notebook3notebook4notebook5

For to find which country each weather station is in, convert the the third member of each “city data” into the country (which is given which each city’s location). It is easiest to make a list of rules to do this for you. Make the code spit out a list giving the weather station and the country using the rules that were previously created.

notebook6

Playing with our New Data

Now that that’s over with, you can do interesting things with this data! A rudimentary example is finding which cities have the most weather stations by “Tally[]-ing” by the occurrence of each city.

notebook12

You can also see that the data works by plotting all of the stations in Japan (or any country). The coordinates have to be reversed because the coordinates of the stations are given in opposite from the coordinates system used in Graphics[].

notebook8notebook9

This isn’t limited to countries and cities! Stations in specific states in the US can be found as well. The weather stations in California are plotted b

notebook10notebook11

weatherstations.png
Meanwhile at Wolfram HQ…
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s