# Import NumPy, Pandas, xarray, Matplotlib, and datetime at the top of your code import numpy as np import pandas as pd import xarray as xr import matplotlib.pyplot as plt from datetime import datetime, timedelta # Download netCDF4 library !pip install netcdf4 # You can comment this out once it has run # Give Colab access to Google Drive from google.colab import drive drive.mount('/content/drive') # Filepath for World Ocean Atlas 2018 (WOA18) temperature netCDF file # Note: you may need to change this to match your own filepath, # which you can get by opening the left sidebar (folder icon), # navigating to the file, clicking the "..." on the file, and # selecting "Copy path" filepath = '/content/drive/My Drive/OCEAN 215 - Autumn \'20/OCEAN 215 - Autumn \'20 - Course documents/Zoom class slides and notebooks/2020-11-10 - class #11 - data/woa18_temp.nc' # Load the data file from Google Drive as an xarray Dataset temp = xr.open_dataset(filepath) # View data and stats display(temp) # Answers # a. 2 variables # b. Average temperature and number of observations per grid cell # c. 1960-2011 # d. 1° # e. 5500 m # Write code here: # -35°N, 110°E temp.sel(depth=0,lat=-35,lon=110,time=datetime(2011,1,1),method='nearest')['t_an'].item() time_series = temp.sel(depth=0,lat=46,lon=-128,method='nearest')['t_an'] display(time_series) depth_profile = temp.sel(lat=46,lon=-128,time=datetime(2011,1,1),method='nearest')['t_an'] display(depth_profile) fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(14,5)) ax1.plot(time_series['time'].values,time_series,c='k') ax1.set_xlabel('Time') ax1.set_ylabel('Temperature (°C)') ax1.set_title('Time series') ax1.grid() ax2.plot(depth_profile.values,depth_profile['depth'],c='k') ax2.invert_yaxis() ax2.set_xlabel('Temperature (°C)') ax2.set_ylabel('Depth (m)') ax2.set_title('Profile') ax2.grid() temperature = temp['t_an'].values.flatten() print(temperature.shape) fig = plt.figure(figsize=(10,10)) p = plt.hist(temperature,bins=50, rwidth=0.85) temp_ave = np.nanmean(temperature) temp_std = np.nanstd(temperature) plt.plot([temp_ave,temp_ave],[0,2.7e6],c='k',lw=2) plt.text(temp_ave,1.5e6,'Average='+str(round(temp_ave,2))+'˚C') plt.plot([temp_ave+temp_std,temp_ave+temp_std],[0,2.7e6],c ='k',ls='--',lw=2) plt.plot([temp_ave-temp_std,temp_ave-temp_std],[0,2.7e6],c ='k',ls='--',lw=2) plt.grid() plt.ylim((0,2.7e6)) plt.xlabel('Temperatures ˚C',fontsize=14) plt.ylabel('Counts',fontsize=14) plt.title('WOA temperatures',fontsize=14) plt.savefig('drive/My Drive/WOA_hist.png') # Import statements and drive mount import numpy as np import matplotlib.pyplot as pl from google.drive import drive drive.mount('/content/drive') # Where the file is and its name folderpath= '/content/drive/My Drive/Data_folder/' filename = 'Dissolved Inorganic Nutrients.csv # Read the data data = pd.read_csv(folderpath + filname, parse_dates=[1], na_values=-999) data.describe # Clean the data and take averages for each day of data nitrate = data['Nitrite and Nitrate (µmol/L)'] data['Nitrite and Nitrate (µmol/L)'] = nitrate.where(nitrate>0) cleandata = data.groupby('Datetime GMT').mean # Assign variable names and change to np arrays P = cleandata['Phosphate (µmol/L)'.values S = clandata['Silicate (µmol/L)'].values N = cleandata["Nitrite and Nitrate (µmol/L)'].values dates = cleandata.index.values # Make a figure and plot the data fig = plt.figure(figsize=(10,10)) ax.plot(data,S,'s-',c='mediumblue',label='S') ax.plot(dates,N,'*-',c='darkgreen',label='N') ax.plot(datas,P,'o-',c='firebrick',label='P') # Format the figure plt.set_xlabel('Dates') plt.set_ylabel('Concentration (µmol/L)") plt.title('Inorganic nutrients') plt.grid() plt.yscale('log') plt.gca.legend() # Save the figure plt.savefig(folderpath 'lineplots.png')