
First, I will write how to download the data manually as the R code is not very efficient in extracting data for all variables. And we will choose “Weather Underground” as our source. I am using this source from a couple of years.
Step 1: We will first learn how to find the station code. Each city has one station installed by WU and it has a unique identity.
Visit Weather Underground website and search for the city. In my case it is Cape Town. Now go to “Histroy” tab and then you can see the code (circled below), it is “FACT”.
Step 2: Edit the below code accordingly. You need to edit only a few parameters. I make them bold what you need to replace. Please note when you try to download the data for a couple of years, it won’t come in one go. You have to manually adjust the dates and download 3-4 times depending on the length of timeframe. I think in one go it allows to download 370 rows, say 1 year.
https://www.wunderground.com/history/airport/FACT/2013/3/1/CustomHistory.html?dayend=31&monthend=12&yearend=2016&req_city=NA&req_state=NA&req_statename=NA&format=1
Here: FACT is the station id, 2013 is start year, 3 is start month and 1 is start day and then dayend, monthend and yearend are your end date. So you need to adjust the start date, end date will always be same for all iterations.
Step 3: Once you edit the link, paste it in your browser and open it. You will see the data like below image. Copy the data and paste into excel. Don’t worry about the format. Keep appending the data below (don’t paste header every time).
Step 4: Now that you have the data, we can format our excel file. Clt+A to select all the rows, go to Data>>Text to Columns (Alt+A+E) and select comma separator and press Enter. We are done! You have the data prepared for the given station id.
I am giving another option but this option will help you to download only 3 variables (max, mean and min temperature) for the given station id. Again station id you can find from step 1. I think the code is well commented and self-explanatory but if you find any trouble please comment below.
################################################################# ############ DOWNLOAD WEATHER DATA FROM WEATHER UNDERGROUND ##### # AUTHOR: DEEPESH SINGH # DATA: 12 APR 2017 # PURPOSE: TO DOWNLOAD WEAHTER INFORMATION AUTOMATICALLY # METHOD DETAILS: R CODE WITH BACKEND API AND MANUAL ################################################################ # First we install the package written by Ram Narasimhan <ramnarasimhan@gmail.com> install.packages("weatherData") library(weatherData) # User input StationID <- "FACT" # Now we make a variable 'years' and store all the years for which we need weather info years <- c(2010:2017) # Now we need to find the station ID which you can find from above method # Let's check if data is available for the given time period or not data_flag <- checkDataAvailabilityForDateRange(StationID, "2010-01-01", "2017-03-31") print(data_flag) # Here data_flag = 0 means data is not available for given internal and 1 means data is available # Now that we know we have data available for Cape Town from 2010-2017 we would like to download # Before applying this function (getWeatherForYear) read more about the package ?getWeatherForYear # If we make opt_detailed = T it will download all the columns and will take lot of time, hence manual # method is faster. opt_detailed = F will give only few columns which we need # Let's create an empty data frame getData <- data.frame() for (year in years){ tempData <- getWeatherForYear(StationID, year, opt_detailed = F) # NEVER MAKE OPT_DETAILED = T getData <- rbind(getData, tempData) } View(getData) write.csv(getData,paste("WeatherData_",StationID,".csv", sep=""),row.names = F) # Refrence # https://cran.r-project.org/web/packages/weatherData/weatherData.pdf
Thank you for reading my article. Please like and share if this article helped you. Also, don’t forget to comment below your thoughts.
Sir above mention URL code is not working properly please help me to retrieve weather data. Thank you
Do you have any idea about how to solve this issue or did they just change the url
It doesn’t work now. How to revise it and make it work again?
It doesn’t work now. How to revise it and make it work again? Please.
The routine that you coded, how do you run that? Is it JavaScript or what?
The link seems to be blocked, is there a way around it? BLocked by akami
the link seems to be blocked, is there a way around it? Blocked by Akami
The weatherData package working with XML structure and Weatherunderground change it. Take a look at the weatherData discussion on https://github.com/Ram-N/weatherData/issues/39
Thanks Eloy for your comment. I think the package is still not working. Though we can use other technique like web-scraping to get the data.
Hi
Can you help please, I would like extract the minimum temperature field from approx 100 wunderground stations and import to excel. Need to grab data at 9am each day.
Would only need station name and temperature
Example
Meopham 4.5c
Many thanks
Bruce stevens
Hi Bruce,
If you are a developer try their API. I will write another article in next few weeks.
Did you already post that article you talked about? Do you have any link for me?
Hi, you need a premium account for that. I don’t know if they are providing any trail. Please check on their website.
Also, you may want to try: https://cloud1.visualcrossing.com/weather/weather-data-services.html
We have just released this to meet the needs of folks left behind by some of the other solutions that don’t offer bulk downloads and were not all that accurate. Let us know what you think and how we can improve.
I think this is a really good article. You make this information interesting and engaging.