Question: 1 . Write a function called get _ stations _ by _ loc. It takes two parameters: a target ( query ) location, and a
Write a function called getstationsbyloc. It takes two parameters: a target query location, and a radius in meters; and it returns a set of stations whose distance is within the radius to the target query location. The starter code provides a function called distance that allows you to calculate the distance in meters between two GPS coords. This function basically computes an arc length on earths sphere, and you do NOT need to modify this function. Specifically, your function should:
Take a target GPS location a element tuple and a radius as parameters.
Loop over the stations dictionary, for each station calculate its distance to the target by using the stations GPS coord, and calling the distance function; then compare the result with radius. All stations within radius to the target are collected into a set. At the end of the loop, return the set.
If there are no stations within the radius to the target, the function naturally returns an empty set.
Do NOT ask the user for any input. Do NOT print anything in your function.
Note that an empty set is created using set In contrast, creates an empty dictionary. Note stations is a global object so you can and should use it in this function. Below are example outputs:
printgetstationsbyloc # search radius m
Output set # empty set
printgetstationsbyloc # search radius km
Output AMHER 'UMCS 'UMEN 'UMCS
printgetstationsbyloc # search radius km
Output BOSTB 'AMHER', 'UMCS 'UMEN 'UMCS 'BOSTA', HBA 'BOSTH'
Because a set is unordered, your output may look different but should contain the same set of stations as the above. Tip: if you choose to use set comprehension, you can write this function in line.
Write a function called getavgtempC which takes a target query location and a radius, and returns the average temperature in Celsius calculated using weather stations within the radius to the target. This is a basic task of any weather service provider: as we dont have a weather station at every query point, to calculate the temperature, we need to find available weather stations within a certain radius to the query point, and use them to calculate the average temperature. To do so your function should:
Take a target GPS location a element tuple and a radius as parameters.
Call getstationsbyloc you implemented above to obtain a set of stations nearby; then loop over this set, for each station in this set, look up the datatoday dictionary to get its reported temperature value today. Calculate the average temperature in Celsius, and return it
Note that not all stations report data today; if they do they may not report temperatures. Also, some stations may report temperature in Fahrenheit tempF instead of Celsius, in which case you need to convert that to Celsius the formula is given below Your code needs to handle all these cases. In the case you cannot compute temperature either because Step returned an empty set, or none of the stations returned in Step exists in datatoday or none reports temperature this function should return None to indicate its unable to fulfill the request.
Do NOT ask the user for any input. Do NOT print anything in your function.
You can assume a station will NOT report both tempC and tempF at the same time. Note datatoday is a global object so you can and should use it in this function. Below are example outputs:
printgetavgtempC
Output None # search radius too small, resulting in no stations found
printgetavgtempC
Output
printgetavgtempC
Output
printgetavgtempC
Output None # none of the stations found reports temperature
Tip: use the in operator to check if a key exists in a dictionary. For example, name in datatoday tells you whether a station name exists in datatoday. Similarly, you can use in to check if the key 'tempC' or 'tempF' exists in the stations data. The formula to convert tempF to tempC is:
tempC tempF
If you notice any small floating point errors such as instead of you can ignore that. The autograder will account for such small errors when comparing your result with the reference.
pythoon language
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
