function []=extend_xx_mod(dirIn,dirOut,ndayIn,nyearOut,nx,ny);
%copy from gcmfaces/eccov4/, modified to remove requirement
%to load gcmfaces stuff and grid
%object:	extend atmospheric controls to additional years
%inputs:	dirIn is the input directory
%               dirOut is the output directory
%               ndayIn is the control vector frecuency, in days
%		nyearOut is the extended number of years
%
%note:		we will take the time mean seasonal cycle, and transition
%		to it linearly over the last year that was already covered

%gcmfaces_global;
%keyboard
%%%%%%%%%%%%
%define ASTE
%%%%%%%%%%%%

%if(sum(mygrid.ioSize-[270*1350 1])==0);
%  use_atn_wetpt=1;
%  ioSize=mygrid.ioSize;
%  dimSum=2;
%  mskC=squeeze(convert2gcmfaces(mygrid.mskC(:,:,1)));		%ioSize
%  iwet=find(mskC==1);						%146620
%  Lwet=length(iwet);
%  RAC=squeeze(convert2gcmfaces(mygrid.RAC));RAC=RAC(iwet);	%[Lwet 1]
%  mskC=mskC(iwet);						%[Lwet 1]
%else;
  %dimSum=3;
  %ioSize=mygrid.ioSize;
  %iwet=1:prod(ioSize);						%105300
  %Lwet=length(iwet);
  %mskC=mygrid.mskC(:,:,1);					%ioSize
  %RAC=mygrid.RAC;						%[Lwet 1]
%end;
%nx=270;ny=1350;
dirGrid= %real4 or real8
hFacC=readbin([dirGrid 'hFacC.data'],[nx ny],1,precIn);
iwet=find(hFacC(:)>0);

%do_copy=0;
%if(do_copy);
%%first copy time independent controls
%listCp={'diffkr','kapgm','kapredi','salt','theta'};
%for ii=1:length(listCp);
%  fileIn=dir([dirIn 'ADXXfiles/xx_' listCp{ii} '.00*data']);
%  copyfile([dirIn 'ADXXfiles/' fileIn.name] , [dirOut fileIn.name]);
%end;
%end;

%then extend the time dependent ones
%atn: do 2 loops, 1 for xx, for xx_effective
%for iloop=1:1;%2;
%  if(iloop==1);ext='';else;ext='.effective';end;
%for ii=1:7;
%switch ii;
%case 1; xxName='atemp'; 
%case 2; xxName='aqh'; 
%case 3; if(dimSum==3);xxName='tauu'; else;xxName='uwind';end;
%case 4; if(dimSum==3);xxName='tauv'; else;xxName='vwind';end;
%case 5; xxName='lwdown'; 
%case 6; xxName='swdown'; 
%case 7; xxName='precip';
%end;

str={'precip','aqh','..}
for ivar=1:7
  tmp=dir([dirIn 'xx_' str{ivar} '.' sprintf('%10.10i',62) '.data']); %tmp.bytes
  nrec=tmp.bytes/nx/ny/4;                               %for ASTER1, nrec=419
  fld_xx=readbin([tmp.folder '/' tmp.name],[nx*ny nrec]);
  fld_xx=fld_xx(iwet,:);  
  sz=size(xx);
  dimSum=length(sz);

%read model cost output
%if(dimSum==3);
%  file0=[dirIn 'ADXXfiles/xx_' xxName ext '.00*.data'];
%  tmp=dir(file0);
%  file0=tmp.name;nrec=tmp.bytes/prod(ioSize)/4;		%367 for 2002-2015, now extend to 419
%  fld_xx=NaN*repmat(RAC,[1 1 nrec]);			%gcmfaces2d | ioSize
%  fld_xx=rdmds2gcmfaces([dirIn 'ADXXfiles/xx_' xxName ext '.00*']);
%  %nrec=size(fld_xx{1},3);
%elseif(dimSum==2);;
%  file0=[dirIn 'ADXXfiles/xx_' xxName ext '.00*.data'];
%  tmp=dir(file0);
%  file0=tmp.name;nrec=tmp.bytes/prod(ioSize)/4;		%367 for 2002-2015, now extend to 419
%  fld_xx=NaN*repmat(RAC,[1 nrec]);			%[Lwet nrec]
%  fid=fopen([dirIn 'ADXXfiles/' file0],'r','b');
%  for mcur=1:nrec
%    tmp=fread(fid,[ioSize(1) ioSize(2)],'float32');	%opens once, skip sequentially, should be a lot faster: 2.73sec
%    %tmp=read_slice([dirIn 'ADXXfiles/' file0],ioSize(1),ioSize(2),mcur);	%ioSize , 5.43sec
%    fld_xx(:,mcur)=tmp(iwet);							%ioSize | [Lwet 1]
%  end;
%  fclose(fid);
%end;

%determine already covered time period
  fprintf('nrec old: %i\n',nrec);
  nyearIn=nrec*ndayIn/365;                              %16.0712
  nrecInOneYear=round(365/ndayIn);                      %26
  nrecOut=nrecInOneYear*nyearOut+12;%1;			%atn: seems i need to add 3 , not 1; for 2002:2022 (add12) = 558
  fprintf('nrec new: %i\n',nrecOut);

%determine seasonal cycle
  if(dimSum==3);
    season_xx=0*fld_xx(:,:,1:nrecInOneYear);
    for tt=1:nrecInOneYear;
      season_xx(:,:,tt)=mean(fld_xx(:,:,tt:nrecInOneYear:nrec),3);
    end;
  elseif(dimSum==2);
    season_xx=0*fld_xx(:,1:nrecInOneYear);
    for tt=1:nrecInOneYear;
      season_xx(:,tt)=mean(fld_xx(:,tt:nrecInOneYear:nrec),2);
    end;
  end;

%if nyearOut=0, we're asking for the seasonal cycle
  if nyearOut==0;
    more_xx=season_xx;			%[nx ny 1] or [Lwet 1]
  else;
%determine transition factor (fld_xx -> season_xx)
%atn:  starting 29 rec before to do transition, which is 1 year (1yr has 26 rec)
    nrec0=nrecInOneYear*(floor(nyearIn)-1); 	%338, for nrec=367, gael code
    fac=([1:nrecOut]-nrec0)/(nrec-nrec0);	%[1 419] in size
    fac=max(min(fac,1),0);		        %zeros 1-338, scale up from [0.0345 to 1] from 338-367, then 1 after

%build extended time series
    if(dimSum==3);
      more_xx=zeros(nx,ny,nrecOut);
      more_xx(:,:,1:nrec0)=fld_xx(:,:,1:nrec0);
      for tt=nrec0+1:nrecOut;
        ttt=mod(tt,nrecInOneYear);
        if ttt==0; ttt=nrecInOneYear; end;
        if tt<nrec; 
          more_xx(:,:,tt)=fac(tt)*season_xx(:,:,ttt)+(1-fac(tt))*fld_xx(:,:,tt);
        else; 
          more_xx(:,:,tt)=season_xx(:,:,ttt);
        end;
      end;
    else;
      more_xx=zeros(size(fld_xx,1),nrecOut);
      more_xx(:,1:nrec0)=fld_xx(:,1:nrec0);
      for tt=nrec0+1:nrecOut;
        ttt=mod(tt,nrecInOneYear);
        if ttt==0; ttt=nrecInOneYear; end;
        if tt<nrec; 
          more_xx(:,tt)=fac(tt)*season_xx(:,ttt)+(1-fac(tt))*fld_xx(:,tt);
        else; 
          more_xx(:,tt)=season_xx(:,ttt);
        end;
      end;
    end;

%to check the transition:
    if(1);		%<-- atn put this here, because not sure what to do during checking
      if(dimSum==3);
        tmp1=fld_xx;  sz=size(tmp1);tmp1=reshape(tmp1,sz(1)*sz(2),sz(3));tmp11=squeeze(nanmean(tmp1,1));	%compact
        tmp2=more_xx; sz=size(tmp2);tmp2=reshape(tmp2,sz(1)*sz(2),sz(3));tmp22=squeeze(nanmean(tmp2,1));  %compact
      elseif(dimSum==2);
        tmp11=squeeze(mean(fld_xx,1));				%[Lwet 1]
        tmp22=squeeze(mean(more_xx,1));  				%[Lwet 1]
      end;
      figure(1);clf;plot(tmp22,'r-');hold on;plot(tmp11,'b-');hold off;
      legend('new xx mean','old xx mean');xlabel('record');ylabel([xxName ' mean for each record']);
      if(length(ext)>0);extp=['_' ext(2:end)];else;extp='';end;
      fpr=[dirOut xxName extp '_extended' '_' num2str(nrecOut) '.png'];saveas(gca,fpr,'png');
    end;
  end;

%save to file
  fileIn=dir([dirIn 'ADXXfiles/xx_' str{ivar} ext '.00*data']);
  if(dimSum==3);
    write2file([dirOut fileIn.name],more_xx);
  elseif(dimSum==2);
    fidout=fopen([dirOut fileIn.name],'w','b');
    for mcur=1:nrecOut;
      tmp=zeros(nx,ny);tmp(iwet)=more_xx(:,mcur);
      fwrite(fidout,tmp,'float32');
    end;
    fclose(fidout);		%3.95sec
    fprintf('%s\n',[dirOut fileIn.name]);
  end;

end;%for ii=1:6; %loop over ctrls

end;%iloop % xx vs xx.effective
