Publication-Quality Plots Using MATLAB and METAR Data From Nearby Airports
This tutorial will focus on producing publication-quality plots by utilizing MATLAB and the Meteorological Aerodrome Report (METAR). METAR stations record atmospheric conditions of nearby airports to ensure that aviators operate under safe conditions in the proximal airspace. Meteorologists also use METAR data for forecasting and calibration of remote sensing instruments. The procedure outlined below will use your zip code to find a nearby METAR station, parse the data from the National Weather Service, and ultimately produce a plot of recent weather data published by that METAR station. By the end of this tutorial, the user will have a better grasp of MATLAB's figure and plot tools and produce publication-quality results.
Finding Your METAR Station
First and foremost, a METAR station needs to be identified. The METAR station can be random, a local station, or a chosen location. A 4-letter identifier has to be selected in order for the MATLAB routine to request data.
Steps for Finding METAR Station
- Visit http://www.weather.gov/
- Enter a Zip Code in the red box shown in Figure 1, then click 'Go'
- After clicking 'Go' a new page should appear showing the current conditions of the METAR station local to that Zip Code. Find and record the 4-letter identifier (see Figure 2's red box), and continue with this tutorial
Now that a station is identified, it's time to explore the METAR data. For every METAR station there is a government database where the data is stored. The data can be accessed the following way:
In my case, my station ID is 'KNYC' so my full path would be:
And if the link above is clicked, the METAR database can be observed for Central Park, NY (KNYC) for the past 72 hours. Figure 3 shows the METAR station data for KNYC for a few hours on February 14, 2018. If the station selected does not show the same table, then another station should be chosen.
Reading and Parsing METAR Data With MATLAB
Now that a station ID has been identified, it is time to tell MATLAB to read the data from the URL created above. In the code below, MATLAB is creating a URL for the METAR station, and subsequently reading the webpage using 'webread.' The variable 'web_str_crawl' now contains the data from the METAR station for the last 72 hours.
From here, I will demonstrate how to parse that data, which is currently formatted in html. The code below is a function for taking METAR station data and parsing out the variables we want and formatting them into a nice table in MATLAB. All the function needs is the URL for the METAR station.
MATLAB Figure Formatting
In science and industry alike, visualization plays an important role in research and analysis. MATLAB also requires extensive alteration of its default plotting tools to produce beautiful visualizations. Therefore, I have included a MATLAB function that produces a figure which mimics a popular graphics tool used in the R programming language, called ggplot. With this new template, any dull MATLAB figure can be transformed into a contrasting, visually compelling visualization.
Plotting The METAR Variables
The final and most important part of this tutorial is the data visualization. Now that we have found our METAR station, parsed the data from weather.gov, and created our figure, we now need to plot the data. The station data above exists in a cell of strings within MATLAB. One major complication with the data is the fact that the time spans two columns: 'Date' and 'Time (Local).' Therefore, the first thing we need to do is convert those times to a single datenumber in order to plot the variables in the correct time sequence. The function for this is below:
Finally, we have the x-variable ('datenum_vec') and y-variable ('var_pick'), along with the variable name ('var_name'), which can all be used to plot the data onto our figure above. The function shown above plots the data for a given column of data using the station table created in the program. To determine which column to plot, take a look at the table data, or just plot the variables based on the columns in the weather.gov METAR website. They should be identical. To plot the raw temperature values, the only lines of code needed are:
The plot is a simple line plot, and the visibility of the plot is set to visible once the data is plotted. The figure should now look something like this:
Perfecting the MATLAB Plot Appearance
At this point, we have a fully functional plotting routine that grabs data from the internet (weather.gov METAR station data), decomposes its html codec into a MATLAB table, and plots a chosen variable into a nicely-formatted MATLAB figure. Now, we have a few tweaks and adjustments to make before we can call this 'publication quality.' Several issues are apparent: the line is too skinny, there are no labels or title, the x-axis has date numbers which speak nothing to the actual dates, and the limits on the x-axis are too arbitrary. Below is the fixed figure with the proper titles, axes, line width, and I even included scatter points to show the individual data points.
Figure 7 is a good example of a near publication-quality visual. It has a resolution of 600 dpi (sometimes even higher is recommended), it has formatted axes, visible font sizes, colorful visuals with thick lines and markers, and a clear and concise title that allows the reader to know exactly what they are viewing. Depending on the journal or form of publication, the formatting or color palette may need to be altered. Some journals do not use color in their plots, some do not want color backgrounds, some have specific formatting for units, etc. The figure above is not particularly interesting, however it meets publication expectations in visual representation. Below is a more detailed plot showing the relative behavior between temperature and humidity.
The figure shown above, Figure 8, is a perfect example of the quality you might see in scientific research journals. As I mentioned above, the format may not fit the targeted journal's specifications, however, the basics are there. Some alterations could be made to the axes colors, or the time format on the x-axis, or the legend, or figure background color, or the limits - all of these are possibilities that are specific to both user and journal. The code below outlines the function used to produce the figure above:
Now, with all the tools above, one should be capable of producing a plot similar to the one above. The only hard coding the user needs to do is call the function as such:
The function above needs four variables:
Station table
Station name
Station ID
Weather variables to plot
For example, if you wanted to plot the temperature and dew point for Austin-Bergstrom International Airport (KAUS), the function needs to be formatted as follows:
This produces the plot shown in Figure 9, below.
If you'd like the full MATLAB code for a seamless script, you can download it here (you can also copy and paste the code directly, here). From there it should handle the rest of the work!
Conclusions
When it comes to exhibiting professional work, the human eye determines the scale of expectation. That is precisely why I meticulously craft representations of my research. Peer reviewed journals scour through manuscripts, which is why it is important to maintain excellence when publishing figures. Above, I demonstrated how to create powerful visuals in MATLAB by utilizing the METAR database. The tutorial showed how to take publicly available weather data, import and parse it into MATLAB, and produce high quality figures. MATLAB is a robust tool that, when used properly, can be used to generate publication-worthy plots to represent data across all fields.
NOTE: The figures shown above may appear fuzzy. This is because the images are automatically resized and reformatted by my web provider. Therefore, the 600 dpi resolution is not showing honored here, however, rest assured that MATLAB is capable of exporting resolutions of 600 dpi and greater. For a high resolution example, see here.
See more in Programming: