{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Assignment #2.ipynb","provenance":[{"file_id":"1SkKsXzL6d7LhRPXbkwRZGfV69-FiNFbb","timestamp":1602994096582}],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"n4l8wAHX6qp_"},"source":["# Assignment \\#2\n","\n","**Due:** Saturday, October 31 at 11:59 pm PT\n","\n","**Objective:**\n","This assignment will give you experience using lists, logical operations, control flow (*for* loops and *if* statements), and NumPy arrays and functions.\n","\n","**Instructions:**\n","1. This version of the assignment cannot be edited. To save an editable version, copy this Colab file to your individual class Google Drive folder (\"OCEAN 215 - Autumn '20 - {your name}\") by right clicking on this file and selecting \"Move to\".\n","2. Open the version you copied.\n","3. Complete the assignment by writing and executing text and code cells as specified. **To complete this assignment, you do not need any material beyond Lesson #7.** However, you may use material from beyond Lesson #7 if you wish as long as it has been discussed in a lesson or class, and is not prohibited in the question.\n","4. When you're finished and are ready to submit the assignment, simply save the Colab file (\"File\" menu –> \"Save\") before the deadline, close the file, and keep it in your individual class Google Drive folder.\n","5. If you need more time, please see the section \"Late work policy\" in the syllabus for details.\n","\n","**Honor code:** In the space below, you can acknowledge and describe any assistance you've received on this assignment, whether that was from an instructor, classmate (either directly or on Piazza), and/or online resources other than official Python documentation websites like docs.python.org or numpy.org. Alternatively, if you prefer, you may acknowledge assistance at the relevant point(s) in your code using a Python comment (#). You do not have to acknowledge OCEAN 215 class or lesson resources."]},{"cell_type":"markdown","metadata":{"id":"uiNuT-h96t-D"},"source":["*Acknowledge assistance here:*"]},{"cell_type":"markdown","metadata":{"id":"TUyvldHiduU3"},"source":["## Question 1 (5 points)\n","\n","### ***For* loops and list comprehensions**\n","\n","*Useful resources:* Lesson #3 on list functions and indexing; Lesson #4 on *for* loops and list comprehensions\n","\n","For this question, you are given a list called *decimals* that contains 5 floating-point numbers:\n","\n","* decimals = \\[0.0003, 0.2342, 0.5629, 0.6376, 0.9731]\n","\n","Please create a new list in which these decimal fractions have been first converted to percents (i.e. between 0-100%), then rounded to 1 decimal place, and finally converted to strings with a percent sign (%) on the end. In other words, your final result should be the following list:\n","\n","* percents = \\['0.0%', '23.4%', '56.3%', '63.8%', '97.3%']\n","\n","This question has two parts, which should both result in a list identical to *percents* above:\n","\n","1. Create this new list using a *for* loop. Print the list.\n","2. Create this new list using a list comprehension. Print the list.\n","\n","To round a number, you should use Python's *round()* function, which takes two arguments: the number, and the number of decimal places to round to. For example, *round(23.4173, 2)* gives 23.42 as the result.\n","\n","\n"]},{"cell_type":"code","metadata":{"id":"IMtpMWuFeC4k"},"source":["# Keep this starting line of code:\n","decimals = [0.0003,0.2342,0.5629,0.6376,0.9731]\n","\n","# Write your code for Part 1 here:\n","\n","\n","# Write your code for Part 2 here:\n","\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"RWMAcoSrHbFB"},"source":["## Question 2 (15 points)\n","\n","![picture](https://drive.google.com/uc?export=view&id=1Vef5rWSHWuhFOO-jogERwF-wLMK62UE6)\n","*Image: Bathymetry of the seafloor around Axial Seamount (source: Wikipedia, originally from NOAA/PMEL).*\n","\n","### **Axial Seamount eruption**\n","\n","*Useful resources:* Lesson #3 on list indexing; Lesson #4 on *for* loops and *if* statements; Lesson #7 on basic plots\n","\n","During the eruption of a submarine volcano, the seafloor deflates rapidly as magma is expelled into the ocean. Bottom pressure gauges use pressure to measure depth (as you might recall from Assignment #1, pressure is nearly equivalent to depth in the ocean), and so these gauges can detect this deflation. After an eruption, the seafloor slowly inflates and rises as magma accumulates in preparation for the next eruption.\n","\n","Axial Seamount is an active submarine volcano offshore of the Oregon coast. It last erupted in 2015. Below, we provide a time series of monthly bottom pressure data released by the [Ocean Observatories Initiative \\(OOI\\)](https://dataexplorer.oceanobservatories.org/#default-data/4) from August 15, 2014 to December 15, 2019, with measurements taken on the 15th day of each month. Times are specified in \"fractional years.\" For example, July 1, 2020 is approximately 2020.5 in fractional years.\n","\n","**Please write only one *for* loop for this entire question. Do not use NumPy.** Organize your code for Parts 1-3 at the top, followed by the *print()* statements for your answers, followed by your code for the Part 4 plot at the bottom. Write code to answer the following questions:\n","\n","1. The seafloor dropped more than 2.0 m during the 2015 eruption. This can be used to identify when the eruption happened. Answer these questions using a *print()* statement, e.g. \"Part 1a: The seafloor drop during the eruption was \\ m.\"\n","> **a.** Calculate the actual seafloor drop during the eruption. Round to 1 decimal place (see Question 1 for details on *round()*).\n",">\n","> **b.** Identify the two measurement times (in fractional years) immediately before and after the eruption.\n",">\n","> **c.** Find the seafloor depth measurement immediately before the eruption. Round to 1 decimal place.\n",">\n","\n","2. The seafloor has been steadily inflating since the 2015 eruption. Express answers to the following questions in meters and fractional years, respectively, each rounded to 1 decimal place:\n","> **a.** How much has the seafloor inflated between the measurement immediately after the eruption and the most recent measurement in this data?\n",">\n","> **b.** How much time has elapsed between these two measurements? \n",">\n","\n","3. Calculate the approximate rate of seafloor inflation using the values from Part 2. Express your answer in units of centimeters per month, rounded to 1 decimal place.\n","\n","4. Use Matplotlib to create a plot of the data with time on the x-axis and depth on the y-axis. Shallower depths should point in the positive y-direction (you will need to search the [Matplotlib API](https://matplotlib.org/3.3.1/api/axes_api.html) to find out how to do this). **Format and label your plot such that it looks as similar to this plot as possible**:\n","\n","![picture](https://drive.google.com/uc?export=view&id=17LjK79LlBLUmqjkg6CDXjWhWNzTC8c8R)\n","\n"]},{"cell_type":"code","metadata":{"id":"M7ZjQoVLlSSR"},"source":["# Keep these lines of code:\n","#\n","# Axial Seamount Central Caldera bottom pressure gauge data\n","# (times in fractional years; depths in meters)\n","times = [2014.625,2014.708,2014.792,2014.875,2014.958,2015.042,2015.125,2015.208,2015.292,2015.375,2015.458,2015.542,2015.625,2015.708,2015.792,2015.875,2015.958,2016.042,2016.125,2016.208,2016.292,2016.375,2016.458,2016.542,2016.625,2016.708,2016.792,2016.875,2016.958,2017.042,2017.125,2017.208,2017.292,2017.375,2017.458,2017.542,2017.625,2017.708,2017.792,2017.875,2017.958,2018.042,2018.125,2018.208,2018.292,2018.375,2018.458,2018.542,2018.625,2018.708,2018.792,2018.875,2018.958,2019.042,2019.125,2019.208,2019.292,2019.375,2019.458,2019.542,2019.625,2019.708,2019.792,2019.875,2019.958]\n","depths = [1510.24,1510.19,1510.14,1510.08,1510.04,1509.96,1509.93,1509.87,1509.83,1512.23,1512.11,1512.04,1511.96,1511.89,1511.79,1511.7,1511.64,1511.57,1511.54,1511.5,1511.47,1511.47,1511.4,1511.35,1511.33,1511.32,1511.31,1511.26,1511.21,1511.19,1511.14,1511.12,1511.05,1511.0,1511.06,1511.04,1511.01,1510.98,1510.93,1510.87,1510.87,1510.91,1510.86,1510.77,1510.75,1510.7,1510.67,1510.68,1510.64,1510.59,1510.56,1510.54,1510.45,1510.51,1510.41,1510.42,1510.39,1510.37,1510.4,1510.42,1510.41,1510.38,1510.33,1510.3,1510.26]\n","\n","# Your code for Parts 1-3:\n","\n","\n","# Your print() statements for Parts 1-3:\n","\n","\n","# Your code to make the plot for Part 4:\n","\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"z1PRR28O7z96"},"source":["## Question 3 (20 points)\n","\n","![picture](https://drive.google.com/uc?export=view&id=1Ey--pwPCTMfTtOm2HQO6okHCtNZBxqPY)\n","*Image: The boundary between air and sea as seen from a research vessel.*\n","\n","### **Air-sea heat fluxes**\n","\n","*Useful resources:* Lesson #2 on mathematical operations, Lesson #3 on list indexing; Lesson #4 on *for* loops and *if* statements; Lesson #5 on NumPy arrays and functions\n","\n","**Background information:**\n","\n","A body of water will lose heat when exposed to cooler air, and it will lose heat faster when there is airflow over it. Think about how you might cool a cup of coffee or tea by blowing on it. The ocean behaves similarly.\n","\n","\"Air-sea heat flux\" refers to the rate of this exchange of heat between the ocean surface and atmosphere. When the ocean is warmer than the air, the ocean loses heat. When the ocean is cooler than the air, the ocean gains heat. This exchange happens more rapidly when wind speeds are higher. Here is the equation that describes this heat flux. Notice that this rate of heat exchange, $H$, becomes larger if the wind speed, $U_{10}$, becomes larger:\n","\n","$H = \\rho_a \\cdot c_p \\cdot C_h \\cdot U_{10} \\cdot (T_a - T_s)$\n","\n","* $H$ = air-to-sea heat flux (units: Watts/m$^2$, or W/m$^2$, where positive values represent heat moving from the air into the ocean)\n","* $\\rho_a$ = air density (1.2 kg/m$^3$)\n","* $c_p$ = heat capacity of air (1004 J/(kg °C))\n","* $C_h$ = transfer coefficient (0.0011 \\[unitless])\n","* $U_{10}$ = wind speed 10 m above the ocean (units: m/s)\n","* $T_a$ = air temperature 10 m above the ocean (units: °C)\n","* $T_s$ = sea surface temperature (units: °C)\n","\n","The Irminger Sea in the North Atlantic, near Greenland, experiences some of the most extreme air-sea heat fluxes in the world. Below, we provide three lists containing monthly-average data for $T_a$, $T_s$, and $U_{10}$ from 2018 in the Irminger Sea. Here is a plot showing these monthly data:\n","\n","![picture](https://drive.google.com/uc?export=view&id=1VMcZg-FZCakBvU-AFBoISgHWAMCQsfUP)\n","\n","**Questions:**\n","\n","Answer each question below using a *print()* statement, e.g. \"Part 1: The air-sea heat flux in October is \\ W/m^2.\" For these parts, you may use *for* loops, but **not NumPy**:\n","\n","1. What is the air-sea heat flux, $H$, in October? Round your answer to 1 decimal place.\n","2. Is the ocean gaining or losing heat in October? Don't do any calculations for this part; just give your answer in a *print()* statement.\n","3. Use a *for* loop to calculate $H$ for each month, and round each value to 1 decimal place. These values should be stored in a list. Print the list, e.g. \"Part 3: The monthly air-sea heat fluxes are: \\{list of $H$ values} .\"\n","4. During which month is ocean heat loss at a maximum? Identify and print this month using **code only**. In other words, use a *for* loop — don't identify the month simply by looking at the data, and don't type the name of any months for this part.\n","5. What are the two reasons that ocean heat loss was greatest in this month? You may wish to consult the plot and/or the $H$ equation above. There is no need to do calculations for this part; just type a short answer in a *print()* statement.\n","6. Use a *for* loop to calculate the average $H$ value in 2018. Round your answer to 1 decimal place.\n","\n","For these next calculations, you may use NumPy, but **not *for* loops**:\n","\n","7. Convert the lists of $T_a$, $T_s$, and $U_{10}$ data to NumPy arrays. Use new variable names for these arrays. There is no need to print anything for this part.\n","8. Calculate $H$ using the NumPy arrays from Part 7. Round the NumPy array of $H$ values to 1 decimal place using NumPy's [*.round()*](https://numpy.org/doc/stable/reference/generated/numpy.round_.html) function. Print the result, which should be a new NumPy array with a variable name different from the $H$ list you created in Part 3. If you did everything correctly, the numbers in this array will be the same as the numbers in the $H$ list from Part 3.\n","9. Answer the same question from Part 4 using the NumPy array of $H$ values from Part 8. For this, use either NumPy's [*argmax()*](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html) or [*argmin()*](https://numpy.org/doc/stable/reference/generated/numpy.argmin.html) function. As before, identify and print this month using code only. You should get the same answer as you did in Part 4.\n","10. Calculate the average $H$ value in 2018 using the NumPy array of $H$ values from Part 8. For this, use NumPy's [*mean()*](https://numpy.org/doc/stable/reference/generated/numpy.mean.html) function. Round your answer to 1 decimal place. You should get the same answer as you did in Part 6."]},{"cell_type":"code","metadata":{"id":"H71IybJQ73ab"},"source":["# Keep these lines of code:\n","#\n","# Monthly ERA-Interim data for the North Atlantic (Irminger Sea at 59.5°N, 40.0°W) from 2018\n","months = ['January','February','March','April','May','June','July','August','September','October','November','December']\n","U_10 = [12.91,14.63,8.58,9.32,10.88,7.73,7.15,8.09,8.49,9.77,11.34,9.14]\n","T_a = [0.55,-1.95,1.52,2.81,2.71,5.13,7.06,8.19,7.08,4.56,3.68,2.61]\n","T_s = [4.18,3.86,3.72,3.91,4.35,5.29,6.76,8.21,7.37,6.24,5.09,4.42]\n","\n","# Your code for Part 1:\n","\n","\n","# Your code for Part 3:\n","\n","\n","# Your code for Part 4:\n","\n","\n","# Your code for Part 6:\n","\n","\n","# Your code for Part 7:\n","\n","\n","# Your code for Part 8:\n","\n","\n","# Your code for Part 9:\n","\n","\n","# Your code for Part 10:\n","\n","\n","# Your print() statements for Parts 1-6 and 8-10:\n","\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"q0LTX4Z4wMjd"},"source":["## Question 4 (10 points)\n","\n","![picture](https://www.awi.de/fileadmin/_processed_/9/9/csm_12072012_Polarstern_FMehrtens_001_5b8b956779.jpg)\n","*Image: The research vessel R/V Polarstern.*\n","\n","### **Thermosalinograph time series**\n","*Useful resources:* Lesson #5 on NumPy arrays and functions, Lesson #6 on multidimensional arrays and datetime objects\n","\n","During an oceanographic cruise, research vessels will often have an instrument called a thermosalinograph taking temperature measurements throughout the duration of the cruise. To get the most accurate picture of the surrounding conditions, there are usually 2 different temperature sensors in the water, situated at different depths. For this problem you are provided two NumPy arrays of sample data from a cruise in 2003 aboard the [R/V Polarstern](https://www.awi.de/en/expedition/ships/polarstern.html).\n","\n","
\n","\n","**Data arrays:**\n","\n","* **T_data** contains 4 columns (longitude, latitude, temperature [˚C] at 5 meters, and temperature [˚C] at 11 meters)\n","\n","* **time_data** contains a single dimension with strings containing the date/time information for the temperature measurements with the format %Y-%m-%d %H:%M:%S\n","\n","
\n","\n","**Questions:**\n","\n","Answer each question below using a *print()* statement, e.g. \"Part 1a: The measurements were taken over \\ hours.\"\n","\n","1. Create a new 1-dimensional array with the date strings in the **time_data** array converted into datetime objects. Answer the following:\n","> **a.** Over how many hours were these measurements taken?\n",">\n","> **b.** What is the frequency of these measurements (e.g. how often did measurements occur)? You can assume that the measurements were all collected at the same frequency.\n",">\n","\n","2. Find the maximum and minimum values of latitude and longitude in **T_data**.\n","\n","3. Create and print a new 1-dimensional array containing temperatures averaged between 5 meters and 11 meters for each measurement time. If ocean temperatures vary linearly between these two depths, what is the approximate depth in the ocean that these average temperatures represent?\n","\n","4. Reshape the average temperature array from Part 3 into a 2-dimensional array with the same number of rows as the number of hours that these measurements span (your solution from Part 1a). Calculate and print a new array that represents the average temperature in each row of the reshaped array. (Notice that this is equivalent to the hourly average temperature!)"]},{"cell_type":"markdown","metadata":{"id":"z9qz2KscwSVH"},"source":["### Data is contained in the cell below. \n","*Make sure to run the cell below before running your solution cell. Please do not alter the data arrays. Write your code and answers in the cell at the bottom, not this cell.*"]},{"cell_type":"code","metadata":{"id":"n-ROs_u1wQaE"},"source":["# Do not alter the code in this cell\n","\n","import numpy as np\n","\n","# Data in this array consists of 4 columns:\n","# Latitude, longitude, T at 5 m (˚C), T at 11 m (˚C)\n","\n","T_data = np.array([[51.7439,2.4476,14.726,14.736],[51.7147,2.4071,14.746,14.756],[51.6851,2.3664,14.796,14.816],[51.6561,2.3254,14.856,14.866],\n"," [51.627,2.2854,14.866,14.876],[51.5981,2.2454,14.896,14.916],[51.5689,2.2055,14.936,14.946],[51.5404,2.1661,14.946,14.956],\n"," [51.5122,2.127,14.936,14.946],[51.4831,2.087,14.956,14.966],[51.4545,2.0478,15.016,15.026],[51.4271,2.01,15.106,15.116],\n"," [51.3959,1.9686,15.136,15.146],[51.3635,1.9252,15.086,15.086],[51.3304,1.8848,14.826,14.826],[51.2986,1.8437,14.616,14.626],\n"," [51.2679,1.8036,14.527,14.547],[51.2371,1.7642,14.636,14.646],[51.207,1.7255,14.666,14.686],[51.1782,1.6886,14.766,14.786],\n"," [51.1497,1.6519,14.736,14.756],[51.1215,1.6156,14.716,14.726],[51.0984,1.581,14.656,14.666],[51.077,1.5485,14.567,14.577],\n"," [51.0586,1.5198,14.467,14.477],[51.0354,1.4841,14.247,14.257],[51.0088,1.4431,14.117,14.147],[50.9829,1.4033,14.307,14.327],\n"," [50.957,1.3635,14.337,14.347],[50.9314,1.324,14.307,14.327],[50.9077,1.2801,14.327,14.337],[50.8867,1.2301,14.207,14.217],\n"," [50.8654,1.1789,14.157,14.177],[50.8436,1.1266,14.167,14.187],[50.8213,1.0736,14.137,14.157],[50.7988,1.0196,14.257,14.277],\n"," [50.776,0.9649,14.437,14.447],[50.7527,0.9096,14.626,14.646],[50.7295,0.8538,14.796,14.806],[50.7059,0.7976,14.836,14.846],\n"," [50.6826,0.7407,14.806,14.816],[50.6626,0.6806,14.806,14.816],[50.6388,0.6227,14.826,14.836],[50.615,0.5641,14.826,14.836],\n"," [50.6005,0.4986,14.786,14.796],[50.5881,0.4317,14.786,14.786],[50.5756,0.3649,14.756,14.766],[50.5632,0.2975,14.826,14.836],\n"," [50.5509,0.2306,14.886,14.896],[50.5386,0.1641,15.006,15.016],[50.5263,0.0974,15.176,15.186],[50.5138,0.0313,15.196,15.196],\n"," [50.5018,-0.0345,15.186,15.196],[50.4897,-0.0997,15.286,15.296],[50.4778,-0.1644,15.346,15.356],[50.466,-0.2284,15.386,15.396],\n"," [50.454,-0.2916,15.376,15.386],[50.4426,-0.3536,15.366,15.376],[50.4313,-0.4153,15.416,15.416],[50.4168,-0.4275,15.456,15.466],\n"," [50.409,-0.4882,15.436,15.446],[50.4017,-0.5474,15.466,15.476],[50.3933,-0.6047,15.426,15.426],[50.3796,-0.6583,15.396,15.406],\n"," [50.3668,-0.7114,15.396,15.406],[50.3524,-0.763,15.396,15.406],[50.3396,-0.8151,15.396,15.406],[50.3288,-0.8668,15.476,15.486],\n"," [50.3223,-0.9188,15.556,15.566],[50.316,-0.97,15.616,15.636],[50.3092,-1.0191,15.696,15.706],[50.3024,-1.0675,15.746,15.756]])\n","\n","\n","# Data in this array has only one dimension:\n","# Date/Time (Y-m-d H:M:S) string\n","\n","time_data = np.array(['2003-10-23 06:25:00','2003-10-23 06:35:00',\n"," '2003-10-23 06:45:00','2003-10-23 06:55:00','2003-10-23 07:05:00',\n"," '2003-10-23 07:15:00','2003-10-23 07:25:00','2003-10-23 07:35:00',\n"," '2003-10-23 07:45:00','2003-10-23 07:55:00','2003-10-23 08:05:00',\n"," '2003-10-23 08:15:00','2003-10-23 08:25:00','2003-10-23 08:35:00',\n"," '2003-10-23 08:45:00','2003-10-23 08:55:00','2003-10-23 09:05:00',\n"," '2003-10-23 09:15:00','2003-10-23 09:25:00','2003-10-23 09:35:00',\n"," '2003-10-23 09:45:00','2003-10-23 09:55:00','2003-10-23 10:05:00',\n"," '2003-10-23 10:15:00','2003-10-23 10:25:00','2003-10-23 10:35:00',\n"," '2003-10-23 10:45:00','2003-10-23 10:55:00','2003-10-23 11:05:00',\n"," '2003-10-23 11:15:00','2003-10-23 11:25:00','2003-10-23 11:35:00',\n"," '2003-10-23 11:45:00','2003-10-23 11:55:00','2003-10-23 12:05:00',\n"," '2003-10-23 12:15:00','2003-10-23 12:25:00','2003-10-23 12:35:00',\n"," '2003-10-23 12:45:00','2003-10-23 12:55:00','2003-10-23 13:05:00',\n"," '2003-10-23 13:15:00','2003-10-23 13:25:00','2003-10-23 13:35:00',\n"," '2003-10-23 13:45:00','2003-10-23 13:55:00','2003-10-23 14:05:00',\n"," '2003-10-23 14:15:00','2003-10-23 14:25:00','2003-10-23 14:35:00',\n"," '2003-10-23 14:45:00','2003-10-23 14:55:00','2003-10-23 15:05:00',\n"," '2003-10-23 15:15:00','2003-10-23 15:25:00','2003-10-23 15:35:00',\n"," '2003-10-23 15:45:00','2003-10-23 15:55:00','2003-10-23 16:05:00',\n"," '2003-10-23 16:15:00','2003-10-23 16:25:00','2003-10-23 16:35:00',\n"," '2003-10-23 16:45:00','2003-10-23 16:55:00','2003-10-23 17:05:00',\n"," '2003-10-23 17:15:00','2003-10-23 17:25:00','2003-10-23 17:35:00',\n"," '2003-10-23 17:45:00','2003-10-23 17:55:00','2003-10-23 18:05:00',\n"," '2003-10-23 18:15:00','2003-10-23 18:25:00'])"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"u-b2Qag6wbZq"},"source":["### Write your code and answers in the cell below:\n"]},{"cell_type":"code","metadata":{"id":"Jv2ZgNAMwX3X"},"source":[""],"execution_count":null,"outputs":[]}]}