% read in GPS time series, create arrays, plot them
clear all;
close all;
clc;

% This is where I got the data initially from:

%[BZ09_time, BZ09_lon, BZ09_lat, BZ09_height] = textread('BZ09.dat', '%f%f%f%f');
%[FAIR_time, FAIR_lon, FAIR_lat, FAIR_height] = textread('FAIR.dat', '%f%f%f%f');
%[WHIT_time, WHIT_lon, WHIT_lat, WHIT_height] = textread('WHIT.dat', '%f%f%f%f');

% This is how I saved the whole workspace to a .mat file 

%save('gps_data.mat');

% This is how I load a .mat file that contains all sorts of useful data.
load gps_data;

%% INITIALIZATION

% create structs for GPS stations and populate them on initialization;
% perfectly legitimate if you're careful with the cell!. Note how I
% populate the name field! See separate script on dangers of structs!
BZ09_struct = struct('time', BZ09_time, 'lon',  BZ09_lon, 'lat', BZ09_lat, 'height', BZ09_height, 'name', {{'BZ09'}});
FAIR_struct = struct('time', FAIR_time, 'lon',  FAIR_lon, 'lat', FAIR_lat, 'height', FAIR_height, 'name', {{'FAIR'}});
WHIT_struct = struct('time', WHIT_time, 'lon',  WHIT_lon, 'lat', WHIT_lat, 'height', WHIT_height, 'name', {{'WHIT'}});

% now put everything in an array of structs ... This is simply stuffing a
% struct into a field of an array. We create this on the fly adding a field
% at a time to the array - Keep in mind that this is not efficient for
% large arrays! Matlab has to allocate memory for each field as it's added 
% - that's kinda like making a separate trip to the store for each item on
% your shopping list
GPS_stations = [BZ09_struct; FAIR_struct; WHIT_struct];

% We can also put everything into an array right away which is much easier 
% to handle during the following plotting exercise (see below) ... this
% would be the most efficient way under the given circumstances.
GPS_stations2(1) = struct('time', BZ09_time, 'lon',  BZ09_lon, 'lat', BZ09_lat, 'height', BZ09_height, 'name', {{'BZ09'}});
GPS_stations2(2) = struct('time', FAIR_time, 'lon',  FAIR_lon, 'lat', FAIR_lat, 'height', FAIR_height, 'name', {{'FAIR'}});
GPS_stations2(3) = struct('time', WHIT_time, 'lon',  WHIT_lon, 'lat', WHIT_lat, 'height', WHIT_height, 'name', {{'WHIT'}});

%% CLEAR ARRAY DATA

% our TA asked us to delete the loaded data ... here we go:
clear BZ09_time BZ09_lon BZ09_lat BZ09_height ...
	  FAIR_time FAIR_lon FAIR_lat FAIR_height ... 
	  WHIT_time WHIT_lon WHIT_lat WHIT_height

%% PLOTTING  

% for the plotting I personally decide for a 'for' loop. It's the more
% efficient way to treat those data, especially since now they're set up in
% an array of structs
for n=1:length(GPS_stations2)
    figure(n)
    subplot(3,1,1)
    %we gotta subtract mean values to get useful variation values plotted.
    plot(GPS_stations(n).time, GPS_stations(n).lon - mean(GPS_stations(n).lon))

    % for the title acessing the name is a little awkward since it is
    % stored in a cell array of size 1, you still have to give the index
    % since you need to properly reference it using '{' and '}'
    title(sprintf('%s', GPS_stations(n).name{1}))
    ylabel('lon (m)');
    axis tight;

    subplot(3,1,2)
    plot(GPS_stations(n).time, GPS_stations(n).lat - mean(GPS_stations(n).lat))
    ylabel('lat (m)');
    axis tight;

    subplot(3,1,3)
    plot(GPS_stations(n).time, GPS_stations(n).height - mean(GPS_stations(n).height))
    ylabel('height (m)');
    xlabel('epoch');
    axis tight;
end