Question: 1 . Write a function called get _ stations _ by _ loc. It takes two parameters: a target ( query ) location, and a

1. Write a function called get_stations_by_loc. 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 2-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:
print(get_stations_by_loc((42.39,-72.53),1)) # search radius 1 m
Output -> set() # empty set
print(get_stations_by_loc((42.39,-72.53),1000)) # search radius 1 km
Output ->{'AMHER', 'UMCS2', 'UMEN1', 'UMCS1'}
print(get_stations_by_loc((42.39,-72.53),200000)) # search radius 200 km
Output ->{'BOSTB', 'AMHER', 'UMCS2', 'UMEN1', 'UMCS1', 'BOSTA', 'H2B4A', '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 1 line.
2. Write a function called get_avg_tempC 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 2-element tuple) and a radius as parameters.
Call get_stations_by_loc you implemented above to obtain a set of stations nearby; then loop over this set, for each station in this set, look up the data_today 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 2 returned an empty set, or none of the stations returned in Step 2 exists in data_today 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 data_today is a global object so you can (and should) use it in this function. Below are example outputs:
print(get_avg_tempC((42.39,-72.53),1))
Output -> None # search radius too small, resulting in no stations found
print(get_avg_tempC((42.39,-72.53),1000))
Output ->13.0
print(get_avg_tempC((42.214,-71.325),50000))
Output ->11.85
print(get_avg_tempC((42.356,-71.046),1000))
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 data_today tells you whether a station name exists in data_today. 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 -32)*(5/9)
If you notice any small floating point errors (such as 13.000001 instead of 13.0), 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!