Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If you think this task simply involves calculating mean and median averages, think again. Calculating the averages requires only a few lines of code and you're done. Now this task goes much deeper. Maybe not as deep as Lake Pend Oreille, but deep enough. Let's take a look at the way the Pend Oreille raw data is organized by going to the website. Then clicking the raw data link on the main page and it's deep more weather data that we want. And the environmental data for 2001 through to 2010 is stored in these large text files.
They range in size from about one and half megabytes. To this one for 2008, which is nearly seven megabytes. But if we look at the date for 2010, we can see that it comes to an end in late May. Thereafter, each measurement is in a separate file that contains just one day's data. So if we open this 2014 folder, we can see inside the series of other folders one for each day. And if we just open one at random we can see there are 11 files in there one for each different type of measurement.
We're interested in only three, air temperature, barometric pressure and wind speed. Getting and processing the data for one or two days can be done very quickly. But let's say someone requests a longer range. Even at several pages a second, the response is going to be too slow for a web service. And anyway the historical weather data is never going to change so it makes sense for a web service to cache it. So the approach that I took was to download the environmental data files for 2001 to 2010.
So let's go back to my editing program. And I've got them all stored here in the raw data folder. And I created a script to process them, to batch process them. Extracting the information that I wanted at inserting this into a database. Because the data is stored in a predictable format, PHP's file and string manipulation functions make it easy to crunch thousands of lines of data. But there's a nasty surprise. The data from 2007 is stored in a different format from proceeding years.
So this script needs to take account of that. The next task was to handle one day at a time. So I created a class which does exactly that. And then, ran it in a loop to get one year's data at a time. And this same class now updates the database automatically, once every 24 hours. The final part of the task was to create the web service. The great news is the PHP provides all the necessary tools. We can use PHP file manipulation functions to get the data from the Pend Oreille website.
With the help of string manipulation functions and loops, we can extract all the information we need and inserted directly into our database. Array functions and simple arithmetic calculate the mean and median averages. And building the web service uses a number of PHP features. Including grabbing variables from a query string, communicating with a database, and encoding adjacent output. There is a lot of coding involved, so let’s get on with it.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96920 Viewers
56 Video lessons · 110602 Viewers
71 Video lessons · 79434 Viewers
131 Video lessons · 38113 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.