Index: ../trunk-jpl/src/m/contrib/netCDF/export_netCDF.m
===================================================================
--- ../trunk-jpl/src/m/contrib/netCDF/export_netCDF.m	(revision 19079)
+++ ../trunk-jpl/src/m/contrib/netCDF/export_netCDF.m	(revision 19080)
@@ -1,4 +1,3 @@
-
 function export_netCDF(md,filename)	
 	
 %Now going on Real treatment
@@ -14,8 +13,8 @@
 	  end
   end
 	%open file and write description
-	mode = netcdf.getConstant('NETCDF4');
-	mode = bitor(mode,netcdf.getConstant('NOCLOBBER'));%NOCLOBBER to avoid overwrite
+	mode = netcdf.getConstant('NC_NETCDF4');
+	mode = bitor(mode,netcdf.getConstant('NC_NOCLOBBER'));%NOCLOBBER to avoid overwrite
 	ncid = netcdf.create(filename,mode);
 	netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Title',['Results for run ' md.miscellaneous.name]);
 	netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Date',['Created ' datestr(now)]);
@@ -32,6 +31,9 @@
 	DimSize(1).index=netcdf.defDim(ncid,'EltNum',md.mesh.numberofelements);
 	DimSize(2).index=netcdf.defDim(ncid,'VertNum',md.mesh.numberofvertices);
 	DimSize(3).index=netcdf.defDim(ncid,'VertperElt',size(md.mesh.elements,2));
+	DimSize(4).index=netcdf.defDim(ncid,'Time',StepNum);
+	DimSize(5).index=netcdf.defDim(ncid,'StringLength',20);
+	DimSize(6).index=netcdf.defDim(ncid,'StructLength',2);
 	
 	for i=1:length(DimSize),
 		[DimSize(i).name,DimSize(i).value]=netcdf.inqDim(ncid,DimSize(i).index);
@@ -44,38 +46,36 @@
 	%get all model classes and create respective groups
 	groups=fieldnames(md);
 	for i=1:length(groups),
-		%disp(sprintf('group name in tree %s ',groups{i}));
+		disp(sprintf('group name in tree %s ',groups{i}));
 		groupID=netcdf.defGrp(ncid,groups{i});
 		%In each group gather the fields of the class
 		groupfields=fields(md.(groups{i}));
 		%Special treatment for the results
 		if strcmp(groups(i),'results'),
 			for j=1:length(groupfields)%looping on the different solutions
-				%disp(sprintf('=====Field name in tree %s ',groupfields{j}));
+																 %disp(sprintf('=====Field name in tree %s ',groupfields{j}));
 				if length(md.results.(groupfields{j}))>1,
 					%the solution have several timestep get last timesteps and output frequency
 					last_step = length(md.results.(groupfields{j}));
-					step_freq = md.settings.output_frequency;
 					%grab first time step
 					subfields=fields(md.results.(groupfields{j})(1));
 					for k=1:length(subfields),
 						netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results');
 						if ~strcmp(subfields(k),'errlog') && ~strcmp(subfields(k),'outlog') && ~strcmp(subfields(k),'SolutionType'),
-							disp(sprintf('==========SubField name in tree %s ',subfields{k}));
-							Var=md.results.(groupfields{1})(1).(subfields{k});
-							[truc,DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue);
-							%CreateVar(Var,True,last_step,step_freq)
+							%disp(sprintf('==========SubField name in tree %s ',subfields{k}));
+							Var=md.results.(groupfields{j})(1).(subfields{k});
+							[DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue,true,last_step,md,groupfields{j});
 			      end
 		      end
 				elseif length(md.results.(groupfields{j}))==1,
 					%only one timestep
 					subfields=fields(md.results.(groupfields{j}));
 					for k=1:length(subfields),
-						disp(sprintf('==========SubField name in tree %s ',subfields{k}));
-						netcdf.putAtt(groupID,netcdf.getConstant('GLOBAL'),'classtype','results');
+						%disp(sprintf('==========SubField name in tree %s ',subfields{k}));
+						netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results');
 						if ~strcmp(subfields(k),'errlog') && ~strcmp(subfields(k),'outlog') && ~strcmp(subfields(k),'SolutionType'),
 							Var=md.results.(groupfields{1})(1).(subfields{k});
-							[truc,DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue);
+							[DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue,false);
 			      end
 		      end
 				else
@@ -84,17 +84,17 @@
 	    end
 		else
 			for j=1:length(groupfields),
-				disp(sprintf('=====Field name in tree %s ',groupfields{j}));
-				netcdf.putAtt(groupID,netcdf.getConstant('GLOBAL'),'classtype',class(md.(groups{i})));
+				%disp(sprintf('=====Field name in tree %s ',groupfields{j}));
+				netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype',class(md.(groups{i})));
 				Var=md.(groups{i}).(groupfields{j});
-				[truc,DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,groupfields{j},DimSize,DimValue);
+				[DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,groupfields{j},DimSize,DimValue,false);
 	    end
 	  end	
   end
 	netcdf.close(ncid);
 end
 
-function [varclass,DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,field,DimSize,DimValue)
+function [DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,field,DimSize,DimValue,istime,last,md,midfield)
 	varclass=class(Var);
 	varsize=size(Var);
 	varlength=length(Var);
@@ -104,48 +104,133 @@
 		else,
 			LogicString='False';
   	end
-		netcdf.putAtt(groupID,netcdf.getConstant('GLOBAL'),field,LogicString);
+		netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),field,LogicString);
 	elseif isa(Var,'char'),
-		netcdf.putAtt(groupID,netcdf.getConstant('GLOBAL'),field,Var);
+		netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),field,Var);
 	elseif isa(Var,'double'), %dealing with arrays
-		[dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,field,DimSize,DimValue);
- 		varid = netcdf.defVar(groupID,field,'double',dims);
-		if length(Var)==0,
-			netcdf.putVar(groupID,varid,NaN);
+		[dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,field,DimSize,DimValue,istime);
+ 		varid = netcdf.defVar(groupID,field,'NC_DOUBLE',dims);
+		if istime,
+			VarTab=Var;
+			for i=2:last,
+				TimeVar=md.results.(midfield)(i).(field);
+				VarTab=[VarTab TimeVar];
+	    end
+			netcdf.putVar(groupID,varid,VarTab);
 		else
-			netcdf.putVar(groupID,varid,Var);
-  	end
+			if length(Var)==0,
+				netcdf.putVar(groupID,varid,NaN);
+			else
+				netcdf.putVar(groupID,varid,Var);
+	    end
+	  end
 	elseif isa(Var,'cell'),
-		disp(sprintf('no support for class %s for field %s so far',varclass,field));
+		[dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,field,DimSize,DimValue,istime);
+		%dirty hack to be able to pass strings
+		varid = netcdf.defVar(groupID,field,'NC_CHAR',dims);
+		for i=1:length(Var),
+			startpoint=zeros(size(Var));
+			startpoint(:,i)=i-1;
+			if length(Var)>1,
+				endpoint=[min(length(Var{i}),20) 1];
+			else
+				endpoint=min(length(Var{i}),20);
+	    end
+			if length(Var{i})>20,
+				netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i}(1:20))
+			else
+				netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i})
+	    end
+	  end
 	elseif isa(Var,'struct'),
-		disp(sprintf('no support for class %s for field %s so far',varclass,field));
+		%Start by getting the structure fields and size
+		locfields=fields(Var);
+		[dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,locfields,DimSize,DimValue,istime);
+		varid = netcdf.defVar(groupID,field,'NC_CHAR',dims);
+		for i=1:length(locfields),
+			for j=1:2,
+				if j==1,
+					startpoint=[0,0,i-1];
+					CharVar=locfields{i};
+				else
+					startpoint=[0,1,i-1];
+					if isa(Var.(locfields{i}),'char'),
+						CharVar=Var.(locfields{i});
+					else
+						CharVar=num2str(Var.(locfields{i}));
+		      end
+		    end
+				endpoint=[min(length(CharVar),20),1,1];
+				if length(CharVar)>20,
+					netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar(1:20))
+				else
+					netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar)
+		    end
+	    end
+	  end
 	else
 		disp(sprintf('no support for class %s of field %s',varclass,field));
   end
 	return
 end
 
-function [dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,field,DimSize,DimValue)
+function [dims,DimSize,DimValue]=GetDims(ncid,Var,groupID,field,DimSize,DimValue,istime)
 	dims=[];
-	varlength=length(Var);
-	varsize=size(Var);
-	MatOrVec=varsize>1; %checking if we have a matrix (1 1) are vector (1 0)
-	for i=1:sum(MatOrVec), %loop on the number of (non 1) dimensions
-		currentdim=varsize(i);
-		dimexist=DimValue==currentdim;
-		if sum(dimexist)==0, %dimension is new to us, need to create it
-			dimname=strcat(field,int2str(i));
-			dimindex=length(DimSize)+1;
-			DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
-			[DimSize(dimindex).name,DimSize(dimindex).value]=netcdf.inqDim(ncid,DimSize(dimindex).index);
+	%specific treatment for structures
+	if isa(Var,'struct')
+		varsize=size(field); %we pass here the fields of the current structure
+		MatOrVec=varsize>1; %checking if we have a matrix (1 1) or vector (1 0)
+		for i=1:sum(MatOrVec), %loop on the number of (non 1) dimensions
+			currentdim=varsize(i);
+			dimexist=DimValue==currentdim;
+			if sum(dimexist)==0, %dimension is new to us, need to create it
+				dimname=strcat(field{1},int2str(i));
+				dimindex=length(DimSize)+1;
+				DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
+				[DimSize(dimindex).name,DimSize(dimindex).value]=netcdf.inqDim(ncid,DimSize(dimindex).index);
+				dims(i)=DimSize(dimindex).index;
+				DimValue(dimindex)=currentdim;
+			else
+				dimindex=find(dimexist);
+				if DimSize(dimindex).value~=currentdim,
+					error('Indexation problem with the dimension structure')
+		    end
+	    end
 			dims(i)=DimSize(dimindex).index;
-			DimValue(dimindex)=currentdim;
+	  end
+		dims=[DimSize(6).index dims];
+	else
+		%with a cell array need to grab the transposed size to work
+		if isa(Var,'cell'),
+			varsize=size(Var');
 		else
-			dimindex=find(dimexist);
-			if DimSize(dimindex).value~=currentdim,
-				error('Indexation problem with the dimension structure')
-  	  end
-	  end
-		dims(i)=DimSize(dimindex).index;
+			varsize=size(Var);
+    end
+		MatOrVec=varsize>1; %checking if we have a matrix (1 1) or vector (1 0)
+		for i=1:sum(MatOrVec), %loop on the number of (non 1) dimensions
+			currentdim=varsize(i);
+			dimexist=DimValue==currentdim;
+			if sum(dimexist)==0, %dimension is new to us, need to create it
+				dimname=strcat(field,int2str(i));
+				dimindex=length(DimSize)+1;
+				DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
+				[DimSize(dimindex).name,DimSize(dimindex).value]=netcdf.inqDim(ncid,DimSize(dimindex).index);
+				dims(i)=DimSize(dimindex).index;
+				DimValue(dimindex)=currentdim;
+			else
+				dimindex=find(dimexist);
+				if DimSize(dimindex).value~=currentdim,
+					error('Indexation problem with the dimension structure')
+		    end
+	    end
+			dims(i)=DimSize(dimindex).index;
+    end
   end
+	if istime,
+		dims=[dims DimSize(4).index];%adding the time dimension if necessary
+  end
+	%if we have a cell variable we need to add a stringlength dimension 
+	if isa(Var,'cell') || isa(Var,'struct'),
+		dims=[DimSize(5).index dims];
+  end
 end
Index: ../trunk-jpl/src/m/contrib/netCDF/export_netCDF.py
===================================================================
--- ../trunk-jpl/src/m/contrib/netCDF/export_netCDF.py	(revision 19079)
+++ ../trunk-jpl/src/m/contrib/netCDF/export_netCDF.py	(revision 19080)
@@ -87,8 +87,9 @@
 				for time in range(freq-1,last,freq):
 					if time!=0:
 						timevar=md.results.__dict__[supfield].__getitem__(time).__dict__[field]
-#						print 'Treating '+str(group)+'.'+str(supfield)+'.'+str(field)+' for time '+str(time)
+						print 'Treating '+str(group)+'.'+str(supfield)+'.'+str(field)+' for time '+str(time)
 						vartab=numpy.column_stack((vartab,timevar))
+				print numpy.shape(vartab)
 				try:
 					ncvar[:,:]=vartab[:,:]
 				except ValueError:
@@ -128,10 +129,14 @@
 	EltNum=NCData.createDimension('EltNum',md.mesh.numberofelements)
 	VertNum=NCData.createDimension('VertNum',md.mesh.numberofvertices)
 	VertperElt=NCData.createDimension('VertperElt',numpy.shape(md.mesh.elements)[1])
+	Time=NCData.createDimension('Time',StepNum)
+	DictDim=NCData.createDimension('DictDim',2) 
 
 	DimDict = {len(EltNum):'EltNum',
 						 len(VertNum):'VertNum',
-						 len(VertperElt):'VertperElt'}
+						 len(VertperElt):'VertperElt',
+						 len(Time):'Time',
+						 len(DictDim):'DictDim'}
 
 	TypeDict = {float:'f8',
 							'float64':'f8',
