source: trunk/src/read_field.m @ 1088

Last change on this file since 1088 was 1088, checked in by sommeria, 3 years ago

particle_detect introduced, load mt files

File size: 14.3 KB
Line 
1%'read_field': read the fields from files in different formats (netcdf files, images, video)
2%--------------------------------------------------------------------------
3%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
4%
5% OUTPUT:
6% Field: matlab structure representing the field
7% ParamOut: structure representing parameters:
8%        .FieldName; field name
9%        .VelType
10%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....
11%        .Npx,.Npy: for images, nbre of pixels in x and y
12% errormsg: error message, ='' by default
13%
14%INPUT
15% FileName: name of the input file
16% FileType: type of file, as determined by the function get_file_info.m
17% ParamIn: movie object or Matlab structure of input parameters
18%     .FieldName: name (char string) of the input field (for Civx data)
19%     .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...)
20%     .ColorVar: variable used for vector color
21%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
22%     .TimeDimName: name of the dimension considered as 'time', selected index value then set by input 'num'
23% num: frame number for movies
24%
25% see also read_image.m,read_civxdata.m,read_civdata.m,
26
27%=======================================================================
28% Copyright 2008-2020, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
29%   http://www.legi.grenoble-inp.fr
30%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
31%
32%     This file is part of the toolbox UVMAT.
33%
34%     UVMAT is free software; you can redistribute it and/or modify
35%     it under the terms of the GNU General Public License as published
36%     by the Free Software Foundation; either version 2 of the license,
37%     or (at your option) any later version.
38%
39%     UVMAT is distributed in the hope that it will be useful,
40%     but WITHOUT ANY WARRANTY; without even the implied warranty of
41%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42%     GNU General Public License (see LICENSE.txt) for more details.
43%=======================================================================
44
45function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
46%% default output and check input
47Field=[];
48if ~exist('num','var')
49    num=1;
50end
51if isempty(num)
52    num=1;
53end
54if ~exist('ParamIn','var')
55    ParamIn=[];
56end
57ParamOut=ParamIn;%default
58errormsg='';
59if isempty(regexp(FileName,'^http://'))&& ~exist(FileName,'file')
60    errormsg=['input file ' FileName ' does not exist'];
61    return
62end
63A=[];
64InputField={};
65check_colorvar=0;
66if isstruct(ParamIn)
67    if isfield(ParamIn,'FieldName')
68        if ischar(ParamIn.FieldName)
69            InputField={ParamIn.FieldName};
70        else
71            InputField= ParamIn.FieldName;
72        end
73    end
74    check_colorvar=zeros(size(InputField));
75    if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar)
76        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
77        check_colorvar(numel(InputField))=1;
78    end
79end
80
81%% distingush different input file types
82switch FileType
83    case 'civdata'% new format for civ results
84        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
85        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
86        ParamOut.CivStage=Field.CivStage;
87    case 'pivdata_fluidimage'
88        [Field,ParamOut.VelType,errormsg]=read_pivdata_fluidimage(FileName,InputField,ParamIn.VelType);
89        ParamOut.CivStage=Field.CivStage;
90    case 'civx'% old (obsolete) format for civ results
91        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
92        [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
93        if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end
94        ParamOut.CivStage=Field.CivStage;
95    case 'netcdf'% general netcdf file (not recognized as civ)
96        ListVarName={};
97        Role={};
98        ProjModeRequest={};
99        %ListInputField={};
100        %checkU=0;
101        %checkV=0;
102        % scan the list InputField
103        Operator=cell(1,numel(InputField));
104        InputVar=cell(1,numel(InputField));
105        for ilist=1:numel(InputField)
106            % look for input variables to read
107            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
108            if isempty(r)%  no operator used
109                ListVarName=[ListVarName InputField(ilist)];%append the variable name
110                %InputVar{ilist}=InputField(ilist);
111                %ListInputField=[ListInputField InputField(ilist)];
112                if check_colorvar(ilist)% case of field used for vector color
113                    Role{numel(ListVarName)}='ancillary';% not projected with interpolation
114                    ProjModeRequest{numel(ListVarName)}='';
115                else
116                    Role{numel(ListVarName)}='scalar';
117                    ProjModeRequest{numel(ListVarName)}='interp_lin';%scalar field (requires interpolation for plot)
118                end
119                Operator{numel(ListVarName)}='';
120            else  % an operator 'vec' or 'norm' is used
121                ListVarName=[ListVarName {r.UName}]; % append the variable in the list if not previously listed
122                if  strcmp(r.Operator,'norm')
123                    if check_colorvar(ilist)
124                    Role=[Role {'ancillary'}];
125                    else
126                       Role=[Role {'scalar'}];
127                    end
128                else
129                     Role=[Role {'vector_x'}];
130                end
131                %ListInputField=[ListInputField InputField(ilist)];
132                ListVarName=[ListVarName {r.VName}];% append the variable in the list if not previously listed
133                Role=[Role {'vector_y'}];
134                %ListInputField=[ListInputField InputField(ilist)];
135                Operator{numel(ListVarName)-1}=r.Operator;
136                Operator{numel(ListVarName)}='';           
137                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
138                    ProjModeRequest{numel(ListVarName)}='interp_lin';%scalar field (requires interpolation for plot)
139                    ProjModeRequest{numel(ListVarName)-1}='interp_lin';%scalar field (requires interpolation for plot)
140                else
141                    ProjModeRequest{numel(ListVarName)}='';
142                    ProjModeRequest{numel(ListVarName)-1}='';
143                end
144            end
145        end
146        if ~isfield(ParamIn,'Coord_z')
147            ParamIn.Coord_z=[];
148        end
149        NbCoord=~isempty(ParamIn.Coord_x)+~isempty(ParamIn.Coord_y)+~isempty(ParamIn.Coord_z);
150        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
151            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]);
152        elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time  in a multidimensional array
153            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeVarName',ParamIn.TimeVarName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]);
154            if numel(num)~=1
155                NbCoord=NbCoord+1;% adds time coordinate, except if a single time has been selected
156            end
157        else
158            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]);
159        end
160        if ~isempty(errormsg)
161            return
162        end
163        CheckStructured=1;
164        %scan all the variables
165        NbCoord=0;
166        if ~isempty(ParamIn.Coord_x)
167            index_Coord_x=find(strcmp(ParamIn.Coord_x,Field.ListVarName));
168            Field.VarAttribute{index_Coord_x}.Role='coord_x';%
169            NbCoord=NbCoord+1;
170        end
171        if ~isempty(ParamIn.Coord_y)
172            if ischar(ParamIn.Coord_y)
173                index_Coord_y=find(strcmp(ParamIn.Coord_y,Field.ListVarName));
174                Field.VarAttribute{index_Coord_y}.Role='coord_y';%
175                NbCoord=NbCoord+1;
176            else
177                for icoord_y=1:numel(ParamIn.Coord_y)
178                    index_Coord_y=find(strcmp(ParamIn.Coord_y{icoord_y},Field.ListVarName));
179                    Field.VarAttribute{index_Coord_y}.Role='coord_y';%
180                    NbCoord=NbCoord+1;
181                end
182            end
183        end
184        NbDim=1;
185        if ~isempty(ParamIn.Coord_z)
186            index_Coord_z=find(strcmp(ParamIn.Coord_z,Field.ListVarName));
187            Field.VarAttribute{index_Coord_z}.Role='coord_z';%
188            NbCoord=NbCoord+1;
189            NbDim=3;
190        elseif ~isempty(ParamIn.FieldName)
191            NbDim=2;
192        end
193        NormName='';
194        UName='';
195        VName='';
196        if numel(Field.ListVarName)>NbCoord % if there are variables beyond coord (exclude 1 D plots)
197            VarAttribute=cell(1,numel(ListVarName));
198            for ilist=1:numel(ListVarName)
199                index_var=find(strcmp(ListVarName{ilist},Field.ListVarName),1);
200                VarDimName{ilist}=Field.VarDimName{index_var};
201                DimOrder=[];
202                if NbDim ==2
203                    DimOrder=[find(strcmp(ParamIn.Coord_y,VarDimName{ilist})) find(strcmp(ParamIn.Coord_x,VarDimName{ilist}))];
204                elseif NbDim ==3
205                    DimOrder=[find(strcmp(ParamIn.Coord_z,VarDimName{ilist}))...
206                        find(strcmp(ParamIn.Coord_y,VarDimName{ilist}))...
207                        find(strcmp(ParamIn.Coord_x,VarDimName{ilist}))];
208                end
209                if ~isempty(DimOrder)
210                    Field.(ListVarName{ilist})=permute(Field.(ListVarName{ilist}),DimOrder);
211                    VarDimName{ilist}=VarDimName{ilist}(DimOrder);
212                end
213                if numel(Field.VarAttribute)>=index_var
214                VarAttribute{ilist}=Field.VarAttribute{index_var};% read var attributes from input if exist
215                end
216            end
217            check_remove=false(1,numel(Field.ListVarName));
218            for ilist=1:numel(ListVarName)
219                VarAttribute{ilist}.Role=Role{ilist};
220                VarAttribute{ilist}.ProjModeRequest=ProjModeRequest{ilist};
221                if isfield(ParamIn,'FieldName')
222                    VarAttribute{ilist}.Operator=Operator{ilist};
223                end
224                if strcmp(Operator{ilist},'norm')
225                    UName=ListVarName{ilist};
226                    VName=ListVarName{ilist+1};
227                    ListVarName{ilist}='norm';
228                    Field.norm=Field.(UName).*Field.(UName)+Field.(VName).*Field.(VName);
229                    Field.norm=sqrt(Field.norm);
230                    check_remove(ilist+1)=true;
231                    VarAttribute{ilist}.Operator='';
232                end
233            end
234            ListVarName(check_remove)=[];
235            VarDimName(check_remove)=[];
236            VarAttribute(check_remove)=[];
237            Field.ListVarName=[Field.ListVarName(1:NbCoord) ListVarName];% complement the list of vqriables, which may be listed twice
238            Field.VarDimName=[Field.VarDimName(1:NbCoord) VarDimName];
239            Field.VarAttribute=[Field.VarAttribute(1:NbCoord) VarAttribute];
240        end
241    case 'mat'
242         Field=load(FileName); 
243    case 'video'
244        if strcmp(class(ParamIn),'VideoReader')
245            A=read(ParamIn,num);
246        else
247            ParamOut=VideoReader(FileName);
248            A=read(ParamOut,num);
249        end
250    case 'mmreader'
251        if strcmp(class(ParamIn),'mmreader')
252            A=read(ParamIn,num);
253        else
254            ParamOut=mmreader(FileName);
255            A=read(ParamOut,num);
256        end
257    case 'vol'
258        A=imread(FileName);
259        Npz=size(A,1)/ParamIn.Npy;
260        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
261        A=permute(A,[3 2 1]);
262    case 'multimage'
263        %  warning 'off'
264        A=imread(FileName,num);
265    case 'image'
266        A=imread(FileName);
267    case 'rdvision'
268        [A,FileInfo,timestamps,errormsg]=read_rdvision(FileName,num);
269    case 'image_DaVis'
270        Input=readimx(FileName);
271        if numel(Input.Frames)==1
272            num=1;
273        end
274        A=Input.Frames{num}.Components{1}.Planes{1}';
275        for ilist=1:numel(Input.Frames{1}.Attributes)
276            if strcmp(Input.Frames{1}.Attributes{ilist}.Name,'AcqTimeSeries')
277                timestamps=str2num(Input.Frames{1}.Attributes{ilist}.Value(1:end-3))/1000000;
278                break
279            end
280        end
281    case 'cine_phantom'
282        [A,FileInfo] = read_cine_phantom(FileName,num );
283    otherwise
284        errormsg=[ FileType ': invalid input file type for uvmat'];
285       
286end
287
288%% case of image
289if ~isempty(A)
290    if strcmp(FileType,'rdvision')||strcmp(FileType,'image_DaVis')
291        Field.Time=timestamps;
292    end
293    if isstruct(ParamOut)
294        ParamOut.FieldName='image';
295    end
296    Npz=1;%default
297    npxy=size(A);
298    Field.NbDim=2;%default
299    Field.AName='image';
300    Field.ListVarName={'Coord_y','Coord_x','A'}; %
301    Field.VarAttribute{1}.Unit='pixel';
302    Field.VarAttribute{2}.Unit='pixel';
303    Field.VarAttribute{1}.Role='coord_y';
304    Field.VarAttribute{2}.Role='coord_x';
305    Field.VarAttribute{3}.Role='scalar';
306    if ndims(A)==3
307        if Npz==1;%color
308            Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x','rgb'}}; %
309            Field.Coord_y=[npxy(1)-0.5 0.5];
310            Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
311            if isstruct(ParamOut)
312                ParamOut.Npx=npxy(2);% display image size on the interface
313                ParamOut.Npy=npxy(1);
314            end
315        else
316            Field.NbDim=3;
317            Field.ListVarName=['AZ' Field.ListVarName];
318            Field.VarDimName={'AZ','Coord_y','Coord_x',{'AZ','Coord_y','Coord_x'}};
319            Field.AZ=[npxy(1)-0.5 0.5];
320            Field.Coord_y=[npxy(2)-0.5 0.5];
321            Field.Coord_x=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
322            if isstruct(ParamOut)
323                ParamOut.Npx=npxy(3);% display image size on the interface
324                ParamOut.Npy=npxy(2);
325            end
326        end
327    else
328        Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}}; %
329        Field.Coord_y=[npxy(1)-0.5 0.5];
330        Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
331    end
332    Field.A=A;
333    Field.CoordUnit='pixel'; %used for mouse_motion
334end
335
336
337
Note: See TracBrowser for help on using the repository browser.