
close all

zoom=1;
colorplot=1;
strong=0;

NWCUSP=gcwaypts(-21.816000000,114.166000000,-18.150000000,178.444000000,30);
VTXUSP=gcwaypts(8.387000000,77.753000000,-18.150000000,178.444000000,30);
NPMUSP=gcwaypts(21.42,-158.154,-18.150000000,178.444000000,30);
NLKUSP=gcwaypts(48.203,-121.917,-18.150000000,178.444000000,30);
NAAUSP=gcwaypts(44.646,-67.281,-18.150000000,178.444000000,30);
NPMUSP(NPMUSP(:,2)<0,2)=NPMUSP(NPMUSP(:,2)<0,2)+360;
NLKUSP(NLKUSP(:,2)<0,2)=NLKUSP(NLKUSP(:,2)<0,2)+360;
NAAUSP(NAAUSP(:,2)<0,2)=NAAUSP(NAAUSP(:,2)<0,2)+360;
PAMT=load('Evan_Track_JTWC_mat.txt');
PAMT(PAMT(:,6)<0,6)=PAMT(PAMT(:,6)<0,6)+360;
latM=[70 -70];
lonM=[70 210];
load coast
try (size(T))
catch
    T=[];
    LatL=[];
    LonL=[];
    AL=[];
    for d=9:19
        filename=sprintf(['A201212/A201212%02d' '.loc'],d);
        L=load(filename)';
        T=[T d*24*3600+L(2,:)*3600+L(3,:)*60+L(4,:)];
        LatL=[LatL L(5,:)];
        LonL=[LonL L(6,:)];
        LonL(LonL<-150)=LonL(LonL<-150)+360;
        AL=[AL L(7,:)];
    end
end
k=0;
for d=9:19
    for h=0:23
        k=k+1;
        t0=d*24*3600+(h-0.5)*3600;
        II=and(T>=t0,T<t0+3600);
        if (strong==1)
            II=and(II,abs(AL)>=25);
        end
        if (zoom==1)
            II=and(II,and(LatL>=-40,LatL<10));
            II=and(II,and(LonL>=140,LonL<200));
        else
            II=and(II,and(LatL>=-70,LatL<70));
            II=and(II,and(LonL>=70,LonL<210));
        end
        II=find(II);
            filename=sprintf('MTSAT/MT1R1212%02d%02dIR1.pgm',d,h);
            filename
            JL=floor(LonL(II)-70)+1;
            IL=floor(LatL(II)+70)+1;
            if (colorplot==0) 
                C=double(imread(filename));
                H=imagesc(lonM,latM,C)
            else
                C=zeros(141,141);
                for i=1:length(IL)
                    C(IL(i),JL(i))=C(IL(i),JL(i))+AL(II(i));
                    C(IL(i)+1,JL(i))=C(IL(i)+1,JL(i))+AL(II(i));
                    C(IL(i),JL(i)+1)=C(IL(i),JL(i)+1)+AL(II(i));
                    C(1+IL(i),1+JL(i))=C(1+IL(i),1+JL(i))+AL(II(i));
                end
                H=imagesc(lonM,-latM,C)
            end
            set(gca,'YDir','normal');
            cm=min(min(C));
            cM=max(max(C));
            set(gca,'CLim',[cm cM])
            if (zoom==1)
                axis([140 200 -40 10])
            else
                axis([70 210 -70 70])
            end
            if (colorplot==0)
                colormap gray
            end 
            hold
            if (zoom==1)
                FT=20;
            else
                FT=10;
            end
            plot(long,lat,'Color','y','Linewidth',2)
            plot(PAMT(:,6),PAMT(:,5),'Color','k','Linewidth',3 )
            plot(NWCUSP(:,2),NWCUSP(:,1),'Color','r','Linewidth',3)
            text(145,-25,'NWC','FontSize',FT,'Color','r')
            plot(VTXUSP(:,2),VTXUSP(:,1),'Color','r','Linewidth',3)
            text(145,-15,'VTX','FontSize',FT,'Color','r')
            plot(NPMUSP(:,2),NPMUSP(:,1),'Color','r','Linewidth',3)
            text(187,5,'NPM','FontSize',FT,'Color','r')
            plot(NLKUSP(:,2),NLKUSP(:,1),'Color','r','Linewidth',3)
            text(195,5,'NLK','FontSize',FT,'Color','r')
            plot(NAAUSP(:,2),NAAUSP(:,1),'Color','r','Linewidth',3)
            text(195,-5,'NAA','FontSize',FT,'Color','r')
            set(gca,'DataAspectRatio',[1 1 0.1]);
            set(gca,'FontSize',20);
            if (colorplot==0)
                for j=II
                    if (AL(j)>0)
                        plot(LonL(j),LatL(j),'xb','Linewidth',3,'MarkerSize',log10(floor(abs(AL(j))+4)*10));
                    else
                        plot(LonL(j),LatL(j),'xb','Linewidth',3,'MarkerSize',log10(floor(abs(AL(j))+4)*10));
                    end
                end
            end
            hold 
            
            title(sprintf('MT1 IR1 channel 2012/12/%02d %02d:00 UTC',d,h));
            set(gcf,'Position',[63           1        1858        1133]);
            A=getframe(gcf);
            
            if (zoom==0)
                FF='Evan';
            else
                FF='Evan_zoom';
            end
            
            if (strong==1)
                FF=[FF '_strong'];
            end
            
            if (colorplot==1)
                FF=[FF '_density'];
            end
            cd(FF)
            imwrite(A.cdata,sprintf('Evan_%04d.jpg',k));
            cd ..
            %pause
    end
end