Index: /issm/trunk/src/m/contrib/musselman/README.txt
===================================================================
--- /issm/trunk/src/m/contrib/musselman/README.txt	(revision 27888)
+++ /issm/trunk/src/m/contrib/musselman/README.txt	(revision 27889)
@@ -37,4 +37,8 @@
         Verbose examples:
             write_netCDF(md, adress_to_save/../filename.nc, verbose = true);
+	    
+          or:
+
+	    write_netCDF(md, adress_to_save/../filename.nc, verbose);
             md = read_netCDF(adress_to_file/../filename.nc, verbose = true);
 
Index: /issm/trunk/src/m/contrib/musselman/read_netCDF.m
===================================================================
--- /issm/trunk/src/m/contrib/musselman/read_netCDF.m	(revision 27888)
+++ /issm/trunk/src/m/contrib/musselman/read_netCDF.m	(revision 27889)
@@ -16,5 +16,5 @@
 
 
-function model_copy = read_netCDF(filename)
+function model_copy = read_netCDF(filename, varargin)
     if nargin > 1
         verbose = true;
@@ -27,5 +27,4 @@
     end
     % make a model framework to fill that is in the scope of this file
-    global model_copy;
     model_copy = model();
 
@@ -37,5 +36,4 @@
 
         % Open the given netCDF4 file
-        global NCData;
         NCData = netcdf.open(filename, 'NOWRITE');
         % Remove masks from netCDF data for easy conversion: NOT WORKING
@@ -44,6 +42,6 @@
         % see if results is in there, if it is we have to instantiate some classes
         try
-            results_group_id = netcdf.inqNcid(NCData, "results", verbose);
-            make_results_subclasses(verbose);
+            results_group_id = netcdf.inqNcid(NCData, "results");
+            model_copy = make_results_subclasses(model_copy, NCData, verbose);
         catch
         end % 'results' group doesn't exist 
@@ -52,5 +50,5 @@
         try
             inversion_group_id = netcdf.inqNcid(NCData, "inversion");
-            check_inversion_class(verbose);
+            model_copy = check_inversion_class(model_copy, NCData, verbose);
         catch
         end % 'inversion' group doesn't exist 
@@ -61,5 +59,5 @@
             %disp(netcdf.inqGrpNameFull(group_id))
             % hand off first level to recursive search
-            walk_nested_groups(group_id, verbose);
+            model_copy = walk_nested_groups(group_id, model_copy, NCData, verbose);
         end
         
@@ -75,7 +73,5 @@
 
 
-function make_results_subclasses(verbose)
-    global model_copy;
-    global NCData;
+function model_copy = make_results_subclasses(model_copy, NCData, verbose)
     resultsGroup = netcdf.inqNcid(NCData, "results");
     variables = netcdf.inqVarIDs(resultsGroup);
@@ -90,4 +86,5 @@
         %model_copy.results = setfield(model_copy.results, class_instance, class_instance_name);
     end
+    model_copy = model_copy;
     if verbose
         disp('Successfully recreated results structs:')
@@ -99,8 +96,6 @@
 
 
-function check_inversion_class(verbose)
+function model_copy = check_inversion_class(model_copy, NCData, verbose)
     % get the name of the inversion class: either inversion or m1qn3inversion or taoinversion
-    global model_copy;
-    global NCData;
     inversionGroup = netcdf.inqNcid(NCData, "inversion");
     varid = netcdf.inqVarID(inversionGroup, 'inversion_class_name');
@@ -121,10 +116,9 @@
         end
     end
-end
-
-
-function walk_nested_groups(group_location_in_file, verbose)
-    global model_copy;
-    global NCData;    
+    model_copy = model_copy;
+end
+
+
+function model_copy = walk_nested_groups(group_location_in_file, model_copy, NCData, verbose)  
     % we search the current group level for variables by getting this struct
     variables = netcdf.inqVarIDs(group_location_in_file); 
@@ -137,9 +131,9 @@
         if strcmp(varname, 'this_is_a_nested')
             is_nested = true;
-            copy_nested_struct(group_location_in_file, verbose)
+            model_copy = copy_nested_struct(group_location_in_file, model_copy, NCData, verbose);
         elseif strcmp(varname, 'solution')
             % band-aid pass..
         else
-            copy_variable_data_to_new_model(group_location_in_file, varname, xtype, verbose);
+            model_copy = copy_variable_data_to_new_model(group_location_in_file, varname, xtype, model_copy, NCData, verbose);
         end
     end
@@ -157,5 +151,5 @@
                 group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group));
                 %disp(netcdf.inqGrpNameFull(group_id))
-                walk_nested_groups(group, verbose);
+                model_copy = walk_nested_groups(group, model_copy, NCData, verbose);
             end
         end
@@ -167,7 +161,5 @@
 
 
-function copy_nested_struct(group_location_in_file, verbose)
-    global model_copy;
-    global NCData;
+function model_copy = copy_nested_struct(group_location_in_file, model_copy, NCData, verbose)
     %{
         A common multidimensional struct array is the 1xn md.results.TransientSolution struct. 
@@ -235,7 +227,8 @@
         model_copy.(address_in_model).(name_of_struct)(current_layer);
         if verbose
-            fprintf("Successfully saved layer %s to multidimension struct array\n", num2str(current_layer))
-        end
-    end
+            fprintf("Successfully loaded layer %s to multidimension struct array\n", num2str(current_layer))
+        end
+    end
+    model_copy = model_copy;
     if verbose
         fprintf('Successfully recreated multidimensional structure array %s in md.%s\n', name_of_struct, address_in_model)
@@ -252,7 +245,5 @@
 %}
 
-function copy_variable_data_to_new_model(group_location_in_file, varname, xtype, verbose)
-    global model_copy;
-    global NCData;
+function model_copy = copy_variable_data_to_new_model(group_location_in_file, varname, xtype, model_copy, NCData, verbose)
     %disp(varname)
     % this is an inversion band-aid
@@ -305,5 +296,5 @@
                 arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = ' , 'double(data);'];
                 eval(arg_to_eval);
-                %disp('saved int64 as int16')
+                %disp('Loaded int64 as int16')
             else
                 arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = data;'];
@@ -315,5 +306,5 @@
                 %disp(xtype)
                 %class(data)
-                fprintf('Successfully saved %s to %s\n', varname, full_addy);
+                fprintf('Successfully loaded %s to %s\n', varname, full_addy);
             end
 
@@ -332,3 +323,4 @@
         end
     end
-end
+    model_copy = model_copy;
+end
Index: /issm/trunk/src/m/contrib/musselman/read_netCDF.py
===================================================================
--- /issm/trunk/src/m/contrib/musselman/read_netCDF.py	(revision 27888)
+++ /issm/trunk/src/m/contrib/musselman/read_netCDF.py	(revision 27889)
@@ -34,5 +34,4 @@
 
         # open the given netCDF4 file
-        global NCData   
         NCData = Dataset(filename, 'r')
         # remove masks from numpy arrays for easy conversion
@@ -45,5 +44,5 @@
     try:
         NCData.groups['results']
-        make_results_subclasses(verbose)
+        make_results_subclasses(NCData, verbose)
     except:
         pass
@@ -52,5 +51,5 @@
     try:
         NCData.groups['inversion']
-        check_inversion_class(verbose)
+        check_inversion_class(NCData, verbose)
     except:
         pass
@@ -63,12 +62,12 @@
             # have to send a custom name to this function: filename.groups['group']
             name = "NCData.groups['" + str(group) + "']"
-            walk_nested_groups(name, verbose)
+            walk_nested_groups(name, NCData, verbose)
     
     if verbose:
-        print("Model Successfully Recreated.")
+        print("Model Successfully Loaded.")
     return model_copy
 
 
-def make_results_subclasses(verbose = False):
+def make_results_subclasses(NCData, verbose = False):
     '''
         There are 3 possible subclasses: solution, solutionstep, resultsdakota.
@@ -93,5 +92,5 @@
 
 
-def check_inversion_class(verbose = False):
+def check_inversion_class(NCData, verbose = False):
     # get the name of the inversion class: either inversion or m1qn3inversion or taoinversion
     inversion_class_is = NCData.groups['inversion'].variables['inversion_class_name'][:][...].tobytes().decode()
@@ -109,5 +108,5 @@
 
 
-def walk_nested_groups(group_location_in_file, verbose = False):
+def walk_nested_groups(group_location_in_file, NCData, verbose = False):
     # first, we enter the group by: filename.groups['group_name']
     # second we search the current level for variables: filename.groups['group_name'].variables.keys()
@@ -123,5 +122,5 @@
             matches = re.findall(pattern, group_location_in_file)
             name_of_struct = matches[-1] #eval(group_location_in_file + ".variables['solution']") 
-            copy_multidimensional_results_struct(group_location_in_file, name_of_struct)
+            copy_multidimensional_results_struct(group_location_in_file, name_of_struct, NCData)
             istruct = True
 
@@ -145,5 +144,5 @@
             variable_name = matches[-1]
             location_of_variable_in_model = '.'.join(matches[:-1])
-            copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, verbose=verbose)
+            copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, NCData, verbose=verbose)
             
     if 'istruct' in locals():
@@ -152,5 +151,5 @@
         for nested_group in eval(group_location_in_file + '.groups.keys()'):
             new_nested_group = group_location_in_file + ".groups['" + str(nested_group) + "']"
-            walk_nested_groups(new_nested_group, verbose=verbose)
+            walk_nested_groups(new_nested_group, NCData, verbose=verbose)
 
 
@@ -163,5 +162,5 @@
 '''
 
-def copy_multidimensional_results_struct(group_location_in_file, name_of_struct, verbose = False):
+def copy_multidimensional_results_struct(group_location_in_file, name_of_struct, NCData, verbose = False):
     '''
     A common multidimensional array is the 1xn md.results.TransientSolution object.
@@ -195,5 +194,5 @@
         steps.append(solutionstep_instance)
         if verbose:
-            print('Succesfully saved layer ' + str(layer) + ' to results.' + str(class_instance_name) + ' struct.')
+            print('Succesfully loaded layer ' + str(layer) + ' to results.' + str(class_instance_name) + ' struct.')
         else: pass
         layer += 1
@@ -203,5 +202,5 @@
 
 
-def copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, verbose = False):
+def copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, NCData, verbose = False):
     # as simple as navigating to the location_of_variable_in_model and setting it equal to the location_of_variable_in_file
     # NetCDF4 has a property called "_FillValue" that sometimes saves empty lists, so we have to catch those
@@ -245,11 +244,11 @@
                     setattr(eval('model_copy.' + location_of_variable_in_model), variable_name, eval(location_of_variable_in_file + '[:]'))
     except AttributeError:
-        copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, verbose=verbose)
+        copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, NCData, verbose=verbose)
 
     if verbose:
-        print('Successfully saved ' + location_of_variable_in_model + '.' + variable_name + ' to model.')
-
-
-def copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, verbose = False):
+        print('Successfully loaded ' + location_of_variable_in_model + '.' + variable_name + ' into model.')
+
+
+def copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, NCData, verbose = False):
     # as simple as navigating to the location_of_variable_in_model and setting it equal to the location_of_variable_in_file
 
@@ -300,3 +299,3 @@
                 dict_object.update({key: data})
     else:
-        print(f"Unrecognized object was saved and cannot be reconstructed: {location_of_variable_in_model}")
+        print(f"Unrecognized object was saved to NetCDF file and cannot be reconstructed: {location_of_variable_in_model}")
Index: /issm/trunk/src/m/contrib/musselman/write_netCDF.m
===================================================================
--- /issm/trunk/src/m/contrib/musselman/write_netCDF.m	(revision 27888)
+++ /issm/trunk/src/m/contrib/musselman/write_netCDF.m	(revision 27889)
@@ -10,5 +10,5 @@
 
 
-function write_netCDF(model_var, filename)
+function write_netCDF(model_var, filename, varargin)
     if nargin > 2
         verbose = true;
@@ -19,24 +19,23 @@
         disp('MATLAB C2NetCDF4 v1.1.14');
     end
+    
     % model_var = class object to be saved
     % filename = path and name to save file under
     
     % Create a NetCDF file to write to
-    make_NetCDF(filename, verbose);
+    NetCDF = make_NetCDF(filename, verbose);
     
     % Create an instance of an empty model class to compare model_var against
-    global empty_model;
     empty_model = model();
 
     % Walk through the model_var class and compare subclass states to empty_model
-    walk_through_model(model_var, verbose);
+    walk_through_model(model_var, empty_model, NetCDF, verbose);
 
     % in order to handle some subclasses in the results class, we have to utilize this band-aid
     % there will likely be more band-aids added unless a class name library is created with all class names that might be added to a model
-    global NetCDF;
     try
         % if results had meaningful data, save the name of the subclass and class instance
         netcdf.inqNcid(NetCDF,'results');
-        results_subclasses_bandaid(model_var, verbose);
+        results_subclasses_bandaid(model_var, NetCDF, verbose);
         % otherwise, ignore
     catch
@@ -51,5 +50,5 @@
 
 
-function make_NetCDF(filename, verbose)
+function NetCDF = make_NetCDF(filename, verbose)
     % matlab can't handle input in the jupyter interface, so we just yell at the user to rename
     % their file if needed
@@ -71,5 +70,5 @@
     else
         % Otherwise create the file and define it globally so other functions can call it
-        global NetCDF;
+        
         NetCDF = netcdf.create(filename, 'NETCDF4');
         netcdf.putAtt(NetCDF, netcdf.getConstant('NC_GLOBAL'), 'history', ['Created ', datestr(now)]);
@@ -81,4 +80,6 @@
             fprintf('Successfully created %s\n', filename);
         end
+
+        return 
     end
 end
@@ -93,6 +94,6 @@
 %}
 
-function results_subclasses_bandaid(model_var, verbose)
-    global NetCDF;
+function results_subclasses_bandaid(model_var, NetCDF, verbose)
+    
     % The results class may have nested fields within it, so we need to record the name of 
     % the nested field as it appears in the model that we're trying to save
@@ -120,6 +121,4 @@
             quality_control{end+1} = 1;
             solutionsteps{end+1} = class_instance_name;
-            %varname = ['solutionstep', num2str(i)]
-            %write_string_to_netcdf(varname, class_instance_name, groupid);
             if verbose
                 disp('Successfully stored class python subclass instance: solutionstep')
@@ -131,6 +130,4 @@
             quality_control{end+1} = 1;
             solutions{end+1} = class_instance_name;
-            %varname = ['solution', num2str(i)]
-            %write_string_to_netcdf(varname, class_instance_name, groupid);
             if verbose
                 disp('Successfully stored class python subclass instance: solution')
@@ -142,6 +139,4 @@
             quality_control{end+1} = 1;
             resultsdakotas{end+1} = class_instance_name;
-            %varname = ['resultsdakota', num2str(i)]
-            %write_string_to_netcdf(varname, class_instance_name, groupid);
             if verbose
                 disp('Successfully stored class python subclass instance: resultsdakota')
@@ -150,11 +145,11 @@
     end
     if ~isempty(solutionsteps)
-        write_cell_with_strings('solutionstep', solutionsteps, groupid, verbose)
+        write_cell_with_strings('solutionstep', solutionsteps, groupid, NetCDF, verbose)
     end
     if ~isempty(solutions)
-        write_cell_with_strings('solution', solutions, groupid, verbose)
+        write_cell_with_strings('solution', solutions, groupid, NetCDF, verbose)
     end
     if ~isempty(resultsdakotas)
-        write_cell_with_strings('resultsdakota', resultsdakotas, groupid, verbose)
+        write_cell_with_strings('resultsdakota', resultsdakotas, groupid, NetCDF, verbose)
     end
     
@@ -166,12 +161,13 @@
         disp(class(results_var.(class_instance_name)));
     else
-        disp('The results class was successfully stored on disk');
-    end
-end
-
-
-
-function walk_through_model(model_var, verbose)
-    global empty_model;
+        if verbose
+            disp('The results class was successfully stored on disk');
+        end
+    end
+end
+
+
+
+function walk_through_model(model_var, empty_model, NetCDF, verbose)
     % Iterate over first layer of model_var attributes and assume this first layer is only classes fundamental to the model() class
     % note that groups are the same as class instances/subfields in this context
@@ -183,10 +179,10 @@
         % Now we can recursively walk through the remaining subclasses
         list_of_layers = {groups{group}};
-        walk_through_subclasses(model_subclass, empty_model_subclass, list_of_layers, verbose);
-    end
-end
-        
-
-function walk_through_subclasses(model_subclass, empty_model_subclass, given_list_of_layers, verbose)
+        walk_through_subclasses(model_subclass, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
+    end
+end
+        
+
+function walk_through_subclasses(model_subclass, empty_model_subclass, given_list_of_layers, empty_model, NetCDF, verbose)
     % Recursivley iterate over each subclass' attributes and look for more subclasses and variables with relevant data
     % model_subclass is an object (ie, md.mesh.elements)
@@ -195,6 +191,6 @@
     if numel(given_list_of_layers) == 1
         if strcmp(given_list_of_layers{1}, 'inversion')
-            create_group(model_subclass, given_list_of_layers);
-            check_inversion_class(model_subclass, verbose);
+            create_group(model_subclass, given_list_of_layers, NetCDF, verbose);
+            check_inversion_class(model_subclass, NetCDF, verbose);
         end
     end
@@ -220,5 +216,5 @@
                 % if the current field is a nested struct assume it has valuable data that needs to be saved
                 if isstruct(location_of_child) && any(size(location_of_child) > 1)
-                    create_group(location_of_child, list_of_layers, verbose);
+                    create_group(location_of_child, list_of_layers, NetCDF, verbose);
                 
                 % this would mean that the layer above the layer we're interested in is a struct, so
@@ -230,19 +226,19 @@
                     % if the current attribute is a numerical array assume it has valuable data that needs to be saved
                     if isnumeric(location_of_child) && logical(numel(location_of_child) > 1)
-                        create_group(location_of_child, list_of_layers, verbose);
+                        create_group(location_of_child, list_of_layers, NetCDF, verbose);
                     % if the attributes are identical we don't need to save anything
                     elseif (all(isnan(location_of_child)) && all(isnan(location_of_child_in_empty_model))) || isempty(setxor(location_of_child, location_of_child_in_empty_model))
-                        walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
+                        walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
                     % if the attributes are not the same we need to save ours
                     else
                         % THE ORDER OF THESE LINES IS CRITICAL
-                        walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
-                        create_group(location_of_child, list_of_layers, verbose);
+                        walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
+                        create_group(location_of_child, list_of_layers, NetCDF, verbose);
                     end
                 % this would mean that the layer we're interested in is not fundamental to the model architecture
                 % and thus needs to be saved to the netcdf
                 else
-                    walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
-                    create_group(location_of_child, list_of_layers, verbose);
+                    walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
+                    create_group(location_of_child, list_of_layers, NetCDF, verbose);
                 end
             % this would mean it's not a struct, and must be a class/subclass
@@ -255,5 +251,5 @@
                         % if the current attribute is a numerical array assume it has valuable data that needs to be saved
                         if isnumeric(location_of_child) && logical(numel(location_of_child) > 1)
-                            create_group(location_of_child, list_of_layers, verbose);
+                            create_group(location_of_child, list_of_layers, NetCDF, verbose);
                         
                         elseif iscell(location_of_child)
@@ -263,22 +259,22 @@
                             else
                             % otherwise we need to save
-                                walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
-                                create_group(location_of_child, list_of_layers, verbose);
+                                walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
+                                create_group(location_of_child, list_of_layers, NetCDF, verbose);
                             end
                         elseif (all(isnan(location_of_child)) && all(isnan(location_of_child_in_empty_model)))
-                            walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
+                            walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
                         % if the attributes are not the same we need to save ours
                         else
                             % THE ORDER OF THESE LINES IS CRITICAL
-                            walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
-                            create_group(location_of_child, list_of_layers, verbose);
+                            walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
+                            create_group(location_of_child, list_of_layers, NetCDF, verbose);
                         end
                     else
-                        walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
-                        create_group(location_of_child, list_of_layers, verbose);
+                        walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
+                        create_group(location_of_child, list_of_layers, NetCDF, verbose);
                     end
                 catch
-                    walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
-                    create_group(location_of_child, list_of_layers, verbose);
+                    walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
+                    create_group(location_of_child, list_of_layers, NetCDF, verbose);
                 end
             end
@@ -290,6 +286,6 @@
         % this is if we come accross instances/subfields in our model that are not fundamental to the model class (ie, taoinversion)
         elseif strcmp(ME.identifier, 'MATLAB:UndefinedFunction')
-            walk_through_subclasses(location_of_child, empty_model_subclass, given_list_of_layers, verbose);
-            create_group(location_of_child, list_of_layers, verbose);
+            walk_through_subclasses(location_of_child, empty_model_subclass, given_list_of_layers, empty_model, NetCDF, verbose);
+            create_group(location_of_child, list_of_layers, NetCDF, verbose);
         % If it's a different error, rethrow it to MATLAB's default error handling
         else
@@ -302,6 +298,5 @@
         
 
-function create_group(location_of_child, list_of_layers, verbose)
-    global NetCDF;
+function create_group(location_of_child, list_of_layers, NetCDF, verbose)
     % location_of_child is an object
     % list_of_layers is a list like {'inversion', 'StressbalanceSolution','cost_functions_coefficients'}
@@ -336,16 +331,16 @@
         elseif isstruct(location_of_child) && any(size(location_of_child) > 1)
             % we have a nested struct
-            copy_nested_struct(variable_name, location_of_child, group, verbose)
+            copy_nested_struct(variable_name, location_of_child, group, NetCDF, verbose)
         end
     catch
         % if that line doesn't work, it means we're dealing with raw data
         % not a nested struct, so we can pass
-        create_var(variable_name, location_of_child, group, verbose);
-    end
-end
-
-
-
-function copy_nested_struct(parent_struct_name, address_of_struct, group, verbose)
+        create_var(variable_name, location_of_child, group, NetCDF, verbose);
+    end
+end
+
+
+
+function copy_nested_struct(parent_struct_name, address_of_struct, group, NetCDF, verbose)
     %{
         This function takes a struct of structs and saves them to netcdf. 
@@ -387,5 +382,5 @@
         for variable_name = string(substruct_fields)
             address_of_child = current_substruct.(variable_name);
-            create_var(variable_name, address_of_child, subgroup, verbose);
+            create_var(variable_name, address_of_child, subgroup, NetCDF, verbose);
         end
     end
@@ -399,6 +394,6 @@
 % ironically inversion does not have the same problem as results as inversion subfields
 % are actually subclasses and not fields
-function check_inversion_class(model_var, verbose)
-    global NetCDF;
+function check_inversion_class(model_var, NetCDF, verbose)
+    
     % Define a persistent variable to ensure this function is only run once
     persistent executed;
@@ -412,15 +407,15 @@
 
         if isa(model_var, 'm1qn3inversion')
-            write_string_to_netcdf('inversion_class_name', 'm1qn3inversion', groupid, verbose);
+            write_string_to_netcdf('inversion_class_name', 'm1qn3inversion', groupid, NetCDF, verbose);
             if verbose
                 disp('Successfully saved inversion class instance m1qn3inversion')
             end
         elseif isa(model_var, 'taoinversion')
-            write_string_to_netcdf('inversion_class_name', 'taoinversion', groupid, verbose);
+            write_string_to_netcdf('inversion_class_name', 'taoinversion', groupid, NetCDF, verbose);
             if verbose 
                 disp('Successfully saved inversion class instance taoinversion')
             end
         else
-            write_string_to_netcdf('inversion_class_name', 'inversion', groupid, verbose);
+            write_string_to_netcdf('inversion_class_name', 'inversion', groupid, NetCDF,  verbose);
             if verbose
                 disp('Successfully saved inversion class instance inversion')
@@ -433,6 +428,5 @@
 
 
-function create_var(variable_name, address_of_child, group, verbose)
-    global NetCDF;
+function create_var(variable_name, address_of_child, group, NetCDF, verbose)
     % There are lots of different variable types that we need to handle from the model class
     
@@ -444,9 +438,9 @@
     % This first conditional statement will catch numeric arrays (matrices) of any dimension and save them
     if any(size(address_of_child)>1) && ~iscellstr(address_of_child) && ~ischar(address_of_child)
-        write_numeric_array_to_netcdf(variable_name, address_of_child, group, verbose);
+        write_numeric_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose);
 
     % check if it's a string
     elseif ischar(address_of_child)
-        write_string_to_netcdf(variable_name, address_of_child, group, verbose);
+        write_string_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose);
 
     % or an empty variable
@@ -456,5 +450,5 @@
     % or a list of strings
     elseif iscellstr(address_of_child) || iscell(address_of_child) && ischar(address_of_child{1})
-        write_cell_with_strings(variable_name, address_of_child, group, verbose)
+        write_cell_with_strings(variable_name, address_of_child, group, NetCDF, verbose)
         
     % or an empty list
@@ -503,9 +497,8 @@
 
 
-function write_cell_with_strings(variable_name, address_of_child, group, verbose)
+function write_cell_with_strings(variable_name, address_of_child, group, NetCDF, verbose)
     %{
     Write cell array (ie {'one' 'two' 'three'}) to netcdf
     %}
-    global NetCDF
     
     if isempty(address_of_child)
@@ -542,7 +535,6 @@
 
 
-function write_string_to_netcdf(variable_name, address_of_child, group, verbose)
+function write_string_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose)
     % netcdf and strings don't get along.. we have to do it 'custom':
-    global NetCDF;
 
     the_string_to_save = address_of_child;
@@ -590,6 +582,5 @@
 
 
-function write_numeric_array_to_netcdf(variable_name, address_of_child, group, verbose)
-    global NetCDF;
+function write_numeric_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose)
     % get the dimensions we'll need
     intdim = netcdf.inqDimID(NetCDF,'int');
Index: /issm/trunk/src/m/contrib/musselman/write_netCDF.py
===================================================================
--- /issm/trunk/src/m/contrib/musselman/write_netCDF.py	(revision 27888)
+++ /issm/trunk/src/m/contrib/musselman/write_netCDF.py	(revision 27889)
@@ -35,12 +35,11 @@
     
     # Create a NetCDF file to write to
-    make_NetCDF(filename, verbose)
+    NetCDF = make_NetCDF(filename, verbose)
     
     # Create an instance of an empty md class to compare md_var against
-    global empty_model
     empty_model = model()
 
     # Walk through the md class and compare subclass states to empty_model
-    walk_through_model(md, verbose)
+    walk_through_model(md, empty_model, NetCDF, verbose)
 
     # in order to handle some subclasses in the results class, we have to utilize this band-aid
@@ -49,5 +48,5 @@
         # if results has meaningful data, save the name of the subclass and class instance
         NetCDF.groups['results']
-        results_subclasses_bandaid(md, verbose)
+        results_subclasses_bandaid(md, NetCDF, verbose)
         # otherwise, ignore
     except KeyError:
@@ -60,5 +59,5 @@
     
 
-def results_subclasses_bandaid(md, verbose = False):
+def results_subclasses_bandaid(md, NetCDF, verbose = False):
     # since the results class may have nested classes within it, we need to record the name of the 
     # nested class instance variable as it appears in the md that we're trying to save
@@ -90,11 +89,11 @@
 
     if solutionsteps != []:
-        write_string_to_netcdf(variable_name=str('solutionstep'), address_of_child=solutionsteps, group=NetCDF.groups['results'], list=True, verbose=verbose)
+        write_string_to_netcdf(variable_name=str('solutionstep'), address_of_child=solutionsteps, group=NetCDF.groups['results'], list=True, NetCDF=NetCDF, verbose=verbose)
 
     if solutions != []:
-        write_string_to_netcdf(variable_name=str('solution'), address_of_child=solutions, group=NetCDF.groups['results'], list=True, verbose=verbose)
+        write_string_to_netcdf(variable_name=str('solution'), address_of_child=solutions, group=NetCDF.groups['results'], list=True, NetCDF=NetCDF, verbose=verbose)
 
     if resultsdakotas != []:
-        write_string_to_netcdf(variable_name=str('resultsdakota'), address_of_child=resultsdakotas, group=NetCDF.groups['results'], list=True, verbose=verbose)
+        write_string_to_netcdf(variable_name=str('resultsdakota'), address_of_child=resultsdakotas, group=NetCDF.groups['results'], list=True, NetCDF=NetCDF, verbose=verbose)
 
     
@@ -123,5 +122,4 @@
     else:
         # Otherwise create the file and define it globally so other functions can call it
-        global NetCDF
         NetCDF = Dataset(filename, 'w', format='NETCDF4')
         NetCDF.history = 'Created ' + time.ctime(time.time())
@@ -133,6 +131,8 @@
         print('Successfully created ' + filename)
 
-
-def walk_through_model(md, verbose = False):
+    return NetCDF
+
+
+def walk_through_model(md, empty_model, NetCDF, verbose= False):
     # Iterate over first layer of md_var attributes and assume this first layer is only classes
     for group in md.__dict__.keys():
@@ -143,8 +143,8 @@
 
         # Recursively walk through subclasses
-        walk_through_subclasses(address, empty_address, layers, verbose)       
-
-
-def walk_through_subclasses(address, empty_address, layers: list, verbose = False):
+        walk_through_subclasses(address, empty_address, layers, NetCDF, empty_model, verbose)       
+
+
+def walk_through_subclasses(address, empty_address, layers: list, NetCDF, empty_model, verbose = False):
     # See if we have an object with keys or a not
     try:
@@ -167,9 +167,9 @@
             # if the current object is a results.<solution> object and has the steps attr it needs special treatment
             if isinstance(address_of_child, solution) and len(address_of_child.steps) != 0:
-                create_group(address_of_child, current_layer, is_struct = True, verbose = verbose)
+                create_group(address_of_child, current_layer, is_struct = True, NetCDF=NetCDF, verbose = verbose)
 
             # if the variable is an array, assume it has relevant data (this is because the next line cannot evaluate "==" with an array)
             elif isinstance(address_of_child, np.ndarray):
-                create_group(address_of_child, current_layer, is_struct = False, verbose = verbose)
+                create_group(address_of_child, current_layer, is_struct = False, NetCDF=NetCDF, verbose = verbose)
             
             # see if the child exists in the empty md. If not, record it in the netcdf
@@ -181,18 +181,18 @@
                     # if the attributes are identical we don't need to save anything
                     if address_of_child == address_of_child_in_empty_class:
-                        walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, verbose)
+                        walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, NetCDF, empty_model, verbose)
     
                     # If it has been modified, record it in the NetCDF file
                     else:
-                        create_group(address_of_child, current_layer, is_struct = False, verbose = verbose)
-                        walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, verbose)
+                        create_group(address_of_child, current_layer, is_struct = False, NetCDF=NetCDF, verbose = verbose)
+                        walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, NetCDF, empty_model, verbose)
     
                 except KeyError: # record in netcdf and continue to walk thru md
-                    walk_through_subclasses(address_of_child, empty_address, current_layer, verbose)
-                    create_group(address_of_child, current_layer, is_struct = False, verbose = verbose)
+                    walk_through_subclasses(address_of_child, empty_address, current_layer, NetCDF, empty_model, verbose)
+                    create_group(address_of_child, current_layer, is_struct = False, NetCDF=NetCDF, verbose = verbose)
     else: pass
 
 
-def create_group(address_of_child, layers, is_struct = False, verbose = False):
+def create_group(address_of_child, layers, is_struct = False, NetCDF=None, verbose = False):
 
     # Handle the first layer of the group(s)
@@ -205,5 +205,5 @@
     # need to check if inversion or m1qn3inversion class
     if group_name == 'inversion':
-        check_inversion_class(address_of_child, verbose)
+        check_inversion_class(address_of_child, NetCDF, verbose)
     else: pass
 
@@ -220,9 +220,9 @@
     if is_struct:
         parent_struct_name = layers[-1]
-        copy_nested_results_struct(parent_struct_name, address_of_child, group, verbose)
+        copy_nested_results_struct(parent_struct_name, address_of_child, group, NetCDF, verbose)
     
     else:
         variable_name = layers[-1]
-        create_var(variable_name, address_of_child, group, verbose)
+        create_var(variable_name, address_of_child, group, NetCDF, verbose)
             
 
@@ -242,21 +242,21 @@
 
 @singleton
-def check_inversion_class(address_of_child, verbose = False):
+def check_inversion_class(address_of_child, NetCDF, verbose = False):
     # need to make sure that we have the right inversion class: inversion, m1qn3inversion, taoinversion
     if isinstance(address_of_child, m1qn3inversion):
-        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('m1qn3inversion'), group=NetCDF.groups['inversion'], verbose = verbose)
+        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('m1qn3inversion'), group=NetCDF.groups['inversion'], NetCDF=NetCDF, verbose = verbose)
         if verbose:
             print('Successfully saved inversion class instance ' + 'm1qn3inversion')
     elif isinstance(address_of_child, taoinversion):
-        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('taoinversion'), group=NetCDF.groups['inversion'], verbose = verbose)
+        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('taoinversion'), group=NetCDF.groups['inversion'], NetCDF=NetCDF, verbose = verbose)
         if verbose:
             print('Successfully saved inversion class instance ' + 'taoinversion')
     else:
-        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('inversion'), group=NetCDF.groups['inversion'], verbose = verbose)
+        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('inversion'), group=NetCDF.groups['inversion'], NetCDF=NetCDF, verbose = verbose)
         if verbose:
             print('Successfully saved inversion class instance ' + 'inversion')
 
 
-def copy_nested_results_struct(parent_struct_name, address_of_struct, group, verbose = False):
+def copy_nested_results_struct(parent_struct_name, address_of_struct, group, NetCDF, verbose = False):
     '''
         This function takes a solution class instance and saves the solutionstep instances from <solution>.steps to the netcdf. 
@@ -274,5 +274,5 @@
 
     # make sure other systems can flag the nested struct type
-    write_string_to_netcdf('this_is_a_nested', 'struct', group, list=False, verbose = verbose)
+    write_string_to_netcdf('this_is_a_nested', 'struct', group, list=False, NetCDF=NetCDF, verbose = verbose)
 
     # other systems know the name of the parent struct because it's covered by the results/qmu functions above
@@ -290,5 +290,5 @@
         for variable in substruct_fields:
             address_of_child = current_substruct.__dict__[variable]
-            create_var(variable, address_of_child, subgroup, verbose = verbose)
+            create_var(variable, address_of_child, subgroup, NetCDF, verbose = verbose)
     
     if verbose:
@@ -296,10 +296,10 @@
     
         
-def create_var(variable_name, address_of_child, group, verbose = False):
+def create_var(variable_name, address_of_child, group, NetCDF, verbose = False):
     # There are lots of different variable types that we need to handle from the md class
     
     # This first conditional statement will catch numpy arrays of any dimension and save them
     if isinstance(address_of_child, np.ndarray):
-        write_numpy_array_to_netcdf(variable_name, address_of_child, group, verbose=verbose)
+        write_numpy_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose=verbose)
     
     # check if it's an int
@@ -315,5 +315,5 @@
     # or a string
     elif isinstance(address_of_child, str):
-        write_string_to_netcdf(variable_name, address_of_child, group, verbose=verbose)
+        write_string_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose=verbose)
 
     #or a bool
@@ -331,5 +331,5 @@
     elif isinstance(address_of_child,list) and isinstance(address_of_child[0],str):
         for string in address_of_child:
-            write_string_to_netcdf(variable_name, string, group, list=True, verbose=verbose)
+            write_string_to_netcdf(variable_name, string, group, list=True, NetCDF=NetCDF, verbose=verbose)
 
     # or a regular list
@@ -355,5 +355,5 @@
     
 
-def write_string_to_netcdf(variable_name, address_of_child, group, list=False, verbose = False):
+def write_string_to_netcdf(variable_name, address_of_child, group, list=False, NetCDF=None, verbose = False):
     # netcdf and strings dont get along.. we have to do it 'custom':
     # if we hand it an address we need to do it this way:
@@ -431,5 +431,5 @@
 
 
-def write_numpy_array_to_netcdf(variable_name, address_of_child, group, verbose = False):
+def write_numpy_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose = False):
     # to make a nested array in netCDF, we have to get the dimensions of the array,
     # create corresponding dimensions in the netCDF file, then we can make a variable
