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.


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[].


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)!


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.


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.


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[].


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


Meanwhile at Wolfram HQ…

Another Way to Visualize Riemann Sums

Most of the projects I have posted on here have been rather abstract. I want to connect what I do with reality more often (although math is the very essence of the universe).

In mathematics, we often “cheat” by going as close to infinity as possible and pulling out at the last second. This can be seen in integrals, derivatives, and Taylor series. All of these make use of infinitely small or infinitely large values. However, most functions cannot be integrated by hand and most Taylor series cannot be calculated to an infinite degree. As a result, we often rely on computers to calculate these values with brute force, that is, calculating the Riemann sum in the case of the definite integral or finding the Taylor series to a certain degree.

“Real” Riemann Sums

A while back, I made a post (link) regarding Riemann sums. By using Mathematica, I was able to instantly generate hundreds of rectangles using left, right, and midpoint rule. Instead of relying on unrealistically large numbers of rectangles for my Riemann sum, I decided to go with 29 slices on a relatively large interval. I used the function shown below on the interval [1,6.25].




Instead of doing a 2-dimensional Riemann sum, I did a volume integral so a 3-dimensional model could be physically constructed. The Riemann sum was composed of squares perpendicular to the x-axis. I planned to cut out squares from foam board and glue them together to make a model of the function. The thickness of the foam board was 3/16″, and 1 unit corresponded to 1 inch.

All of the calculations were done in Google Sheets (link to the original document).

CodeCogsEqn (2)


As you can see, the final Riemann sum, shown as the cumulative volume, was an overestimate, and the error was 3.6406. This number is surprisingly accurate of 27 slices on a large interval. The accuracy of the function can really be seen when the squares (which literally took hours to cut out) are glued together. The width of each slice is represented as f(x) on the table.


Here are all of the slices laid out.


Gluing in process. I really should have used wet glue, but everything worked out.

…And finally, here are two views of the final product! Even with extreme variation in the beginning of the interval, the model turned out fairly accurate. It is now easy to imagine how more complex models can be constructed with high accuracy in more professional settings, such as when a satellite is constructed.

**NOTE: All of the equations were done in a CodeCogsEqn (3) interpreter, because I cannot directly put nice-looking equations in WordPress.


Removing Watermarks from Stock Images

It is a common misconception that hacking and other slightly “illegal” activities can only be done by the greatest professionals. However, these processes are actually fairly simple to replicate given that one has knowledge of how they work and a decent computer program.

Removing iStock Watermarks

Watermarks are used to prevent people from stealing copyrighted images. They should not be removed if the images will be used for commercial purposes. Credit should still be given to the individual or company who took or created the picture.

Now that that’s out of the way, let’s get to the interesting stuff.

Watermarks are automatically applied by multiplying an image by the watermark “function.” Take the following watermark from iStock, for example.


The watermark was applied by taking the original image and multiplying it to the watermark. This can be seen by splicing out the watermark and subsequently turning it into data. To turn the cropped watermark (named img) into data, the command ImageData[img,”Byte”] can be used. Then, the data (named whiteData) can be multiplied by a sine graph.


To reverse the effect of the watermark, the modified image can be divided by the watermark “function.”


In order to create the inverse image that can delete the watermark, whiteData can by divided by 255 (this is now called whiteDataMask). whiteData must be divided by 255 to ensure the final image will have data from 0 to 255. By multiplying 1/whiteDataMask by img, the watermark will be deleted. Images can be multiplied to each other by using ImageMultiply[].


It worked! This method can be applied to other images by using Manipulate[] to change the position of the mask over the image. Take note that black and white masks must be used to delete the shaded part first, then to re-brighten the image with the white mask.


Update: New Pages and Polar Coordinates

Hello! I have been busy this month with science fair and with making new pages for this blog. I have added a page for the project my partner, Azja Czajkowski, and I brought to the Greater San Diego Science and Engineering Fair. I also added a page for my (actual) experiments. Although there currently is not much on that page, I will be uploading more files there soon. Please check out those pages by following the links above!

I also have something else that was too short to be worthy of its own post.

Polar vs. Cartesian Coordinates

When I was learning Calculus BC, I was confused by polar coordinates. Although I understood how to plot points, trigonometric functions confused me. How could a sine function become a circle? How did shifting the sine function up or down in Cartesian coordinates cause the polar graph to turn into an odd loop shape? This program (link to code) helped me understand how polar coordinates worked. The reason why a loop shape comes out of a vertically shifted sine function is because a normal sine function actually drew the circle twice. Shifting the function up or down would shift where the point was drawn (the radius). I could also understand the parallels between polar and Cartesian coordinates, and it helped me realize how easy polar coordinates were.

Here is a video demonstration of the program (link).polar

Mathematica is not just an “Expensive Calculator” (pt. 2): Calculus-Based “Calculators”

(part 1)

It would be a waste to make a Riemann sum finder and not make more calculus-related “calculators.”

The Equation of a Tangent Line

We can find the equation of a function’s various tangent lines using the function’s derivative. This tool allows the user to manipulate the point where the tangent line hits any given function. Here is a video demonstration (link) and the source code (link). This is how the code was constructed:

  1. use Plot[] to draw the original function and the general equation for a tangent line (I used “n” as a placeholder for the x-value)
  2. use Epilog to draw the point where the tangent line meets the graph
  3. PlotLabel (in the graph) will equation of the tangent line, so use a combination of <> and ToString[]
  4. display the contact point’s coordinates next to the point using Text[] (tip: to get the position of the text, take the original point and offset it by a small amount)
  5. optional: change the colors to make it look cool


Revolution of Solids

The volume of a solid found by rotating a graph around the x-axis, y-axis, or another line can be found using integrals. This small widget displays the graph of the function, its revolution, and the area. The degree of revolution can be manipulated and a function can be entered. Here is a demonstration (link) and here is the source code (link). The following steps show how the code was created:

  1. plot a function using RevolutionPlot3D[]
  2. change the degree of revolution to 0 to “a” (the letter “a” will be manipulated as the degree of revolution)
  3. calculate the area of the solid for the PlotLabel (tip: the area should be pi times the integral of the function squared, from 0 to “a”)
  4. make sure the code works, then replace the function with the letter “f” (you should be able to enter the function, but make sure that there is an initial function)
  5. use Manipulate[] to change the values of “a” and “f” – make sure that “a” starts from a number close to but not equal to zero




Mathematica is not just an “Expensive Calculator” (pt. 1): Making a Few Small Widgets

I am putting the survey analysis on hold for a little while, because this is a bit more interesting. It’s quite different from what I have done on this blog up to this point.

People often joke that Mathematica is just an expensive calculator, even though it is capable of so much more. Here are a few small widgets just to humor these critics.

A Basic, One-Digit Calculator

Now we will reconstruct a basic, one-digit calculator. Here’s the code (link), and here’s the calculator in action (link). The process is listed below:

  1. create a grid of buttons using Column[], Row[], and Button[], and put a frame around it to make it look cool
  2. make 2 chosen variables equal “Null” (num1 and num2 for the two numbers that will be operated upon)
  3. make the button change the value of the 2 variables based on their value using If[]
  4. make operation buttons that will only act upon the variables if they are numbers

Here is a picture of the finished calculator, in case you can’t click the links. The second link (above) will show it in action.


Riemann Sum Finder

How about something more sophisticated, like a widget that will draw and find the Riemann sum of a function? Here’s the code (link), and here is are two demonstrations of the program (link 1, link 2). The process is listed below:

  1. make a plot that will draw a given function
  2. use Graphics[] to draw rectangles: the height should be based on the output of the function and the width should be 1 divided by the number of desired rectangles (tip: start with a constant number of rectangles before using Manipulate[])
  3. the height of the rectangles should be able to shift from left to middle to right by shifting the inputted value
  4. use Show[] to plot the function with the rectangle graphics and add Manipulate[] to make it interactive