clear all
close all

nplank=50;


load ../Diags_nasaviz/run33_6_gradients_timeseries_2d.mat
gmon=5; gmonstr=['may'];  glong=158;
%load ../Diags_nasaviz/run33_6_g4g5_timeseries_2d.mat
%gmon=11; gmonstr=['nov'];  glong=140;

ntim=12;
for it=1:ntim,
 carmon(:,:,:,it)=squeeze(mean(car(:,:,:,(it-1)*10+1:it*10),4)); 
 time(it)=it;
end % for it

long=glong*ones(size(lat));
nx=length(long);
ny=length(lat);
nz=length(depth);

planknum=[1:50];


 tmpcar=carmon;
 outstr=['darwin_transect_0to250m_allmonths.nc'];
 ncid=netcdf.create([outstr],'NC_WRITE');

 dimnx=netcdf.defDim(ncid,'domainLong',nx);
 dimny=netcdf.defDim(ncid,'domainLat',ny);
 dimnz=netcdf.defDim(ncid,'domainDepth',nz);
 dimnt=netcdf.defDim(ncid,'domainMonth',ntim);
 dimnp=netcdf.defDim(ncid,'planktonNum',nplank);

 long_ID=netcdf.defVar(ncid,'long','double',[dimnx]);
 lat_ID=netcdf.defVar(ncid,'lat','double',[dimny]);
 depth_ID=netcdf.defVar(ncid,'depth','double',[dimnz]);
 time_ID=netcdf.defVar(ncid,'month','double',[dimnt]);
 plankton_ID=netcdf.defVar(ncid,'planktonNum','double',[dimnp]);

 carbon_ID=netcdf.defVar(ncid,'biomass','double',[dimnp dimny dimnz dimnt]);

 netcdf.endDef(ncid);

 netcdf.putVar(ncid,long_ID,long);
 netcdf.putVar(ncid,lat_ID,lat);
 netcdf.putVar(ncid,depth_ID,depth);
 netcdf.putVar(ncid,time_ID,time);
 netcdf.putVar(ncid,plankton_ID,planknum);
 netcdf.putVar(ncid,carbon_ID,tmpcar);

 netcdf.close(ncid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
 nmon=gmon;
 tmpcar=squeeze(carmon(:,:,:,nmon));
 outstr=['darwin_transect_0to250m_',gmonstr,'.nc'];
 ncid=netcdf.create([outstr],'NC_WRITE');

 dimnx=netcdf.defDim(ncid,'domainLong',nx);
 dimny=netcdf.defDim(ncid,'domainLat',ny);
 dimnz=netcdf.defDim(ncid,'domainDepth',nz);
 dimnt=netcdf.defDim(ncid,'domainMonth',1);
 dimnp=netcdf.defDim(ncid,'planktonNum',nplank);

 long_ID=netcdf.defVar(ncid,'long','double',[dimnx]);
 lat_ID=netcdf.defVar(ncid,'lat','double',[dimny]);
 depth_ID=netcdf.defVar(ncid,'depth','double',[dimnz]);
 time_ID=netcdf.defVar(ncid,'month','double',[dimnt]);
 plankton_ID=netcdf.defVar(ncid,'planktonNum','double',[dimnp]);

 carbon_ID=netcdf.defVar(ncid,'biomass','double',[dimnp dimny dimnz ]);

 netcdf.endDef(ncid);

 netcdf.putVar(ncid,long_ID,long);
 netcdf.putVar(ncid,lat_ID,lat);
 netcdf.putVar(ncid,depth_ID,depth);
 netcdf.putVar(ncid,time_ID,nmon);
 netcdf.putVar(ncid,plankton_ID,planknum);
 netcdf.putVar(ncid,carbon_ID,tmpcar);

 netcdf.close(ncid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 nmon=gmon; ndep=depth(1);
 tmpcar=squeeze(carmon(:,:,1,nmon));
 outstr=['darwin_transect_surface_',gmonstr,'.nc'];
 ncid=netcdf.create([outstr],'NC_WRITE');

 dimnx=netcdf.defDim(ncid,'domainLong',nx);
 dimny=netcdf.defDim(ncid,'domainLat',ny);
 dimnz=netcdf.defDim(ncid,'domainDepth',1);
 dimnt=netcdf.defDim(ncid,'domainMonth',1);
 dimnp=netcdf.defDim(ncid,'planktonNum',nplank);

 long_ID=netcdf.defVar(ncid,'long','double',[dimnx]);
 lat_ID=netcdf.defVar(ncid,'lat','double',[dimny]);
 depth_ID=netcdf.defVar(ncid,'depth','double',[dimnz]);
 time_ID=netcdf.defVar(ncid,'month','double',[dimnt]);
 plankton_ID=netcdf.defVar(ncid,'planktonNum','double',[dimnp]);

 carbon_ID=netcdf.defVar(ncid,'biomass','double',[dimnp dimny]);

 netcdf.endDef(ncid);

 netcdf.putVar(ncid,long_ID,long);
 netcdf.putVar(ncid,lat_ID,lat);
 netcdf.putVar(ncid,depth_ID,ndep);
 netcdf.putVar(ncid,time_ID,nmon);
 netcdf.putVar(ncid,plankton_ID,planknum);
 netcdf.putVar(ncid,carbon_ID,tmpcar);

 netcdf.close(ncid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 ndep=depth(1);
 tmpcar=squeeze(carmon(:,:,1,:));
 outstr=['darwin_transect_surface_allmonths.nc'];
 ncid=netcdf.create([outstr],'NC_WRITE');

 dimnx=netcdf.defDim(ncid,'domainLong',nx);
 dimny=netcdf.defDim(ncid,'domainLat',ny);
 dimnz=netcdf.defDim(ncid,'domainDepth',1);
 dimnt=netcdf.defDim(ncid,'domainMonth',ntim);
 dimnp=netcdf.defDim(ncid,'planktonNum',nplank);

 long_ID=netcdf.defVar(ncid,'long','double',[dimnx]);
 lat_ID=netcdf.defVar(ncid,'lat','double',[dimny]);
 depth_ID=netcdf.defVar(ncid,'depth','double',[dimnz]);
 time_ID=netcdf.defVar(ncid,'month','double',[dimnt]);
 plankton_ID=netcdf.defVar(ncid,'planktonNum','double',[dimnp]);

 carbon_ID=netcdf.defVar(ncid,'biomass','double',[dimnp dimny dimnt]);

 netcdf.endDef(ncid);

 netcdf.putVar(ncid,long_ID,long);
 netcdf.putVar(ncid,lat_ID,lat);
 netcdf.putVar(ncid,depth_ID,ndep);
 netcdf.putVar(ncid,time_ID,time);
 netcdf.putVar(ncid,plankton_ID,planknum);
 netcdf.putVar(ncid,carbon_ID,tmpcar);

 netcdf.close(ncid);


