Index: trunk/src/struct2nc.m
===================================================================
--- trunk/src/struct2nc.m	(revision 938)
+++ trunk/src/struct2nc.m	(revision 943)
@@ -1,5 +1,5 @@
 % 'struct2nc': create a netcdf file from a Matlab structure
 %---------------------------------------------------------------------
-% errormsg=struct2nc(flname,Data)
+% errormsg=struct2nc(flname,Data,action)
 %
 % OUTPUT:
@@ -16,4 +16,5 @@
 %       (optional) .VarAttribute: cell array of structures of the form .VarAttribute{ivar}.key=value, defining an attribute key name and value for the variable #ivar
 %      (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
+% action: if ='keep_open', don't close the file after creation, keep it open for further data input
 
 %=======================================================================
@@ -35,20 +36,18 @@
 %=======================================================================
 
-function [errormsg,nc]=struct2nc(flname,Data,action)
+function [errormsg,nc]=struct2nc(flname,Data,action,ListDimName,DimValue,VarDimIndex)
 nc=[];
-% if ~ischar(flname)
-%     errormsg='invalid input for the netcf file name';
-%     return
-% end
+if ~ischar(flname)
+    errormsg='invalid input for the netcf file name';
+    return
+end
 if ~exist('Data','var')
      errormsg='no data  input for the netcdf file';
     return
 end 
-if ~exist('action','var')
-    action='one_input'; %fill the file with data and close it
-end
 
 
 %% check the validity of the input field structure
+if ~ (exist('action','var') && strcmp(action,'keep_open'))
 [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data);
 if ~isempty(errormsg)
@@ -56,8 +55,9 @@
     return
 end
+end
 ListVarName=Data.ListVarName;
 
 %% create the netcdf file with name flname in format NETCDF4
-if ischar(flname)
+% if ischar(flname)
     FilePath=fileparts(flname);
     if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
@@ -69,7 +69,7 @@
     cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
     nc = netcdf.create(flname, cmode);
-else
-    nc=flname;
-end
+% else
+%     nc=flname;
+% end
 
 %% write global constants
@@ -113,7 +113,7 @@
 for ivar=1:length(ListVarName)
     if isfield(Data,ListVarName{ivar})
-        VarClass=class(Data.(ListVarName{ivar}));
+        VarClass{ivar}=class(Data.(ListVarName{ivar}));
         VarType='';
-        switch VarClass
+        switch VarClass{ivar}
             case {'single','double'}
                 VarType='nc_float'; % store all floating reals as single
@@ -146,31 +146,33 @@
 
 %% fill the variables with input data
-for ivar=1:length(ListVarName)
-    if ~isnan(varid(ivar))
-        VarVal=Data.(ListVarName{ivar}); 
-        %varval=values of the current variable 
-        VarDimName=Data.VarDimName{ivar};
-        if ischar(VarDimName)
-            VarDimName={VarDimName};
-        end
-        siz=size(VarVal);
-        testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
-        testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
-        %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
-        if testline || testrange
-            if testrange
-                VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values
-            end
-            netcdf.putVar(nc,varid(ivar), double(VarVal'));
-        else
-            netcdf.putVar(nc,varid(ivar), double(VarVal));
-        end      
-    end
-end
-if strcmp(action,'one_input')
-netcdf.close(nc)
+if ~(exist('action','var') && strcmp(action,'keep_open'))
+    for ivar=1:length(ListVarName)
+        if ~isnan(varid(ivar))
+            VarVal=Data.(ListVarName{ivar});
+            if strcmp(VarClass{ivar},'single')
+                VarVal=single(VarVal);
+            end
+            %varval=values of the current variable
+            VarDimName=Data.VarDimName{ivar};
+            if ischar(VarDimName)
+                VarDimName={VarDimName};
+            end
+            siz=size(VarVal);
+            testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
+            testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
+            %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
+            if testline || testrange
+                if testrange
+                    VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values from the first and last values
+                end
+                netcdf.putVar(nc,varid(ivar), VarVal');
+            else
+                netcdf.putVar(nc,varid(ivar), VarVal);
+            end
+        end
+    end
+end
+ netcdf.close(nc)
 [success,errormsg] = fileattrib(flname ,'+w');% allow writing access for the group of users
-end
-
 %'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
 %------------------------------------------------------------------------
