flist = dir('Baro-CorrectedSeaL*.final');

clear month day year hours minutes seconds
clear baroCorrected_time baroCorrected_baro1 baroCorrected_baro2
baroCorrected_time = [];
baroCorrected_baro1 = [];
baroCorrected_baro2 = [];

for II = 1: length(flist)
    temp = load(flist(II).name);
    temp_month = floor(temp(:,1)/1e6);
    temp_day = floor(temp(:,1)/1e4)-100*temp_month;
    temp_year = temp(:,1) - 1000000*temp_month - 10000*temp_day;
    temp_hours = floor(temp(:,2)/1e4);
    temp_minutes = floor(temp(:,2)/1e2) - 1e2*temp_hours;
    temp_seconds = temp(:,2) - 1e4*temp_hours - 100*temp_minutes;
    temp_datenum = datenum(temp_year, temp_month, temp_day, temp_hours, temp_minutes, temp_seconds); 
    baroCorrected_time = [baroCorrected_time' temp_datenum']';
    baroCorrected_baro1 = [baroCorrected_baro1' temp(:,3)']';
    baroCorrected_baro2 = [baroCorrected_baro2' temp(:,4)']';
end

[~, II] = sort (baroCorrected_time);
baroCorrected_time = baroCorrected_time(II);
baroCorrected_baro1 = baroCorrected_baro1(II);
baroCorrected_baro2 = baroCorrected_baro2(II);

save BaroCorrectedSeaLevel.mat baroCorrected_time baroCorrected_baro1 baroCorrected_baro2

flist = dir('Barometer-MSG*.final');

clear month day year hours minutes seconds
clear baroMSG_time baroMSG_baro1 
baroMSG_time = [];
baroMSG_baro1 = [];

for II = 1: length(flist)
    temp = load(flist(II).name);
    temp_month = floor(temp(:,1)/1e6);
    temp_day = floor(temp(:,1)/1e4)-100*temp_month;
    temp_year = temp(:,1) - 1000000*temp_month - 10000*temp_day;
    temp_hours = floor(temp(:,2)/1e4);
    temp_minutes = floor(temp(:,2)/1e2) - 1e2*temp_hours;
    temp_seconds = temp(:,2) - 1e4*temp_hours - 100*temp_minutes;
    temp_datenum = datenum(temp_year, temp_month, temp_day, temp_hours, temp_minutes, temp_seconds); 
    baroMSG_time = [baroCorrected_time' temp_datenum']';
    baroMSG_baro1 = [baroCorrected_baro1' temp(:,3)']';
end

[temp II] = sort (baroMSG_time);
baroMSG_time = baroMSG_time(II);
baroMSG_baro1 = baroMSG_baro1(II);

save BaroMSG.mat baroMSG_time baroMSG_baro1 

figure;
    plot(baroCorrected_time, baroCorrected_baro1);
    hold on;
    plot(baroCorrected_time, baroCorrected_baro2,'r');
    datetick;
    set(gca,'xlim',[datenum(2013,8,1) datenum(2013,8,25)])
    plot(baroMSG_time, baroMSG_baro1,'k');
 
 
cd ../

load ondeck.prn
station = ondeck(:,1);
pr_before = fillmiss(ondeck(:,2),-9,1,NaN);
pr_after = fillmiss(ondeck(:,3),-9,1,NaN);;
station_time = datenum(2013*ones(size(ondeck,1),1), ondeck(:,4), ondeck(:,5), ondeck(:,6), ondeck(:,7), zeros(size(ondeck,1),1));


figure
    plot(station_time, pr_before)
    hold on
    plot(station_time, pr_after,'r')
    datetick;
    set(gca,'xlim',[datenum(2013,8,1) datenum(2013,8,25)])
    plot(baroCorrected_time, meanmiss(pr_before)+(baroCorrected_baro1-meanmiss(baroCorrected_baro1))/100,'k');
    legend('On Deck Pressure Before Cast', 'On Deck Pressure After Cast', 'Barometric Pressure Anomaly','Location','Best')
    grid
    print -depsc on_deck_pressure_vs_barometer.eps

pr_correction = interp1(baroCorrected_time, meanmiss(pr_before)+(baroCorrected_baro1-meanmiss(baroCorrected_baro1))/100, station_time);

figure
    plot(station_time, pr_before-pr_correction)
    hold on
    plot(station_time, pr_after-pr_correction,'r')
    datetick;
    set(gca,'xlim',[datenum(2013,8,1) datenum(2013,8,25)])
    %plot(baroCorrected_time, (baroCorrected_baro1-meanmiss(baroCorrected_baro1))/100,'k');
    legend('On Deck Pressure Before Cast', 'On Deck Pressure After Cast','Location','Best')
    title('Corrected Pressures')
    grid
    print -depsc corrected_on_deck_pressure.eps

fid=fopen('corrected_pressure.txt','wt');
output = [ondeck pr_correction];
fprintf(fid,'%Station\t Before\t After\t Mos\t Day \t Hr \t Min \t Correction \n');

for i = 1: size(output,1)
  fprintf (fid,'%11.7f', output(i,:));
  fprintf (fid,'\n');
end

