Home > database > ensemble_datastruct2tree.m

ensemble_datastruct2tree

PURPOSE ^

Converts from an Ensemble data structure to a tree structure in the same format that mysql_extract_metadata uses.

SYNOPSIS ^

function outtree = ensemble_datastruct2tree(dataStruct,params)

DESCRIPTION ^

 Converts from an Ensemble data structure to a tree structure in the same format that mysql_extract_metadata uses.

 outtree = ensemble_datastruct2tree(dataStruct,params);

 WARNING: If one of the data fields in the data structure has the same name
 as one of the datastruct fields, e.g. name, type, meta, the data will be
 overwritten unless a method of conflict resolution other than
 'datastruct_rules' is specified in params.conflict.(fieldname), where
 fieldname is the name of the field in question.

 The reason that the datastruct field names are tacked on to the output
 structure is to preserve back-and-forth convertability between the tree
 and datastruct format. This is not optimal.

 If you know you don't need to preserve convertability, then go ahead and
 set a non-empty conflict resolution rule

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function outtree = ensemble_datastruct2tree(dataStruct,params)
0002 % Converts from an Ensemble data structure to a tree structure in the same format that mysql_extract_metadata uses.
0003 %
0004 % outtree = ensemble_datastruct2tree(dataStruct,params);
0005 %
0006 % WARNING: If one of the data fields in the data structure has the same name
0007 % as one of the datastruct fields, e.g. name, type, meta, the data will be
0008 % overwritten unless a method of conflict resolution other than
0009 % 'datastruct_rules' is specified in params.conflict.(fieldname), where
0010 % fieldname is the name of the field in question.
0011 %
0012 % The reason that the datastruct field names are tacked on to the output
0013 % structure is to preserve back-and-forth convertability between the tree
0014 % and datastruct format. This is not optimal.
0015 %
0016 % If you know you don't need to preserve convertability, then go ahead and
0017 % set a non-empty conflict resolution rule
0018 
0019 % 16 March, 2007 Stefan Tomic
0020 % 28 April, 2007 Petr Janata - changed tree to outtree, and added check to
0021 %                make sure outtree isn't empty prior to attempting the call
0022 %                to convert_structarray
0023 %  5 July, 2007  Stefan Tomic - allowing empty data fields and
0024 %                               differing data lengths. No longer
0025 %                               performing conversion to array of structs
0026 % 10/19/07  PJ Fixed trapping of empty values
0027 % 2/22/10   ST Fixed conversion of regular (non-cell) struct arrays
0028 % 16Jun2011 PJ Added conflict resolution
0029   
0030 outtree = [];
0031 
0032 defaultResolution = 'datastruct_rules';
0033 
0034 fnames = dataStruct.vars;
0035 
0036 for ifld = 1:length(fnames)
0037     
0038     if(ifld > length(dataStruct.data))
0039         dataVals = [];
0040     else
0041         dataVals = dataStruct.data{ifld};
0042     end
0043     
0044     %dealing with whether a child structure is embedded in a cell or not
0045     if(iscell(dataVals) && (length(dataVals) >= 1) && isstruct(dataVals{1}))
0046         for idx = 1:length(dataVals)
0047             if(isfield(dataVals{idx},'vars'))
0048                 %perform ensemble_datastruct2tree if this is an ensemble
0049                 %data struct
0050                 dataVals{idx} = ensemble_datastruct2tree(dataVals{idx});
0051             end
0052         end
0053     end
0054     
0055     %deal with struct not in a cell
0056     if(isstruct(dataVals))
0057         clear convertedData;
0058         for idx = 1:length(dataVals)
0059             if(isfield(dataVals(idx),'vars')) && ~isempty(dataVals(idx).vars)
0060                 %perform ensemble_datastruct2tree if this is an ensemble
0061                 %data struct
0062                 convertedData(idx) = ensemble_datastruct2tree(dataVals(idx));
0063             else
0064                 convertedData(idx) = dataVals(idx);
0065             end
0066             
0067         end
0068         clear dataVals
0069         dataVals = convertedData;
0070     end
0071     
0072   outtree.(fnames{ifld}) = dataVals;
0073   
0074 end
0075 
0076 %copy ensemble data struct fields (name,type,report,meta)
0077 
0078 % Check for naming conflicts, i.e. if a fieldname matches one of the
0079 % ensemble struct data field names
0080 ensemble_struct_names = {'name','type','report','meta'};
0081 conflict_mask = ismember(ensemble_struct_names, fnames);
0082 conflicts = ensemble_struct_names(conflict_mask);
0083 
0084 if ~isempty(conflicts)
0085     warning('Found %d conflicting field names: %s\n', length(conflicts), cell2str(conflicts,','));
0086     
0087     % Try conflict resolution
0088     for ic = 1:length(conflicts)
0089         currConflict = conflicts{ic};
0090         
0091         % See if we've specified a resolutionMethod
0092         if exist('params','var') && isfield(params, 'conflict') && isfield(params.conflict, currConflict)
0093             resolutionMethod = params.conflict.(currConflict);
0094         else
0095             resolutionMethod = '';
0096         end
0097         
0098         % Report on resolution method and indicate use of default resolution
0099         % otherwise
0100         if ~isempty(resolutionMethod)
0101             fprintf('Resolving conflict for "%s" using method "%s"\n', currConflict, resolutionMethod);
0102         else
0103             fprintf('No resolution method specified for conflict "%s". using "%s"\n', currConflict, defaultResolution);
0104             resolutionMethod = defaultResolution;
0105         end
0106         
0107         % Implement resolution method
0108         switch resolutionMethod
0109             case 'datastruct_rules'
0110                 outtree.(currConflict) = currConflict;
0111             otherwise
0112                 % Leave the field as is
0113         end
0114     end
0115 end
0116 
0117 % Copy all non-conflict fields
0118 for fld = ensemble_struct_names(~conflict_mask)
0119     outtree.(fld{1}) = fld{1};
0120 end
0121 
0122 return

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003