clear all
dirIn='/net/nares/raid9/ecco-shared/DATA/atmos/matlab/';
dirOut=dirIn;
str={'JRA25g','EIG'};str1={'jra','eig'};
varstr={'dlw','dsw','tmp2m_degC','dlw_cor25'};
%yrrange=1991:2012;
yrrange=1991:2014;

%--- era-interim grid -----------------------------------------
nlon        = 512;
nlat        = 256;
lon0 = 0;
dlon = 0.7031250;
lon  = lon0:dlon:360-dlon;
lat0 = -89.4628220;
dlat = [0,0.6958694,0.6999817,0.7009048,0.7012634,0.7014313,...
        0.7017418.*ones(1,245),...
        0.7014313,0.7012634,0.7009048,0.6999817,0.6958694];
lat=lat0+cumsum(dlat);
%-------------------------------------------------------------
dirIn='/net/nares/raid11/ecco-shared/atmos-reanalysis/era-interim/';
dirOut='/net/nares/raid9/ecco-shared/DATA/atmos/era-interim/';if(exist(dirOut)==0);mkdir(dirOut);end;

%remove 25to40 W/m^2 in seasonal cycle
make_dlwcor25=0;
if(make_dlwcor25==1);
strIn='EIG_dlw_';strOut='EIG_dlw_cor25_';

%9-12,1-3: subtract 25W/m^2 from dlw in eig, blend between 65N to 70N
%09 10 11 12 01 02 03
%25 40 30 25 25 25 25
%note; dlw is negative down, so add positive = remove dlw = what i want
i_noleap=[1:90,90+16:243,243+15,273+15,304+15,334+15:366];
cor_noleap=[25.*ones(1,90),0.*ones(1,length(90+16:243)),25,40,30,25.*ones(1,length(334+15:366))];
cor_noleap1=interp1(i_noleap,cor_noleap,1:.25:365.75,'linear');
cor_leap1=[cor_noleap1,25.*ones(1,4)];

ilat=find(lat>65&lat<70);Lblend=length(ilat);
ilat1=find(lat>65);
[cor1,b]=meshgrid(cor_noleap1,1:length(ilat1)*nlon);clear b
[cor2,b]=meshgrid(cor_leap1,1:length(ilat1)*nlon);clear b
cor1=reshape(cor1,nlon,length(ilat1),4*365);
cor2=reshape(cor2,nlon,length(ilat1),4*366);
for k=1:length(yrrange);
  fIn=dir([dirIn strIn sprintf('%4.4i',(yrrange(k))]);
  nt=fIn.bytes/nlon/nlat/4;
  if(nt==365*4);cor=cor1;elseif(nt==366*4);cor=cor2;end;
  ff=readbin([dirIn fIn.name],[nlon nlat nt]);
  ffp=ff;ffp(:,ilat1,:)=ffp(:,ilat1,:)+cor;
  for i=1:Lblend;
    w1=i./Lblend;w2=1-w1;%fprintf('%i %f %f\n',[i,w1,w2]);
    ffp(:,ilat(i),:)=w2.*ff(:,ilat(i),:)+w1.*ffp(:,ilat(i),:);
  end;
  fOut=[dirOut strOut sprintf('%4.4i',yrrange(k))];writebin(fOut,ffp,1,'real*4');fprintf('%s\n',fOut);
end;
end;

%remove 5% dsw above 65, to be blended between 65N to 70N
make_dlwcor5p=0;
fract=0.95;
if(make_dlwcor5p==1);
  strIn='EIG_dlw_';strOut='EIG_dlw_sub5p_';
%note; dlw is negative down, so add positive = remove dlw = what i want
 ilat=find(lat>65&lat<70);Lblend=length(ilat);
 ilat1=find(lat>65);
 for k=1:length(yrrange);
   fIn=dir([dirIn strIn sprintf('%4.4i',yrrange(k))]);
   nt=fIn.bytes/nlon/nlat/4;
   ff=readbin([dirIn fIn.name],[nlon nlat nt]);
   ffp=ff;ffp(:,ilat1,:)=fract.*ffp(:,ilat1,:);
   for i=1:Lblend;
     w1=i./Lblend;w2=1-w1;%fprintf('%i %f %f\n',[i,w1,w2]);
     ffp(:,ilat(i),:)=w2.*ff(:,ilat(i),:)+w1.*ffp(:,ilat(i),:);
   end;
   fOut=[dirOut strOut sprintf('%4.4i',yrrange(k))];writebin(fOut,ffp,1,'real*4');fprintf('%s\n',fOut);
 end;
figure(1);clf;subplot(131);mypcolor(-ff(:,:,1)');cc=caxis;caxis(cc);thincolorbar;grid;
	title(['dlw 0-6h, jan/' sprintf('%4.4i',yrrange(k))]);
subplot(132);mypcolor(-ffp(:,:,1)');caxis(cc);thincolorbar;grid;title('dlw sub 5per');
subplot(133);mypcolor(abs(ff(:,:,1)')-abs(ffp(:,:,1)'));thincolorbar;grid;('difference');
  fpr=[dirOut 'dlw_sub5p.png'];set(gcf,'paperunits','inches','paperposition',[0 0 13 5]);
  print(fpr,'-dpng');fprintf('%s\n',fpr);
end;

make_tmpClat=0;
if(make_tmpClat==1);
  strIn='EIG_tmp2m_degC_';strOut='EIG_tmp2mcor_degC_';

  lati= [65  70  75  80  85  90];ilat=find(lat>lati(1));
  Ti  =-[0.0 0.5 1.0 1.5 2.0 2.5];
  Tp=zeros(size(lat));Tp(ilat)=interp1(lati,Ti,lat(ilat),'linear');
  Tcors=meshgrid(Tp(ilat),1:4*365);Tcors=mk3D_mod(Tcors,zeros(4*365,length(ilat),nlon));Tcors=permute(Tcors,[3,2,1]);
  Tcorl=meshgrid(Tp(ilat),1:4*366);Tcorl=mk3D_mod(Tcorl,zeros(4*366,length(ilat),nlon));Tcorl=permute(Tcorl,[3,2,1]);
  for k=1:length(yrrange);
    clear Tcor ff ffp fIn nt
    fIn=dir([dirIn strIn sprintf('%4.4i',yrrange(k))]);
    nt=fIn.bytes/nlon/nlat/4;
    if(nt==365*4);Tcor=Tcors;elseif(nt==366*4);Tcor=Tcorl;end;
    ff=readbin([dirIn fIn.name],[nlon nlat nt]);
    ffp=ff;ffp(:,ilat,:)=ff(:,ilat,:)+Tcor;
    fOut=[dirOut strOut sprintf('%4.4i',yrrange(k))];writebin(fOut,ffp,1,'real*4');fprintf('%s\n',fOut);
  end;
  figure(1);clf;subplot(121);mypcolor(ff(:,:,1)');thincolorbar;subplot(122);mypcolor(ffp(:,:,1)');thincolorbar;

%this only works after everything is already generated but didn't print out fig above, so will read in monthly field instead:
  plot_it=0;
  if(plot_it==1);
    b=readbin('/net/nares/raid11/ecco-shared/atmos-reanalysis/era-interim/EIG_tmp2m_degC_2012_monav',[nlon,nlat]);
    a=readbin('/net/nares/raid9/ecco-shared/DATA/atmos/era-interim/+monav/EIG_tmp2mcor_degC_2012_monav',[nlon,nlat]);
    figure(1);clf;
    subplot(131);mypcolor(b');thincolorbar;grid;title('EIG\_tmp2m\_degC\Jan2012monthlymean');
    subplot(132);mypcolor(a');thincolorbar;grid;title('EIG\_tmp2mcor\_degC\Jan2012monthlymean');
    subplot(133);mypcolor(b'-a');thincolorbar;grid;title('original minus corrected');
    fpr=[dirOut 'tmp2mcor.png'];set(gcf,'paperunits','inches','paperposition',[0 0 13 5]);
    print(fpr,'-dpng');fprintf('%s\n',fpr);
  end;
end;

%25.aug.2014: post WWOSC meeting in MOntreal: should correct air temperature, max 1.5 deg
interp2llc270=0;			%<-- this is to put era to xx0 for reading in.
make_tmpClatv2=1;
use_icemask=1;if(use_icemask==1);ext_IceMask='icemask';else;ext_IceMask='';end;
dirIceMask='/net/nares/raid9/ecco-shared/DATA/atmos/era-interim/+osisaf_icemask/';
if(make_tmpClatv2==1);
  strIn='EIG_tmp2m_degC_';strOut=['EIG_tmp2mcorv2' ext_IceMask '_degC_'];

  lati= [65  70  75  80  85  90];ilat=find(lat>lati(1));
  Ti  =-[0.0 0.5 1.0 1.5 1.5 1.5];
  Tp=zeros(size(lat));Tp(ilat)=interp1(lati,Ti,lat(ilat),'linear');
  lonpp=[lon(1)-dlon,lon,lon(end)+dlon,lon(end)+2*dlon];		%wrap, [515 x 257]
  Tmap=meshgrid(Tp,lonpp);Tmap=[Tmap,Tmap(:,end)];latpp=[lat,lat(end)+dlat(end)];
  if(interp2llc270==1);
    xc=readbin(['/net/nares/raid8/ecco-shared/llc270/global/GRID/XC.data'],[270 270*13]);
    yc=readbin(['/net/nares/raid8/ecco-shared/llc270/global/GRID/YC.data'],[270 270*13]);
    ixc=find(xc(:)<0);xc(ixc)=xc(ixc)+360;
    Tllc270=griddata(lonq,latq,Tmap,xc,yc,'linear');
    writebin('/net/nares/raid8/ecco-shared/llc270/global/input/input_atmos/EIG_tmp2mcorv2_degC_llc270.bin',Tllc270);
    nfx=[270 0 270 180 450];nfy=[450 0 270 270 270];
    temp=get_aste_tracer(Tllc270,nfx,nfy);temp=aste_tracer2compact(temp,nfx,nfy);
    writebin('/net/nares/raid8/ecco-shared/llc270/aste_270x450x180/input/input_atmos/EIG_tmp2mcorv2_degC_llc270.bin',temp);
  end;
  Tcors=meshgrid(Tp(ilat),1:4*365);Tcors=mk3D_mod(Tcors,zeros(4*365,length(ilat),nlon));Tcors=permute(Tcors,[3,2,1]);
  Tcorl=meshgrid(Tp(ilat),1:4*366);Tcorl=mk3D_mod(Tcorl,zeros(4*366,length(ilat),nlon));Tcorl=permute(Tcorl,[3,2,1]);
  for k=1:length(yrrange);
    clear Tcor ff ffp fIn nt
    fIn=dir([dirIn strIn sprintf('%4.4i',yrrange(k))]);
    nt=fIn.bytes/nlon/nlat/4;
    if(nt==365*4);Tcor=Tcors;elseif(nt==366*4);Tcor=Tcorl;end;

    if(use_icemask==1);
      fIce=dir([dirIceMask '_*' sprintf('%4.4i',yrrange(k))]);
      IceMask=readbin([dirIceMask fIce],[nlon nlat nt]);
    else;
      IceMask=1;
    end;

    ff=readbin([dirIn fIn.name],[nlon nlat nt]);
    ffp=ff;ffp(:,ilat,:)=ff(:,ilat,:)+Tcor.*IceMask;
    fOut=[dirOut strOut sprintf('%4.4i',yrrange(k))];writebin(fOut,ffp,1,'real*4');fprintf('%s\n',fOut);
  end;
  figure(1);clf;subplot(121);mypcolor(ff(:,:,1)');thincolorbar;subplot(122);mypcolor(ffp(:,:,1)');thincolorbar;

%this only works after everything is already generated but didn't print out fig above, so will read in monthly field instead:
  plot_it=0;
  if(plot_it==1);
    b=readbin('/net/nares/raid11/ecco-shared/atmos-reanalysis/era-interim/EIG_tmp2m_degC_2012_monav',[nlon,nlat]);
    a=readbin('/net/nares/raid9/ecco-shared/DATA/atmos/era-interim/+monav/EIG_tmp2mcor_degC_2012_monav',[nlon,nlat]);
    figure(1);clf;
    subplot(131);mypcolor(b');thincolorbar;grid;title('EIG\_tmp2m\_degC\Jan2012monthlymean');
    subplot(132);mypcolor(a');thincolorbar;grid;title('EIG\_tmp2mcor\_degC\Jan2012monthlymean');
    subplot(133);mypcolor(b'-a');thincolorbar;grid;title('original minus corrected');
    fpr=[dirOut 'tmp2mcor.png'];set(gcf,'paperunits','inches','paperposition',[0 0 13 5]);
    print(fpr,'-dpng');fprintf('%s\n',fpr);
  end;
end;

%20.Oct.2014: reduce temp correction further
make_tmpClatv3=0;
if(make_tmpClatv3==1);
  strIn='EIG_tmp2m_degC_';strOut='EIG_tmp2mcorv3_degC_';

  lati= [65  70  75  80  85  90];ilat=find(lat>lati(1));
  Ti  =-[0.0 0.0 0.25 0.5 0.75 1.0];
  Tp=zeros(size(lat));Tp(ilat)=interp1(lati,Ti,lat(ilat),'linear');
  Tcors=meshgrid(Tp(ilat),1:4*365);Tcors=mk3D_mod(Tcors,zeros(4*365,length(ilat),nlon));Tcors=permute(Tcors,[3,2,1]);
  Tcorl=meshgrid(Tp(ilat),1:4*366);Tcorl=mk3D_mod(Tcorl,zeros(4*366,length(ilat),nlon));Tcorl=permute(Tcorl,[3,2,1]);
  for k=1:length(yrrange);
    clear Tcor ff ffp fIn nt
    fIn=dir([dirIn strIn sprintf('%4.4i',yrrange(k))]);
    nt=fIn.bytes/nlon/nlat/4;
    if(nt==365*4);Tcor=Tcors;elseif(nt==366*4);Tcor=Tcorl;end;
    ff=readbin([dirIn fIn.name],[nlon nlat nt]);
    ffp=ff;ffp(:,ilat,:)=ff(:,ilat,:)+Tcor;
    fOut=[dirOut strOut sprintf('%4.4i',yrrange(k))];writebin(fOut,ffp,1,'real*4');fprintf('%s\n',fOut);
  end;
  figure(1);clf;subplot(121);mypcolor(ff(:,:,1)');thincolorbar;subplot(122);mypcolor(ffp(:,:,1)');thincolorbar;

%this only works after everything is already generated but didn't print out fig above, so will read in monthly field instead:
  plot_it=0;
  if(plot_it==1);
    b=readbin('/net/nares/raid11/ecco-shared/atmos-reanalysis/era-interim/EIG_tmp2m_degC_2012_monav',[nlon,nlat]);
    a=readbin('/net/nares/raid9/ecco-shared/DATA/atmos/era-interim/+monav/EIG_tmp2mcor_degC_2012_monav',[nlon,nlat]);
    figure(1);clf;
    subplot(131);mypcolor(b');thincolorbar;grid;title('EIG\_tmp2m\_degCv3\Jan2012monthlymean');
    subplot(132);mypcolor(a');thincolorbar;grid;title('EIG\_tmp2mcor\_degCv3\Jan2012monthlymean');
    subplot(133);mypcolor(b'-a');thincolorbar;grid;title('original minus corrected');
    fpr=[dirOut 'tmp2mcorv3.png'];set(gcf,'paperunits','inches','paperposition',[0 0 13 5]);
    print(fpr,'-dpng');fprintf('%s\n',fpr);
  end;
end;

