[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 | |
---|
[810] | 41 | %======================================================================= |
---|
| 42 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
---|
| 43 | % http://www.legi.grenoble-inp.fr |
---|
| 44 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
| 45 | % |
---|
| 46 | % This file is part of the toolbox UVMAT. |
---|
| 47 | % |
---|
| 48 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 49 | % it under the terms of the GNU General Public License as published |
---|
| 50 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 51 | % or (at your option) any later version. |
---|
| 52 | % |
---|
| 53 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 54 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 55 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 56 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 57 | %======================================================================= |
---|
| 58 | |
---|
[462] | 59 | function ParamOut=merge_proj(Param) |
---|
[29] | 60 | |
---|
[606] | 61 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed |
---|
[592] | 62 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
[606] | 63 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
[632] | 64 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
[606] | 65 | ParamOut.NbSlice='off'; %nbre of slices ('off' by default) |
---|
| 66 | ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
| 67 | ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
| 68 | ParamOut.FieldTransform = 'on';%can use a transform function |
---|
| 69 | ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only) |
---|
| 70 | ParamOut.ProjObject='on';%can use projection object(option 'off'/'on', |
---|
[635] | 71 | ParamOut.Mask='on';%can use mask option (option 'off'/'on', 'off' by default) |
---|
[592] | 72 | ParamOut.OutputDirExt='.mproj';%set the output dir extension |
---|
[605] | 73 | 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] | 74 | %check the input files |
---|
| 75 | first_j=[]; |
---|
| 76 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
| 77 | PairString=''; |
---|
| 78 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
| 79 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
| 80 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
| 81 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
| 82 | if ~exist(FirstFileName,'file') |
---|
| 83 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
[605] | 84 | elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject') |
---|
[635] | 85 | msgbox_uvmat('WARNING','You may need a projection object of type plane for merge_proj') |
---|
[605] | 86 | end |
---|
[606] | 87 | return |
---|
[29] | 88 | end |
---|
| 89 | |
---|
[457] | 90 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[632] | 91 | ParamOut=[]; %default output |
---|
[592] | 92 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
| 93 | checkrun=1; |
---|
[457] | 94 | if ischar(Param) |
---|
[592] | 95 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
| 96 | checkrun=0; |
---|
[374] | 97 | end |
---|
[624] | 98 | hseries=findobj(allchild(0),'Tag','series'); |
---|
| 99 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
| 100 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
[592] | 101 | |
---|
[624] | 102 | %% define the directory for result file (with path=RootPath{1}) |
---|
| 103 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files |
---|
| 104 | |
---|
[550] | 105 | if ~isfield(Param,'InputFields') |
---|
| 106 | Param.InputFields.FieldName=''; |
---|
| 107 | end |
---|
[457] | 108 | |
---|
[577] | 109 | %% root input file type |
---|
[457] | 110 | RootPath=Param.InputTable(:,1); |
---|
| 111 | RootFile=Param.InputTable(:,3); |
---|
| 112 | SubDir=Param.InputTable(:,2); |
---|
| 113 | NomType=Param.InputTable(:,4); |
---|
| 114 | FileExt=Param.InputTable(:,5); |
---|
[374] | 115 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
[474] | 116 | %%%%%%%%%%%% |
---|
| 117 | % The cell array filecell is the list of input file names, while |
---|
| 118 | % filecell{iview,fileindex}: |
---|
[457] | 119 | % iview: line in the table corresponding to a given file series |
---|
| 120 | % fileindex: file index within the file series, |
---|
| 121 | % 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 |
---|
| 122 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
[474] | 123 | %%%%%%%%%%%% |
---|
[606] | 124 | % NbSlice=1;%default |
---|
| 125 | % if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
| 126 | % NbSlice=Param.IndexRange.NbSlice; |
---|
| 127 | % end |
---|
| 128 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
| 129 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
| 130 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
| 131 | NbField=NbField_j*NbField_i; %total number of fields |
---|
[457] | 132 | |
---|
[673] | 133 | %% determine the file type on each line from the first input file |
---|
[462] | 134 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
| 135 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
[606] | 136 | for iview=1:NbView |
---|
[462] | 137 | if ~exist(filecell{iview,1}','file') |
---|
[668] | 138 | disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun) |
---|
[462] | 139 | return |
---|
| 140 | end |
---|
[784] | 141 | [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1}); |
---|
[783] | 142 | FileType{iview}=FileInfo{iview}.FileType; |
---|
[462] | 143 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
[880] | 144 | if CheckImage{iview} |
---|
| 145 | ParamIn{iview}=MovieObject{iview}; |
---|
| 146 | else |
---|
| 147 | ParamIn{iview}=Param.InputFields; |
---|
| 148 | end |
---|
[462] | 149 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
| 150 | if ~isempty(j1_series{iview}) |
---|
| 151 | frame_index{iview}=j1_series{iview}; |
---|
| 152 | else |
---|
| 153 | frame_index{iview}=i1_series{iview}; |
---|
| 154 | end |
---|
[457] | 155 | end |
---|
[739] | 156 | if NbView >1 && max(cell2mat(CheckImage))>0 && ~isfield(Param,'ProjObject') |
---|
| 157 | 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) |
---|
| 158 | return |
---|
| 159 | end |
---|
[457] | 160 | |
---|
| 161 | %% calibration data and timing: read the ImaDoc files |
---|
[470] | 162 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
| 163 | if size(time,1)>1 |
---|
[457] | 164 | diff_time=max(max(diff(time))); |
---|
[474] | 165 | if diff_time>0 |
---|
[676] | 166 | disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun) |
---|
[457] | 167 | end |
---|
| 168 | end |
---|
[676] | 169 | if ~isempty(errormsg) |
---|
[829] | 170 | disp_uvmat('WARNING',errormsg,checkrun) |
---|
[676] | 171 | end |
---|
| 172 | time=mean(time,1); %averaged time taken for the merged field |
---|
[457] | 173 | |
---|
[409] | 174 | %% coordinate transform or other user defined transform |
---|
[606] | 175 | transform_fct='';%default fct handle |
---|
[478] | 176 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
[606] | 177 | currentdir=pwd; |
---|
| 178 | cd(Param.FieldTransform.TransformPath) |
---|
| 179 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
| 180 | cd (currentdir) |
---|
[883] | 181 | if isfield(Param,'TransformInput') |
---|
| 182 | for iview=1:NbView |
---|
| 183 | XmlData{iview}.TransformInput=Param.TransformInput; |
---|
| 184 | end |
---|
| 185 | end |
---|
[374] | 186 | end |
---|
[457] | 187 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
| 188 | % EDIT FROM HERE |
---|
[116] | 189 | |
---|
[457] | 190 | %% check the validity of input file types |
---|
[606] | 191 | for iview=1:NbView |
---|
[673] | 192 | if ~isequal(CheckImage{iview},1)&&~isequal(CheckNc{iview},1) |
---|
[668] | 193 | disp_uvmat('ERROR','input set of input series: need either netcdf either image series',checkrun) |
---|
[632] | 194 | return |
---|
[464] | 195 | end |
---|
[457] | 196 | end |
---|
[673] | 197 | |
---|
| 198 | %% output file type |
---|
| 199 | if min(cell2mat(CheckImage))==1 && (~Param.CheckObject || strcmp(Param.ProjObject.Type,'plane')) |
---|
| 200 | FileExtOut='.png'; %image output (input and proj result = image) |
---|
| 201 | for iview=1:NbView |
---|
| 202 | BitDepth(iview)=FileInfo{iview}.BitDepth; |
---|
| 203 | end |
---|
| 204 | BitDepth=max(BitDepth); |
---|
| 205 | else |
---|
| 206 | FileExtOut='.nc'; %netcdf output |
---|
| 207 | end |
---|
[880] | 208 | if isempty(j1_series{1}) |
---|
| 209 | NomTypeOut='_1'; |
---|
| 210 | else |
---|
| 211 | NomTypeOut='_1_1'; |
---|
| 212 | end |
---|
[867] | 213 | %NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series |
---|
[673] | 214 | RootFileOut=RootFile{1}; |
---|
| 215 | for iview=2:NbView |
---|
| 216 | if ~strcmp(RootFile{iview},RootFile{1}) |
---|
| 217 | RootFileOut='mproj'; |
---|
| 218 | break |
---|
| 219 | end |
---|
| 220 | end |
---|
[457] | 221 | |
---|
[642] | 222 | %% mask (TODO: case of multilevels) |
---|
[668] | 223 | MaskData=cell(NbView,1); |
---|
[642] | 224 | if Param.CheckMask |
---|
[676] | 225 | if ischar(Param.MaskTable)% case of a single mask (char chain) |
---|
| 226 | Param.MaskTable={Param.MaskTable}; |
---|
| 227 | end |
---|
[668] | 228 | for iview=1:numel(Param.MaskTable) |
---|
[673] | 229 | if exist(Param.MaskTable{iview},'file') |
---|
| 230 | [MaskData{iview},tild,errormsg] = read_field(Param.MaskTable{iview},'image'); |
---|
| 231 | if ~isempty(transform_fct) && nargin(transform_fct)>=2 |
---|
| 232 | MaskData{iview}=transform_fct(MaskData{iview},XmlData{iview}); |
---|
| 233 | end |
---|
[642] | 234 | end |
---|
| 235 | end |
---|
| 236 | end |
---|
| 237 | |
---|
[457] | 238 | %% Set field names and velocity types |
---|
[462] | 239 | %use Param.InputFields for all views |
---|
[457] | 240 | |
---|
[640] | 241 | %% MAIN LOOP ON FIELDS |
---|
[457] | 242 | %%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[606] | 243 | % for i_slice=1:NbSlice |
---|
| 244 | % index_slice=i_slice:NbSlice:NbField;% select file indices of the slice |
---|
| 245 | % NbFiles=0; |
---|
| 246 | % nbmissing=0; |
---|
[474] | 247 | |
---|
[457] | 248 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
[880] | 249 | tstart=tic; %used to record the computing time |
---|
[606] | 250 | for index=1:NbField |
---|
[626] | 251 | update_waitbar(WaitbarHandle,index/NbField) |
---|
[642] | 252 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
[624] | 253 | disp('program stopped by user') |
---|
[606] | 254 | return |
---|
| 255 | end |
---|
| 256 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
| 257 | Data=cell(1,NbView);%initiate the set Data |
---|
[676] | 258 | timeread=zeros(1,NbView); |
---|
[606] | 259 | for iview=1:NbView |
---|
[880] | 260 | %% reading input file(s) |
---|
| 261 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},ParamIn{iview},frame_index{iview}(index)); |
---|
[606] | 262 | if ~isempty(errormsg) |
---|
[746] | 263 | disp_uvmat('ERROR',['ERROR in merge_proj/read_field/' errormsg],checkrun) |
---|
[614] | 264 | return |
---|
[457] | 265 | end |
---|
[857] | 266 | ListVar=Data{iview}.ListVarName; |
---|
| 267 | for ilist=1:numel(ListVar) |
---|
| 268 | Data{iview}.(ListVar{ilist})=double(Data{iview}.(ListVar{ilist}));% transform all fields in double before all operations |
---|
| 269 | end |
---|
[676] | 270 | % get the time defined in the current file if not already defined from the xml file |
---|
| 271 | if ~isempty(time) && isfield(Data{iview},'Time') |
---|
[606] | 272 | timeread(iview)=Data{iview}.Time; |
---|
[595] | 273 | end |
---|
[606] | 274 | if ~isempty(NbSlice_calib) |
---|
| 275 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
| 276 | end |
---|
[642] | 277 | |
---|
[632] | 278 | %% transform the input field (e.g; phys) if requested (no transform involving two input fields) |
---|
[606] | 279 | if ~isempty(transform_fct) |
---|
| 280 | if nargin(transform_fct)>=2 |
---|
| 281 | Data{iview}=transform_fct(Data{iview},XmlData{iview}); |
---|
| 282 | else |
---|
| 283 | Data{iview}=transform_fct(Data{iview}); |
---|
[29] | 284 | end |
---|
[606] | 285 | end |
---|
[632] | 286 | |
---|
[642] | 287 | %% calculate tps coefficients if needed |
---|
[614] | 288 | check_proj_tps= isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps'); |
---|
[606] | 289 | Data{iview}=tps_coeff_field(Data{iview},check_proj_tps); |
---|
[642] | 290 | |
---|
[606] | 291 | %% projection on object (gridded plane) |
---|
| 292 | if Param.CheckObject |
---|
| 293 | [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject); |
---|
| 294 | if ~isempty(errormsg) |
---|
[746] | 295 | disp_uvmat('ERROR',['ERROR in merge_proge/proj_field: ' errormsg],checkrun) |
---|
[606] | 296 | return |
---|
[29] | 297 | end |
---|
[464] | 298 | end |
---|
[640] | 299 | |
---|
| 300 | %% mask |
---|
[642] | 301 | if Param.CheckMask && ~isempty(MaskData{iview}) |
---|
| 302 | [Data{iview},errormsg]=mask_proj(Data{iview},MaskData{iview}); |
---|
[640] | 303 | end |
---|
[606] | 304 | end |
---|
[673] | 305 | %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%% |
---|
[606] | 306 | |
---|
| 307 | %% merge the NbView fields |
---|
[739] | 308 | [MergeData,errormsg]=merge_field(Data); |
---|
| 309 | if ~isempty(errormsg) |
---|
| 310 | disp_uvmat('ERROR',errormsg,checkrun); |
---|
[606] | 311 | return |
---|
| 312 | end |
---|
| 313 | |
---|
[673] | 314 | %% time of the merged field: take the average of the different views |
---|
[676] | 315 | if ~isempty(time) |
---|
| 316 | timeread=time(index); |
---|
| 317 | elseif ~isempty(find(timeread))% time defined from ImaDoc |
---|
| 318 | timeread=mean(timeread(timeread~=0));% take average over times form the files (when defined) |
---|
| 319 | else |
---|
| 320 | timeread=index;% take time=file index |
---|
[606] | 321 | end |
---|
| 322 | |
---|
[673] | 323 | %% generating the name of the merged field |
---|
| 324 | i1=i1_series{1}(index); |
---|
| 325 | if ~isempty(i2_series{end}) |
---|
| 326 | i2=i2_series{end}(index); |
---|
[606] | 327 | else |
---|
| 328 | i2=i1; |
---|
| 329 | end |
---|
| 330 | j1=1; |
---|
| 331 | j2=1; |
---|
[673] | 332 | if ~isempty(j1_series{1}) |
---|
| 333 | j1=j1_series{1}(index); |
---|
| 334 | if ~isempty(j2_series{end}) |
---|
| 335 | j2=j2_series{end}(index); |
---|
[606] | 336 | else |
---|
| 337 | j2=j1; |
---|
[464] | 338 | end |
---|
[606] | 339 | end |
---|
[880] | 340 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFileOut,FileExtOut,NomTypeOut,i1,i2,j1,j2); |
---|
[606] | 341 | |
---|
[673] | 342 | %% recording the merged field |
---|
| 343 | if strcmp(FileExtOut,'.png') %output as image |
---|
| 344 | if BitDepth==8 |
---|
| 345 | imwrite(uint8(MergeData.A),OutputFile,'BitDepth',8) |
---|
[464] | 346 | else |
---|
[673] | 347 | imwrite(uint16(MergeData.A),OutputFile,'BitDepth',16) |
---|
[464] | 348 | end |
---|
[673] | 349 | if index==1 |
---|
| 350 | %write xml calibration file, using the first file |
---|
| 351 | siz=size(MergeData.A); |
---|
| 352 | npy=siz(1); |
---|
| 353 | npx=siz(2); |
---|
[880] | 354 | if isfield(MergeData,'Coord_x') && isfield(MergeData,'Coord_y') |
---|
| 355 | Rangx=MergeData.Coord_x; |
---|
| 356 | Rangy=MergeData.Coord_y; |
---|
[673] | 357 | elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY') |
---|
| 358 | Rangx=[MergeData.AX(1) MergeData.AX(end)]; |
---|
| 359 | Rangy=[MergeData.AY(1) MergeData.AY(end)]; |
---|
| 360 | else |
---|
| 361 | Rangx=[0.5 npx-0.5]; |
---|
| 362 | Rangy=[npy-0.5 0.5];%default |
---|
| 363 | end |
---|
| 364 | pxcmx=(npx-1)/(Rangx(2)-Rangx(1)); |
---|
| 365 | pxcmy=(npy-1)/(Rangy(1)-Rangy(2)); |
---|
| 366 | T_x=-pxcmx*Rangx(1)+0.5; |
---|
| 367 | T_y=-pxcmy*Rangy(2)+0.5; |
---|
| 368 | GeometryCal.CalibrationType='rescale'; |
---|
| 369 | GeometryCal.CoordUnit=MergeData.CoordUnit; |
---|
| 370 | GeometryCal.focal=1; |
---|
| 371 | GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1]; |
---|
| 372 | GeometryCal.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 373 | ImaDoc.GeometryCalib=GeometryCal; |
---|
| 374 | t=struct2xml(ImaDoc); |
---|
| 375 | t=set(t,1,'name','ImaDoc'); |
---|
| 376 | save(t,[fileparts(OutputFile) '.xml']) |
---|
| 377 | end |
---|
| 378 | |
---|
[606] | 379 | else |
---|
[676] | 380 | MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim'}; |
---|
[606] | 381 | MergeData.Conventions='uvmat'; |
---|
| 382 | MergeData.nb_coord=2; |
---|
| 383 | MergeData.nb_dim=2; |
---|
| 384 | dt=[]; |
---|
| 385 | if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt) |
---|
| 386 | dt=Data{1}.dt; |
---|
| 387 | end |
---|
| 388 | for iview =2:numel(Data) |
---|
| 389 | if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt)) |
---|
| 390 | dt=[];%dt not the same for all fields |
---|
[464] | 391 | end |
---|
| 392 | end |
---|
[676] | 393 | if ~isempty(timeread) |
---|
| 394 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'Time'}]; |
---|
| 395 | MergeData.Time=timeread; |
---|
| 396 | end |
---|
| 397 | if ~isempty(dt) |
---|
| 398 | MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'dt'}]; |
---|
[606] | 399 | MergeData.dt=dt; |
---|
[29] | 400 | end |
---|
[606] | 401 | error=struct2nc(OutputFile,MergeData);%save result file |
---|
| 402 | if isempty(error) |
---|
[739] | 403 | disp(['output file ' OutputFile ' written']) |
---|
[606] | 404 | else |
---|
[739] | 405 | disp(error) |
---|
[606] | 406 | end |
---|
[29] | 407 | end |
---|
| 408 | end |
---|
[880] | 409 | disp(['total ellapsed time ' num2str(toc(tstart))]) |
---|
[29] | 410 | |
---|
[222] | 411 | %'merge_field': concatene fields |
---|
| 412 | %------------------------------------------------------------------------ |
---|
[739] | 413 | function [MergeData,errormsg]=merge_field(Data) |
---|
[222] | 414 | %% default output |
---|
[29] | 415 | if isempty(Data)||~iscell(Data) |
---|
| 416 | MergeData=[]; |
---|
| 417 | return |
---|
| 418 | end |
---|
[739] | 419 | errormsg=''; |
---|
[867] | 420 | MergeData=Data{1};% merged field= first field by default, reproduces the global attributes of the first field |
---|
[606] | 421 | NbView=length(Data); |
---|
[867] | 422 | if NbView==1% if there is only one field, just reproduce it in MergeData |
---|
| 423 | return |
---|
[29] | 424 | end |
---|
[222] | 425 | |
---|
[522] | 426 | %% group the variables (fields of 'Data') in cells of variables with the same dimensions |
---|
[668] | 427 | [CellInfo,NbDim,errormsg]=find_field_cells(Data{1}); |
---|
[739] | 428 | if ~isempty(errormsg) |
---|
| 429 | return |
---|
| 430 | end |
---|
[670] | 431 | |
---|
[89] | 432 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
[668] | 433 | for icell=1:length(CellInfo) |
---|
[670] | 434 | if NbDim(icell)~=1 % skip field cells which are of dim 1 |
---|
| 435 | switch CellInfo{icell}.CoordType |
---|
[867] | 436 | case 'scattered' %case of input fields with unstructured coordinates: just concatene data |
---|
[670] | 437 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
| 438 | VarName=Data{1}.ListVarName{ivar}; |
---|
| 439 | for iview=2:NbView |
---|
| 440 | MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)]; |
---|
[89] | 441 | end |
---|
| 442 | end |
---|
[670] | 443 | case 'grid' %case of fields defined on a structured grid |
---|
| 444 | FFName=''; |
---|
[676] | 445 | if isfield(CellInfo{icell},'VarIndex_errorflag') && ~isempty(CellInfo{icell}.VarIndex_errorflag) |
---|
[670] | 446 | FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable |
---|
[867] | 447 | MergeData.ListVarName(CellInfo{icell}.VarIndex_errorflag)=[];%remove error flag variable in MergeData (will use NaN instead) |
---|
| 448 | MergeData.VarDimName(CellInfo{icell}.VarIndex_errorflag)=[]; |
---|
| 449 | MergeData.VarAttribute(CellInfo{icell}.VarIndex_errorflag)=[]; |
---|
[670] | 450 | end |
---|
| 451 | % select good data on each view |
---|
| 452 | for ivar=CellInfo{icell}.VarIndex % indices of the selected variables in the list FieldData.ListVarName |
---|
| 453 | VarName=Data{1}.ListVarName{ivar}; |
---|
| 454 | for iview=1:NbView |
---|
| 455 | if isempty(FFName) |
---|
| 456 | check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else |
---|
| 457 | else |
---|
| 458 | check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else |
---|
| 459 | end |
---|
[867] | 460 | Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag |
---|
[670] | 461 | if iview==1 |
---|
[867] | 462 | %MergeData.(VarName)=Data{1}.(VarName);% initiate MergeData with the first field |
---|
[874] | 463 | MergeData.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag |
---|
[670] | 464 | NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point |
---|
[739] | 465 | elseif size(Data{iview}.(VarName))~=size(MergeData.(VarName)) |
---|
| 466 | errormsg='sizes of the input matrices do not agree, need to interpolate on a common grid using a projection object'; |
---|
| 467 | return |
---|
[867] | 468 | else |
---|
| 469 | MergeData.(VarName)=MergeData.(VarName) +double(Data{iview}.(VarName));%add data |
---|
[670] | 470 | NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else |
---|
| 471 | end |
---|
| 472 | end |
---|
| 473 | MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point |
---|
[867] | 474 | MergeData.(VarName)(NbAver==0)=NaN;% set to NaN the points with no good data |
---|
[670] | 475 | end |
---|
[89] | 476 | end |
---|
[867] | 477 | % if isempty(FFName) |
---|
| 478 | % FFName='FF'; |
---|
| 479 | % end |
---|
| 480 | % MergeData.(FFName)(NbAver~=0)=0;% flag to 1 undefined summed data |
---|
| 481 | % MergeData.(FFName)(NbAver==0)=1;% flag to 1 undefined summed data |
---|
[89] | 482 | end |
---|
| 483 | end |
---|
[222] | 484 | |
---|
[670] | 485 | |
---|
[810] | 486 | |
---|