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"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.