clear
close all

wgs84=almanac('earth','wgs84');
wgs84(1) = wgs84(1) * 1000.0;

fid = fopen('DrifterTrackDMY.txt');
drifterData = textscan(fid, '%s %s %f %f %f %f');
fclose(fid);
LonDeg = drifterData{5}(:);
LonMin = drifterData{6}(:);
drifterLon = LonDeg - LonMin/60.0;
LatDeg = drifterData{3}(:);
LatMin = drifterData{4}(:);
drifterLat = LatDeg + LatMin/60.0;
[drifterDistMatlab, drifterAz]=distance(drifterLat(end),drifterLon(end),drifterLat(1),drifterLon(1), wgs84);

p1 = [drifterLat(end), drifterLon(end)];
z1 = utmzone(p1);
utmstruct = defaultm('utm');
utmstruct.zone = z1;
utmstruct.geoid = wgs84;
utmstruct = defaultm(utmstruct);
[x1,y1] = mfwdtran(utmstruct,drifterLat(1),drifterLon(1));
[x2,y2] = mfwdtran(utmstruct,drifterLat(end),drifterLon(end));
deltaX = x2 - x1;
deltaY = y2 - y1;
drifterDistUTM = sqrt( deltaX^2 + deltaY^2 );

% sableData = importdata('SIDSableFixesSep10.txt');
% sableLon = sableData(:,4);
% sableLat = sableData(:,3);
% [sableDistMatlab, sableAz]=distance(sableLat(end),sableLon(end),sableLat(1),sableLon(1), wgs84);
fid = fopen('CTaylor-SID array fixes.txt');
SIDData = textscan(fid, '%s %s %f %f', 'HeaderLines', 2);
fclose(fid);
SIDLon = SIDData{4}(:);
SIDLat = SIDData{3}(:);

fid = fopen('SG148 transect coordinates.txt');
SG148Data = textscan(fid, '%s %s %f %f %f %f', 'Headerlines', 3);
fclose(fid);
SG148Lon = -1.0 * SG148Data{4};
SG148Lat = SG148Data{3};
SG148EOTLat = 24.0 + 45.0/60.0;
SG148EOTLon = -156.0 - 30/60.0;

fid = fopen('trap_argos fixes.txt');
trapData = textscan(fid, '%s %s %f %f', 'Headerlines', 4);
fclose(fid);
trapLon = -1.0 * (360.0 - trapData{4});
trapLat = trapData{3};

StationData = importdata('KM1125stations.txt');
StationLon = StationData(:,4);
StationLat = StationData(:,3);
for(k= 1:2)
    if k==1
        h = axesm(  'mapprojection', 'mercator', ...
            'maplatlim', [21.0 26.0], ...
            'maplonlim', [-160 -155], ...
            'grid', 'on', 'frame', 'on', ...
            'MLineLocation', 1, 'PLineLocation', 1, ...
            'meridianLabel', 'on', 'parallelLabel', 'on');
    elseif k==2
        figure
        h = axesm(  'mapprojection', 'mercator', ...
            'maplatlim', [24.3 25.2], ...
            'maplonlim', [-158.2 -157.2], ...
            'grid', 'on', 'frame', 'on', ...
            'MLineLocation', 0.2, 'PLineLocation', 0.2, ...
            'meridianLabel', 'on', 'parallelLabel', 'on', ...
            'MLabelRound', -1, 'PLabelRound', -1, ...
            'LabelRotation', 'off', 'LabelFormat', 'signed');
    end
    
    geoshow('landareas.shp', 'FaceColor',  [0.5 0.7 0.5])
    hDrifter = geoshow(drifterLat, drifterLon, 'DisplayType', 'line', 'Color', 'red', ...
            'LineWidth', 1, 'Marker', '+', 'MarkerSize', 2);
    hSID = geoshow(SIDLat, SIDLon, 'DisplayType', 'line', 'Color', 'green', 'LineWidth', 1);
    hTrap = geoshow(trapLat, trapLon, 'DisplayType', 'line', 'Color', 'cyan', 'LineWidth', 1);
    hSG148 = geoshow(SG148Lat, SG148Lon, 'DisplayType', 'line', 'Color', 'blue', 'LineWidth', 1);
    hSG148EOT = geoshow(SG148EOTLat, SG148EOTLon, 'DisplayType', 'point', 'MarkerEdgeColor', 'blue', ...
        'MarkerSize', 6, 'LineWidth', 1.5);
    hStation = geoshow(StationLat, StationLon, 'DisplayType', 'point', 'MarkerEdgeColor', 'magenta', ...
        'MarkerSize', 6, 'LineWidth', 1.5);
    
    load('FSLE_Sept8_2011.mat');
    newlon1 = -1.0 * (360 - newlon0);
    x11 = newlon1(1);
    y11 = newlat0(1);
    [r, c] = size(fsle_hr);
    dx = (newlon1(end) - newlon1(1)) / c;
    dy = (newlat0(end) - newlat0(1)) / r;
    R = makerefmat(x11, y11, dx, dy);
    [C,hFSLE] = contourm(fsle_hr*24*3600, R, [0.0:0.1:0.5]);
    
    load('SSH_Sept8_2011.mat');
    ssh_lonvec1 = -1.0 * (360 - ssh_lonvec);
    x11 = ssh_lonvec1(1);
    y11 = ssh_latvec(1);
    [r, c] = size(ssh);
    dx = (ssh_lonvec1(end) - ssh_lonvec1(1)) / c;
    dy = (ssh_latvec(end) - ssh_latvec(1)) / r;
    R = makerefmat(x11, y11, dx, dy);
    [C,hSSH] = contourm(ssh, R, 'ShowText', 'on');
    
    fid = fopen('HawaiiDriftPCRData.txt');
    PCRData = textscan(fid, '%s %s %f %f %f %f %f %f %f %f %f', 'Headerlines', 1);
    fclose(fid);
    
    LonDeg = PCRData{5}(:);
    LonMin = PCRData{6}(:);
    PCRLon = LonDeg - LonMin/60.0;
    LatDeg = PCRData{3}(:);
    LatMin = PCRData{4}(:);
    PCRLat = LatDeg + LatMin/60.0;
    
    PCRNames = ['GrpA '; 'GrpB '; 'Trich'; 'Het-1'; 'Gamma'];
    
    for i=1:5
        legs(:,i) = PCRData{6+i};
    end
    logLegs = log10(legs);
    
    angle = 0;
    dA = degtorad(360.0 / 5.0);
    colorNum= ['b' 'g' 'c' 'r' 'm' 'y' 'w' 'k'];
    scale = 1.0 / 70.0;
    for j= 1:8
        for i=1:5
            geoshow([PCRLat(j) PCRLat(j) + scale*sin(angle)*logLegs(j,i)], ...
                [PCRLon(j) PCRLon(j) + scale*cos(angle)*logLegs(j,i)], ...
                'DisplayType', 'line', 'Color', colorNum(i), 'LineWidth', 1.5);
            angle = angle + dA;
        end
    end
   
    if k==1
        hArray = [hDrifter hSID hSG148 hTrap hStation hSG148EOT];
        hLegend = legend(hArray,'ESP', 'SID', 'SG148', 'Sed Trap', 'KM Stations', 'SG148EOT',...
            'Location', [.4 .26 .005, .005]);
        set(hLegend, 'FontSize', 4);
        scaleruler('MajorTick', [0 50 100], 'MinorTick', [37.040], 'MinorTickLabel', '20nm', ...
            'XLoc', -0.03, 'Yloc', .377, 'FontSize', 6, 'RulerStyle', 'patches');
        starLat = 23.5;
        starLon = -159.5;
        for i=1:5
            geoshow([starLat starLat + scale*sin(angle)*6], ...
                [starLon starLon + scale*cos(angle)*6], ...
                'DisplayType', 'line', 'Color', colorNum(i), 'LineWidth', 1.5);
            textm(starLat + scale*sin(angle)*8, starLon+scale*cos(angle)*8, [PCRNames(i,:) ' (10^8)'], ...
                'FontSize', 6, 'Rotation', radtodeg(angle));
            angle = angle + dA;
        end
   elseif k==2
        hArray = [hDrifter hSID hSG148 hTrap hStation];
        hLegend = legend(hArray,'ESP', 'SID', 'SG148', 'Sed Trap', 'KM Stations',...
            'Location', [.4 .5 .002, .002]);
        set(hLegend, 'FontSize', 4);
        scaleruler('MajorTick', [0 10 20], 'MinorTick', [37.040/2.0], 'MinorTickLabel', '10nm', ...
            'XLoc', 0.001, 'Yloc', 0.44, 'FontSize', 6, 'RulerStyle', 'patches');
        starLat = 24.9;
        starLon = -157.9;
        for i=1:5
            geoshow([starLat starLat + scale*sin(angle)*6], ...
                [starLon starLon + scale*cos(angle)*6], ...
                'DisplayType', 'line', 'Color', colorNum(i), 'LineWidth', 1.5);
            textm(starLat + scale*sin(angle)*6, starLon+scale*cos(angle)*6, [PCRNames(i,:) ' (10^6)'], ...
                'FontSize', 6);
            angle = angle + dA;
        end
    end
end

