Index: /issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.m
===================================================================
--- /issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.m	(revision 19095)
+++ /issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.m	(revision 19096)
@@ -29,10 +29,10 @@
  
 	%define netcdf dimensions
-	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);
+	DimSize(1).index=netcdf.defDim(ncid,'Dimension1',md.mesh.numberofelements);
+	DimSize(2).index=netcdf.defDim(ncid,'Dimension2',md.mesh.numberofvertices);
+	DimSize(3).index=netcdf.defDim(ncid,'Dimension3',size(md.mesh.elements,2));
+	DimSize(4).index=netcdf.defDim(ncid,'Dimension4',StepNum);
+	DimSize(5).index=netcdf.defDim(ncid,'Dimension5',40);
+	DimSize(6).index=netcdf.defDim(ncid,'Dimension6',2);
 	
 	for i=1:length(DimSize),
@@ -53,6 +53,9 @@
 		%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}));
+			for j=1:length(groupfields)%looping on the differents solutions
+				netcdf.putAtt(groupID,netcdf.getConstant('NC_GLOBAL'),'classtype','results');
+				subgroupID=netcdf.defGrp(groupID,groupfields{j});
+				netcdf.putAtt(subgroupID,netcdf.getConstant('NC_GLOBAL'),'classtype',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
@@ -61,9 +64,8 @@
 					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{j})(1).(subfields{k});
-							[DimSize,DimValue]=DefCreateVar(ncid,Var,groupID,subfields{k},DimSize,DimValue,true,last_step,md,groupfields{j});
+							[DimSize,DimValue]=DefCreateVar(ncid,Var,subgroupID,subfields{k},DimSize,DimValue,true,last_step,md,groupfields{j});
 			      end
 		      end
@@ -73,5 +75,4 @@
 					for k=1:length(subfields),
 						%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});
@@ -85,5 +86,5 @@
 		else
 			for j=1:length(groupfields),
-				%disp(sprintf('=====Field name in tree %s ',groupfields{j}));
+				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});
@@ -129,16 +130,21 @@
 		%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})
+		if length(Var)==0,
+			netcdf.putVar(groupID,varid,0,9,'emptycell')
+		else
+			for i=1:length(Var),
+				startpoint=zeros(size(Var));
+				startpoint(:,i)=i-1;
+				if length(Var)>1,
+					endpoint=[min(length(Var{i}),40) 1];
+				else
+					endpoint=min(length(Var{i}),40);
+		    end
+				if length(Var{i})>40,
+					netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i}(1:40))
+					disp(sprintf('some variable have been truncated'));
+			  else
+					netcdf.putVar(groupID,varid,startpoint,endpoint,Var{i})
+	      end
 	    end
 	  end
@@ -148,25 +154,30 @@
 		[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});
+		if length(locfields)==0,
+			netcdf.putVar(groupID,varid,[0,0],[11,1],'emptystruct')
+		else
+			for i=1:length(locfields),
+				for j=1:2,
+					if j==1,
+						startpoint=[0,0,i-1];
+						CharVar=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
+						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),40),1,1];
+					if length(CharVar)>40,
+						netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar(1:40))
+						disp(sprintf('some variable have been truncated'));
+					else
+						netcdf.putVar(groupID,varid,startpoint,endpoint,CharVar)
+		      end
+		    end
+	    end
+		end
 	else
 		disp(sprintf('no support for class %s of field %s',varclass,field));
@@ -185,5 +196,5 @@
 			dimexist=DimValue==currentdim;
 			if sum(dimexist)==0, %dimension is new to us, need to create it
-				dimname=strcat(field{1},int2str(i));
+				dimname=strcat('Dimension',int2str(length(DimValue)+1));
 				dimindex=length(DimSize)+1;
 				DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
@@ -212,5 +223,5 @@
 			dimexist=DimValue==currentdim;
 			if sum(dimexist)==0, %dimension is new to us, need to create it
-				dimname=strcat(field,int2str(i));
+				dimname=strcat('Dimension',int2str(length(DimValue)+1));
 				dimindex=length(DimSize)+1;
 				DimSize(dimindex).index=netcdf.defDim(ncid,dimname,currentdim);
@@ -232,5 +243,5 @@
 	%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];
+		dims=[DimSize(5).index dims]
   end
 end
Index: /issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.py
===================================================================
--- /issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.py	(revision 19095)
+++ /issm/trunk-jpl/src/m/contrib/netCDF/export_netCDF.py	(revision 19096)
@@ -20,6 +20,7 @@
 				except KeyError: #if not create it
 					if (shape[dim])>1:
-						NewDim=NCData.createDimension(str(field)+str(dim),(shape[dim]))
-						DimDict[len(NewDim)]=str(field)+str(dim)
+						index=len(DimDict)+1
+						NewDim=NCData.createDimension('Dimension'+str(index),(shape[dim]))
+						DimDict[len(NewDim)]='Dimension'+str(index)
 						output=output+[str(DimDict[shape[dim]])]
 						print 'Defining dimension ' +str(shape[dim]) +' for '+str(field)+str(dim)
@@ -28,16 +29,17 @@
 					output=[str(DimDict[numpy.shape(shape)[0]])]+['DictDim']
 				except KeyError:
-					NewDim=NCData.createDimension(str(field),numpy.shape(shape)[0])
-					DimDict[len(NewDim)]=str(field)
-					output=[str(DimDict[numpy.shape(dict.keys(var))[0]])]+['DictDim']
+					index=len(DimDict)+1
+					NewDim=NCData.createDimension('Dimension'+str(index),numpy.shape(shape)[0])
+					DimDict[len(NewDim)]='Dimension'+str(index)
+					output=[str(DimDict[numpy.shape(dict.keys(var))[0]])]+['Dimension5']
 					print 'Defining dimension ' +str(numpy.shape(shape)[0]) +' for '+str(field)
 				break
 		if istime:
-			output=output+['Time']
+			output=output+['Dimension4']
 		return tuple(output)
 	#============================================================================
 
   #Define the variables
-	def CreateVar(var,istime,*step_args):
+	def CreateVar(var,Group,istime,*step_args):
 		#grab type
 		try:
@@ -56,9 +58,9 @@
 		#treating scalar string or bool as atribute
 		if val_type==str or val_type==bool:
-			NCgroup.__setattr__(str(field), str(var))
+			Group.__setattr__(str(field), str(var))
 
 		#treating list as string table
 		elif val_type==list:
-			ncvar = NCgroup.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
+			ncvar = Group.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
 			for elt in range(0,val_dim):
 				try:
@@ -68,10 +70,10 @@
 		#treating bool tables as string tables
 		elif val_type=='bool':
-			ncvar = NCgroup.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
+			ncvar = Group.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
 			for elt in range(0,val_shape[0]):
 				ncvar[elt] = str(var[elt])
 		#treating dictionaries as string tables of dim 2
 		elif val_type==collections.OrderedDict:
-			ncvar = NCgroup.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
+			ncvar = Group.createVariable(str(field),str,GetDim(var,val_shape,val_dim,istime),zlib=True)
 			for elt in range(0,val_dim):
 				ncvar[elt,0]=dict.keys(var)[elt]
@@ -79,5 +81,5 @@
 		#Now dealing with numeric variables
 		else:
-			ncvar = NCgroup.createVariable(str(field),TypeDict[val_type],GetDim(var,val_shape,val_dim,istime),zlib=True)
+			ncvar = Group.createVariable(str(field),TypeDict[val_type],GetDim(var,val_shape,val_dim,istime),zlib=True)
 			
 			if istime:
@@ -127,15 +129,15 @@
 		StepNum=1
 		
-	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) 
+	Dimension1=NCData.createDimension('Dimension1',md.mesh.numberofelements)
+	Dimension2=NCData.createDimension('Dimension2',md.mesh.numberofvertices)
+	Dimension3=NCData.createDimension('Dimension3',numpy.shape(md.mesh.elements)[1])
+	Dimension4=NCData.createDimension('Dimension4',StepNum)
+	Dimension5=NCData.createDimension('Dimension5',2) 
 
-	DimDict = {len(EltNum):'EltNum',
-						 len(VertNum):'VertNum',
-						 len(VertperElt):'VertperElt',
-						 len(Time):'Time',
-						 len(DictDim):'DictDim'}
+	DimDict = {len(Dimension1):'Dimension1',
+						 len(Dimension2):'Dimension2',
+						 len(Dimension3):'Dimension3',
+						 len(Dimension4):'Dimension4',
+						 len(Dimension5):'Dimension5'}
 
 	TypeDict = {float:'f8',
@@ -154,4 +156,7 @@
 		if str(group)=='results':
 			for supfield in fields:#looping on the different solutions
+				NCgroup.__setattr__('classtype', "results")
+				Subgroup=NCgroup.createGroup(str(supfield))
+				Subgroup.__setattr__('classtype',str(supfield))
 				if type(md.results.__dict__[supfield])==list:#the solution have several timestep
 					#get last timesteps and output frequency
@@ -161,17 +166,15 @@
 					subfields=dict.keys(md.results.__dict__[supfield].__getitem__(0).__dict__)
 					for field in subfields:
-						NCgroup.__setattr__('classtype', "results")
 						if str(field)!='errlog' and str(field)!='outlog' and str(field)!='SolutionType':
 							Var=md.results.__dict__[supfield].__getitem__(0).__dict__[field]
-							CreateVar(Var,True,last_step,step_freq)
+							CreateVar(Var,Subgroup,True,last_step,step_freq)
 					
 				elif type(md.results.__dict__[supfield])==results:#only one timestep
 					subfields=dict.keys(md.results.__dict__[supfield].__dict__)
 					for field in subfields:
-						NCgroup.__setattr__('classtype', "results")
 						if str(field)!='errlog' and str(field)!='outlog' and str(field)!='SolutionType':
 #							print 'Treating '+str(group)+'.'+str(supfield)+'.'+str(field)
 							Var=md.results.__dict__[supfield].__dict__[field]
-							CreateVar(Var,False)
+							CreateVar(Var,Subgroup,False)
 				else:
 					print 'Result format not suported'
@@ -182,5 +185,5 @@
 				NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__)
 				Var=md.__dict__[group].__dict__[field]
-				CreateVar(Var,False)
+				CreateVar(Var,NCgroup,False)
 	NCData.close()
 
Index: /issm/trunk-jpl/src/m/contrib/netCDF/read_netCDF.m
===================================================================
--- /issm/trunk-jpl/src/m/contrib/netCDF/read_netCDF.m	(revision 19096)
+++ /issm/trunk-jpl/src/m/contrib/netCDF/read_netCDF.m	(revision 19096)
@@ -0,0 +1,134 @@
+function self=read_netCDF(filename)
+
+% Different types in the netcdf standard are:
+%   2 for char
+%   4 for integer
+%   6 for doubles	
+	
+	ncid=netcdf.open(filename,'NC_NOWRITE');
+	groupIDs=netcdf.inqGrps(ncid);%retrieve group IDs
+	self=model;
+	%loop on groups
+	for i=1:length(groupIDs)
+		whichclass = netcdf.getAtt(groupIDs(i),netcdf.getConstant('NC_GLOBAL'),'classtype');
+		groupName = netcdf.inqGrpName(groupIDs(i));		
+		%results needs a special treatment as it is a structure
+		if strcmp(whichclass,'results'),
+			subgroupIDs=netcdf.inqGrps(groupIDs(i));%retrieve group IDs
+			%define the model structure
+			self=setfield(self,groupName,struct);
+			for j=1:length(subgroupIDs)
+				subclass = netcdf.getAtt(subgroupIDs(j),netcdf.getConstant('NC_GLOBAL'),'classtype');
+				self.results=setfield(self.results,subclass,struct);
+				[ndims nvar natts]=netcdf.inq(groupIDs(j));
+				varIDs=netcdf.inqVarIDs(subgroupIDs(j));
+				%first loop on group atributes
+				for k=1:natts,
+					attname = netcdf.inqAttName(subgroupIDs(j),netcdf.getConstant('NC_GLOBAL'),k-1);
+					[xtype,attlen] = netcdf.inqAtt(subgroupIDs(j),netcdf.getConstant('NC_GLOBAL'),attname);
+					%disp(sprintf('In %s, Treating attribute %s of type %i',subclass,attname,xtype));
+					%classtype have done is job, no need to keep it any more
+					if ~strcmp(attname,'classtype'),
+						attval=netcdf.getAtt(subgroupIDs(i),netcdf.getConstant('NC_GLOBAL'),attname);
+						if strcmp(attval,'False'),
+							self.(groupName).(subclass).(attname)=false;
+						elseif strcmp(attval,'True')
+							self.(groupName).(subclass).(attname)=true;
+						else
+							self.(groupName).(subclass).(attname)=attval;
+			      end
+		      end
+		    end
+				%now loop on variable in group
+				for k=1:length(varIDs),
+					[varname, xtype, varDimIDs, varAtts] =netcdf.inqVar(subgroupIDs(j),varIDs(k));
+					%disp(sprintf('In %s, Treating variable %s of type %i',whichclass,varname,xtype));
+					%time dimension seems to be last in our construction
+					for l=1:length(varDimIDs),
+						[dimname, dimlen] = netcdf.inqDim(ncid,varDimIDs(l));
+						count(l)=[dimlen];
+		      end
+					startpoint=zeros(size(varDimIDs));
+					timestep=count(end);
+					count(end)=1;
+					for l=1:timestep,
+						data=netcdf.getVar(subgroupIDs(j),varIDs(k),startpoint,count);
+						self.(groupName).(subclass)(l).(varname)=data;
+						startpoint(end)=startpoint(end)+1;
+						self.(groupName).(subclass)(l).('errlog')='';
+						self.(groupName).(subclass)(l).('outlog')='';
+						self.(groupName).(subclass)(l).('SolutionType')=subclass;
+		     end
+				 clear count
+		    end
+	    end
+			%toolkits too require a specific treatment
+		elseif strcmp(whichclass,'toolkits'),
+			%just one variable here
+			varID=netcdf.inqVarIDs(groupIDs(i));
+			[varname, xtype, varDimIDs, varAtts] =netcdf.inqVar(groupIDs(i),varID);
+			%disp(sprintf('In %s, Treating variable %s of type %i',whichclass,varname,xtype));
+			[dimname,numoffields] = netcdf.inqDim(ncid,varDimIDs(end));
+			self.(groupName)=eval(whichclass);
+			for j=1:numoffields,
+				varval=netcdf.getVar(groupIDs(i),varID,[0,1,j-1],[40,1,1])';
+				fieldname=netcdf.getVar(groupIDs(i),varID,[0,0,j-1],[40,1,1])';
+				varval=cellstr(varval);
+				fieldname=cellstr(fieldname);
+				[val, status]=str2num(varval{1});
+				if strcmp(varval{1},'false') |strcmp(varval{1},'true')  ,
+					status=0;
+		    end
+				if status==1,
+					self.(groupName).(varname).(fieldname{1})=val;
+				else
+					self.(groupName).(varname).(fieldname{1})=varval{1};
+			 end
+	   end
+			%Now for the other fields
+
+		else,
+			%define the model structure
+			self.(groupName)=eval(whichclass);
+			varIDs=netcdf.inqVarIDs(groupIDs(i));
+			[ndims nvar natts]=netcdf.inq(groupIDs(i));
+			%first loop on group atributes
+			for j=1:natts,
+				attname = netcdf.inqAttName(groupIDs(i),netcdf.getConstant('NC_GLOBAL'),j-1);
+				[xtype,attlen] = netcdf.inqAtt(groupIDs(i),netcdf.getConstant('NC_GLOBAL'),attname);
+				%disp(sprintf('In %s, Treating attribute %s of type %i',whichclass,attname,xtype));
+				%classtype have done is job, no need to keep it any more
+				if ~strcmp(attname,'classtype'),
+					attval=netcdf.getAtt(groupIDs(i),netcdf.getConstant('NC_GLOBAL'),attname);
+					if strcmp(attval,'False'),
+						self.(groupName).(attname)=false;
+					elseif strcmp(attval,'True')
+						self.(groupName).(attname)=true;
+					else
+					self.(groupName).(attname)=attval;
+		    end
+		  end
+	  end
+			%now loop on variable in group
+			for j=1:length(varIDs),
+				[varname, xtype, varDimIDs, varAtts] =netcdf.inqVar(groupIDs(i),varIDs(j));
+				%disp(sprintf('In %s, Treating variable %s of type %i',whichclass,varname,xtype));			
+				%if the value is a single string, we need to transpose it (cross check with python file is necessary)
+				if xtype==2
+					varval=netcdf.getVar(groupIDs(i),varIDs(j))';
+					varval=cellstr(varval)';
+					if strcmp(varval{1},'emptystruct'),
+						self.(groupName).(varname)=struct;
+					elseif strcmp(varval{1},'emptycell'),
+						self.(groupName).(varname)=cell(0,0);
+					else
+						self.(groupName).(varname)=varval;
+					end
+				else
+					self.(groupName).(varname)=netcdf.getVar(groupIDs(i),varIDs(j));
+		    end
+		  end
+	  end
+  end
+	netcdf.close(ncid)
+end
