Answer To: Question 1: Read the given CSV file and create corresponding ship objects. Good News: This portion...
Pratap answered on Jul 30 2021
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
- Summer 2021 - Assignment 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### >> Put your name here >> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The Scenario: \n",
"The date is November 17, 2020. Hurricane Iota has just slammed into an already storm-ravaged Nicaragua as a Category 4 storm. The landfall was just 15 miles south of where Hurricane Eta hit two weeks earlier. Eta left thousands displaced and dozens killed in Central America.\n",
"\n",
"The storm has created an urgent need for humanitarian aid in the city of Puerto Cabezas. Unfortunately, the storm has destroyed the local airport and blocked all incoming roads, making it accessible only by sea.\n",
"\n",
"Fortunately, there are a number of ships in the vicinity, each of which has a varied inventory of cargo. The most relevant cargo are (1) food, (2) medicine, (3) supplies, (4) equipment, and (5) fuel. Not every ship has every commodity, and the quantities of each are different.\n",
"\n",
"The file ``nearby_ships_20201117.csv`` contains information about these ships, specifically:\n",
"* ship name\n",
"* current position (lat, lon)\n",
"* max speed (in knots)\n",
"* cargo contents\n",
"\n",
"**Puerto Cabezas** is located at (14.01996° N, 83.38492° W).\n",
"\n",
"![midterm_map_1a_small.png](attachment:midterm_map_1a_small.png)\n",
"\n",
"The **start time** (at which this file was generated, and for the purposes of this assignment) is **1200 GMT Tuesday 17 November 2020**. (Note: \"start\" is NOT the current time at which you are taking this exam!)`\n",
"\n",
"You will write Python code to answer several questions about the overall scenario and the capability of these ships to provide relief."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The Code:** You have been provided with two Python classes that you will use to perform your analysis.\n",
"* ``Coord`` -- exactly as used in Lab 1 Assignment, defined below\n",
"* ``Ship`` -- a new class, defined below\n",
"\n",
"**Note: you are not allowed to use the Pandas library on this exam.**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Here is the first of two class definitions that you will use.\n",
"# This code is taken directly from the Lab 1 assignement.\n",
"# You do NOT need to change anything here; just load it.\n",
"import math \n",
"\n",
"\n",
"class Coord:\n",
" '''An improved class to represent lat/lon values.'''\n",
" \n",
" def __init__(self,lat,lon):\n",
" self.lat = float(lat) # make sure it's a float\n",
" self.lon = float(lon)\n",
" \n",
" # Follows the specification described in the Aviation Formulary v1.46\n",
" # by Ed Williams (originally at http://williams.best.vwh.net/avform.htm)\n",
" def dist_to(self, other):\n",
" lat1 = Coord.deg2rad(self.lat)\n",
" lon1 = Coord.deg2rad(self.lon)\n",
" lat2 = Coord.deg2rad(other.lat)\n",
" lon2 = Coord.deg2rad(other.lon)\n",
" \n",
" # there are two implementations of this function.\n",
" # implementation #1:\n",
" #dist_rad = math.acos(math.sin(lat1) * math.sin(lat2) \n",
" # + math.cos(lat1) * math.cos(lat2) * math.cos(lon1-lon2))\n",
"\n",
" # implementation #2: (less subject to numerical error for short distances)\n",
" dist_rad=2*math.asin(math.sqrt((math.sin((lat1-lat2)/2))**2 +\n",
" math.cos(lat1)*math.cos(lat2)*(math.sin((lon1-lon2)/2))**2))\n",
"\n",
" return Coord.rad2nm(dist_rad)\n",
" \n",
"\n",
" def __str__(self):\n",
" return \"(%f,%f)\" % (self.lat,self.lon)\n",
" \n",
" def __repr__(self):\n",
" return \"Coord(%f,%f)\" % (self.lat,self.lon) \n",
"\n",
" def deg2rad(degrees):\n",
" '''Converts degrees (in decimal) to radians.'''\n",
" return (math.pi/180)*degrees\n",
"\n",
" def rad2nm(radians):\n",
" '''Converts a distance in radians to a distance in nautical miles.'''\n",
" return ((180*60)/math.pi)*radians\n",
" \n",
"# End of class Coord"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Here is the second of two class definitions for this exam.\n",
"# You may add additional methods to this class (if you wish), \n",
"# but YOU SHOULD NOT CHANGE THE init METHOD.\n",
"\n",
"import datetime\n",
"\n",
"class Ship:\n",
" '''A simple class to represent a ship and its cargo.'''\n",
" \n",
" def __init__(self,name,loc,max_speed,cargo_items):\n",
" '''builds and returns a complete Ship object'''\n",
" self.name = name # assumed a String\n",
" self.loc = loc # assumed a Coordinate\n",
" self.max_speed = max_speed # assumed a float\n",
" \n",
" self.cargo = {} # a dictionary\n",
" # cargo_items is assumed to be a list of strings, each of the form \"key=value\"\n",
" # where \"key\" is a string, and \"value\" can be converted to a numerical value\n",
" for item in cargo_items:\n",
" if item.strip(): # if something other than whitespace\n",
" key,value = item.split('=')\n",
" self.cargo[key] = float(value)\n",
" # end of __init__\n",
"\n",
" def get_cargo(self,cargo_name):\n",
" '''returns the quantity of named cargo from dictionary if exists, or None otherwise'''\n",
" return self.cargo.get(cargo_name)\n",
" \n",
" def __str__(self):\n",
" return \"Ship %s %s %.1f %s\" % (self.name,self.loc, self.max_speed, self.cargo)\n",
"\n",
"# end of class Ship"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Question 1: Read the given CSV file and create corresponding ship objects. \n",
"\n",
"#### Good News: This portion is already working to read the file and create ``Ship`` objects, although the objects are not being stored (yet) in any data structure.\n",
"\n",
"#### [2 Points] Modfiy this code so that these ``Ship`` objects are stored in a dictionary that you can use to manipulate them. In the dictionary, use the ``shipname`` as the key and the ``Ship`` object as the value."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"f = open(\"nearby_ships_20201117.csv\",\"r\")\n",
"reader = csv.reader(f)\n",
"\n",
"ship_dict = dict()\n",
"for row in reader:\n",
" shipname = row[0]\n",
" coord = Coord(row[1],row[2])\n",
" speed = float(row[3])\n",
" next_ship = Ship(shipname,coord,speed,row[4:])\n",
" ship_dict[shipname] = next_ship\n",
" \n",
"f.close()"
]
},
{
"cell_type": "markdown",
...