% Power  by lat


% Total Power 



clear
clc
folderVg = ['/nobackupp8/awinetee/global/Vg_noIce_hamming_daily/'];
folderUg = ['/nobackupp8/awinetee/global/Ug_noIce_hamming_daily/'];

nx = 2160;
siz = [8640 7560 1];

lats = quikreadpcolor_llc('~dmenemen/llc_2160/grid/YC.data',nx);
[aV aU] = quikreadpcolor_RASRAW_llc('~dmenemen/llc_2160/grid/RAS.data','~dmenemen/llc_2160/grid/RAW.data',nx);
landV = quikreadpcolor_llc('~dmenemen/llc_2160/grid/hFacS.data',nx);
landU = quikreadpcolor_llc('~dmenemen/llc_2160/grid/hFacW.data',nx);

p_tot = 0;
n = 1;
strNames = {};


pn='~dmenemen/llc_2160/MITgcm/'; % for ice read in - assume an hourly reading is good enough


n = 0;
Vdiff = 0;
Udiff = 0;
Vgt = 0;
Ugt  = 0;
Ut = 0;
Vt = 0;
Uekt = 0;
Vekt = 0;
for ii = 92160:(80*24):1586400;
                
    dy=ts2dte(ii,45,2011,1,17,30)
    
    fnamV = ['/nobackupp8/awinetee/global/V_30hLPF_hamming_daily/V' '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
    fnamU = ['/nobackupp8/awinetee/global/U_30hLPF_hamming_daily/U' '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];

    fnamUg = [folderUg 'Ug_noIce_hamming' int2str((siz(1))) 'x' int2str((siz(2))) '.' dy];
    fnamVg = [folderVg 'Vg_noIce_hamming' int2str((siz(1))) 'x' int2str((siz(2))) '.' dy];
    
    fnamVek = ['/nobackupp8/awinetee/global/Vek_30hLPF_hamming_daily/Vek_30hLPF_hamming_oceTAUY_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
    fnamUek = ['/nobackupp8/awinetee/global/Uek_30hLPF_hamming_daily/Uek_30hLPF_hamming_oceTAUX_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
        
    if exist(fnamUg) && exist(fnamVg) && exist(fnamU) && exist(fnamV) && exist(fnamVek) && exist(fnamUek)
        n = n + 1
        strNames{n} = dy;
        
        Ug = readbin(fnamUg,siz);
        Vg = readbin(fnamVg,siz);
        
        [U, V] = quikreadpcolor_uv_llc(fnamU,fnamV,nx);
        if (ii<1198080)
            pin=[pn 'run_day49_624/'];
        else
            pin=[pn 'run/'];
        end
        
        SIheff = quikreadpcolor_llc([pin 'SIheff.' myint2str(ii,10) '.data'],nx);
        
        
        Uek = readbin(fnamUek,siz);
        Vek = readbin(fnamVek,siz);
        
        Ug(Ug == 0 | abs(Ug) == inf | isnan(Ug)) = 0;
        Vg(Vg == 0 | abs(Vg) == inf | isnan(Vg)) = 0;
        
        U(U == 0 | abs(U) == inf) = 0;
        V(V == 0 | abs(V) == inf) = 0;
        
          
        
        Vt = Vt + V;
        Ut = Ut + U;
        Ugt = Ugt + Ug;
        Vgt = Vgt + Vg;
        Vekt = Vekt + Vek;
        Uekt = Uekt + Uek;
        
        Ugt(SIheff > .1) = Ugt(SIheff > .1) - Ug(SIheff > .1) + 1i;
        Vgt(SIheff > .1) = Vgt(SIheff > .1) - Vg(SIheff > .1) + 1i;
        Ut(SIheff > .1) = Ut(SIheff > .1) - U(SIheff > .1) + 1i;
        Vt(SIheff > .1) = Vt(SIheff > .1) -V(SIheff > .1) + 1i;
        Uekt(SIheff > .1) = Uekt(SIheff > .1) - Uek(SIheff > .1) + 1i;
        Vekt(SIheff > .1) = Vekt(SIheff > .1) - Vek(SIheff > .1) + 1i;
        
        %Vdiff = Vdiff + (Vp-Vgp);
        %Udiff = Udiff + (Up-Ugp);
    else
        disp('dne');
        
        
    end
end
%%


Vgtm = real(Vgt)./(n-imag(Vgt));
Ugtm = real(Ugt)./(n-imag(Ugt));

Utm = real(Ut)./(n-imag(Ut));
Vtm = real(Vt)./(n-imag(Vt));

Uektm = real(Uekt)./(n-imag(Uekt));
Vektm = real(Vekt)./(n-imag(Vekt));


Vgtm(landV==0) = nan;
Ugtm(landU==0) = nan;
Vtm(landV==0) = nan;
Utm(landU==0) = nan;
Vektm(landV==0) = nan;
Uektm(landU==0) = nan;

Ut_calc = Ugtm + Uektm;
Vt_calc = Vgtm + Vektm;

c = 0;
for ii = -90:2:90
    
    c = c + 1
    latVect(c)  = ii;
    latidx = find(abs(lats-ii)<2.5);
    ug_lats(c) = nanmean(Ugtm(latidx));
    vg_lats(c) = nanmean(Vgtm(latidx));
    
    u_lats(c) = nanmean(Utm(latidx));
    v_lats(c) = nanmean(Vtm(latidx));
    uek_lats(c) = nanmean(Uektm(latidx));
    vek_lats(c) = nanmean(Vektm(latidx));
    
    ut_calc_lats(c) = nanmean(Ut_calc(latidx));
    vt_calc_lats(c) = nanmean(Vt_calc(latidx));
end
  
    
figure('color','white');
plot(latVect,ug_lats,'r.-','linewidth',2);
hold on
plot(latVect,vg_lats,'b.-','linewidth',2);
plot(latVect,u_lats,'g.-','linewidth',4);
plot(latVect,v_lats,'m.-','linewidth',4);
plot(latVect,uek_lats,'k.-','linewidth',2);
plot(latVect,vek_lats,'c.-','linewidth',2);

plot(latVect,ut_calc_lats,'y.-','linewidth',4);
plot(latVect,vt_calc_lats,'b.-','linewidth',4);
legend('U_g','V_g','U_{tot}','V_{tot}','U_{ek}','V_{ek}','U_{g+ek}','V_{g+ek}')
    
    
figure('color','white');

hold on

plot(latVect,u_lats,'g.-','linewidth',4);
plot(latVect,v_lats,'m.-','linewidth',4);

plot(latVect,ut_calc_lats,'y.-','linewidth',4);
plot(latVect,vt_calc_lats,'b.-','linewidth',4);
legend('U_{tot}','V_{tot}','U_{g+ek}','V_{g+ek}')
    




fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor(Uektm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
set(c,'fontsize',16)
print('-dpng','-r400','~awinetee/Desktop/thesis/uek_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor(Vektm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
set(c,'fontsize',16)
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/vek_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor(Vgtm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/vg_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor(Ugtm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(c,'fontsize',16)
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/ug_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor(Vtm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/vp_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor(Utm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/up_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Vgtm+Vektm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
set(c,'fontsize',16)
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
set(c,'fontsize',16)
print('-dpng','-r400','~awinetee/Desktop/thesis/vg_vek_total_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Ugtm+Uektm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/ug_uek_total_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Utm + Vtm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/up_vp_total_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Ugtm+Vgtm+Vektm+Uektm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/ug_vg_uek_vek_total_2160.png');

fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Utm-Ugtm+Vtm-Vgtm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/ageo_total_2160.png');


fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Vtm-Vgtm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/v_ageo_2160.png');


fig = figure('color','white','units','normalized','outerposition',[0 0 1 1],'name','1');
quikpcolor((Utm-Ugtm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.6 .35])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c, 'Surface Current (m/s)')
set(get(gca,'title'),'fontsize',16);
a = get(c,'xlabel');
set(a,'fontsize',16);
grid on
print('-dpng','-r400','~awinetee/Desktop/thesis/u_ageo_2160.png');
