function adjust_image(tripodNumber, startTime, deltaT, theOrient, theHeight)
%
%   adjust_image(tripodNumber, startTime, deltaT, theOrient, theHeight)
%
%   Function which will load all of the images for tripodNumber (starting
%   at startTime (GMT) with a time difference between images of deltaT) and will
%   create the animation.  "theOrient" is the compass direction (in
%   degrees CW from N) of the tripod (this is the direction of the compass
%   in the images + 15 degrees, compensating for the difference between true
%   north and magnetic north), and "theHeight" is the height (in cm)
%   of the camera above the seafloor.

%Soupy Alexander, 11/17/03

%Update 7/15/06 to remove image alignment adjustment, now done by WHOI

greenColor = [0.2 0.6 0.2];

theWidth= 2*theHeight*tan(27*pi/180);   %Get the width of the images on the seafloor

targetDirectory = ['C:\SCHTUFF\TRIPOD_MOVIE\DS06_01\TRIPOD' num2str(tripodNumber, '%03.0f') ...
        '\IMAGES'];

if tripodNumber == 535
    dataDirectory = 'C:\SCHTUFF\DATAARCHIVE\DATAFILES\MBAY_IWAVE';
    attnFile = fullfile(dataDirectory, '5351tcp-a.nc');
    psdFile = fullfile(dataDirectory, '5351psd-a.nc');
    currFile = fullfile(dataDirectory, '5344-a.nc');
    currFileH = fullfile(dataDirectory, '5344-a1h.cdf');
    currEast = 'east';
    currNorth = 'north';
    theFactor = 1;
else
    dataDirectory = 'C:\SCHTUFF\MASS_BAY\MBLT_REPORT\DATAFILES';
    attnFile = fullfile(dataDirectory, 'a_attn_1mab.nc');
    psdFile = fullfile(dataDirectory, 'a_sdp_1mab.nc');
    currFile = fullfile(dataDirectory, 'a_curr_1mab.nc');
    currFileH = fullfile(dataDirectory, 'a_curr_1mab.nc');
    currEast = 'u_1205';
    currNorth = 'v_1206';
    tempFile = fullfile(dataDirectory, 'a_temp_1mab.nc');
    theFactor = 1;
end
saveDirectory = ['C:\SCHTUFF\TRIPOD_MOVIE\DS06_01\TRIPOD' num2str(tripodNumber, '%03.0f')];
frameDirectory = ['C:\SCHTUFF\TRIPOD_MOVIE\DS06_01\TRIPOD' num2str(tripodNumber, '%03.0f') '\FRAMES'];

imageWidth = 600;
imageHeight = 337;
frameWidth = imageWidth + 100;
frameHeight = 50 + imageHeight + 50 + imageWidth/4 + 50;

dayLimits = [-2 2];

tic
%The time difference
startTime = julian(startTime);
dxTime = deltaT/(60*60*24); 

%Load in the data
attnID = netcdf(attnFile, 'nowrite');
attnTime = singlejd(attnID{'time'}(:), attnID{'time2'}(:));
attnData = attnID{'ATTN_55'}(:);
attnData(find(attnData > 100 | attnData < -100)) = NaN;

psdID = netcdf(psdFile, 'nowrite');
psdTime = singlejd(psdID{'time'}(:), psdID{'time2'}(:));
psdData = psdID{'SDP_850'}(:);
psdData(find(psdData > 1e6 | psdData < -1e6)) = NaN;

currID = netcdf(currFile, 'nowrite');
currTime = singlejd(currID{'time'}(:), currID{'time2'}(:));
currU = currID{currEast}(:) * theFactor;
currU(find(currU > 1e4)) = NaN;
currV = currID{currNorth}(:) * theFactor;
currV(find(currV > 1e4)) = NaN;
currData = sqrt(currU.^2 + currV.^2);

currIDH = netcdf(currFileH, 'nowrite');
currTimeH = singlejd(currIDH{'time'}(:), currIDH{'time2'}(:));
currUH = currIDH{currEast}(:) * theFactor;
currUH(find(currUH > 1e4)) = NaN;
currVH = currIDH{currNorth}(:) * theFactor;
currVH(find(currVH > 1e4)) = NaN;
[currU_lp, currTime_lp] = plfilt(currUH, currTimeH, 6);
[currV_lp, currTime_lp] = plfilt(currVH, currTimeH, 6);

tempID = netcdf(tempFile, 'nowrite');
tempTime = singlejd(tempID{'time'}(:), tempID{'time2'}(:));
theTemp = tempID{'T_20'}(:);
theTemp(find(theTemp > 1e3 | theTemp < -1e3)) = NaN;

theFiles = dir(fullfile(targetDirectory, '*.tif'));
[fileNames{1:length(theFiles)}] = deal(theFiles.name);
goodNames = sort(fileNames);

startGreg = gregorian(startTime);
startYear = num2str(startGreg(1));
startYear = startYear(3:4);
endTime = startTime + (length(goodNames)-1)*dxTime;
endGreg = gregorian(endTime);
endYear = num2str(endGreg(1));
endYear = endYear(3:4);

movieName = ['t' num2str(tripodNumber, '%3.0f') '_' ...
        num2str(startGreg(2), '%02.0f') startYear '_' ...
        num2str(endGreg(2), '%02.0f') endYear '.avi'];

testMovie = avifile(fullfile(saveDirectory, movieName), 'compression', 'none', ...
    'fps', 3);
f = figure('position', [50 50 700 671], 'DoubleBuffer', 'on');

Isub = subplot('position', [(50/frameWidth) (250/frameHeight) ...
        (imageWidth/frameWidth) (imageHeight/frameHeight)]);
theIAngle = 90 - theOrient;
theIAngle = theIAngle * pi /180;

s2 = subplot('position', [(250/frameWidth) (50/frameHeight) ...
        ((2/3)*imageWidth/frameWidth) ((imageWidth/4)/frameHeight)]);
psdAx = gca;
thePosition = get(psdAx, 'position');
set(psdAx, 'ylim', [0 50], 'ytick', [0:10:50])
theTicks = [min(dayLimits):1:max(dayLimits)];
set(psdAx, 'xtick', theTicks, 'xlim', dayLimits, 'fontsize', 8)
set(psdAx, 'xticklabel', ...
    {' '; ' '; 'Photo time'; ' '; ' '});
theLabel = ylabel(', mb');
theLabelPosition = get(theLabel, 'position');
set(theLabel, 'horiz', 'left')
l = line([0 0], [0 50]);
set(l, 'color', 'k')
box('on')
hold on
tPSD = title('PSDEV,                    and  ');
set(tPSD, 'color', 'k', 'fontsize', 10, 'horiz', 'center');

tempAx = axes('position', thePosition);
theYTicks = {'  /0'; '  /4'; '  /8'; '  /12'; '  /16'; '  /20'};
set(tempAx, 'ylim', [0 20], 'color', 'none', 'xlim', dayLimits, ...
    'xtick', [], 'ytick', [0:4:20], 'yticklabel', theYTicks, 'fontsize', 8, ...
    'yaxisloc', 'right', 'ycolor', greenColor);
ylabel('^oC, ')
theLabel = get(tempAx, 'ylabel');
labPosition = get(theLabel, 'position');
newPosition = labPosition;
newPosition(1) = newPosition(1)-0.04;
set(theLabel, 'color', greenColor, 'fontsize', 8, 'horiz', 'right', 'position', newPosition);
tTemp = title('     Temperature,');
set(tTemp, 'color', greenColor, 'horiz', 'center', 'fontsize', 10);

attnAx = axes('position', thePosition);
set(attnAx, 'ylim', [0 5], 'ytick', [0:1:5], 'yaxisloc', 'right', 'color', 'none')
ylabel('m^-^1')
set(attnAx, 'xtick', [], 'xlim', dayLimits, 'ycolor', 'b', 'fontsize', 8)
theLabel = get(attnAx, 'ylabel');
thisPosition = get(theLabel, 'position');
thisPosition(1) = newPosition(1);
set(theLabel, 'color', 'b', 'fontsize', 8, 'horiz', 'left', 'position', thisPosition)
box('off')
hold on
tATTN = title('                                                               Beam Attenuation');                 
set(tATTN, 'color', 'b', 'horiz', 'center', 'fontsize', 10);


currAx = axes('position', thePosition); 
set(currAx, 'ylim', [0 50], 'color', 'none', 'xlim', dayLimits, ...
    'xtick', [], 'ytick', [], 'fontsize', 8)
ylabel('cm/s')
theLabel = get(currAx, 'ylabel');
set(theLabel, 'color', 'r', 'fontsize', 8, 'horiz', 'right', ...
    'position', theLabelPosition)
box('on')
hold on
tCurr = title('Current Speed,                                                              ');
set(tCurr, 'color', 'r', 'horiz', 'center', 'fontsize', 10)

s3 = subplot('position', [(50/frameWidth) (50/frameHeight) ...
        ((imageWidth/4)/frameWidth) ((imageWidth/4)/frameHeight)]);
p = polar_n(0, 20);
set(p, 'visible', 'off')
hold on
%axis([-10 10 -10 10])
%box('off')
%set(gca, 'color', 'none')
%axis('off')
%l = line([-10 10], [0 0]);
%set(l, 'color', 'k', 'clipping', 'off')
%l2 = line([0 0], [-10 10]);
%set(l2, 'color', 'k', 'clipping', 'off')
%tickLocs = [-10 -5 5 10];
%for index = 1:length(tickLocs);
%    l = line([tickLocs(index) tickLocs(index)], [-1 1]);
%    set(l, 'color', 'k', 'clipping', 'off')
%    l2 = line([-1 1], [tickLocs(index) tickLocs(index)]);
%    set(l2, 'color', 'k', 'clipping', 'off')
%end
%l = line([-10 10], [-12 -12]);
%set(l, 'color', 'k', 'clipping', 'off')
%l2 = line([-10 -10], [-12 -11]);
%set(l2, 'color', 'k', 'clipping', 'off')
%l3 = line([10 10], [-12 -11]);
%set(l3, 'color', 'k', 'clipping', 'off')
%t = text(0, -15, '20 cm/s');
%set(t, 'clipping', 'off', 'horiz', 'center', 'fontsize', 8, 'visible', 'on');
%axis equal
tDir = title('Hour-averaged Current');
set(tDir, 'color', 'r')
theSpot = get(tDir, 'position');
theSpot(2) = -1*theSpot(2);
tDir2 = xlabel('Low-passed Current');
set(tDir2, 'color', 'b', 'position', theSpot)
    
for indexFile = 1:length(goodNames)
    thisTime = startTime + (indexFile-1)*dxTime;
    fileName = goodNames{indexFile};
    [thePath, shortName, theExt] = fileparts(fileName);
    
    if findstr(shortName, '_b')
        I = repmat(0, [371 600 3]);
        fileName = ' ';
    else
         I = imread(fullfile(targetDirectory, fileName), 'tif');
%         %lastBlack = edgeLocs(indexFile);
%         
%         Inew = I;
%         theSlide = 32 - lastBlack;
%         if theSlide < 0
%             Inew(:,1:size(I,2)+theSlide+1,:) = I(:,-theSlide:end,:);
%             Inew(:,size(I,2)+theSlide:size(I,2),:) = 0;
%         elseif theSlide == 0
%             Inew = I;
%         elseif theSlide > 0
%             Inew(:,theSlide:size(I,2),:) = I(:,1:size(I,2)-theSlide+1,:); 
%             Inew(:,1:theSlide,:) = 0;
%         end
    end
    
    %Uniform black strip
    %Inew(:,570:end,:) = 0;
    scale = (max(xlim)-min(xlim))/(max(ylim)-min(ylim));
    subplot(Isub)
    Iplot = imshow(I);
    hold on
    t = text(5, 20, ['File: ' fileName]);
    set(t, 'color', 'w', 'horiz', 'left');
    scale = (max(xlim)-min(xlim))/(max(ylim)-min(ylim));
    theU = 20*cos(theIAngle);
    theV = -1*20*sin(theIAngle)/scale;
    theArrow = quiver(25, 60, theU, theV);
    set(theArrow, 'linewidth', 2, 'color', 'w')
    t2 = text(55, 60, 'N');
    set(t2, 'color', 'w', 'horiz', 'center', 'fontsize', 12)
    p = plot([150 - 5*imageWidth/theWidth 150 + 5*imageWidth/theWidth], [300 300], 'w');
    t3 = text(150, 310, '10 cm');
    set(t3, 'color', 'w', 'horiz', 'center', 'fontsize', 10)
    
    hitTimeCurr_lp = value2Index(currTime_lp, thisTime, 4*1/24);
    
    subplot(s2)
    psdPlot = plot(psdTime-thisTime, psdData, 'k', 'parent', psdAx, 'linewidth', 1);
    hitPSD = value2Index(psdTime, thisTime, 1/24);
    if ~isnan(hitPSD) & ~isnan(psdData(hitPSD))
        psdText = text(-1.3, 43, [num2str(psdData(hitPSD), '%.1f') ' mb   '], ...
            'parent', currAx, 'color', 'k', 'horiz', 'right'); 
    else
        psdText = text(-1.8, 43, ' ');
    end
    attnPlot = plot(attnTime-thisTime, attnData, 'b', 'parent', attnAx, 'linewidth', 1);
    hitATTN = value2Index(attnTime, thisTime, 1/24);   
    if ~isnan(hitATTN) & ~isnan(attnData(hitATTN))
        attnText = text(-1.3, 39, [num2str(attnData(hitATTN), '%.1f') ' m^-^1  '], ...
            'parent', currAx, 'color', 'b', 'horiz', 'right');
    else
        attnText = text(-1.8,39, ' ');
    end
    currPlot = plot(currTime-thisTime, currData, 'r', 'parent', currAx, 'linewidth', 1);
    hitCurr = value2Index(currTime, thisTime, 1/24); 
    if ~isnan(hitCurr) & ~isnan(currData(hitCurr))
        currText = text(-1.3, 47, [num2str(currData(hitCurr), '%.1f') ' cm/s'], ...
            'parent', currAx, 'color', 'r', 'horiz', 'right');
    else
        currText = text(-1.8, 47, ' ');
    end
    
    hitTemp = value2Index(tempTime, thisTime, 1/24);
    if ~isnan(hitTemp) & ~isnan(theTemp(hitTemp))
        tempText = text(-1.3, 35, [num2str(theTemp(hitTemp), '%.1f') ' ^oC^    '], ...
            'parent', currAx, 'color', greenColor, 'horiz', 'right');
    else
        tempText = text(-1.8, 35, ' ');
    end
    tempPlot = plot(tempTime - thisTime, theTemp,'g', 'parent', currAx, 'linewidth', 1);
    set(tempPlot, 'color', greenColor)
    xlabel([datestr(julian2datenum(thisTime), 0) ' GMT'])
    
    subplot(s3)
    if ~isnan(hitTimeCurr_lp)
        [theAngle, theLength] = cart2pol(currU_lp(hitTimeCurr_lp), currV_lp(hitTimeCurr_lp));
    else
        theAngle = 0;
        theLength = 0;
    end
        %theAngle = theAngle*180/pi;
    %q = arrowsafe(0, 0, theLength, theAngle);
    q = polar_n([0 theAngle], [0 theLength]);
    set(q, 'clipping', 'off', 'linewidth', 2, 'color', 'b')
    hitCurrH = value2Index(currTimeH, thisTime, 1/24);
    if ~isnan(hitCurrH)
        [theAngle2, theLength2] = cart2pol(currUH(hitCurrH), currVH(hitCurrH));
    else
        theAngle2 = 0;
        theLength2 = 0;
    end
    %theAngle2 = theAngle2*180/pi;
    %q2 = arrowsafe(0, 0, theLength2, theAngle2);
    q2 = polar_n([0 theAngle2], [0 theLength2]);
    set(q2, 'clipping', 'off', 'linewidth', 2, 'color', 'r');
    %arrowsafe
    
    fileName = fullfile(frameDirectory, ['frame' num2str(indexFile, '%03.0f') '.tif']);
    F = getframe(f);
    testMovie = addframe(testMovie, F);
    %imwrite(F.cdata, fileName, 'tif');
    
    delete(Iplot)
    delete(psdPlot)
    delete(attnPlot)
    delete(currPlot)
    delete(q)
    delete(q2)
    delete(t)
    delete(psdText)
    delete(attnText)
    delete(currText)
    delete(tempText)
    delete(theArrow)
    delete(tempPlot)
    delete(t3)
    delete(p)
    clear I
end
testMovie = close(testMovie);
toc
close
    
    
