[573] | 1 | %'merge_proj': concatene several fields from series, can project them on a regular grid in phys coordinates |
---|
[457] | 2 | %------------------------------------------------------------------------ |
---|
[464] | 3 | % function ParamOut=merge_proj(Param) |
---|
[457] | 4 | %------------------------------------------------------------------------ |
---|
| 5 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
[169] | 6 | % |
---|
| 7 | %OUTPUT |
---|
[596] | 8 | % ParamOut: sets options in the GUI series.fig needed for the function |
---|
[169] | 9 | % |
---|
| 10 | %INPUT: |
---|
[457] | 11 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
| 12 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
[596] | 13 | % when Param.Action.RUN=0 (as activated when the current Action is selected |
---|
| 14 | % in series), the function ouput paramOut set the activation of the needed GUI elements |
---|
[169] | 15 | % |
---|
[596] | 16 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
---|
| 17 | % see the current structure Param) |
---|
[457] | 18 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
| 19 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
| 20 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
[474] | 21 | % .OutputDirExt: directory extension for data outputs |
---|
[457] | 22 | % .Action: .ActionName: name of the current activated function |
---|
| 23 | % .ActionPath: path of the current activated function |
---|
[596] | 24 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct |
---|
| 25 | % .RUN =0 for GUI input, =1 for function activation |
---|
| 26 | % .RunMode='local','background', 'cluster': type of function use |
---|
| 27 | % |
---|
[457] | 28 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
| 29 | % .FieldTransform: .TransformName: name of the selected transform function |
---|
| 30 | % .TransformPath: path of the selected transform function |
---|
| 31 | % .InputFields: sub structure describing the input fields withfields |
---|
[596] | 32 | % .FieldName: name(s) of the field |
---|
[457] | 33 | % .VelType: velocity type |
---|
| 34 | % .FieldName_1: name of the second field in case of two input series |
---|
| 35 | % .VelType_1: velocity type of the second field in case of two input series |
---|
[596] | 36 | % .Coord_y: name of y coordinate variable |
---|
| 37 | % .Coord_x: name of x coordinate variable |
---|
[457] | 38 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 40 | |
---|
[462] | 41 | function ParamOut=merge_proj(Param) |
---|
[29] | 42 | |
---|
[606] | 43 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed |
---|
[592] | 44 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
[606] | 45 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
[632] | 46 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
[606] | 47 | ParamOut.NbSlice='off'; %nbre of slices ('off' by default) |
---|
| 48 | ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
| 49 | ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
| 50 | ParamOut.FieldTransform = 'on';%can use a transform function |
---|
| 51 | ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only) |
---|
| 52 | ParamOut.ProjObject='on';%can use projection object(option 'off'/'on', |
---|
[635] | 53 | ParamOut.Mask='on';%can use mask option (option 'off'/'on', 'off' by default) |
---|
[592] | 54 | ParamOut.OutputDirExt='.mproj';%set the output dir extension |
---|
[605] | 55 | ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
---|
[716] | 56 | %check the input files |
---|
| 57 | first_j=[]; |
---|
| 58 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
| 59 | PairString=''; |
---|
| 60 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
| 61 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
| 62 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
| 63 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
| 64 | if ~exist(FirstFileName,'file') |
---|
| 65 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
[605] | 66 | elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject') |
---|
[635] | 67 | msgbox_uvmat('WARNING','You may need a projection object of type plane for merge_proj') |
---|
[605] | 68 | end |
---|
[606] | 69 | return |
---|
[29] | 70 | end |
---|
| 71 | |
---|
[457] | 72 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[632] | 73 | ParamOut=[]; %default output |
---|
[592] | 74 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
| 75 | checkrun=1; |
---|
[457] | 76 | if ischar(Param) |
---|
[592] | 77 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
| 78 | checkrun=0; |
---|
[374] | 79 | end |
---|
[624] | 80 | hseries=findobj(allchild(0),'Tag','series'); |
---|
| 81 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
| 82 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
[592] | 83 | |
---|
[624] | 84 | %% define the directory for result file (with path=RootPath{1}) |
---|
| 85 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files |
---|
| 86 | |
---|
[550] | 87 | if ~isfield(Param,'InputFields') |
---|
| 88 | Param.InputFields.FieldName=''; |
---|
| 89 | end |
---|
[457] | 90 | |
---|
[577] | 91 | %% root input file type |
---|
[457] | 92 | RootPath=Param.InputTable(:,1); |
---|
| 93 | RootFile=Param.InputTable(:,3); |
---|
| 94 | SubDir=Param.InputTable(:,2); |
---|
| 95 | NomType=Param.InputTable(:,4); |
---|
| 96 | FileExt=Param.InputTable(:,5); |
---|
[374] | 97 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
[474] | 98 | %%%%%%%%%%%% |
---|
| 99 | % The cell array filecell is the list of input file names, while |
---|
| 100 | % filecell{iview,fileindex}: |
---|
[457] | 101 | % iview: line in the table corresponding to a given file series |
---|
| 102 | % fileindex: file index within the file series, |
---|
| 103 | % i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j |
---|
| 104 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
[474] | 105 | %%%%%%%%%%%% |
---|
[606] | 106 | % NbSlice=1;%default |
---|
| 107 | % if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
| 108 | % NbSlice=Param.IndexRange.NbSlice; |
---|
| 109 | % end |
---|
| 110 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
| 111 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
| 112 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
| 113 | NbField=NbField_j*NbField_i; %total number of fields |
---|
[457] | 114 | |
---|
[673] | 115 | %% determine the file type on each line from the first input file |
---|
[462] | 116 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
| 117 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
[606] | 118 | for iview=1:NbView |
---|
[462] | 119 | if ~exist(filecell{iview,1}','file') |
---|
[668] | 120 | disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun) |
---|
[462] | 121 | return |
---|
| 122 | end |
---|
[784] | 123 | [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1}); |
---|
[783] | 124 | FileType{iview}=FileInfo{iview}.FileType; |
---|
[462] | 125 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
| 126 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
| 127 | if ~isempty(j1_series{iview}) |
---|
| 128 | frame_index{iview}=j1_series{iview}; |
---|
| 129 | else |
---|
| 130 | frame_index{iview}=i1_series{iview}; |
---|
| 131 | end |
---|
[457] | 132 | end |
---|
[739] | 133 | if NbView >1 && max(cell2mat(CheckImage))>0 && ~isfield(Param,'ProjObject') |
---|
| 134 | disp_uvmat('ERROR','projection on a common grid is needed to concatene images: use a Projection Object of type ''plane'' with ProjMode=''interp_lin''',checkrun) |
---|
| 135 | return |
---|
| 136 | end |
---|
[457] | 137 | |
---|
| 138 | %% calibration data and timing: read the ImaDoc files |
---|
[470] | 139 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
| 140 | if size(time,1)>1 |
---|
[457] | 141 | diff_time=max(max(diff(time))); |
---|
[474] | 142 | if diff_time>0 |
---|
[676] | 143 | disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun) |
---|
[457] | 144 | end |
---|
| 145 | end |
---|
[676] | 146 | if ~isempty(errormsg) |
---|
| 147 | disp_uvmat('WARNING',erromsg,checkrun) |
---|
| 148 | end |
---|
| 149 | time=mean(time,1); %averaged time taken for the merged field |
---|
[457] | 150 | |
---|
[409] | 151 | %% coordinate transform or other user defined transform |
---|
[606] | 152 | transform_fct='';%default fct handle |
---|
[478] | 153 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
[606] | 154 | currentdir=pwd; |
---|
| 155 | cd(Param.FieldTransform.TransformPath) |
---|
| 156 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
| 157 | cd (currentdir) |
---|
[374] | 158 | end |
---|
[457] | 159 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
| 160 | % EDIT FROM HERE |
---|
[116] | 161 | |
---|
[457] | 162 | %% check the validity of input file types |
---|
[606] | 163 | for iview=1:NbView |
---|
[673] | 164 | if ~isequal(CheckImage{iview},1)&&~isequal(CheckNc{iview},1) |
---|
[668] | 165 | disp_uvmat('ERROR','input set of input series: need either netcdf either image series',checkrun) |
---|
[632] | 166 | return |
---|
[464] | 167 | end |
---|
[457] | 168 | end |
---|
[673] | 169 | |
---|
| 170 | %% output file type |
---|
| 171 | if min(cell2mat(CheckImage))==1 && (~Param.CheckObject || strcmp(Param.ProjObject.Type,'plane')) |
---|
| 172 | FileExtOut='.png'; %image output (input and proj result = image) |
---|
| 173 | for iview=1:NbView |
---|
| 174 | BitDepth(iview)=FileInfo{iview}.BitDepth; |
---|
| 175 | end |
---|
| 176 | BitDepth=max(BitDepth); |
---|
| 177 | else |
---|
| 178 | FileExtOut='.nc'; %netcdf output |
---|
| 179 | end |
---|
[462] | 180 | NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series |
---|
[673] | 181 | RootFileOut=RootFile{1}; |
---|
| 182 | for iview=2:NbView |
---|
| 183 | if ~strcmp(RootFile{iview},RootFile{1}) |
---|
| 184 | RootFileOut='mproj'; |
---|
| 185 | break |
---|
| 186 | end |
---|
| 187 | end |
---|
[457] | 188 | |
---|
[642] | 189 | %% mask (TODO: case of multilevels) |
---|
[668] | 190 | MaskData=cell(NbView,1); |
---|
[642] | 191 | if Param.CheckMask |
---|
[676] | 192 | if ischar(Param.MaskTable)% case of a single mask (char chain) |
---|
| 193 | Param.MaskTable={Param.MaskTable}; |
---|
| 194 | end |
---|
[668] | 195 | for iview=1:numel(Param.MaskTable) |
---|
[673] | 196 | if exist(Param.MaskTable{iview},'file') |
---|
| 197 | [MaskData{iview},tild,errormsg] = read_field(Param.MaskTable{iview},'image'); |
---|
| 198 | if ~isempty(transform_fct) && nargin(transform_fct)>=2 |
---|
| 199 | MaskData{iview}=transform_fct(MaskData{iview},XmlData{iview}); |
---|
| 200 | end |
---|
[642] | 201 | end |
---|
| 202 | end |
---|
| 203 | end |
---|
| 204 | |
---|
[457] | 205 | %% Set field names and velocity types |
---|
[462] | 206 | %use Param.InputFields for all views |
---|
[457] | 207 | |
---|
[640] | 208 | %% MAIN LOOP ON FIELDS |
---|
[457] | 209 | %%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[606] | 210 | % for i_slice=1:NbSlice |
---|
| 211 | % index_slice=i_slice:NbSlice:NbField;% select file indices of the slice |
---|
| 212 | % NbFiles=0; |
---|
| 213 | % nbmissing=0; |
---|
[474] | 214 | |
---|
[457] | 215 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
[606] | 216 | for index=1:NbField |
---|
[626] | 217 | update_waitbar(WaitbarHandle,index/NbField) |
---|
[642] | 218 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
[624] | 219 | disp('program stopped by user') |
---|
[606] | 220 | return |
---|
| 221 | end |
---|
[673] | 222 | |
---|
[606] | 223 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
| 224 | Data=cell(1,NbView);%initiate the set Data |
---|
[676] | 225 | timeread=zeros(1,NbView); |
---|
[606] | 226 | for iview=1:NbView |
---|
| 227 | %% reading input file(s) |
---|
| 228 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index)); |
---|
| 229 | if ~isempty(errormsg) |
---|
[746] | 230 | disp_uvmat('ERROR',['ERROR in merge_proj/read_field/' errormsg],checkrun) |
---|
[614] | 231 | return |
---|
[457] | 232 | end |
---|
[676] | 233 | % get the time defined in the current file if not already defined from the xml file |
---|
| 234 | if ~isempty(time) && isfield(Data{iview},'Time') |
---|
[606] | 235 | timeread(iview)=Data{iview}.Time; |
---|
[595] | 236 | end |
---|
[606] | 237 | if ~isempty(NbSlice_calib) |
---|
| 238 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
| 239 | end |
---|
[642] | 240 | |
---|
[632] | 241 | %% transform the input field (e.g; phys) if requested (no transform involving two input fields) |
---|
[606] | 242 | if ~isempty(transform_fct) |
---|
| 243 | if nargin(transform_fct)>=2 |
---|
| 244 | Data{iview}=transform_fct(Data{iview},XmlData{iview}); |
---|
| 245 | else |
---|
| 246 | Data{iview}=transform_fct(Data{iview}); |
---|
[29] | 247 | end |
---|
[606] | 248 | end |
---|
[632] | 249 | |
---|
[642] | 250 | %% calculate tps coefficients if needed |
---|
[614] | 251 | check_proj_tps= isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps'); |
---|
[606] | 252 | Data{iview}=tps_coeff_field(Data{iview},check_proj_tps); |
---|
[642] | 253 | |
---|
[606] | 254 | %% projection on object (gridded plane) |
---|
| 255 | if Param.CheckObject |
---|
| 256 | [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject); |
---|
| 257 | if ~isempty(errormsg) |
---|
[746] | 258 | disp_uvmat('ERROR',['ERROR in merge_proge/proj_field: ' errormsg],checkrun) |
---|
[606] | 259 | return |
---|
[29] | 260 | end |
---|
[464] | 261 | end |
---|
[640] | 262 | |
---|
| 263 | %% mask |
---|
[642] | 264 | if Param.CheckMask && ~isempty(MaskData{iview}) |
---|
| 265 | [Data{iview},errormsg]=mask_proj(Data{iview},MaskData{iview}); |
---|
[640] | 266 | end |
---|
[606] | 267 | end |
---|
[673] | 268 | %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%% |
---|
[606] | 269 | |
---|
| 270 | %% merge the NbView fields |
---|
[739] | 271 | [MergeData,errormsg]=merge_field(Data); |
---|
| 272 | if ~isempty(errormsg) |
---|
| 273 | disp_uvmat('ERROR',errormsg,checkrun); |
---|
[606] | 274 | return |
---|
| 275 | end |
---|
| 276 | |
---|
[673] | 277 | %% time of the merged field: take the average of the different views |
---|
[676] | 278 | if ~isempty(time) |
---|
| 279 | timeread=time(index); |
---|
| 280 | elseif ~isempty(find(timeread))% time defined from ImaDoc |
---|
| 281 | timeread=mean(timeread(timeread~=0));% take average over times form the files (when defined) |
---|
| 282 | else |
---|
| 283 | timeread=index;% take time=file index |
---|
[606] | 284 | end |
---|
| 285 | |
---|
[673] | 286 | %% generating the name of the merged field |
---|
| 287 | i1=i1_series{1}(index); |
---|
| 288 | if ~isempty(i2_series{end}) |
---|
| 289 | i2=i2_series{end}(index); |
---|
[606] | 290 | else |
---|
| 291 | i2=i1; |
---|
| 292 | end |
---|
| 293 | j1=1; |
---|
| 294 | j2=1; |
---|
[673] | 295 | if ~isempty(j1_series{1}) |
---|
| 296 | j1=j1_series{1}(index); |
---|
| 297 | if ~isempty(j2_series{end}) |
---|
| 298 | j2=j2_series{end}(index); |
---|
[606] | 299 | else |
---|
| 300 | j2=j1; |
---|
[464] | 301 | end |
---|
[606] | 302 | end |
---|
[673] | 303 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFileOut,FileExtOut,NomType{1},i1,i2,j1,j2); |
---|
[606] | 304 | |
---|
[673] | 305 | %% recording the merged field |
---|
| 306 | if strcmp(FileExtOut,'.png') %output as image |
---|
| 307 | if BitDepth==8 |
---|
| 308 | imwrite(uint8(MergeData.A),OutputFile,'BitDepth',8) |
---|
[464] | 309 | else |
---|
[673] | 310 | imwrite(uint16(MergeData.A),OutputFile,'BitDepth',16) |
---|
[464] | 311 | end |
---|
[673] | 312 | if index==1 |
---|
| 313 | %write xml calibration file, using the first file |
---|
| 314 | siz=size(MergeData.A); |
---|
| 315 | npy=siz(1); |
---|
| 316 | npx=siz(2); |
---|
| 317 | if isfield(MergeData,'coord_x') && isfield(MergeData,'coord_y') |
---|
| 318 | Rangx=MergeData.coord_x; |
---|
| 319 | Rangy=MergeData.coord_y; |
---|
| 320 | elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY') |
---|
| 321 | Rangx=[MergeData.AX(1) MergeData.AX(end)]; |
---|
| 322 | Rangy=[MergeData.AY(1) MergeData.AY(end)]; |
---|
| 323 | else |
---|
| 324 | Rangx=[0.5 npx-0.5]; |
---|
| 325 | Rangy=[npy-0.5 0.5];%default |
---|
| 326 | end |
---|
| 327 | pxcmx=(npx-1)/(Rangx(2)-Rangx(1)); |
---|
| 328 | pxcmy=(npy-1)/(Rangy(1)-Rangy(2)); |
---|
| 329 | T_x=-pxcmx*Rangx(1)+0.5; |
---|
| 330 | T_y=-pxcmy*Rangy(2)+0.5; |
---|
| 331 | GeometryCal.CalibrationType='rescale'; |
---|
| 332 | GeometryCal.CoordUnit=MergeData.CoordUnit; |
---|
| 333 | GeometryCal.focal=1; |
---|
| 334 | GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1]; |
---|
| 335 | GeometryCal.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 336 | ImaDoc.GeometryCalib=GeometryCal; |
---|
| 337 | t=struct2xml(ImaDoc); |
---|
| 338 | t=set(t,1,'name','ImaDoc'); |
---|
| 339 | save(t,[fileparts(OutputFile) '.xml']) |
---|
| 340 | end |
---|
| 341 | |
---|
[606] | 342 | else |
---|
[676] | 343 | MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim'}; |
---|
[606] | 344 | MergeData.Conventions='uvmat'; |
---|
| 345 | MergeData.nb_coord=2; |
---|
| 346 | MergeData.nb_dim=2; |
---|
| 347 | dt=[]; |
---|
| 348 | if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt) |
---|
| 349 | dt=Data{1}.dt; |
---|
| 350 | end |
---|
| 351 | for iview =2:numel(Data) |
---|
| 352 | if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt)) |
---|
| 353 | dt=[];%dt not the same for all fields |
---|
[464] | 354 | end |
---|
| 355 | end |
---|
[676] | 356 | if ~isempty(timeread) |
---|
| 357 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'Time'}]; |
---|
| 358 | MergeData.Time=timeread; |
---|
| 359 | end |
---|
| 360 | if ~isempty(dt) |
---|
| 361 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'dt'}]; |
---|
[606] | 362 | MergeData.dt=dt; |
---|
[29] | 363 | end |
---|
[606] | 364 | error=struct2nc(OutputFile,MergeData);%save result file |
---|
| 365 | if isempty(error) |
---|
[739] | 366 | disp(['output file ' OutputFile ' written']) |
---|
[606] | 367 | else |
---|
[739] | 368 | disp(error) |
---|
[606] | 369 | end |
---|
[29] | 370 | end |
---|
| 371 | end |
---|
| 372 | |
---|
[606] | 373 | |
---|
[222] | 374 | %'merge_field': concatene fields |
---|
| 375 | %------------------------------------------------------------------------ |
---|
[739] | 376 | function [MergeData,errormsg]=merge_field(Data) |
---|
[222] | 377 | %% default output |
---|
[29] | 378 | if isempty(Data)||~iscell(Data) |
---|
| 379 | MergeData=[]; |
---|
| 380 | return |
---|
| 381 | end |
---|
[739] | 382 | errormsg=''; |
---|
[673] | 383 | MergeData=Data{1};% merged field= first field by default, reproduces the glabal attributes of the first field |
---|
[606] | 384 | NbView=length(Data); |
---|
| 385 | if NbView==1 |
---|
[29] | 386 | return |
---|
| 387 | end |
---|
[222] | 388 | |
---|
[522] | 389 | %% group the variables (fields of 'Data') in cells of variables with the same dimensions |
---|
[668] | 390 | [CellInfo,NbDim,errormsg]=find_field_cells(Data{1}); |
---|
[739] | 391 | if ~isempty(errormsg) |
---|
| 392 | return |
---|
| 393 | end |
---|
[670] | 394 | |
---|
[89] | 395 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
[668] | 396 | for icell=1:length(CellInfo) |
---|
[670] | 397 | if NbDim(icell)~=1 % skip field cells which are of dim 1 |
---|
| 398 | switch CellInfo{icell}.CoordType |
---|
| 399 | case 'scattered' %case of input fields with unstructured coordinates: just concacene data |
---|
| 400 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
| 401 | VarName=Data{1}.ListVarName{ivar}; |
---|
| 402 | %MergeData=Data{1};% merged field= first field by default, reproduces the glabal attributes of the first field |
---|
| 403 | for iview=2:NbView |
---|
| 404 | MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)]; |
---|
[89] | 405 | end |
---|
| 406 | end |
---|
[670] | 407 | case 'grid' %case of fields defined on a structured grid |
---|
| 408 | FFName=''; |
---|
[676] | 409 | if isfield(CellInfo{icell},'VarIndex_errorflag') && ~isempty(CellInfo{icell}.VarIndex_errorflag) |
---|
[670] | 410 | FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable |
---|
| 411 | end |
---|
| 412 | % select good data on each view |
---|
| 413 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
| 414 | VarName=Data{1}.ListVarName{ivar}; |
---|
| 415 | for iview=1:NbView |
---|
| 416 | if isempty(FFName) |
---|
| 417 | check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else |
---|
| 418 | else |
---|
| 419 | check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else |
---|
| 420 | end |
---|
| 421 | Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or masked data |
---|
| 422 | if iview==1 |
---|
| 423 | MergeData.(VarName)=Data{1}.(VarName);% correct the field of MergeData |
---|
| 424 | NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point |
---|
[739] | 425 | elseif size(Data{iview}.(VarName))~=size(MergeData.(VarName)) |
---|
| 426 | errormsg='sizes of the input matrices do not agree, need to interpolate on a common grid using a projection object'; |
---|
| 427 | return |
---|
| 428 | else |
---|
[670] | 429 | MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);%add data |
---|
| 430 | NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else |
---|
| 431 | end |
---|
| 432 | end |
---|
| 433 | MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point |
---|
| 434 | end |
---|
[89] | 435 | end |
---|
[670] | 436 | if isempty(FFName) |
---|
| 437 | FFName='FF'; |
---|
[89] | 438 | end |
---|
[670] | 439 | MergeData.(FFName)(NbAver~=0)=0;% flag to 1 undefined summed data |
---|
| 440 | MergeData.(FFName)(NbAver==0)=1;% flag to 1 undefined summed data |
---|
[89] | 441 | end |
---|
| 442 | end |
---|
[222] | 443 | |
---|
[670] | 444 | |
---|
[89] | 445 | |
---|