clear all

%function galleryPageMaker(uniqueName)
uniqueNameArray{1}='2005_TAMMY'; segmentNum{1}=['1'];
gifStartTimeArray{1}=datenum(2005,10,05,07,00,00);
gifFinishTimeArray{1}=datenum(2005,10,06,12,45,00);

uniqueNameArray{2}='2005_TWELVE'; segmentNum{2}=['1'];
gifStartTimeArray{2}=  datenum(2005,8,28,15,00,00);
gifFinishTimeArray{2}=datenum(2005,8,29,18,00,00);

uniqueNameArray{3}='2005_TWENTY-FOUR'; segmentNum{2}=['1'];
gifStartTimeArray{3}=  datenum(2005,10,18,12,00,00);
gifFinishTimeArray{3}=datenum(2005,10,24,20,00,00);

uniqueNameArray{4}='2005_TWENTY-FOUR'; segmentNum{3}=['2'];
gifStartTimeArray{4}=  datenum(2005,10,23,00,00,00);
gifFinishTimeArray{4}=datenum(2005,10,24,20,00,00);

uniqueNameArray{5}='2005_TWENTY-FIVE'; segmentNum{4}=['1'];
gifStartTimeArray{5}=  datenum(2005,10,22,15,00,00);
gifFinishTimeArray{5}=datenum(2005,10,24,21,45,00);

uniqueNameArray{6}='2005_TWENTY-SIX'; segmentNum{5}=['1'];
gifStartTimeArray{6}=  datenum(2005,10,27,13,00,00);
gifFinishTimeArray{6}=datenum(2005,10,30,20,45,00);

for pageNum=2 %2:length(uniqueNameArray);
    uniqueName=uniqueNameArray{pageNum};
    gifStartTime=gifStartTimeArray{pageNum};
    gifFinishTime=gifFinishTimeArray{pageNum};
    
    curName0=textread(['../ver1/cyclones/' uniqueName '/curName.txt'],'%s');
    curName=curName0{1};
    webName=['gallery' curName segmentNum{pageNum}];
    
    % Get a list of all the morphed images
    fileListing=dir(['../ver1/cyclones/' uniqueName '/morphImages2/20*']);
    if ~strcmp('.', fileListing(1).name(1))
        for ci=1:length(fileListing)
            fileNames{ci}=fileListing(ci).name;
            fileListings{ci}=['../ver1/cyclones/' uniqueName '/morphImages2/' fileListing(ci).name];
        end
    else % (No files in directory)
        fileListings=[];
    end
    
    % Make gallery gif
    for ti=1:length(fileNames)
        fn=fileNames{ti};
        if strcmp([datestr(gifStartTime, 'yyyymmddTHHMMSS') '.gif'], fn), startIndex=ti; end
        if strcmp([datestr(gifFinishTime, 'yyyymmddTHHMMSS') '.gif'], fn), finishIndex=ti; end
    end
    
    fileString=[];
    for hi=startIndex:finishIndex
        fileString=[fileString ' ' fileListings{hi}];
    end
    animatedGifName=[webName '.gif'];
    disp(['Making animated gif ' animatedGifName ' ...'])
    commandString=['! gifsicle -l0 -d10 -O2 --colors 256 ' fileString ' > ' animatedGifName];
    eval(commandString);
    
    % Make a webpage for the gallery, by amending the generic template for animated gifs
    fidGeneric=fopen('../ver1/sharedFiles/genericGifDisplayBy12hr.html');
    fidGalleryPage=fopen([webName '.html'],'w');
    while 1
        tline = fgetl(fidGeneric);
        if ~ischar(tline), break, end
        dateStrLoc=findstr('(dateStr)',tline);
        gifStrLoc=findstr('(gifStr)',tline);
        nameLoc=findstr('(nameStr)',tline);
        if ~isempty(dateStrLoc)
            fi1=datestr(gifStartTime, 'yyyymmddTHHMMSS');
            fi2=datestr(gifFinishTime, 'yyyymmddTHHMMSS');
            dateRangeStr=[fi1(1:4) '-' fi1(5:6) '-' fi1(7:8) ' ' fi1(10:11) ':' fi1(12:13) ' -- ' fi2(1:4) '-' fi2(5:6) '-' fi2(7:8) ' ' fi2(10:11) ':' fi2(12:13)];
            newLine=[tline(1:dateStrLoc-1) dateRangeStr tline(dateStrLoc+9:end)];
            fprintf(fidGalleryPage,'%s\n',newLine);
        elseif ~isempty(gifStrLoc)
            gifStr=[webName '.gif'];
            newLine=[tline(1:gifStrLoc-1) gifStr tline(gifStrLoc+8:end)];
            fprintf(fidGalleryPage,'%s\n',newLine);
        elseif ~isempty(nameLoc)
            newLine=[tline(1:nameLoc-1) upper(curName(1)) curName(2:end) tline(nameLoc+9:end)];
            fprintf(fidGalleryPage,'%s\n',newLine);
        else
            fprintf(fidGalleryPage,'%s\n',tline);
        end
    end
end

% Commands that helped in the past:
%matlab < galleryPageMaker.m 
%ssh -l tropic cimss.ssec.wisc.edu mkdir /var/apache/cimss/htdocs/tropic/real-time/marti/galleryIR
%scp *.html *.png *.jpg *.gif tropic@cimss.ssec.wisc.edu:/var/apache/cimss/htdocs/tropic/real-time/marti/galleryIR
%scp menu.html tropic@cimss.ssec.wisc.edu:/var/apache/cimss/htdocs/tropic/real-time/marti/galleryIR

%scp menu.html *Wilma* tropic@cimss.ssec.wisc.edu:/var/apache/cimss/htdocs/tropic/real-time/marti/gallery
