Home > marsbar > @marsy > region.m

region

PURPOSE ^

gets / sets data for region or regions

SYNOPSIS ^

function [rs,r_nos] = region(o, r_nos, new_data, fieldname)

DESCRIPTION ^

 gets / sets data for region or regions 
 FORMAT [rs r_nos] = region(o, r_nos) (set) OR
 FORMAT [rs r_nos]= region(o, r_nos, new_data, fieldname) (get)
 
 Inputs
 o              - marsy object
 r_nos          - region number 
                  or array of region numbers
                  or empty - giving all regions
 new_data       - cell array containing new data to set for region
 fieldname      - optional string, to identify field to be set
                  using data in new_data
   
 Returns
 (get call)
 rs             - cell array of region structures
 (set_call)
 rs             - new marsy object with fields set
   
 r_nos          - region nos (empty now -> all region nos)
 
 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [rs,r_nos] = region(o, r_nos, new_data, fieldname)
0002 % gets / sets data for region or regions
0003 % FORMAT [rs r_nos] = region(o, r_nos) (set) OR
0004 % FORMAT [rs r_nos]= region(o, r_nos, new_data, fieldname) (get)
0005 %
0006 % Inputs
0007 % o              - marsy object
0008 % r_nos          - region number
0009 %                  or array of region numbers
0010 %                  or empty - giving all regions
0011 % new_data       - cell array containing new data to set for region
0012 % fieldname      - optional string, to identify field to be set
0013 %                  using data in new_data
0014 %
0015 % Returns
0016 % (get call)
0017 % rs             - cell array of region structures
0018 % (set_call)
0019 % rs             - new marsy object with fields set
0020 %
0021 % r_nos          - region nos (empty now -> all region nos)
0022 %
0023 % $Id$
0024 
0025 r = n_regions(o);
0026 if nargin < 2
0027   r_nos = [];
0028 end
0029 if isempty(r_nos)
0030   r_nos = 1:r;
0031 end    
0032 if any(r_nos > r)
0033   error('Region numbers too large');
0034 end
0035 
0036 st = y_struct(o);
0037 r_f = isfield(st, 'regions');
0038 y_f = isfield(st, 'Y');
0039 def_r_st = struct('name', '',...
0040           'descrip', '',...
0041           'Y', [],...
0042           'weights', [],...
0043           'info', [],...
0044           'vXYZ', [],...
0045           'mat',  []);
0046 sum_func = sumfunc(o);
0047 r_len = length(r_nos);
0048 
0049 if nargin < 3
0050   % get call
0051   if ~r_len, rs = {}; return, end
0052   for i = 1:r_len
0053     r_st = [];
0054     if r_f
0055       r_st = st.regions{r_nos(i)};
0056     end
0057     r_st = mars_struct('fillafromb', def_r_st, r_st);
0058     if isempty(r_st.Y)
0059       if y_f
0060     r_st.Y = st.Y(:,r_nos(i));
0061       end
0062     end
0063     rs{i} = r_st;
0064   end
0065   return
0066 end 
0067 
0068 % set call
0069 if nargin > 3
0070   % field name specified
0071   if ~ismember(fieldname, fieldnames(def_r_st))
0072     error(['Funny data field passed: ' fieldname]);
0073   end
0074 end
0075 
0076 if ~iscell(new_data), new_data = {new_data}; end
0077 if length(new_data) ~= r_len
0078   error('Different numbers of new data cells and regions');
0079 end
0080 if ~r_len, rs = o; return, end
0081 
0082 N = n_time_points(o);
0083 
0084 % we need to fill regions if they are not already there
0085 st.regions = region(o);
0086 
0087 % flag to tell if we need to resummarize
0088 re_sum_f = 0;
0089 
0090 for i = 1:r_len
0091   r = r_nos(i);
0092   r_st = st.regions{r};
0093   if nargin > 3  % fieldname call
0094     n_st = setfield([], fieldname, new_data{i});
0095   else % structure call
0096     n_st = new_data{i};
0097   end
0098   
0099   % check if Y or weights are being set
0100   % if so, we will have to resummarize
0101   if isfield(n_st, 'Y')
0102     re_sum_f = 1;
0103     if size(n_st.Y, 1) ~= N
0104       error('Incorrect number of time points in data set call');
0105     end
0106   end
0107   if isfield(n_st, 'weights')
0108     if strcmp(sum_func, 'wtmean')
0109       re_sum_f = 1;
0110     end      
0111     if ~isempty(n_st.weights) & size(n_st.weights, 1) ~= N
0112       error('Incorrect number of time points in weight set call');
0113     end
0114   end
0115   st.regions{r} = mars_struct('ffillmerge', st.regions{r}, n_st);
0116 end
0117 
0118 if re_sum_f
0119   st = mars_struct('strip', st, {'Y','Yvar'});
0120   rs = resummarize(y_struct(o, st)); 
0121 else
0122   rs = y_struct(o, st); 
0123 end

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019