source: trunk/src/read_field.m @ 1197

Last change on this file since 1197 was 1197, checked in by sommeria, 3 days ago

bugs repaired

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