Changeset 27889


Ignore:
Timestamp:
08/30/23 15:08:57 (19 months ago)
Author:
musselman
Message:

Removed global vars from all files. Updated read log statements to use "load" instead of "save"

Location:
issm/trunk/src/m/contrib/musselman
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/m/contrib/musselman/README.txt

    r27887 r27889  
    3737        Verbose examples:
    3838            write_netCDF(md, adress_to_save/../filename.nc, verbose = true);
     39           
     40          or:
     41
     42            write_netCDF(md, adress_to_save/../filename.nc, verbose);
    3943            md = read_netCDF(adress_to_file/../filename.nc, verbose = true);
    4044
  • issm/trunk/src/m/contrib/musselman/read_netCDF.m

    r27884 r27889  
    1616
    1717
    18 function model_copy = read_netCDF(filename)
     18function model_copy = read_netCDF(filename, varargin)
    1919    if nargin > 1
    2020        verbose = true;
     
    2727    end
    2828    % make a model framework to fill that is in the scope of this file
    29     global model_copy;
    3029    model_copy = model();
    3130
     
    3736
    3837        % Open the given netCDF4 file
    39         global NCData;
    4038        NCData = netcdf.open(filename, 'NOWRITE');
    4139        % Remove masks from netCDF data for easy conversion: NOT WORKING
     
    4442        % see if results is in there, if it is we have to instantiate some classes
    4543        try
    46             results_group_id = netcdf.inqNcid(NCData, "results", verbose);
    47             make_results_subclasses(verbose);
     44            results_group_id = netcdf.inqNcid(NCData, "results");
     45            model_copy = make_results_subclasses(model_copy, NCData, verbose);
    4846        catch
    4947        end % 'results' group doesn't exist
     
    5250        try
    5351            inversion_group_id = netcdf.inqNcid(NCData, "inversion");
    54             check_inversion_class(verbose);
     52            model_copy = check_inversion_class(model_copy, NCData, verbose);
    5553        catch
    5654        end % 'inversion' group doesn't exist
     
    6159            %disp(netcdf.inqGrpNameFull(group_id))
    6260            % hand off first level to recursive search
    63             walk_nested_groups(group_id, verbose);
     61            model_copy = walk_nested_groups(group_id, model_copy, NCData, verbose);
    6462        end
    6563       
     
    7573
    7674
    77 function make_results_subclasses(verbose)
    78     global model_copy;
    79     global NCData;
     75function model_copy = make_results_subclasses(model_copy, NCData, verbose)
    8076    resultsGroup = netcdf.inqNcid(NCData, "results");
    8177    variables = netcdf.inqVarIDs(resultsGroup);
     
    9086        %model_copy.results = setfield(model_copy.results, class_instance, class_instance_name);
    9187    end
     88    model_copy = model_copy;
    9289    if verbose
    9390        disp('Successfully recreated results structs:')
     
    9996
    10097
    101 function check_inversion_class(verbose)
     98function model_copy = check_inversion_class(model_copy, NCData, verbose)
    10299    % get the name of the inversion class: either inversion or m1qn3inversion or taoinversion
    103     global model_copy;
    104     global NCData;
    105100    inversionGroup = netcdf.inqNcid(NCData, "inversion");
    106101    varid = netcdf.inqVarID(inversionGroup, 'inversion_class_name');
     
    121116        end
    122117    end
    123 end
    124 
    125 
    126 function walk_nested_groups(group_location_in_file, verbose)
    127     global model_copy;
    128     global NCData;   
     118    model_copy = model_copy;
     119end
     120
     121
     122function model_copy = walk_nested_groups(group_location_in_file, model_copy, NCData, verbose) 
    129123    % we search the current group level for variables by getting this struct
    130124    variables = netcdf.inqVarIDs(group_location_in_file);
     
    137131        if strcmp(varname, 'this_is_a_nested')
    138132            is_nested = true;
    139             copy_nested_struct(group_location_in_file, verbose)
     133            model_copy = copy_nested_struct(group_location_in_file, model_copy, NCData, verbose);
    140134        elseif strcmp(varname, 'solution')
    141135            % band-aid pass..
    142136        else
    143             copy_variable_data_to_new_model(group_location_in_file, varname, xtype, verbose);
     137            model_copy = copy_variable_data_to_new_model(group_location_in_file, varname, xtype, model_copy, NCData, verbose);
    144138        end
    145139    end
     
    157151                group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group));
    158152                %disp(netcdf.inqGrpNameFull(group_id))
    159                 walk_nested_groups(group, verbose);
     153                model_copy = walk_nested_groups(group, model_copy, NCData, verbose);
    160154            end
    161155        end
     
    167161
    168162
    169 function copy_nested_struct(group_location_in_file, verbose)
    170     global model_copy;
    171     global NCData;
     163function model_copy = copy_nested_struct(group_location_in_file, model_copy, NCData, verbose)
    172164    %{
    173165        A common multidimensional struct array is the 1xn md.results.TransientSolution struct.
     
    235227        model_copy.(address_in_model).(name_of_struct)(current_layer);
    236228        if verbose
    237             fprintf("Successfully saved layer %s to multidimension struct array\n", num2str(current_layer))
    238         end
    239     end
     229            fprintf("Successfully loaded layer %s to multidimension struct array\n", num2str(current_layer))
     230        end
     231    end
     232    model_copy = model_copy;
    240233    if verbose
    241234        fprintf('Successfully recreated multidimensional structure array %s in md.%s\n', name_of_struct, address_in_model)
     
    252245%}
    253246
    254 function copy_variable_data_to_new_model(group_location_in_file, varname, xtype, verbose)
    255     global model_copy;
    256     global NCData;
     247function model_copy = copy_variable_data_to_new_model(group_location_in_file, varname, xtype, model_copy, NCData, verbose)
    257248    %disp(varname)
    258249    % this is an inversion band-aid
     
    305296                arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = ' , 'double(data);'];
    306297                eval(arg_to_eval);
    307                 %disp('saved int64 as int16')
     298                %disp('Loaded int64 as int16')
    308299            else
    309300                arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = data;'];
     
    315306                %disp(xtype)
    316307                %class(data)
    317                 fprintf('Successfully saved %s to %s\n', varname, full_addy);
     308                fprintf('Successfully loaded %s to %s\n', varname, full_addy);
    318309            end
    319310
     
    332323        end
    333324    end
    334 end
     325    model_copy = model_copy;
     326end
  • issm/trunk/src/m/contrib/musselman/read_netCDF.py

    r27884 r27889  
    3434
    3535        # open the given netCDF4 file
    36         global NCData   
    3736        NCData = Dataset(filename, 'r')
    3837        # remove masks from numpy arrays for easy conversion
     
    4544    try:
    4645        NCData.groups['results']
    47         make_results_subclasses(verbose)
     46        make_results_subclasses(NCData, verbose)
    4847    except:
    4948        pass
     
    5251    try:
    5352        NCData.groups['inversion']
    54         check_inversion_class(verbose)
     53        check_inversion_class(NCData, verbose)
    5554    except:
    5655        pass
     
    6362            # have to send a custom name to this function: filename.groups['group']
    6463            name = "NCData.groups['" + str(group) + "']"
    65             walk_nested_groups(name, verbose)
     64            walk_nested_groups(name, NCData, verbose)
    6665   
    6766    if verbose:
    68         print("Model Successfully Recreated.")
     67        print("Model Successfully Loaded.")
    6968    return model_copy
    7069
    7170
    72 def make_results_subclasses(verbose = False):
     71def make_results_subclasses(NCData, verbose = False):
    7372    '''
    7473        There are 3 possible subclasses: solution, solutionstep, resultsdakota.
     
    9392
    9493
    95 def check_inversion_class(verbose = False):
     94def check_inversion_class(NCData, verbose = False):
    9695    # get the name of the inversion class: either inversion or m1qn3inversion or taoinversion
    9796    inversion_class_is = NCData.groups['inversion'].variables['inversion_class_name'][:][...].tobytes().decode()
     
    109108
    110109
    111 def walk_nested_groups(group_location_in_file, verbose = False):
     110def walk_nested_groups(group_location_in_file, NCData, verbose = False):
    112111    # first, we enter the group by: filename.groups['group_name']
    113112    # second we search the current level for variables: filename.groups['group_name'].variables.keys()
     
    123122            matches = re.findall(pattern, group_location_in_file)
    124123            name_of_struct = matches[-1] #eval(group_location_in_file + ".variables['solution']")
    125             copy_multidimensional_results_struct(group_location_in_file, name_of_struct)
     124            copy_multidimensional_results_struct(group_location_in_file, name_of_struct, NCData)
    126125            istruct = True
    127126
     
    145144            variable_name = matches[-1]
    146145            location_of_variable_in_model = '.'.join(matches[:-1])
    147             copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, verbose=verbose)
     146            copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, NCData, verbose=verbose)
    148147           
    149148    if 'istruct' in locals():
     
    152151        for nested_group in eval(group_location_in_file + '.groups.keys()'):
    153152            new_nested_group = group_location_in_file + ".groups['" + str(nested_group) + "']"
    154             walk_nested_groups(new_nested_group, verbose=verbose)
     153            walk_nested_groups(new_nested_group, NCData, verbose=verbose)
    155154
    156155
     
    163162'''
    164163
    165 def copy_multidimensional_results_struct(group_location_in_file, name_of_struct, verbose = False):
     164def copy_multidimensional_results_struct(group_location_in_file, name_of_struct, NCData, verbose = False):
    166165    '''
    167166    A common multidimensional array is the 1xn md.results.TransientSolution object.
     
    195194        steps.append(solutionstep_instance)
    196195        if verbose:
    197             print('Succesfully saved layer ' + str(layer) + ' to results.' + str(class_instance_name) + ' struct.')
     196            print('Succesfully loaded layer ' + str(layer) + ' to results.' + str(class_instance_name) + ' struct.')
    198197        else: pass
    199198        layer += 1
     
    203202
    204203
    205 def copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, verbose = False):
     204def copy_variable_data_to_new_model(location_of_variable_in_file, location_of_variable_in_model, variable_name, NCData, verbose = False):
    206205    # as simple as navigating to the location_of_variable_in_model and setting it equal to the location_of_variable_in_file
    207206    # NetCDF4 has a property called "_FillValue" that sometimes saves empty lists, so we have to catch those
     
    245244                    setattr(eval('model_copy.' + location_of_variable_in_model), variable_name, eval(location_of_variable_in_file + '[:]'))
    246245    except AttributeError:
    247         copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, verbose=verbose)
     246        copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, NCData, verbose=verbose)
    248247
    249248    if verbose:
    250         print('Successfully saved ' + location_of_variable_in_model + '.' + variable_name + ' to model.')
    251 
    252 
    253 def copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, verbose = False):
     249        print('Successfully loaded ' + location_of_variable_in_model + '.' + variable_name + ' into model.')
     250
     251
     252def copy_variable_data_to_new_model_dict(location_of_variable_in_file, location_of_variable_in_model, NCData, verbose = False):
    254253    # as simple as navigating to the location_of_variable_in_model and setting it equal to the location_of_variable_in_file
    255254
     
    300299                dict_object.update({key: data})
    301300    else:
    302         print(f"Unrecognized object was saved and cannot be reconstructed: {location_of_variable_in_model}")
     301        print(f"Unrecognized object was saved to NetCDF file and cannot be reconstructed: {location_of_variable_in_model}")
  • issm/trunk/src/m/contrib/musselman/write_netCDF.m

    r27884 r27889  
    1010
    1111
    12 function write_netCDF(model_var, filename)
     12function write_netCDF(model_var, filename, varargin)
    1313    if nargin > 2
    1414        verbose = true;
     
    1919        disp('MATLAB C2NetCDF4 v1.1.14');
    2020    end
     21   
    2122    % model_var = class object to be saved
    2223    % filename = path and name to save file under
    2324   
    2425    % Create a NetCDF file to write to
    25     make_NetCDF(filename, verbose);
     26    NetCDF = make_NetCDF(filename, verbose);
    2627   
    2728    % Create an instance of an empty model class to compare model_var against
    28     global empty_model;
    2929    empty_model = model();
    3030
    3131    % Walk through the model_var class and compare subclass states to empty_model
    32     walk_through_model(model_var, verbose);
     32    walk_through_model(model_var, empty_model, NetCDF, verbose);
    3333
    3434    % in order to handle some subclasses in the results class, we have to utilize this band-aid
    3535    % 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
    36     global NetCDF;
    3736    try
    3837        % if results had meaningful data, save the name of the subclass and class instance
    3938        netcdf.inqNcid(NetCDF,'results');
    40         results_subclasses_bandaid(model_var, verbose);
     39        results_subclasses_bandaid(model_var, NetCDF, verbose);
    4140        % otherwise, ignore
    4241    catch
     
    5150
    5251
    53 function make_NetCDF(filename, verbose)
     52function NetCDF = make_NetCDF(filename, verbose)
    5453    % matlab can't handle input in the jupyter interface, so we just yell at the user to rename
    5554    % their file if needed
     
    7170    else
    7271        % Otherwise create the file and define it globally so other functions can call it
    73         global NetCDF;
     72       
    7473        NetCDF = netcdf.create(filename, 'NETCDF4');
    7574        netcdf.putAtt(NetCDF, netcdf.getConstant('NC_GLOBAL'), 'history', ['Created ', datestr(now)]);
     
    8180            fprintf('Successfully created %s\n', filename);
    8281        end
     82
     83        return
    8384    end
    8485end
     
    9394%}
    9495
    95 function results_subclasses_bandaid(model_var, verbose)
    96     global NetCDF;
     96function results_subclasses_bandaid(model_var, NetCDF, verbose)
     97   
    9798    % The results class may have nested fields within it, so we need to record the name of
    9899    % the nested field as it appears in the model that we're trying to save
     
    120121            quality_control{end+1} = 1;
    121122            solutionsteps{end+1} = class_instance_name;
    122             %varname = ['solutionstep', num2str(i)]
    123             %write_string_to_netcdf(varname, class_instance_name, groupid);
    124123            if verbose
    125124                disp('Successfully stored class python subclass instance: solutionstep')
     
    131130            quality_control{end+1} = 1;
    132131            solutions{end+1} = class_instance_name;
    133             %varname = ['solution', num2str(i)]
    134             %write_string_to_netcdf(varname, class_instance_name, groupid);
    135132            if verbose
    136133                disp('Successfully stored class python subclass instance: solution')
     
    142139            quality_control{end+1} = 1;
    143140            resultsdakotas{end+1} = class_instance_name;
    144             %varname = ['resultsdakota', num2str(i)]
    145             %write_string_to_netcdf(varname, class_instance_name, groupid);
    146141            if verbose
    147142                disp('Successfully stored class python subclass instance: resultsdakota')
     
    150145    end
    151146    if ~isempty(solutionsteps)
    152         write_cell_with_strings('solutionstep', solutionsteps, groupid, verbose)
     147        write_cell_with_strings('solutionstep', solutionsteps, groupid, NetCDF, verbose)
    153148    end
    154149    if ~isempty(solutions)
    155         write_cell_with_strings('solution', solutions, groupid, verbose)
     150        write_cell_with_strings('solution', solutions, groupid, NetCDF, verbose)
    156151    end
    157152    if ~isempty(resultsdakotas)
    158         write_cell_with_strings('resultsdakota', resultsdakotas, groupid, verbose)
     153        write_cell_with_strings('resultsdakota', resultsdakotas, groupid, NetCDF, verbose)
    159154    end
    160155   
     
    166161        disp(class(results_var.(class_instance_name)));
    167162    else
    168         disp('The results class was successfully stored on disk');
    169     end
    170 end
    171 
    172 
    173 
    174 function walk_through_model(model_var, verbose)
    175     global empty_model;
     163        if verbose
     164            disp('The results class was successfully stored on disk');
     165        end
     166    end
     167end
     168
     169
     170
     171function walk_through_model(model_var, empty_model, NetCDF, verbose)
    176172    % Iterate over first layer of model_var attributes and assume this first layer is only classes fundamental to the model() class
    177173    % note that groups are the same as class instances/subfields in this context
     
    183179        % Now we can recursively walk through the remaining subclasses
    184180        list_of_layers = {groups{group}};
    185         walk_through_subclasses(model_subclass, empty_model_subclass, list_of_layers, verbose);
    186     end
    187 end
    188        
    189 
    190 function walk_through_subclasses(model_subclass, empty_model_subclass, given_list_of_layers, verbose)
     181        walk_through_subclasses(model_subclass, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
     182    end
     183end
     184       
     185
     186function walk_through_subclasses(model_subclass, empty_model_subclass, given_list_of_layers, empty_model, NetCDF, verbose)
    191187    % Recursivley iterate over each subclass' attributes and look for more subclasses and variables with relevant data
    192188    % model_subclass is an object (ie, md.mesh.elements)
     
    195191    if numel(given_list_of_layers) == 1
    196192        if strcmp(given_list_of_layers{1}, 'inversion')
    197             create_group(model_subclass, given_list_of_layers);
    198             check_inversion_class(model_subclass, verbose);
     193            create_group(model_subclass, given_list_of_layers, NetCDF, verbose);
     194            check_inversion_class(model_subclass, NetCDF, verbose);
    199195        end
    200196    end
     
    220216                % if the current field is a nested struct assume it has valuable data that needs to be saved
    221217                if isstruct(location_of_child) && any(size(location_of_child) > 1)
    222                     create_group(location_of_child, list_of_layers, verbose);
     218                    create_group(location_of_child, list_of_layers, NetCDF, verbose);
    223219               
    224220                % this would mean that the layer above the layer we're interested in is a struct, so
     
    230226                    % if the current attribute is a numerical array assume it has valuable data that needs to be saved
    231227                    if isnumeric(location_of_child) && logical(numel(location_of_child) > 1)
    232                         create_group(location_of_child, list_of_layers, verbose);
     228                        create_group(location_of_child, list_of_layers, NetCDF, verbose);
    233229                    % if the attributes are identical we don't need to save anything
    234230                    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))
    235                         walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
     231                        walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
    236232                    % if the attributes are not the same we need to save ours
    237233                    else
    238234                        % THE ORDER OF THESE LINES IS CRITICAL
    239                         walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
    240                         create_group(location_of_child, list_of_layers, verbose);
     235                        walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
     236                        create_group(location_of_child, list_of_layers, NetCDF, verbose);
    241237                    end
    242238                % this would mean that the layer we're interested in is not fundamental to the model architecture
    243239                % and thus needs to be saved to the netcdf
    244240                else
    245                     walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
    246                     create_group(location_of_child, list_of_layers, verbose);
     241                    walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
     242                    create_group(location_of_child, list_of_layers, NetCDF, verbose);
    247243                end
    248244            % this would mean it's not a struct, and must be a class/subclass
     
    255251                        % if the current attribute is a numerical array assume it has valuable data that needs to be saved
    256252                        if isnumeric(location_of_child) && logical(numel(location_of_child) > 1)
    257                             create_group(location_of_child, list_of_layers, verbose);
     253                            create_group(location_of_child, list_of_layers, NetCDF, verbose);
    258254                       
    259255                        elseif iscell(location_of_child)
     
    263259                            else
    264260                            % otherwise we need to save
    265                                 walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
    266                                 create_group(location_of_child, list_of_layers, verbose);
     261                                walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
     262                                create_group(location_of_child, list_of_layers, NetCDF, verbose);
    267263                            end
    268264                        elseif (all(isnan(location_of_child)) && all(isnan(location_of_child_in_empty_model)))
    269                             walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
     265                            walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
    270266                        % if the attributes are not the same we need to save ours
    271267                        else
    272268                            % THE ORDER OF THESE LINES IS CRITICAL
    273                             walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, verbose);
    274                             create_group(location_of_child, list_of_layers, verbose);
     269                            walk_through_subclasses(location_of_child, location_of_child_in_empty_model, list_of_layers, empty_model, NetCDF, verbose);
     270                            create_group(location_of_child, list_of_layers, NetCDF, verbose);
    275271                        end
    276272                    else
    277                         walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
    278                         create_group(location_of_child, list_of_layers, verbose);
     273                        walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
     274                        create_group(location_of_child, list_of_layers, NetCDF, verbose);
    279275                    end
    280276                catch
    281                     walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, verbose);
    282                     create_group(location_of_child, list_of_layers, verbose);
     277                    walk_through_subclasses(location_of_child, empty_model_subclass, list_of_layers, empty_model, NetCDF, verbose);
     278                    create_group(location_of_child, list_of_layers, NetCDF, verbose);
    283279                end
    284280            end
     
    290286        % this is if we come accross instances/subfields in our model that are not fundamental to the model class (ie, taoinversion)
    291287        elseif strcmp(ME.identifier, 'MATLAB:UndefinedFunction')
    292             walk_through_subclasses(location_of_child, empty_model_subclass, given_list_of_layers, verbose);
    293             create_group(location_of_child, list_of_layers, verbose);
     288            walk_through_subclasses(location_of_child, empty_model_subclass, given_list_of_layers, empty_model, NetCDF, verbose);
     289            create_group(location_of_child, list_of_layers, NetCDF, verbose);
    294290        % If it's a different error, rethrow it to MATLAB's default error handling
    295291        else
     
    302298       
    303299
    304 function create_group(location_of_child, list_of_layers, verbose)
    305     global NetCDF;
     300function create_group(location_of_child, list_of_layers, NetCDF, verbose)
    306301    % location_of_child is an object
    307302    % list_of_layers is a list like {'inversion', 'StressbalanceSolution','cost_functions_coefficients'}
     
    336331        elseif isstruct(location_of_child) && any(size(location_of_child) > 1)
    337332            % we have a nested struct
    338             copy_nested_struct(variable_name, location_of_child, group, verbose)
     333            copy_nested_struct(variable_name, location_of_child, group, NetCDF, verbose)
    339334        end
    340335    catch
    341336        % if that line doesn't work, it means we're dealing with raw data
    342337        % not a nested struct, so we can pass
    343         create_var(variable_name, location_of_child, group, verbose);
    344     end
    345 end
    346 
    347 
    348 
    349 function copy_nested_struct(parent_struct_name, address_of_struct, group, verbose)
     338        create_var(variable_name, location_of_child, group, NetCDF, verbose);
     339    end
     340end
     341
     342
     343
     344function copy_nested_struct(parent_struct_name, address_of_struct, group, NetCDF, verbose)
    350345    %{
    351346        This function takes a struct of structs and saves them to netcdf.
     
    387382        for variable_name = string(substruct_fields)
    388383            address_of_child = current_substruct.(variable_name);
    389             create_var(variable_name, address_of_child, subgroup, verbose);
     384            create_var(variable_name, address_of_child, subgroup, NetCDF, verbose);
    390385        end
    391386    end
     
    399394% ironically inversion does not have the same problem as results as inversion subfields
    400395% are actually subclasses and not fields
    401 function check_inversion_class(model_var, verbose)
    402     global NetCDF;
     396function check_inversion_class(model_var, NetCDF, verbose)
     397   
    403398    % Define a persistent variable to ensure this function is only run once
    404399    persistent executed;
     
    412407
    413408        if isa(model_var, 'm1qn3inversion')
    414             write_string_to_netcdf('inversion_class_name', 'm1qn3inversion', groupid, verbose);
     409            write_string_to_netcdf('inversion_class_name', 'm1qn3inversion', groupid, NetCDF, verbose);
    415410            if verbose
    416411                disp('Successfully saved inversion class instance m1qn3inversion')
    417412            end
    418413        elseif isa(model_var, 'taoinversion')
    419             write_string_to_netcdf('inversion_class_name', 'taoinversion', groupid, verbose);
     414            write_string_to_netcdf('inversion_class_name', 'taoinversion', groupid, NetCDF, verbose);
    420415            if verbose
    421416                disp('Successfully saved inversion class instance taoinversion')
    422417            end
    423418        else
    424             write_string_to_netcdf('inversion_class_name', 'inversion', groupid, verbose);
     419            write_string_to_netcdf('inversion_class_name', 'inversion', groupid, NetCDF,  verbose);
    425420            if verbose
    426421                disp('Successfully saved inversion class instance inversion')
     
    433428
    434429
    435 function create_var(variable_name, address_of_child, group, verbose)
    436     global NetCDF;
     430function create_var(variable_name, address_of_child, group, NetCDF, verbose)
    437431    % There are lots of different variable types that we need to handle from the model class
    438432   
     
    444438    % This first conditional statement will catch numeric arrays (matrices) of any dimension and save them
    445439    if any(size(address_of_child)>1) && ~iscellstr(address_of_child) && ~ischar(address_of_child)
    446         write_numeric_array_to_netcdf(variable_name, address_of_child, group, verbose);
     440        write_numeric_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose);
    447441
    448442    % check if it's a string
    449443    elseif ischar(address_of_child)
    450         write_string_to_netcdf(variable_name, address_of_child, group, verbose);
     444        write_string_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose);
    451445
    452446    % or an empty variable
     
    456450    % or a list of strings
    457451    elseif iscellstr(address_of_child) || iscell(address_of_child) && ischar(address_of_child{1})
    458         write_cell_with_strings(variable_name, address_of_child, group, verbose)
     452        write_cell_with_strings(variable_name, address_of_child, group, NetCDF, verbose)
    459453       
    460454    % or an empty list
     
    503497
    504498
    505 function write_cell_with_strings(variable_name, address_of_child, group, verbose)
     499function write_cell_with_strings(variable_name, address_of_child, group, NetCDF, verbose)
    506500    %{
    507501    Write cell array (ie {'one' 'two' 'three'}) to netcdf
    508502    %}
    509     global NetCDF
    510503   
    511504    if isempty(address_of_child)
     
    542535
    543536
    544 function write_string_to_netcdf(variable_name, address_of_child, group, verbose)
     537function write_string_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose)
    545538    % netcdf and strings don't get along.. we have to do it 'custom':
    546     global NetCDF;
    547539
    548540    the_string_to_save = address_of_child;
     
    590582
    591583
    592 function write_numeric_array_to_netcdf(variable_name, address_of_child, group, verbose)
    593     global NetCDF;
     584function write_numeric_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose)
    594585    % get the dimensions we'll need
    595586    intdim = netcdf.inqDimID(NetCDF,'int');
  • issm/trunk/src/m/contrib/musselman/write_netCDF.py

    r27884 r27889  
    3535   
    3636    # Create a NetCDF file to write to
    37     make_NetCDF(filename, verbose)
     37    NetCDF = make_NetCDF(filename, verbose)
    3838   
    3939    # Create an instance of an empty md class to compare md_var against
    40     global empty_model
    4140    empty_model = model()
    4241
    4342    # Walk through the md class and compare subclass states to empty_model
    44     walk_through_model(md, verbose)
     43    walk_through_model(md, empty_model, NetCDF, verbose)
    4544
    4645    # in order to handle some subclasses in the results class, we have to utilize this band-aid
     
    4948        # if results has meaningful data, save the name of the subclass and class instance
    5049        NetCDF.groups['results']
    51         results_subclasses_bandaid(md, verbose)
     50        results_subclasses_bandaid(md, NetCDF, verbose)
    5251        # otherwise, ignore
    5352    except KeyError:
     
    6059   
    6160
    62 def results_subclasses_bandaid(md, verbose = False):
     61def results_subclasses_bandaid(md, NetCDF, verbose = False):
    6362    # since the results class may have nested classes within it, we need to record the name of the
    6463    # nested class instance variable as it appears in the md that we're trying to save
     
    9089
    9190    if solutionsteps != []:
    92         write_string_to_netcdf(variable_name=str('solutionstep'), address_of_child=solutionsteps, group=NetCDF.groups['results'], list=True, verbose=verbose)
     91        write_string_to_netcdf(variable_name=str('solutionstep'), address_of_child=solutionsteps, group=NetCDF.groups['results'], list=True, NetCDF=NetCDF, verbose=verbose)
    9392
    9493    if solutions != []:
    95         write_string_to_netcdf(variable_name=str('solution'), address_of_child=solutions, group=NetCDF.groups['results'], list=True, verbose=verbose)
     94        write_string_to_netcdf(variable_name=str('solution'), address_of_child=solutions, group=NetCDF.groups['results'], list=True, NetCDF=NetCDF, verbose=verbose)
    9695
    9796    if resultsdakotas != []:
    98         write_string_to_netcdf(variable_name=str('resultsdakota'), address_of_child=resultsdakotas, group=NetCDF.groups['results'], list=True, verbose=verbose)
     97        write_string_to_netcdf(variable_name=str('resultsdakota'), address_of_child=resultsdakotas, group=NetCDF.groups['results'], list=True, NetCDF=NetCDF, verbose=verbose)
    9998
    10099   
     
    123122    else:
    124123        # Otherwise create the file and define it globally so other functions can call it
    125         global NetCDF
    126124        NetCDF = Dataset(filename, 'w', format='NETCDF4')
    127125        NetCDF.history = 'Created ' + time.ctime(time.time())
     
    133131        print('Successfully created ' + filename)
    134132
    135 
    136 def walk_through_model(md, verbose = False):
     133    return NetCDF
     134
     135
     136def walk_through_model(md, empty_model, NetCDF, verbose= False):
    137137    # Iterate over first layer of md_var attributes and assume this first layer is only classes
    138138    for group in md.__dict__.keys():
     
    143143
    144144        # Recursively walk through subclasses
    145         walk_through_subclasses(address, empty_address, layers, verbose)       
    146 
    147 
    148 def walk_through_subclasses(address, empty_address, layers: list, verbose = False):
     145        walk_through_subclasses(address, empty_address, layers, NetCDF, empty_model, verbose)       
     146
     147
     148def walk_through_subclasses(address, empty_address, layers: list, NetCDF, empty_model, verbose = False):
    149149    # See if we have an object with keys or a not
    150150    try:
     
    167167            # if the current object is a results.<solution> object and has the steps attr it needs special treatment
    168168            if isinstance(address_of_child, solution) and len(address_of_child.steps) != 0:
    169                 create_group(address_of_child, current_layer, is_struct = True, verbose = verbose)
     169                create_group(address_of_child, current_layer, is_struct = True, NetCDF=NetCDF, verbose = verbose)
    170170
    171171            # if the variable is an array, assume it has relevant data (this is because the next line cannot evaluate "==" with an array)
    172172            elif isinstance(address_of_child, np.ndarray):
    173                 create_group(address_of_child, current_layer, is_struct = False, verbose = verbose)
     173                create_group(address_of_child, current_layer, is_struct = False, NetCDF=NetCDF, verbose = verbose)
    174174           
    175175            # see if the child exists in the empty md. If not, record it in the netcdf
     
    181181                    # if the attributes are identical we don't need to save anything
    182182                    if address_of_child == address_of_child_in_empty_class:
    183                         walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, verbose)
     183                        walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, NetCDF, empty_model, verbose)
    184184   
    185185                    # If it has been modified, record it in the NetCDF file
    186186                    else:
    187                         create_group(address_of_child, current_layer, is_struct = False, verbose = verbose)
    188                         walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, verbose)
     187                        create_group(address_of_child, current_layer, is_struct = False, NetCDF=NetCDF, verbose = verbose)
     188                        walk_through_subclasses(address_of_child, address_of_child_in_empty_class, current_layer, NetCDF, empty_model, verbose)
    189189   
    190190                except KeyError: # record in netcdf and continue to walk thru md
    191                     walk_through_subclasses(address_of_child, empty_address, current_layer, verbose)
    192                     create_group(address_of_child, current_layer, is_struct = False, verbose = verbose)
     191                    walk_through_subclasses(address_of_child, empty_address, current_layer, NetCDF, empty_model, verbose)
     192                    create_group(address_of_child, current_layer, is_struct = False, NetCDF=NetCDF, verbose = verbose)
    193193    else: pass
    194194
    195195
    196 def create_group(address_of_child, layers, is_struct = False, verbose = False):
     196def create_group(address_of_child, layers, is_struct = False, NetCDF=None, verbose = False):
    197197
    198198    # Handle the first layer of the group(s)
     
    205205    # need to check if inversion or m1qn3inversion class
    206206    if group_name == 'inversion':
    207         check_inversion_class(address_of_child, verbose)
     207        check_inversion_class(address_of_child, NetCDF, verbose)
    208208    else: pass
    209209
     
    220220    if is_struct:
    221221        parent_struct_name = layers[-1]
    222         copy_nested_results_struct(parent_struct_name, address_of_child, group, verbose)
     222        copy_nested_results_struct(parent_struct_name, address_of_child, group, NetCDF, verbose)
    223223   
    224224    else:
    225225        variable_name = layers[-1]
    226         create_var(variable_name, address_of_child, group, verbose)
     226        create_var(variable_name, address_of_child, group, NetCDF, verbose)
    227227           
    228228
     
    242242
    243243@singleton
    244 def check_inversion_class(address_of_child, verbose = False):
     244def check_inversion_class(address_of_child, NetCDF, verbose = False):
    245245    # need to make sure that we have the right inversion class: inversion, m1qn3inversion, taoinversion
    246246    if isinstance(address_of_child, m1qn3inversion):
    247         write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('m1qn3inversion'), group=NetCDF.groups['inversion'], verbose = verbose)
     247        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('m1qn3inversion'), group=NetCDF.groups['inversion'], NetCDF=NetCDF, verbose = verbose)
    248248        if verbose:
    249249            print('Successfully saved inversion class instance ' + 'm1qn3inversion')
    250250    elif isinstance(address_of_child, taoinversion):
    251         write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('taoinversion'), group=NetCDF.groups['inversion'], verbose = verbose)
     251        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('taoinversion'), group=NetCDF.groups['inversion'], NetCDF=NetCDF, verbose = verbose)
    252252        if verbose:
    253253            print('Successfully saved inversion class instance ' + 'taoinversion')
    254254    else:
    255         write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('inversion'), group=NetCDF.groups['inversion'], verbose = verbose)
     255        write_string_to_netcdf(variable_name=str('inversion_class_name'), address_of_child=str('inversion'), group=NetCDF.groups['inversion'], NetCDF=NetCDF, verbose = verbose)
    256256        if verbose:
    257257            print('Successfully saved inversion class instance ' + 'inversion')
    258258
    259259
    260 def copy_nested_results_struct(parent_struct_name, address_of_struct, group, verbose = False):
     260def copy_nested_results_struct(parent_struct_name, address_of_struct, group, NetCDF, verbose = False):
    261261    '''
    262262        This function takes a solution class instance and saves the solutionstep instances from <solution>.steps to the netcdf.
     
    274274
    275275    # make sure other systems can flag the nested struct type
    276     write_string_to_netcdf('this_is_a_nested', 'struct', group, list=False, verbose = verbose)
     276    write_string_to_netcdf('this_is_a_nested', 'struct', group, list=False, NetCDF=NetCDF, verbose = verbose)
    277277
    278278    # other systems know the name of the parent struct because it's covered by the results/qmu functions above
     
    290290        for variable in substruct_fields:
    291291            address_of_child = current_substruct.__dict__[variable]
    292             create_var(variable, address_of_child, subgroup, verbose = verbose)
     292            create_var(variable, address_of_child, subgroup, NetCDF, verbose = verbose)
    293293   
    294294    if verbose:
     
    296296   
    297297       
    298 def create_var(variable_name, address_of_child, group, verbose = False):
     298def create_var(variable_name, address_of_child, group, NetCDF, verbose = False):
    299299    # There are lots of different variable types that we need to handle from the md class
    300300   
    301301    # This first conditional statement will catch numpy arrays of any dimension and save them
    302302    if isinstance(address_of_child, np.ndarray):
    303         write_numpy_array_to_netcdf(variable_name, address_of_child, group, verbose=verbose)
     303        write_numpy_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose=verbose)
    304304   
    305305    # check if it's an int
     
    315315    # or a string
    316316    elif isinstance(address_of_child, str):
    317         write_string_to_netcdf(variable_name, address_of_child, group, verbose=verbose)
     317        write_string_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose=verbose)
    318318
    319319    #or a bool
     
    331331    elif isinstance(address_of_child,list) and isinstance(address_of_child[0],str):
    332332        for string in address_of_child:
    333             write_string_to_netcdf(variable_name, string, group, list=True, verbose=verbose)
     333            write_string_to_netcdf(variable_name, string, group, list=True, NetCDF=NetCDF, verbose=verbose)
    334334
    335335    # or a regular list
     
    355355   
    356356
    357 def write_string_to_netcdf(variable_name, address_of_child, group, list=False, verbose = False):
     357def write_string_to_netcdf(variable_name, address_of_child, group, list=False, NetCDF=None, verbose = False):
    358358    # netcdf and strings dont get along.. we have to do it 'custom':
    359359    # if we hand it an address we need to do it this way:
     
    431431
    432432
    433 def write_numpy_array_to_netcdf(variable_name, address_of_child, group, verbose = False):
     433def write_numpy_array_to_netcdf(variable_name, address_of_child, group, NetCDF, verbose = False):
    434434    # to make a nested array in netCDF, we have to get the dimensions of the array,
    435435    # create corresponding dimensions in the netCDF file, then we can make a variable
Note: See TracChangeset for help on using the changeset viewer.