Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

id latitude longitude city label yr1970 yr1975 yr1980 yr1985 yr1990 yr1995 yr200

ID: 3868821 • Letter: I

Question


id latitude longitude city label yr1970 yr1975 yr1980 yr1985 yr1990 yr1995 yr2000 yr2005 yr2010
1 35.6832085 139.8089447 Tokyo Tokyo 23.3 26.61 28.55 30.3 32.53 33.59 34.45 35.62 36.67
2 28.6082802 77.2008133 New_Delhi New Delhi 3.53 4.43 5.56 7.33 9.73 12.41 15.73 19.49 22.16
3 -23.5628395 -46.6546402 Sao_Paulo Sao Paulo 7.62 9.61 12.09 13.39 14.78 15.95 17.1 18.65 20.26
4 18.93013 72.8300934 Mumbai Mumbai 5.81 7.08 8.66 10.34 12.31 14.11 16.09 18.21 20.04
5 19.4319592 -99.1331635 Mexico_City Mexico City 8.77 10.69 13.01 14.11 15.31 16.81 18.02 18.73 19.46
6 40.7820015 -73.8327026 New_York New York 16.19 15.88 15.6 15.83 16.09 16.94 17.85 18.73 19.43
7 31.2551594 121.4747009 Shanghai Shanghai 6.04 5.63 5.97 6.85 7.82 10.17 13.22 15.18 16.58
8 22.5650902 88.3707809 Kolkata Kolkata 6.93 7.89 9.03 9.95 10.89 11.92 13.06 14.28 15.55
9 23.7132301 90.3995667 Dhaka Dhaka 0 0 0 4.66 6.62 8.33 10.28 12.56 14.65
10 24.9267101 67.0343704 Karachi Karachi 0 3.99 5.05 6.03 7.15 8.47 10.02 11.62 13.12
11 -34.6085091 -58.3734894 Buenos_Aires Buenos Aires 8.1 8.74 9.42 9.96 10.51 11.15 11.85 12.55 13.07
12 34.0534897 -118.2453232 Los_Angeles Los Angeles 8.38 8.93 9.51 10.18 10.88 11.34 11.81 12.3 12.76
13 39.9065704 116.3876495 Beijing Beijing 4.43 4.83 5.37 6.02 6.79 8.14 9.76 11.45 12.39
14 -22.9121609 -43.1750107 Rio_de_Janeiro Rio de Janeiro 6.64 7.56 8.58 9.09 9.59 10.17 10.8 11.37 11.95
15 14.58743 120.9836807 Manila Manila 3.53 5 5.95 6.89 7.97 9.4 9.96 10.76 11.63
16 35.0763016 133.5659943 Osaka_Kobe Osaka-Kobe 9.41 9.84 9.99 10.35 11.04 11.05 11.17 11.26 11.34
17 37.0051003 -89.1762695 Cairo Cairo 5.58 6.45 7.35 8.33 9.06 9.71 10.17 10.56 11
18 6.45471 3.3887601 Lagos Lagos 0 0 0 0 0 5.97 7.23 8.77 10.58
19 55.7569695 37.6150208 Moscow Moscow 7.11 7.62 8.14 8.58 8.99 9.2 10 10.42 10.55
20 41.0409584 28.9857998 Istanbul Istanbul 0 0 4.4 5.41 6.55 7.67 8.74 9.71 10.52
21 48.8569298 2.3412001 Paris Paris 8.35 8.56 8.67 8.96 9.33 9.51 9.74 10.11 10.49
22 37.5150185 127.0164719 Seoul Seoul 5.31 6.81 8.26 9.55 10.54 10.26 9.92 9.83 9.77
23 29.5509491 106.5193481 Chongqing Chongqing 0 0 0 0 0 0 0 0 9.4
24 -6.1714902 106.8275223 Jakarta Jakarta 3.92 4.81 5.98 7.01 8.18 8.32 8.39 8.79 9.21
25 41.8842506 -87.6324463 Chicago Chicago 7.11 7.16 7.22 7.29 7.37 7.84 8.33 8.82 9.2
26 22.5562191 114.1187897 Shenzhen Shenzhen 0 0 0 0 0 0 0 7.93 9.01
27 -12.0561399 -77.0268021 Lima Lima 0 3.7 4.44 5.12 5.84 6.58 7.29 8.08 8.94
28 23.1133709 113.2552338 Guangzhou Guangzhou 0 0 0 0 0 0 7.33 8.16 8.88
29 -4.3122201 15.2895298 Kinshasa Kinshasa 0 0 0 0 0 0 0 0 8.75
30 51.5064316 -0.12719 London London 7.51 7.55 7.66 7.67 7.65 7.91 8.22 8.51 8.63
31 39.1449394 117.2005692 Tianjin Tianjin 3.32 0 0 0 0 0 6.67 7.28 0
32 22.3361568 114.1869659 Hong_Kong Hong Kong 3.46 3.94 4.61 5.07 5.68 6.21 6.67 0 0
33 35.6895409 51.4146843 Tehran Tehran 3.29 4.27 5.08 5.84 6.36 6.69 6.88 0 0
34 13.0503302 80.188591 Chennai Chennai 0 0 0 4.75 5.34 5.84 0 0 0
35 13.7533503 100.5048294 Bangkok Bangkok 0 3.84 4.72 5.28 5.89 6.11 0 0 0
36 59.9331818 30.3060303 Saint_Petersburg St. Petersburg 3.98 4.33 4.64 4.84 4.99 0 0 0 0
37 40.4514008 -3.5631499 Madrid Madrid 3.52 3.89 4.25 0 0 0 0 0 0
38 39.9522781 -75.1624527 Philadelphia Philadelphia 4.4 4.47 4.54 0 0 0 0 0 0
39 42.3316803 -83.0479202 Detroit Detroit 3.97 3.89 0 0 0 0 0 0 0

!! Please comment your code!!! Tasks Group I The file CityPop.esv (CityPop.esv) contains 39 cities with their coordinates and populations (in millions) over several years in CSV (comma -scparated valucs) format. If you open this file in Notepad or Notepad++, you will see 40 lines of text with values separated by commas. The first line of the file contains the column names, and the other lines cach pertain Note that all of the column names begin with a letter; this isn't a big deal here, but many programs run into problems when identifying a value using a number, so this is generally good practice. If you open a CSV file in Excel, you will see a table with cach line shown as a row and each comma separated value in its own cell. Excel detects the CSV format and knows to automatically convert newline characters into ow separations and commas into column to to a different city Tasks For the following tasks, you can assume all coordinates are given in valid formats Task 1 Create a python script that reads in the contents of CityPop.csv and stores the data in a container Task 2 will expect that your program can find cities by name within the containcr, so think about that as you set up your data container (see Strategies session below). Briefly describe how you store the data in comments Task 2: Extend Task I so that the user can query the population of a city in a particular year. To Do: Prompt the user for a city and a year (note that the format of year is 'yr1970'). Test their input to make sure it is valid and corresponds to values within the file. Task 3: Extend Task 1 so that the program can calculate the distance between two cities in the file specified by the user To Do: Prompt the user for the city names, find the corresponding latitude and longitude valucs in the file contents (if they exist) and utilize your great circle distance function to calculate the distance Task 4: (this is very important part) Extend Task 1 so that your program calculates the population change over two years specified by the user for all the cities.

Explanation / Answer

import os import math class City: def __init__(self, CityName='none', CityLabel='none', Lat=-999, Lon=-999, Pop={}): self.cityName = CityName self.label = CityLabel self.lat = Lat self.lon = Lon self.pop = Pop # Method to calculate Great Circle Distance between two cities. As it is written, 'self' and 'othercity' must be entered as Cities[i],Cities[j] def printDistance(self, othercity): # change coordinates to radians: latitude1 = math.radians(self.lat) longitude1 = math.radians(self.lon) latitude2 = math.radians(othercity.lat) longitude2 = math.radians(othercity.lon) # create variables for great circle route slat1 = math.sin(latitude1) slat2 = math.sin(latitude2) clat1 = math.cos(latitude1) clat2 = math.cos(latitude2) londif = longitude1 - longitude2 clondif = math.cos(londif) # here I use the variables defined above to calculate the great circle distance in radians, relying on the order of operations to do things correctly d = math.acos(slat1 * slat2 + clat1 * clat2 * clondif) # convert raidans to kilometers distance = 6300 * d # return the distance to the user print 'The distance between %s and %s is %.2f kilometers.' % (self.label, othercity.label, distance) # method to calculate population change in one city over a set amount of time. . As it is written, 'self' and 'othercity' must be entered as Cities[i],Cities[j] # parameters include the city, and the two years to be compared def printPopChange(self, year1, year2): # population dif calculated popDif = self.pop[year1] - self.pop[year2] # return population difference print 'The population in %s changed by %f million between %d and %d' % (self.label, popDif, year1, year2) # ******Script******# # protect the script in a try/except function for file formatting issues try: # set the working directory to where I stored the file based on user input (I don't want to hard-code a path in my program) directory = raw_input('Enter the path of the directory you want to work in> ') os.chdir(directory) # identify the file you want to work with. setFile = raw_input('Enter the csv file you want to open, including its extension> ') # open the file as a read-only. CSV files are text files. cityFile = open(setFile, 'rt') # read the csv file lines. We're good to check the formatting of the CSV doc cityList = cityFile.readlines() Cities = [] cityFile.close() print 'Environment set' print # now that the file is open, check that the formatting is correct try: # read the first line of the csv, testing that the headers are correct. EXPLAIN print 'Testing .csv format...' # create a list of the headers, split by commas cityFile2 = open(setFile, 'rt') line1 = cityFile2.readline() line1b = line1.replace(',', ' ') headings = line1b.split() # create a tuple of the required headings for the script to run testHeadings = ( 'id', 'latitude', 'longitude', 'city', 'label', 'yr1970', 'yr1975', 'yr1980', 'yr1985', 'yr1990', 'yr1995', 'yr2000', 'yr2005', 'yr2010') # Go through each list, comparing the required headings to the headings in the CSV file. Crash the program if they don't match, providing an explanation of what columns are needed. # create count variable to use as a requirement to start the next part of the program. count = 0 for c in range(len(headings)): real = headings[c] test = testHeadings[c] if real == test: count += 1 else: print 'Column', real, 'should be', test f = 1 / 0 print '.csv formatted correctly.' print cityFile2.close() # once you've confirmed the 14 columns, procide (through while loop): while count == 14: # loop through the csv files, one line at a time for city in cityList: # split the list of one line (one city) by commas cityValues = city.split(',') # assign variables for later use. Nested under if function to eliminate first row of headings (would have 'id' as the first column). I know there's a csv module that fixes this, but this works with the tools I know for now. if cityValues[0] != 'id': try: # test each lat/lon value for a proper range; crash the program if not, with directions. if -90