[29] | 1 | function GUI_input=merge_proj(num_i1,num_i2,num_j1,num_j2,Series); |
---|
| 2 | |
---|
| 3 | %requests for the visibility of input windows in the GUI series (activated directly by the selection in the menu ACTION) |
---|
| 4 | if ~exist('num_i1','var') |
---|
| 5 | GUI_input={'RootPath';'two';...%nbre of possible input series (options 'on'/'two'/'many', default:'one') |
---|
| 6 | 'SubDir';'on';... % subdirectory of derived files (PIV fields), ('on' by default) |
---|
| 7 | 'RootFile';'on';... %root input file name ('on' by default) |
---|
| 8 | 'FileExt';'on';... %input file extension ('on' by default) |
---|
| 9 | 'NomType';'on';...%type of file indexing ('on' by default) |
---|
| 10 | 'NbSlice';'on'; ...%nbre of slices ('off' by default) |
---|
| 11 | 'VelTypeMenu';'one';...% menu for selecting the velocity type (civ1,..) options 'off'/'one'/'two', 'off' by default) |
---|
| 12 | 'FieldMenu';'one';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
| 13 | 'CoordType';'on';...%can use a transform function 'off' by default |
---|
| 14 | 'GetObject';'on';...%can use projection object ,'off' by default |
---|
| 15 | %'GetMask';'on'...%can use mask option ,'off' by default |
---|
| 16 | %'PARAMETER'; options: name of the user defined parameter',repeat a line for each parameter |
---|
| 17 | ''}; |
---|
| 18 | return %exit the function |
---|
| 19 | end |
---|
| 20 | |
---|
| 21 | %------------------------------------------------- |
---|
| 22 | hseries=guidata(Series.hseries);%handles of the GUI series |
---|
| 23 | WaitbarPos=get(hseries.waitbar_frame,'Position'); %positiopn of waitbar frame |
---|
| 24 | %------------------------------------------------- |
---|
| 25 | |
---|
[41] | 26 | %projection object |
---|
| 27 | test_object=get(hseries.GetObject,'Value'); |
---|
| 28 | if test_object |
---|
[76] | 29 | hset_object=findobj(allchild(0),'tag','set_object'); |
---|
[41] | 30 | ProjObject=read_set_object(guidata(hset_object)); |
---|
| 31 | if ~isfield(ProjObject,'Style') |
---|
| 32 | msgbox_uvmat('ERROR','Undefined projection object style') |
---|
| 33 | return |
---|
| 34 | end |
---|
| 35 | if ~isequal(ProjObject.Style,'plane') |
---|
| 36 | msgbox_uvmat('ERROR','The projection object must be a plane') |
---|
| 37 | return |
---|
| 38 | end |
---|
| 39 | %answeryes=questdlg({['field series projected on ' Series.ProjObject.Style]}); |
---|
| 40 | answeryes=msgbox_uvmat('INPUT_Y-N',['field series projected on ' ProjObject.Style]); |
---|
| 41 | if ~isequal(answeryes,'Yes') |
---|
| 42 | return |
---|
| 43 | end |
---|
| 44 | end |
---|
| 45 | |
---|
[29] | 46 | %numbers of view fields (nbre of inputs in RootPath) |
---|
| 47 | testcell=iscell(Series.RootFile); |
---|
| 48 | if ~testcell |
---|
| 49 | Series.RootPath={Series.RootPath}; |
---|
| 50 | Series.RootFile={Series.RootFile}; |
---|
| 51 | Series.SubDir={Series.SubDir}; |
---|
| 52 | Series.FileExt={Series.FileExt}; |
---|
| 53 | Series.NomType={Series.NomType}; |
---|
| 54 | num_i1={num_i1}; |
---|
| 55 | num_i2={num_i2}; |
---|
| 56 | num_j1={num_j1}; |
---|
| 57 | num_j2={num_j2}; |
---|
| 58 | end |
---|
| 59 | nbview=length(Series.RootFile);%number of views (file series to merge) |
---|
| 60 | nbfield=size(num_i1{1},1)*size(num_i1{1},2);%number of fields in the time series |
---|
| 61 | transform=Series.CoordType; % field transform function |
---|
| 62 | hhh=which('mmreader'); |
---|
| 63 | for iview=1:nbview |
---|
| 64 | test_movie(iview)=0; |
---|
| 65 | if ~isequal(hhh,'')&& mmreader.isPlatformSupported() |
---|
| 66 | if isequal(lower(FileExt{iview}),'.avi') |
---|
| 67 | MovieObject{iview}=mmreader(fullfile(RootPath{iview},[RootFile{iview} FileExt{iview}])); |
---|
| 68 | test_movie(iview)=1; |
---|
| 69 | end |
---|
| 70 | end |
---|
| 71 | end |
---|
| 72 | |
---|
| 73 | %Calibration data and timing: read the ImaDoc files |
---|
| 74 | mode=''; %default |
---|
| 75 | timecell={}; |
---|
| 76 | itime=0; |
---|
| 77 | NbSlice_calib={}; %test for z index |
---|
| 78 | for iview=1:nbview%Loop on views |
---|
| 79 | XmlData{iview}=[];%default |
---|
| 80 | filebase{iview}=fullfile(Series.RootPath{iview},Series.RootFile{iview}); |
---|
| 81 | if exist([filebase{iview} '.xml'],'file') |
---|
| 82 | [XmlData{iview},error]=imadoc2struct([filebase{iview} '.xml']); |
---|
| 83 | if isfield(XmlData{iview},'Time') |
---|
| 84 | itime=itime+1; |
---|
| 85 | timecell{itime}=XmlData{iview}.Time; |
---|
| 86 | end |
---|
| 87 | if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord') |
---|
| 88 | NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1); |
---|
| 89 | if ~isequal(NbSlice_calib{iview},NbSlice_calib{1}) |
---|
| 90 | msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series'); |
---|
| 91 | end |
---|
| 92 | end |
---|
| 93 | elseif exist([filebase{iview} '.civ'],'file') |
---|
| 94 | [error,time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([filebase{iview} '.civ']); |
---|
| 95 | itime=itime+1; |
---|
| 96 | timecell{itime}=time; |
---|
| 97 | XmlData{iview}.Time=time; |
---|
| 98 | GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0]; |
---|
| 99 | GeometryCalib.Tx=0; |
---|
| 100 | GeometryCalib.Ty=0; |
---|
| 101 | GeometryCalib.Tz=1; |
---|
| 102 | GeometryCalib.dpx=1; |
---|
| 103 | GeometryCalib.dpy=1; |
---|
| 104 | GeometryCalib.sx=1; |
---|
| 105 | GeometryCalib.Cx=0; |
---|
| 106 | GeometryCalib.Cy=0; |
---|
| 107 | GeometryCalib.f=1; |
---|
| 108 | GeometryCalib.kappa1=0; |
---|
| 109 | GeometryCalib.CoordUnit='cm'; |
---|
| 110 | XmlData{iview}.GeometryCalib=GeometryCalib; |
---|
| 111 | if error==1 |
---|
| 112 | msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file'); |
---|
| 113 | end |
---|
| 114 | end |
---|
| 115 | end |
---|
| 116 | |
---|
| 117 | %check coincidence in time |
---|
| 118 | multitime=0; |
---|
| 119 | if length(timecell)==0 |
---|
| 120 | time=[]; |
---|
| 121 | elseif length(timecell)==1 |
---|
| 122 | time=timecell{1}; |
---|
| 123 | elseif length(timecell)>1 |
---|
| 124 | multitime=1; |
---|
| 125 | for icell=1:length(timecell) |
---|
| 126 | if ~isequal(size(timecell{icell}),size(timecell{1})) |
---|
| 127 | msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used') |
---|
| 128 | time=timecell{1}; |
---|
| 129 | multitime=0; |
---|
| 130 | break |
---|
| 131 | end |
---|
| 132 | end |
---|
| 133 | end |
---|
| 134 | if multitime |
---|
| 135 | for icell=1:length(timecell) |
---|
| 136 | time(icell,:,:)=timecell{icell}; |
---|
| 137 | end |
---|
| 138 | diff_time=max(max(diff(time))); |
---|
| 139 | if diff_time>0 |
---|
| 140 | msgbox_uvmat('WARNING',['times of series differ by more than ' num2str(diff_time)]) |
---|
| 141 | end |
---|
| 142 | end |
---|
| 143 | if size(time,2) < num_i2{1}(end) || size(time,3) < num_j2{1}(end)% ime array absent or too short in ImaDoc xml file' |
---|
| 144 | time=[]; |
---|
| 145 | end |
---|
| 146 | |
---|
[41] | 147 | % coordinate transform or other user defined transform |
---|
| 148 | transform_fct=[];%default |
---|
| 149 | if isfield(Series,'transform_fct') |
---|
| 150 | transform_fct=Series.transform_fct; |
---|
| 151 | end |
---|
| 152 | |
---|
[29] | 153 | % Field and velocity type (the same for all views) |
---|
| 154 | Field_str=get(hseries.FieldMenu,'String'); |
---|
| 155 | val=get(hseries.FieldMenu,'Value'); |
---|
| 156 | FieldName=Field_str(val);%the same set of fields for all views |
---|
| 157 | VelType_str=get(hseries.VelTypeMenu,'String'); |
---|
| 158 | VelType_val=get(hseries.VelTypeMenu,'Value'); |
---|
| 159 | VelType=VelType_str{VelType_val}; %the same for all views |
---|
| 160 | if isequal(FieldName,'get_field...') |
---|
| 161 | hget_field=findobj(allchild(0),'Name','get_field');%find the get_field... GUI |
---|
| 162 | % hhget_field=guidata(hget_field);%handles of GUI elements in get_field |
---|
| 163 | SubField=get_field('read_get_field',hObject,eventdata,hget_field); %read the names of the variables to plot in the get_field GUI |
---|
| 164 | % if isequal(get(hhget_field.menu_coord,'Visible'),'on') |
---|
| 165 | % list_transform=get(hhget_field.menu_coord,'String'); |
---|
| 166 | % val_list=get(hhget_field.menu_coord,'Value'); |
---|
| 167 | % transform=list_transform{val_list}; |
---|
| 168 | % end |
---|
| 169 | end |
---|
| 170 | %detect whether all the files are 'images' or 'netcdf' |
---|
| 171 | testima=0; |
---|
| 172 | testvol=0; |
---|
| 173 | testcivx=0; |
---|
| 174 | testnc=0; |
---|
| 175 | FileExt=get(hseries.FileExt,'String'); |
---|
| 176 | for iview=1:nbview |
---|
| 177 | ext=FileExt{iview}; |
---|
| 178 | form=imformats(ext([2:end])); |
---|
| 179 | if isequal(lower(ext),'.vol') |
---|
| 180 | testvol=testvol+1; |
---|
| 181 | elseif ~isempty(form)||isequal(lower(ext),'.avi')% if the extension corresponds to an image format recognized by Matlab |
---|
| 182 | testima=testima+1; |
---|
| 183 | elseif isequal(ext,'.nc') |
---|
| 184 | testnc=testnc+1; |
---|
| 185 | end |
---|
| 186 | end |
---|
| 187 | if testvol |
---|
| 188 | msgbox_uvmat('ERROR','volume images not implemented yet') |
---|
| 189 | return |
---|
| 190 | end |
---|
| 191 | if testnc~=nbview && testima~=nbview && testvol~=nbview |
---|
| 192 | msgbox_uvmat('ERROR','need a set of images or a set of netcdf files with the same fields as input') |
---|
| 193 | return |
---|
| 194 | end |
---|
| 195 | if ~isequal(FieldName,'get_field...') |
---|
| 196 | testcivx=testnc; |
---|
| 197 | end |
---|
[41] | 198 | |
---|
[29] | 199 | %name of output files and directory: |
---|
| 200 | % res_subdir=fullfile(Series.RootPath{1},[Series.SubDir{1} '_STAT']); |
---|
| 201 | ProjectDir=fileparts(fileparts(Series.RootPath{1}));% preoject directory (GERK) |
---|
| 202 | prompt={['result directory (in' ProjectDir ')']}; |
---|
| 203 | RootPath=get(hseries.RootPath,'String'); |
---|
| 204 | SubDir=get(hseries.SubDir,'String'); |
---|
| 205 | if isequal(length(RootPath),1) |
---|
| 206 | fulldir=RootPath{1}; |
---|
[41] | 207 | subdir='merge_proj'; |
---|
[29] | 208 | res_subdir=fullfile(fulldir,subdir); |
---|
| 209 | else |
---|
| 210 | def={fullfile(ProjectDir,'0_RESULTS')}; |
---|
| 211 | dlgTitle='result directory'; |
---|
| 212 | lineNo=1; |
---|
| 213 | answer=msgbox_uvmat('INPUT_TXT',dlgTitle,def); |
---|
| 214 | fulldir=answer{1}; |
---|
| 215 | subdir=[]; |
---|
| 216 | dirlist=sort(Series.RootFile); |
---|
| 217 | for iview=1:nbview |
---|
| 218 | if ~isempty(subdir) |
---|
| 219 | subdir=[subdir '-']; |
---|
| 220 | end |
---|
| 221 | subdir=[subdir dirlist{iview}]; |
---|
| 222 | end |
---|
| 223 | res_subdir=fullfile(fulldir,subdir); |
---|
| 224 | end |
---|
| 225 | ext=FileExt{1}; |
---|
| 226 | if ~exist(fulldir,'dir') |
---|
| 227 | msgbox_uvmat('ERROR',['directory ' fulldir ' needs to be created']) |
---|
| 228 | return |
---|
| 229 | end |
---|
| 230 | if ~exist(res_subdir,'dir') |
---|
| 231 | dircur=pwd; |
---|
| 232 | cd(fulldir) |
---|
| 233 | error=mkdir(subdir); |
---|
| 234 | cd(dircur) |
---|
| 235 | end |
---|
| 236 | filebasesub=fullfile(res_subdir,Series.RootFile{1}); |
---|
| 237 | filebase_merge=fullfile(res_subdir,'merged');%root name for the merged files |
---|
| 238 | |
---|
| 239 | %MAIN LOOP |
---|
| 240 | for ifile=1:nbfield |
---|
| 241 | stopstate=get(hseries.RUN,'BusyAction'); |
---|
| 242 | if isequal(stopstate,'queue')% enable STOP command from the 'series' interface |
---|
| 243 | update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield) |
---|
| 244 | Amerge=0; |
---|
| 245 | |
---|
| 246 | %----------LOOP ON VIEWS---------------------- |
---|
| 247 | nbtime=0; |
---|
| 248 | for iview=1:nbview |
---|
| 249 | %name of the current file |
---|
| 250 | filename=name_generator(filebase{iview},num_i1{iview}(ifile),num_j1{iview}(ifile),Series.FileExt{iview},Series.NomType{iview},1,num_i2{iview}(ifile),num_j2{iview}(ifile),SubDir{iview}); |
---|
| 251 | if ~exist(filename,'file') |
---|
| 252 | msgbox_uvmat('ERROR',['missing input file' filename]) |
---|
| 253 | break |
---|
| 254 | end |
---|
| 255 | |
---|
| 256 | %reading the current file |
---|
| 257 | if testima |
---|
| 258 | if test_movie(iview) |
---|
| 259 | Field{iview}.A=read(MovieObject{iview},num_i1{iview}(ifile)); |
---|
| 260 | else |
---|
[78] | 261 | Field{iview}.A=imread(filename); |
---|
[29] | 262 | end % TODO: introduce ListVarName |
---|
| 263 | npxy=size(Field{iview}.A); |
---|
| 264 | Field{iview}.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
| 265 | Field{iview}.AY=[npxy(1)-0.5 0.5]; |
---|
| 266 | Field{iview}.CoordType='px'; |
---|
| 267 | Field{iview}.AName='image'; |
---|
| 268 | else |
---|
| 269 | if testcivx |
---|
| 270 | [Field{iview},VelTypeOut]=read_civxdata(filename,FieldName,VelType); |
---|
| 271 | else |
---|
| 272 | [Field{iview},var_detect]=nc2struct(filename,SubField.ListVarName); %read the corresponding input data |
---|
| 273 | Field{iview}.VarAttribute=SubField.VarAttribute; |
---|
| 274 | end |
---|
| 275 | if isfield(Field{iview},'Time') |
---|
| 276 | timeread(iview)=Field{iview}.Time; |
---|
| 277 | nbtime=nbtime+1; |
---|
| 278 | end |
---|
| 279 | end |
---|
| 280 | % coord transform |
---|
| 281 | % z index |
---|
| 282 | if ~isempty(NbSlice_calib) |
---|
| 283 | Field{iview}.ZIndex=mod(num_i1{iview}(ifile)-1,NbSlice_calib{1})+1; |
---|
| 284 | end |
---|
[41] | 285 | if ~isempty(transform_fct) |
---|
| 286 | Field{iview}=transform_fct(Field{iview},XmlData{iview});%transform to phys if requested |
---|
[29] | 287 | end |
---|
| 288 | if testcivx |
---|
| 289 | Field{iview}=calc_field(FieldName,Field{iview}); |
---|
| 290 | end |
---|
| 291 | |
---|
| 292 | %projection on object (gridded plane) |
---|
[41] | 293 | if test_object |
---|
| 294 | Field{iview}=proj_field(Field{iview},ProjObject); |
---|
[29] | 295 | end |
---|
| 296 | end |
---|
| 297 | |
---|
| 298 | %----------END LOOP ON VIEWS---------------------- |
---|
| 299 | |
---|
| 300 | %merge the nbview fields |
---|
| 301 | MergeData=merge_field(Field); |
---|
| 302 | if isfield(MergeData,'Txt') |
---|
| 303 | msgbox_uvmat('ERROR',MergeData.Txt) |
---|
| 304 | return |
---|
| 305 | end |
---|
| 306 | |
---|
| 307 | % generating the name of the merged field |
---|
| 308 | mergename=name_generator(filebase_merge,num_i1{iview}(ifile),num_j1{iview}(ifile),Series.FileExt{iview},Series.NomType{iview},1,num_i2{iview}(ifile),num_j2{iview}(ifile)); |
---|
| 309 | |
---|
| 310 | % time: |
---|
| 311 | time_i=0;%default |
---|
| 312 | if isempty(time)% time from ImaDoc prevails |
---|
| 313 | time_i=sum(timeread)/nbtime; |
---|
| 314 | else |
---|
| 315 | time_i=(time(iview,num_i1{iview}(ifile),num_j1{iview}(ifile))+time(iview,num_i2{iview}(ifile),num_j2{iview}(ifile)))/2; |
---|
| 316 | end |
---|
| 317 | |
---|
| 318 | % recording the merged field |
---|
| 319 | if testima %in case of input images an image is produced |
---|
| 320 | if isa(MergeData.A,'uint8') |
---|
| 321 | bitdepth=8; |
---|
| 322 | elseif isa(MergeData.A,'uint16') |
---|
| 323 | bitdepth=16; |
---|
| 324 | end |
---|
| 325 | imwrite(MergeData.A,mergename,'BitDepth',bitdepth); |
---|
| 326 | %write xml calibration file |
---|
| 327 | siz=size(MergeData.A); |
---|
| 328 | npy=siz(1); |
---|
| 329 | npx=siz(2); |
---|
| 330 | if isfield(MergeData,'VarAttribute')&&isfield(MergeData.VarAttribute{1},'Coord_2')&&isfield(MergeData.VarAttribute{1},'Coord_1') |
---|
| 331 | Rangx=MergeData.VarAttribute{1}.Coord_2; |
---|
| 332 | Rangy=MergeData.VarAttribute{1}.Coord_1; |
---|
| 333 | elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY') |
---|
| 334 | Rangx=[MergeData.AX(1) MergeData.AX(end)]; |
---|
| 335 | Rangy=[MergeData.AY(1) MergeData.AY(end)]; |
---|
| 336 | else |
---|
| 337 | Rangx=[0.5 npx-0.5]; |
---|
| 338 | Rangy=[npy-0.5 0.5];%default |
---|
| 339 | end |
---|
| 340 | pxcmx=(npx-1)/(Rangx(2)-Rangx(1)); |
---|
| 341 | pxcmy=(npy-1)/(Rangy(1)-Rangy(2)); |
---|
| 342 | T_x=-pxcmx*Rangx(1)+0.5; |
---|
| 343 | T_y=-pxcmy*Rangy(2)+0.5; |
---|
| 344 | GeometryCal.focal=1; |
---|
| 345 | GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1]; |
---|
| 346 | GeometryCal.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 347 | ImaDoc.GeometryCalib=GeometryCal; |
---|
| 348 | t=struct2xml(ImaDoc); |
---|
| 349 | t=set(t,1,'name','ImaDoc'); |
---|
| 350 | save(t,[filebase_merge '.xml']) |
---|
| 351 | display([filebase_merge '.xml saved']) |
---|
| 352 | else |
---|
| 353 | MergeData.ListGlobalAttribute={'Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'}; |
---|
| 354 | MergeData.nb_coord=2; |
---|
| 355 | MergeData.nb_dim=2; |
---|
| 356 | MergeData.dt=1; |
---|
| 357 | MergeData.Time=time_i; |
---|
| 358 | error=struct2nc(mergename,MergeData); %save result file |
---|
| 359 | if isempty(error) |
---|
| 360 | display(['output file ' mergename ' written']) |
---|
| 361 | else |
---|
| 362 | display(error) |
---|
| 363 | end |
---|
| 364 | end |
---|
| 365 | end |
---|
| 366 | end |
---|
| 367 | |
---|
| 368 | %-------------------------------------------------------------------------- |
---|
| 369 | function MergeData=merge_field(Data) |
---|
| 370 | % initiate Matlab structure for physical field |
---|
| 371 | if isempty(Data)||~iscell(Data) |
---|
| 372 | MergeData=[]; |
---|
| 373 | return |
---|
| 374 | end |
---|
| 375 | MergeData=Data{1};%default |
---|
| 376 | error=0; |
---|
| 377 | nbview=length(Data); |
---|
| 378 | if nbview==1 |
---|
| 379 | return |
---|
| 380 | end |
---|
| 381 | for iview=1:nbview |
---|
| 382 | if ~isequal(MergeData.ListDimName,Data{iview}.ListDimName) |
---|
| 383 | error=1; |
---|
| 384 | end |
---|
| 385 | if ~isequal(MergeData.ListVarName,Data{iview}.ListVarName) |
---|
| 386 | error=1; |
---|
| 387 | end |
---|
| 388 | % if ~isequal(MergeData.VarDimIndex,Data{iview}.VarDimIndex) |
---|
| 389 | % error=1; |
---|
| 390 | % end |
---|
| 391 | end |
---|
| 392 | if error |
---|
| 393 | MergeData.Txt='ERROR: attempt at merging fields of incompatible type'; |
---|
| 394 | return |
---|
| 395 | end |
---|