0001 function an_st = ensemble_data_by_question(data_st,params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 an_st = ensemble_init_data_struct;
0022 an_st.type = 'data_by_compqid';
0023
0024
0025 incol = set_var_col_const(data_st.vars);
0026
0027
0028 if ~isfield(incol,'compqid') && ~all(isfield(incol,{'question_id','subquestion'}))
0029 fprintf(['Did not find necessary question and subquestion or composite question ID' ...
0030 ' information in the input data']);
0031 return
0032 end
0033
0034
0035 param_fld_names = {'ensemble','mysql'};
0036 idxs = find(isfield(params,param_fld_names));
0037 nidx = length(idxs);
0038 if isempty(idxs)
0039 error('%s: Do not have sufficient database connection information', mfilename)
0040 else
0041 for iidx = 1:nidx
0042 if isfield(params.(param_fld_names{idxs(iidx)}), 'database')
0043 conn_id = params.(param_fld_names{idxs(iidx)}).conn_id;
0044 continue
0045 end
0046 end
0047 end
0048
0049
0050 if isfield(params,'filt')
0051 fprintf('Applying filtering criteria\n')
0052 data_st = ensemble_filter(data_st, params.filt);
0053 end
0054
0055
0056
0057
0058
0059 if ~isfield(incol,'compqid')
0060 data_st.vars{end+1} = 'compqid';
0061 data_st.data{end+1} = make_compqid(data_st.data{incol.question_id}, ...
0062 data_st.data{incol.subquestion});
0063 incol = set_var_col_const(data_st.vars);
0064 end
0065
0066
0067
0068 unique_src_compqid = unique(data_st.data{incol.compqid});
0069 if isfield(params,'compqids') && ~isempty(params.compqids)
0070 if ~(all(ismember(unique_src_compqid,params.compqids)))
0071 filt = [];
0072 filt.include.all.compqid = params.compqids;
0073 data_st = ensemble_filter(data_st,filt);
0074 end
0075 compqids = params.compqids;
0076 else
0077 compqids = unique(data_st.data{incol.compqid});
0078 end
0079
0080
0081 nqid = length(compqids);
0082
0083
0084 qinfo = mysql_extract_metadata('table','question', ...
0085 'question_id',unique(fix(compqids)),'conn_id',conn_id);
0086 qinfo = qinfo(ismember([qinfo.compqid],compqids));
0087 an_st.meta.question = qinfo;
0088
0089
0090 if isfield(params,'extract_vars') && ~isempty(params.extract_vars)
0091 extract_vars = params.extract_vars;
0092 else
0093 extract_vars = data_st.vars;
0094 end
0095
0096
0097 [~,src_col] = ismember(extract_vars, data_st.vars);
0098
0099 for iqid = 1:nqid
0100 newVar = sprintf('s%d_%02d',qinfo(iqid).question_id,qinfo(iqid).subquestion);
0101
0102
0103
0104 if isfield(params,'var_name_map') && isfield(params.var_name_map,newVar)
0105 newVar = params.var_name_map.(newVar);
0106 end
0107 an_st.vars{iqid} = newVar;
0108
0109
0110 curr_st = ensemble_init_data_struct;
0111 curr_st.type = 'raw_data';
0112 curr_st.vars = extract_vars;
0113 curr_st_cols = set_var_col_const(curr_st.vars);
0114
0115
0116 clear filt
0117 filt.include.all.compqid = qinfo(iqid).compqid;
0118 tmpdata = ensemble_filter(data_st,filt);
0119
0120
0121 curr_st.data = tmpdata.data(src_col);
0122
0123 an_st.data{iqid} = curr_st;
0124 end
0125
0126 end