Changeset 27877 for issm/trunk/src/m/contrib/musselman/read_netCDF.m
- Timestamp:
- 08/16/23 10:34:25 (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk/src/m/contrib/musselman/read_netCDF.m
r27875 r27877 71 71 for name = variables 72 72 class_instance = netcdf.inqVar(resultsGroup, name); 73 class_instance_name = convertCharsToStrings(netcdf.getVar(resultsGroup, name, 'char')); 74 model_copy.results = setfield(model_copy.results, class_instance, class_instance_name); 75 end 76 disp('Successfully recreated results struct') 73 class_instance_names_raw = netcdf.getVar(resultsGroup, name, 'char').'; 74 class_instance_names = cellstr(class_instance_names_raw); 75 for index = 1:numel(class_instance_names) 76 class_instance_name = class_instance_names{index}; 77 model_copy.results = setfield(model_copy.results, class_instance_name, struct()); 78 end 79 %model_copy.results = setfield(model_copy.results, class_instance, class_instance_name); 80 end 81 disp('Successfully recreated results structs:') 82 for fieldname = string(fieldnames(model_copy.results)) 83 disp(fieldname) 84 end 77 85 end 78 86 … … 100 108 function walk_nested_groups(group_location_in_file) 101 109 global model_copy; 102 global NCData; 103 % try to find vars in current level, if it doesn't work it's because there is nothing there 104 try 105 % we search the current group level for variables by getting this struct 106 variables = netcdf.inqVarIDs(group_location_in_file); 107 108 % from the variables struct get the info related to the variables 109 for variable = variables 110 [varname, xtype, dimids, numatts] = netcdf.inqVar(group_location_in_file, variable); 111 112 % keep an eye out for nested structs: 113 if strcmp(varname, 'this_is_a_nested') 114 is_nested = true; 115 copy_nested_struct(group_location_in_file) 116 else 117 is_nested = false; 118 copy_variable_data_to_new_model(group_location_in_file,varname, xtype); 119 end 120 end 121 catch ME 122 rethrow(ME) 110 global NCData; 111 % we search the current group level for variables by getting this struct 112 variables = netcdf.inqVarIDs(group_location_in_file); 113 114 % from the variables struct get the info related to the variables 115 for variable = variables 116 [varname, xtype, dimids, numatts] = netcdf.inqVar(group_location_in_file, variable); 117 118 % keep an eye out for nested structs: 119 if strcmp(varname, 'this_is_a_nested') 120 is_nested = true; 121 copy_nested_struct(group_location_in_file) 122 elseif strcmp(varname, 'solution') 123 % band-aid pass.. 124 else 125 copy_variable_data_to_new_model(group_location_in_file, varname, xtype); 126 end 123 127 end 124 128 125 129 % try to find groups in current level, if it doesn't work it's because there is nothing there 126 try 127 % if it's not a nested struct, keep searching for subgroups 128 if isnested 129 % do nothing 130 else 131 % search for nested groups in the current level to feed back to this function 132 groups = netcdf.inqGrps(group_location_in_file); 133 if not(isempty(groups)) 134 for group = groups 135 %disp('found nested group!!') 136 group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group)); 137 %disp(netcdf.inqGrpNameFull(group_id)) 138 walk_nested_groups(group); 139 end 140 end 141 end 142 catch % no nested groups here 143 end 130 %try 131 % if it's a nested struct the function copy_nested_struct has already been called 132 if logical(exist('is_nested', 'var')) 133 % do nothing 134 else 135 % search for nested groups in the current level to feed back to this function 136 groups = netcdf.inqGrps(group_location_in_file); 137 if not(isempty(groups)) 138 for group = groups 139 group_id = netcdf.inqNcid(group_location_in_file, netcdf.inqGrpName(group)); 140 %disp(netcdf.inqGrpNameFull(group_id)) 141 walk_nested_groups(group); 142 end 143 end 144 end 145 %catch % no nested groups here 146 %end 144 147 end 145 148 … … 152 155 A common multidimensional struct array is the 1xn md.results.TransientSolution struct. 153 156 The process to recreate is as follows: 154 1. Get the name of the struct from group variable name_of_struct157 1. Get the name of the struct from group name 155 158 2. Get the fieldnames from the subgroups 156 159 3. Recreate the struct with fieldnames … … 159 162 160 163 % step 1 161 varid = netcdf.inqVarID(group_location_in_file, 'name_of_struct'); 162 name_of_struct = netcdf.getVar(group_location_in_file, varid)'; 164 name_of_struct = netcdf.inqGrpName(group_location_in_file); 163 165 164 166 % step 2 … … 174 176 175 177 % step 3 176 address_in_model = strrep(netcdf.inqGrpNameFull(group_location_in_file), '/', ''); 178 address_in_model_raw = split(netcdf.inqGrpNameFull(group_location_in_file), '/'); 179 address_in_model = address_in_model_raw{2}; 180 177 181 % we cannot assign a variable to represent this object as MATLAB treats all variables as copies 178 182 % and not pointers to the same memory address 179 % this means that if address_in_model is more than 1 item, we need to modify the code. For now,180 % we just hope this will do 183 % this means that if address_in_model has more than 1 layer, we need to modify the code. For now, 184 % we just hope this will do. An example of a no-solution would be model().abc.def.ghi.field 181 185 182 186 model_copy.(address_in_model).(name_of_struct) = struct(); 183 184 187 % for every fieldname in the subgroup, create an empty field 185 188 for fieldname = string(fieldnames) … … 250 253 elseif numel(data) == 1 && xtype == 3 && data == -32767 251 254 data = cell(char()); 255 elseif isempty(all(data)) 256 data = [] 252 257 end 253 258 % band-aid for some cell-char-arrays: … … 260 265 data = data.'; 261 266 end 262 267 263 268 % if we have a list of strings 264 269 if xtype == 2 265 270 try 266 if strcmp(netcdf.getAtt(group , varid, "type_is"), 'cell_array_of_strings')267 data = cellstr(data) 271 if strcmp(netcdf.getAtt(group_location_in_file, varid, "type_is"), 'cell_array_of_strings') 272 data = cellstr(data); 268 273 end 269 274 catch … … 280 285 %disp('saved int64 as int16') 281 286 else 282 arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = ' , 'data;'];287 arg_to_eval = ['model_copy', address_to_attr, '.', varname, ' = data;']; 283 288 eval(arg_to_eval); 284 289 end … … 288 293 %class(data) 289 294 fprintf('Successfully saved %s to %s\n', varname, full_addy); 290 catch e %e is an MException struct 295 296 catch Me %e is an MException struct 297 % Some error occurred if you get here. 291 298 fprintf(1,'There was an error with %s! \n', varname) 292 fprintf('The message was:\n%s\n',e.message); 293 fprintf(1,'The identifier was:\n%s\n',e.identifier); 294 disp() 299 errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ME.stack.name, ME.stack.line, ME.message); 300 fprintf(1, '%s\n', errorMessage); 301 uiwait(warndlg(errorMessage)); 302 %line = Me.stack.line 303 %fprintf(1,'There was an error with %s! \n', varname) 304 %fprintf('The message was:\n%s\n',Me.message); 305 %fprintf(1,'The identifier was:\n%s\n',Me.identifier); 306 295 307 % more error handling... 296 308 end
Note:
See TracChangeset
for help on using the changeset viewer.