# # Loading data in Colab - pick one way to do it # # --------------------------------------------------------------------- # # Importing data files from local machine # from google.colab import files # uploaded = files.upload() # # Mounting the Google Drive to access files # from google.colab import drive # drive.mount('/content/drive') # # Import data files using the user interface # # Mount drive using the user interface from google.colab import drive drive.mount('/content/drive') # Using readlines() # --------------------------------------------------------------------- filepath = 'drive/My Drive/Data_folder/Seattle_tides_predicted_20201001_20201024.txt' # Open the file file_obj = open(filepath,'r') # Read the file lines = file_obj.readlines() # Close the file file_obj.close() print(lines) print(len(lines)) # Using readline() # --------------------------------------------------------------------- filepath = 'drive/My Drive/Data_folder/Seattle_tides_predicted_20201001_20201024.txt' # Open the file file_obj = open(filepath,'r') # Read the file - first 30 lines for index in range(30): line = file_obj.readline() print(line) # Close the file file_obj.close() # Using np.genfromtxt() # --------------------------------------------------------------------- import numpy as np filepath = 'drive/My Drive/Data_folder/Seattle_tides_predicted_20201001_20201024.txt' # Read the data and the date information separately # Use function arguments and your knowledge of the file structure to format these lines data = np.genfromtxt(filepath,skip_header=14,dtype=float,usecols=3,delimiter=None) data_time = np.genfromtxt(filepath,skip_header=14,dtype=str,usecols=(0,1,2),delimiter=None) # Looking at the results print('Length:',len(data)) print(data) print() print('Length:',len(data_time)) print(data_time) import matplotlib.pyplot as plt # Figure and empty axes # --------------------------------------------------------------------- fig,ax = plt.subplots() # Figure and empty axes with custom size # --------------------------------------------------------------------- fig,ax = plt.subplots(figsize=(15,5)) # Figure and multiple empty axes # --------------------------------------------------------------------- fig,(ax0,ax1) = plt.subplots(nrows=2,ncols=1) # Line plot # --------------------------------------------------------------------- import numpy as np import matplotlib.pyplot as plt filepath = 'drive/My Drive/Data_folder/Seattle_tides_predicted_20201001_20201024.txt' # Read the data and create a time array data = np.genfromtxt(filepath,skip_header=14,dtype=float,usecols=3,delimiter=None) time = np.linspace(0,len(data)/10,len(data)) # 6 min freq. so len(data)/10 = # of hours # Create the plot and put the data in it fig,ax = plt.subplots() ax.plot(time, data, c='r',linestyle='-', linewidth=2, marker=None) # Figure formatting ax.grid() ax.set_title('Seattle tides (Oct. 1-24, 2020)', fontsize=18) ax.set_xlabel('Time since Oct. 1st 00:00 (hours)', fontsize=14) ax.set_ylabel('Daily tide predictions (m)', fontsize=14) # Scatter plot - CTD data # --------------------------------------------------------------------- # Look at the file filepath = 'drive/My Drive/Data_folder/a03_00011_1993CTD_data.csv' file_obj = open(filepath, 'r') for index in range(90): line = file_obj.readline() print(line) file_obj.close() # Scatter plot - creating the figure # --------------------------------------------------------------------- filepath = 'drive/My Drive/Data_folder/a03_00011_1993CTD_data.csv' # Load the data data = np.genfromtxt(filepath,skip_header=20,skip_footer=1,delimiter=',',usecols=(0,2,4)) # Separate out the columns into individual variables P = data[:,0] T = data[:,1] S = data[:,2] # Create the figure and scatter the data fig,ax = plt.subplots(figsize=(10,8)) scpl = ax.scatter(S, T, s=30, c=P, alpha=0.5) # Format the figure ax.set_title('Temperature and Salinity, 1993 CTD', fontsize=18) ax.set_ylabel('Temperature (degC)', fontsize=14) ax.set_xlabel('Salinity (PSU)', fontsize=14) ax.grid(linestyle='-.') c = fig.colorbar(scpl,ax=ax) c.set_label('Pressure (dbar)',fontsize=12)