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;
% drifterLonLat = importdata('DrifterTrack.txt');
% LonDeg = drifterLonLat(:,4);
% LonMin = drifterLonLat(:,5);
%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 );

meloData = importdata('SIDMeloFixesSep10.txt');
meloLon = meloData(:,4);
meloLat = meloData(:,3);
[meloDistMatlab, meloAz]=distance(meloLat(end),meloLon(end),meloLat(1),meloLon(1), wgs84);

sableData = importdata('SIDSableFixesSep10.txt');
sableLon = sableData(:,4);
sableLat = sableData(:,3);
[sableDistMatlab, sableAz]=distance(sableLat(end),sableLon(end),sableLat(1),sableLon(1), wgs84);

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', [20 25.25], ...
            '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 25.25], ...
            'maplonlim', [-158 -157], ...
            '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', 3);
    hSable = geoshow(sableLat, sableLon, 'DisplayType', 'line', 'Color', 'yellow', 'LineWidth', 3);
    hTrap = geoshow(trapLat, trapLon, 'DisplayType', 'line', 'Color', 'cyan', 'LineWidth', 3);
    hSG148 = geoshow(SG148Lat, SG148Lon, 'DisplayType', 'line', 'Color', 'blue', 'LineWidth', 3);
    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', 'Tricho', '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= ['y' 'm' 'c' 'r' 'g' 'b' '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);
                angle = angle + dA;
            end
        end
    
        hArray = [hDrifter hSable 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');
end

