% Power  by lat


% Total Power 



clear
clc
folderVgp = ['/nobackupp8/awinetee/global/Vg_power_30hLFP_hamming_daily/'];
folderUgp = ['/nobackupp8/awinetee/global/Ug_power_30hLPF_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;
Vgpt = 0;
Ugpt  = 0;
Upt = 0;
Vpt = 0;
Uekpt = 0;
Vekpt = 0;
for ii = 92160:(80*24):1586400;
                
    dy=ts2dte(ii,45,2011,1,17,30)
    
    fnamVp = ['/nobackupp8/awinetee/global/V_oceTAUY_30hLPF_hamming_daily/V_oceTAUY' '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
    fnamUp = ['/nobackupp8/awinetee/global/U_oceTAUX_30hLPF_hamming_daily/U_oceTAUX' '_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];

    fnamUgp = [folderUgp 'Ug_power_' int2str((nx)) 'x' int2str((nx*13)) '.' dy];
    fnamVgp = [folderVgp 'Vg_power_' int2str((nx)) 'x' int2str((nx*13)) '.' 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];
    fnamTx = ['/nobackupp8/awinetee/global/oceTAUX_30hLPF_hamming_daily/oceTAUX_'  int2str((nx)) 'x' int2str((nx*13)) '.' dy];
    fnamTy = ['/nobackupp8/awinetee/global/oceTAUY_30hLPF_hamming_daily/oceTAUY_'  int2str((nx)) 'x' int2str((nx*13)) '.' dy];
        
    if exist(fnamUgp) && exist(fnamVgp) && exist(fnamUp) && exist(fnamVp) && exist(fnamVek) && exist(fnamUek) && exist(fnamTx) && exist(fnamTy)
        n = n + 1
        strNames{n} = dy;
        
%         Ugp = readbin(fnamUgp,siz);
%         Vgp = readbin(fnamVgp,siz);
%         
%         [Up, Vp] = quikreadpcolor_upvp_llc(fnamUp,fnamVp,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);
         [Tx, Ty] = quikreadpcolor_uv_llc(fnamTx,fnamTy,nx);
%         
%         Vekp = Vek.*Ty;
%         Uekp = Uek.*Tx;
%         
%         Ugp(Ugp == 0 | abs(Ugp) == inf | isnan(Ugp)) = 0;
%         Vgp(Vgp == 0 | abs(Vgp) == inf | isnan(Vgp)) = 0;
%         
%         Up(Up == 0 | abs(Up) == inf) = 0;
%         Vp(Vp == 0 | abs(Vp) == inf) = 0;
%         
%         Uekp(Uekp == 0 | abs(Uekp) == inf) = 0;
%         Vekp(Vekp == 0 | abs(Vekp) == inf) = 0;
%        
%          
%         
%         Vpt = Vpt + Vp;
%         Upt = Upt + Up;
%         Ugpt = Ugpt + Ugp;
%         Vgpt = Vgpt + Vgp;
%         Vekpt = Vekpt + Vekp;
%         Uekpt = Uekpt + Uekp;
%         
%         Ugpt(SIheff > .1) = Ugpt(SIheff > .1) - Ugp(SIheff > .1) + 1i;
%         Vgpt(SIheff > .1) = Vgpt(SIheff > .1) - Vgp(SIheff > .1) + 1i;
%         Upt(SIheff > .1) = Upt(SIheff > .1) - Up(SIheff > .1) + 1i;
%         Vpt(SIheff > .1) = Vpt(SIheff > .1) -Vp(SIheff > .1) + 1i;
%         Uekpt(SIheff > .1) = Uekpt(SIheff > .1) - Uekp(SIheff > .1) + 1i;
%         Vekpt(SIheff > .1) = Vekpt(SIheff > .1) - Vekp(SIheff > .1) + 1i;
%         
        %Vdiff = Vdiff + (Vp-Vgp);
        %Udiff = Udiff + (Up-Ugp);
        Txt =  Txt+Tx;
        Tyt = Tyt+Ty;
    else
        disp('dne');
        
        
    end
end
%%


Vgptm = real(Vgpt)./(n-imag(Vgpt));
Ugptm = real(Ugpt)./(n-imag(Ugpt));

Uptm = real(Upt)./(n-imag(Upt));
Vptm = real(Vpt)./(n-imag(Vpt));

Uekptm = real(Uekpt)./(n-imag(Uekpt));
Vekptm = real(Vekpt)./(n-imag(Vekpt));


Vgptm(landV==0) = nan;
Ugptm(landU==0) = nan;
Vptm(landV==0) = nan;
Uptm(landU==0) = nan;
Vekptm(landV==0) = nan;
Uekptm(landU==0) = nan;

Ut_calc = Ugptm + Uekptm;
Vt_calc = Vgptm + Vekptm;

c = 0;
for ii = -90:2:90
    
    c = c + 1
    latVect(c)  = ii;
    latidx = find(abs(lats-ii)<2.5);
    ug_lats(c) = nanmean(Ugptm(latidx));
    vg_lats(c) = nanmean(Vgptm(latidx));
    
    u_lats(c) = nanmean(Uptm(latidx));
    v_lats(c) = nanmean(Vptm(latidx));
    uek_lats(c) = nanmean(Uekptm(latidx));
    vek_lats(c) = nanmean(Vekptm(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(Uekptm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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(Vekptm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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(Vgptm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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(Ugptm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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(Vptm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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(Uptm');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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((Vgptm+Vekptm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
daspect([1 1 1])
set(c,'fontsize',16)
xlabel(c,'KE Flux (W/m^2)')
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((Ugptm+Uekptm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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((Uptm + Vptm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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((Ugptm+Vgptm+Vekptm+Uekptm)');
c=colorbar
colormap([160/255 160/255 160/255 ; flipud(brewermap(256,'Spectral'))])
caxis([-.01  .05])
set(gca,'xtick',[])
set(gca,'ytick',[])
set(c,'fontsize',16)
daspect([1 1 1])
xlabel(c,'KE Flux (W/m^2)')
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');
