Index: /issm/trunk/src/m/contrib/musselman/read_netCDF.m
===================================================================
--- /issm/trunk/src/m/contrib/musselman/read_netCDF.m	(revision 27876)
+++ /issm/trunk/src/m/contrib/musselman/read_netCDF.m	(revision 27877)
@@ -71,8 +71,16 @@
     for name = variables
         class_instance = netcdf.inqVar(resultsGroup, name);
-        class_instance_name = convertCharsToStrings(netcdf.getVar(resultsGroup, name, 'char'));
-        model_copy.results = setfield(model_copy.results, class_instance, class_instance_name);
-    end
-    disp('Successfully recreated results struct')
+        class_instance_names_raw = netcdf.getVar(resultsGroup, name, 'char').';
+        class_instance_names = cellstr(class_instance_names_raw);
+        for index = 1:numel(class_instance_names)
+            class_instance_name = class_instance_names{index};
+            model_copy.results = setfield(model_copy.results, class_instance_name, struct());
+        end
+        %model_copy.results = setfield(model_copy.results, class_instance, class_instance_name);
+    end
+    disp('Successfully recreated results structs:')
+    for fieldname = string(fieldnames(model_copy.results))
+        disp(fieldname)
+    end
 end
 
@@ -100,46 +108,41 @@
 function walk_nested_groups(group_location_in_file)
     global model_copy;
-    global NCData;
-    % try to find vars in current level, if it doesn't work it's because there is nothing there
-    try
-        % we search the current group level for variables by getting this struct
-        variables = netcdf.inqVarIDs(group_location_in_file);    
-    
-        % from the variables struct get the info related to the variables
-        for variable = variables
-            [varname, xtype, dimids, numatts] = netcdf.inqVar(group_location_in_file, variable);
-
-            % keep an eye out for nested structs:
-            if strcmp(varname, 'this_is_a_nested')
-                is_nested = true;
-                copy_nested_struct(group_location_in_file)
-            else
-                is_nested = false;
-                copy_variable_data_to_new_model(group_location_in_file,varname, xtype);
-            end
-        end
-    catch ME
-        rethrow(ME)
+    global NCData;    
+    % we search the current group level for variables by getting this struct
+    variables = netcdf.inqVarIDs(group_location_in_file); 
+
+    % from the variables struct get the info related to the variables
+    for variable = variables
+        [varname, xtype, dimids, numatts] = netcdf.inqVar(group_location_in_file, variable);
+        
+        % keep an eye out for nested structs:
+        if strcmp(varname, 'this_is_a_nested')
+            is_nested = true;
+            copy_nested_struct(group_location_in_file)
+        elseif strcmp(varname, 'solution')
+            % band-aid pass..
+        else
+            copy_variable_data_to_new_model(group_location_in_file, varname, xtype);
+        end
     end
 
     % try to find groups in current level, if it doesn't work it's because there is nothing there
-    try
-        % if it's not a nested struct, keep searching for subgroups
-        if isnested
-            % do nothing
-        else
-            % search for nested groups in the current level to feed back to this function
-            groups = netcdf.inqGrps(group_location_in_file);
-            if not(isempty(groups))
-                for group = groups
-                    %disp('found nested group!!')
-                    group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group));
-                    %disp(netcdf.inqGrpNameFull(group_id))
-                    walk_nested_groups(group);
-                end
-            end
-        end
-    catch % no nested groups here
-    end
+    %try
+    % if it's a nested struct the function copy_nested_struct has already been called
+    if logical(exist('is_nested', 'var'))
+        % do nothing
+    else
+        % search for nested groups in the current level to feed back to this function
+        groups = netcdf.inqGrps(group_location_in_file);
+        if not(isempty(groups))
+            for group = groups
+                group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group));
+                %disp(netcdf.inqGrpNameFull(group_id))
+                walk_nested_groups(group);
+            end
+        end
+    end
+    %catch % no nested groups here
+    %end
 end
 
@@ -152,5 +155,5 @@
         A common multidimensional struct array is the 1xn md.results.TransientSolution struct. 
         The process to recreate is as follows:
-            1. Get the name of the struct from group variable name_of_struct
+            1. Get the name of the struct from group name
             2. Get the fieldnames from the subgroups 
             3. Recreate the struct with fieldnames 
@@ -159,6 +162,5 @@
 
     % step 1
-    varid = netcdf.inqVarID(group_location_in_file, 'name_of_struct');
-    name_of_struct = netcdf.getVar(group_location_in_file, varid)';
+    name_of_struct = netcdf.inqGrpName(group_location_in_file);
 
     % step 2
@@ -174,12 +176,13 @@
 
     % step 3
-    address_in_model = strrep(netcdf.inqGrpNameFull(group_location_in_file), '/', '');
+    address_in_model_raw = split(netcdf.inqGrpNameFull(group_location_in_file), '/');
+    address_in_model = address_in_model_raw{2};
+    
     % we cannot assign a variable to represent this object as MATLAB treats all variables as copies
     % and not pointers to the same memory address
-    % this means that if address_in_model is more than 1 item, we need to modify the code. For now, 
-    % we just hope this will do
+    % this means that if address_in_model has more than 1 layer, we need to modify the code. For now, 
+    % we just hope this will do. An example of a no-solution would be model().abc.def.ghi.field
     
     model_copy.(address_in_model).(name_of_struct) = struct();
-    
     % for every fieldname in the subgroup, create an empty field
     for fieldname = string(fieldnames)
@@ -250,4 +253,6 @@
             elseif numel(data) == 1 && xtype == 3 && data == -32767
                 data = cell(char());
+            elseif isempty(all(data))
+                data = []
             end
             % band-aid for some cell-char-arrays:
@@ -260,10 +265,10 @@
                 data = data.';
             end
-
+    
             % if we have a list of strings
             if xtype == 2
                 try
-                    if strcmp(netcdf.getAtt(group, varid, "type_is"), 'cell_array_of_strings')
-                        data = cellstr(data)
+                    if strcmp(netcdf.getAtt(group_location_in_file, varid, "type_is"), 'cell_array_of_strings')
+                        data = cellstr(data);
                     end
                 catch
@@ -280,5 +285,5 @@
                 %disp('saved int64 as int16')
             else
-                arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = ' , 'data;'];
+                arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = data;'];
                 eval(arg_to_eval);
             end
@@ -288,9 +293,16 @@
             %class(data)
             fprintf('Successfully saved %s to %s\n', varname, full_addy);
-        catch e %e is an MException struct
+
+        catch Me %e is an MException struct
+            % Some error occurred if you get here.
             fprintf(1,'There was an error with %s! \n', varname)
-            fprintf('The message was:\n%s\n',e.message);
-            fprintf(1,'The identifier was:\n%s\n',e.identifier);
-            disp()
+            errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ME.stack.name, ME.stack.line, ME.message);
+            fprintf(1, '%s\n', errorMessage);
+            uiwait(warndlg(errorMessage));
+            %line = Me.stack.line
+            %fprintf(1,'There was an error with %s! \n', varname)
+            %fprintf('The message was:\n%s\n',Me.message);
+            %fprintf(1,'The identifier was:\n%s\n',Me.identifier);
+            
             % more error handling...
         end
Index: /issm/trunk/src/m/contrib/musselman/write_netCDF.m
===================================================================
--- /issm/trunk/src/m/contrib/musselman/write_netCDF.m	(revision 27876)
+++ /issm/trunk/src/m/contrib/musselman/write_netCDF.m	(revision 27877)
@@ -95,13 +95,20 @@
     
     % Loop through each class instance in results
-    class_instance_names = fieldnames(results_var);
+    class_instance_names = fieldnames(results_var)
+
+    % we save lists of instances to the netcdf
+    solutions = {}
+    solutionsteps = {}
+    resultsdakotas = {}
     
     for i = 1:numel(class_instance_names)
         class_instance_name = class_instance_names{i};
-        
+        % there are often mutliple instances of the same class/struct so we have to number them
         % Check to see if there is a solutionstep class instance
         if contains(class_instance_name, 'solutionstep',IgnoreCase=true)
             quality_control{end+1} = 1;
-            write_string_to_netcdf('solutionstep', class_instance_name, groupid);
+            solutionsteps{end+1} = class_instance_name
+            %varname = ['solutionstep', num2str(i)]
+            %write_string_to_netcdf(varname, class_instance_name, groupid);
             disp('Successfully stored class python subclass instance: solutionstep')
         end
@@ -110,5 +117,7 @@
         if contains(class_instance_name, 'solution',IgnoreCase=true)
             quality_control{end+1} = 1;
-            write_string_to_netcdf('solution', class_instance_name, groupid);
+            solutions{end+1} = class_instance_name
+            %varname = ['solution', num2str(i)]
+            %write_string_to_netcdf(varname, class_instance_name, groupid);
             disp('Successfully stored class python subclass instance: solution')
         end
@@ -117,9 +126,22 @@
         if contains(class_instance_name, 'resultsdakota',IgnoreCase=true)
             quality_control{end+1} = 1;
-            write_string_to_netcdf('resultsdakota', class_instance_name, groupid);
+            resultsdakotas{end+1} = class_instance_name
+            %varname = ['resultsdakota', num2str(i)]
+            %write_string_to_netcdf(varname, class_instance_name, groupid);
             disp('Successfully stored class python subclass instance: resultsdakota')
         end
     end
-    
+    if ~isempty(solutionsteps)
+        write_cell_with_strings('solutionstep', solutionsteps, groupid)
+    end
+    if ~isempty(solutions)
+        write_cell_with_strings('solution', solutions, groupid)
+    end
+    if ~isempty(resultsdakotas)
+        write_cell_with_strings('resultsdakota', resultsdakotas, groupid)
+    end
+    
+    
+
     % Check if all class instances were processed correctly
     if numel(quality_control) ~= numel(class_instance_names)
@@ -319,5 +341,7 @@
 
     disp("Beginning transfer of nested MATLAB struct to the NetCDF")
-
+    % make a new subgroup to contain all the others:
+    group = netcdf.defGrp(group, parent_struct_name);
+    
     % make sure other systems can flag the nested struct type
     dimID = netcdf.defDim(group, 'struct', 6);
@@ -327,10 +351,5 @@
     netcdf.putVar(group, string_var, method_ID);
 
-    % make sure other systems know the name of the parent struct
-    uint_method=uint8(parent_struct_name).';
-    method_ID = char(uint_method);
-    dimID = netcdf.defDim(group, 'struct_name', [numel(method_ID)]);
-    string_var = netcdf.defVar(group, 'name_of_struct', "NC_CHAR", dimID);
-    netcdf.putVar(group, string_var, method_ID);
+    % other systems know the name of the parent struct because it's covered by the results/qmu functions above
     
     % 'a' will always be 1 and is not useful to us
@@ -339,5 +358,5 @@
     for substruct = 1:no_of_dims
         % we start by making subgroups with nice names like "TransientSolution_substruct_44"
-        name_of_subgroup = [parent_struct_name, '_substruct_', num2str(substruct)];
+        name_of_subgroup = ['1x', num2str(substruct)];
         subgroup = netcdf.defGrp(group, name_of_subgroup);
 
