from datatool import datatool import numpy as np import math dt = datatool() dt.load_csv('bluefin_whale_data.csv') dt.tilemap_attach_col_lat_long('location-lat', 'location-long', 'individual-local-identifier') # First, the min and max in each direction. Lat is at index 3, long is 4, id is 11 D = dt.get_data_as_array() min_lat = D[0,4] max_lat = D[0,4] min_long = D[0,3] max_long = D[0,3] for i in range(D.shape[0]): # COPY YOUR CODE FROM exercise 3.59 here print('min-lat=', min_lat, 'max-lat=', max_lat) print('min-long=', min_long, 'max-long=', max_long) # COPY YOUR CODE FROM 3.59 to round up/down as directed there. # Round min down, round max up print('min-lat=', min_lat, 'max-lat=', max_lat) print('min-long=', min_long, 'max-long=', max_long) # WRITE CODE in this function to determine whether the point # specified by lat,lon is inside the half-box whose bottom # left corner is box_lat, box_lon. Return True or False accordingly. def within(box_lat, box_lon, lat, lon): # WRITE CODE in this function to count the number of data points # from the 2D array D whose lat-lon's are inside the half-box # whose bottom left corner is box_lat, box_lon. Hint: use within() def count_occurences(box_lat, box_lon): # Next, run through the boxes num_boxes = 0 max_count = 0 best_lat = 0 best_lon = 0 lat = min_lat while lat < max_lat: lon = min_long while lon < max_long: # Count occurrences within lat to lat+0.5, lon to lon+0.5 num_boxes += 1 count = count_occurences(lat, lon) if (count > max_count): # Update best so far, if needed max_count = count best_lat = lat best_lon = lon lon += 0.5 # Inner-loop lat += 0.5 # Outer-loop print('After searching', num_boxes, 'boxes: ') print(' => best box: lat', best_lat, ':', best_lat+0.5, ' lon', best_lon, ':', best_lon+0.5, ' count =', max_count) # Should print # After searching 360 boxes: # => best box: lat 33.5 : 34.0 lon -119.0 : -118.5 count= 2084