%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% script to import and process USGS Earthquake data %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%% INTIALIZE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% > first make sure the workspace is clear ... to avoid issues in developing 
%   and specifically confusion that can arise if a variable already exists
%   before the script is run
% > also close all existing figure windows
clear;
close all;
%
%%% LOAD DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% > open the file for data input
openfile_id = fopen('query.csv');
% > load data
% > the data file is ... tricky ...
% > firstly, ther eis a single header line, so we need to add to textscan:
%   'Headerlines',1
% > secondly, the data is comma-serpated, so we need to add to textscan:
%   'Delimiter',','
% > thirdly, if we only want the columns up to and including magnitude,
%   the format is (string, and then 4 floating point numbers):
%   %s %f %f %f %f
% > finally, lets skip the rest of the file after the magnitude column:
%   %[^\r\n]
mdata = textscan(openfile_id,'%s %f %f %f %f %[^\r\n]','Delimiter',',','Headerlines',1);
% > close the data file
fclose(openfile_id);
%
%%% PROCESS DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% > extract the 1st column of the cell array, as a character array
%   (which you can look at as a 'vector' of fixed length strings)
data_datestr = char(mdata{1}(:));
% > now, we need to get rid of the unwanted characters that appear 
%   in the USGS time format, i.e. we want to convert: 
%   2019-07-04T16:11:45.300Z
%   to
%   2019-07-04 16:11:45.300 
% > we'll do this by 'finding' the characters 'T' and 'Z' and replacing
%   them with spaces
data_datestr(find(data_datestr=='T'))=' ';
data_datestr(find(data_datestr=='Z'))=' ';
% > having done that, the function datevec will 
%   "Convert date and time to vector of components"
%   e.g. DateVector = datevec(DateString)
%   (I already checked and confimed that the USGS data is in one of the
%   preferred MATLAB date/time formats: 'dd-mmm-yyyy HH:MM:SS')
data_datevec = datevec(data_datestr);
% > now to create a time-axis, which will be the number of fractional days 
%   since the start of July
% > we can ignore the year and month columns (1 and 2) in the data_datevec
%   array, and then take the day number (column 3), 
%   add that to the day fraction represented by the number of hours:
%   (hours/24) and then
%   add that to the day fraction represented by the number of minutes:
%   (munites/(24*60))
% > (we'll ignore the number of seconds)
data_date = data_datevec(:,3) + data_datevec(:,4)/(24.0) + data_datevec(:,5)/(24.0*60.0);
% > finally -- becasue USGS time is 'Zulu' time
%   (https://en.wikipedia.org/wiki/Coordinated_Universal_Time)
%   we need to subtract 7 hours from time value (in days) to make PST
data_date = data_date-(7.0/24.0);
% > extract Earthquake magnitude data from the ceell array
%   (converting the 5th column of the cell array, into an ordinary vector)
data_mag = cell2mat(mdata(5));
%
%%% TEST CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% > a quick clude plot to see how it looks
%   (un-comment to test ...)
%%%scatter(data_date,data_mag);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% END %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
