Changeset 27889 for issm/trunk/src/m/contrib/musselman/read_netCDF.m
- Timestamp:
- 08/30/23 15:08:57 (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk/src/m/contrib/musselman/read_netCDF.m
r27884 r27889 16 16 17 17 18 function model_copy = read_netCDF(filename )18 function model_copy = read_netCDF(filename, varargin) 19 19 if nargin > 1 20 20 verbose = true; … … 27 27 end 28 28 % make a model framework to fill that is in the scope of this file 29 global model_copy;30 29 model_copy = model(); 31 30 … … 37 36 38 37 % Open the given netCDF4 file 39 global NCData;40 38 NCData = netcdf.open(filename, 'NOWRITE'); 41 39 % Remove masks from netCDF data for easy conversion: NOT WORKING … … 44 42 % see if results is in there, if it is we have to instantiate some classes 45 43 try 46 results_group_id = netcdf.inqNcid(NCData, "results" , verbose);47 m ake_results_subclasses(verbose);44 results_group_id = netcdf.inqNcid(NCData, "results"); 45 model_copy = make_results_subclasses(model_copy, NCData, verbose); 48 46 catch 49 47 end % 'results' group doesn't exist … … 52 50 try 53 51 inversion_group_id = netcdf.inqNcid(NCData, "inversion"); 54 check_inversion_class(verbose);52 model_copy = check_inversion_class(model_copy, NCData, verbose); 55 53 catch 56 54 end % 'inversion' group doesn't exist … … 61 59 %disp(netcdf.inqGrpNameFull(group_id)) 62 60 % 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); 64 62 end 65 63 … … 75 73 76 74 77 function make_results_subclasses(verbose) 78 global model_copy; 79 global NCData; 75 function model_copy = make_results_subclasses(model_copy, NCData, verbose) 80 76 resultsGroup = netcdf.inqNcid(NCData, "results"); 81 77 variables = netcdf.inqVarIDs(resultsGroup); … … 90 86 %model_copy.results = setfield(model_copy.results, class_instance, class_instance_name); 91 87 end 88 model_copy = model_copy; 92 89 if verbose 93 90 disp('Successfully recreated results structs:') … … 99 96 100 97 101 function check_inversion_class(verbose)98 function model_copy = check_inversion_class(model_copy, NCData, verbose) 102 99 % get the name of the inversion class: either inversion or m1qn3inversion or taoinversion 103 global model_copy;104 global NCData;105 100 inversionGroup = netcdf.inqNcid(NCData, "inversion"); 106 101 varid = netcdf.inqVarID(inversionGroup, 'inversion_class_name'); … … 121 116 end 122 117 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; 119 end 120 121 122 function model_copy = walk_nested_groups(group_location_in_file, model_copy, NCData, verbose) 129 123 % we search the current group level for variables by getting this struct 130 124 variables = netcdf.inqVarIDs(group_location_in_file); … … 137 131 if strcmp(varname, 'this_is_a_nested') 138 132 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); 140 134 elseif strcmp(varname, 'solution') 141 135 % band-aid pass.. 142 136 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); 144 138 end 145 139 end … … 157 151 group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group)); 158 152 %disp(netcdf.inqGrpNameFull(group_id)) 159 walk_nested_groups(group, verbose);153 model_copy = walk_nested_groups(group, model_copy, NCData, verbose); 160 154 end 161 155 end … … 167 161 168 162 169 function copy_nested_struct(group_location_in_file, verbose) 170 global model_copy; 171 global NCData; 163 function model_copy = copy_nested_struct(group_location_in_file, model_copy, NCData, verbose) 172 164 %{ 173 165 A common multidimensional struct array is the 1xn md.results.TransientSolution struct. … … 235 227 model_copy.(address_in_model).(name_of_struct)(current_layer); 236 228 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; 240 233 if verbose 241 234 fprintf('Successfully recreated multidimensional structure array %s in md.%s\n', name_of_struct, address_in_model) … … 252 245 %} 253 246 254 function copy_variable_data_to_new_model(group_location_in_file, varname, xtype, verbose) 255 global model_copy; 256 global NCData; 247 function model_copy = copy_variable_data_to_new_model(group_location_in_file, varname, xtype, model_copy, NCData, verbose) 257 248 %disp(varname) 258 249 % this is an inversion band-aid … … 305 296 arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = ' , 'double(data);']; 306 297 eval(arg_to_eval); 307 %disp(' saved int64 as int16')298 %disp('Loaded int64 as int16') 308 299 else 309 300 arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = data;']; … … 315 306 %disp(xtype) 316 307 %class(data) 317 fprintf('Successfully saved %s to %s\n', varname, full_addy);308 fprintf('Successfully loaded %s to %s\n', varname, full_addy); 318 309 end 319 310 … … 332 323 end 333 324 end 334 end 325 model_copy = model_copy; 326 end
Note:
See TracChangeset
for help on using the changeset viewer.