 clear all
%close al
%%
%dirstr=['/nobackup1/stephdut/DAR3_20190613/'];
%runstr=['run24_2_new_trich2'],
 mnyr=1; %1-mon,2=year
 dirstr=['/nobackup1/stephdut/DAR4_20200507/'];
 runstr=['run29_51_holl2_mixo'],
 if (mnyr==1), run1str=[dirstr,runstr,'/MONTHS']; mystr='m'; timmat=[12:12]; end
 if (mnyr==2), run1str=[dirstr,runstr,'/Years']; mystr='y'; timmat=[12]; yr=10; end
 nphy=31; nplank=50;
%
 time=[timmat];

 for it=time
  gridname=['grid_ecco.nc'];
  X=ncread(gridname,'X');
  Y=ncread(gridname,'Y');
  Z=ncread(gridname,'Z');
  drF=ncread(gridname,'drF');
  rA=ncread(gridname,'rA');
  HFacC=ncread(gridname,'HFacC');
  Zp1=ncread(gridname,'Zp1');
  Depth=ncread(gridname,'Depth');
%
  nx=length(X); ny=length(Y); nz=length(Z);
  for k=1:nz,
    volc(k,:,:)=rA(:,:)*drF(k);
  end
  filand1=find(squeeze(HFacC(:,:,1))==0);

  tstr=['00000',num2str(25920+it*240)],
  for idiag=1:6,
   if (idiag==1), ncid1=netcdf.open([run1str,'/3d.',tstr,'.nc']); end
   if (idiag==2), ncid1=netcdf.open([run1str,'/nutr_tend.',tstr,'.nc']); end
   if (idiag==3), ncid1=netcdf.open([run1str,'/PP.',tstr,'.nc']); end
   if (idiag==4), ncid1=netcdf.open([run1str,'/GR.',tstr,'.nc']); end
   if (idiag==5), ncid1=netcdf.open([run1str,'/GRGN.',tstr,'.nc']); end
   if (idiag==6), ncid1=netcdf.open([run1str,'/chl.',tstr,'.nc']); end
     [ndim,nvar,natt,unlim]=netcdf.inq(ncid1);

     for i=0:ndim-1,
      [dimname, dimlength]=netcdf.inqDim(ncid1,i);
      diml(i+1)=dimlength;
     end % for i

     for i=0:nvar-1;

      [varname, xtype,dimid,natt]=netcdf.inqVar(ncid1,i);
      if (length(dimid)==1),
        eval([varname,'=netcdf.getVar(ncid1,i,0,diml(dimid+1));']);
      end % if
      if (length(dimid)==2),
        eval([varname,'=netcdf.getVar(ncid1,i,[0 0],[diml(dimid(1)+1) diml(dimid(2)+1)] );']);
      end % if
      if (length(dimid)==3),
        eval([varname,'=netcdf.getVar(ncid1,i,[0 0 0],[diml(dimid(1)+1) diml(dimid(2)+1) diml(dimid(3)+1)] );']);
      end % if
      if (length(dimid)==4),
         eval([varname,'=netcdf.getVar(ncid1,i,[0 0 0 0],[diml(dimid(1)+1) diml(dimid(2)+1) diml(dimid(3)+1)  diml(dimid(4)+1)] );']);
       end % if
        eval([varname,'=double(',varname,');']);
     end % for i

   end % for % idiag
   typstr='diag52b'; 
   clear mstr
   mstr=textread([typstr,'_list_rad_car.data'],'%c','delimiter','\n','whitespace','');
   for ip=1:nplank
       str=mstr(6*(ip-1)+1:6*ip)';
       eval(['car(',num2str(ip),',:,:,:)=',str,';']);
   end % for ip
   clear mstr
   mstr=textread([typstr,'_list_rad_chl_noquota.data'],'%c','delimiter','\n','whitespace','');
%        if (diagmistake==1), nplast=nphyt-1; else  nplast=nphyt; end
   for ip=1:nphy
          str=mstr(6*(ip-1)+1:6*ip)';
          eval(['chl(',num2str(ip),',:,:,:)=',str,';']);
   end % for ip


   for ip=1:6
      eval(['nutr(',num2str(ip),',:,:,:)=TRAC0',num2str(ip+1),';']);
      eval(['nutr_source(',num2str(ip),',:,:,:)=Tp_gTr0',num2str(ip+1),';']);
   end %

   for ip=1:nphy
     if (ip<10), 
      eval(['pp(',num2str(ip),',:,:,:)=PP000',num2str(ip),';']);
     else
      eval(['pp(',num2str(ip),',:,:,:)=PP00',num2str(ip),';']);
     end
   end %
  for ip=1:nplank
     if (ip<10),
      eval(['gr(',num2str(ip),',:,:,:)=GR000',num2str(ip),';']);
     else
      eval(['gr(',num2str(ip),',:,:,:)=GR00',num2str(ip),';']);
     end
   end %
  for ip=1:nplank
     if (ip<10),
      eval(['grgn(',num2str(ip),',:,:,:)=GrGn000',num2str(ip),';']);
     else
      eval(['grgn(',num2str(ip),',:,:,:)=GrGn00',num2str(ip),';']);
     end
   end %


  if (mnyr==1), itt=it; end
  if (mnyr==2), itt=yr; end

   eval(['save /nfs/micklab001/stephdut/DAR4_MAT/',runstr,'_',mystr,num2str(itt),'_car_nut_gr.mat X Y Z car chl pp  gr grgn nutr nutr_source S_DIN S_Fe S_PO4 S_Si']);

%  eval(['save /nfs/micklab001/stephdut/DAR4_MAT/',runstr,'_',mystr,num2str(it),'_chl.mat X Y Z chl']);

 end % for it

