source: trunk/src/read_field.m @ 1199

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

bugs repaired

File size: 14.5 KB
RevLine 
[497]1%'read_field': read the fields from files in different formats (netcdf files, images, video)
[181]2%--------------------------------------------------------------------------
[1162]3%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,frame_index)
[181]4%
5% OUTPUT:
6% Field: matlab structure representing the field
7% ParamOut: structure representing parameters:
8%        .FieldName; field name
9%        .VelType
[1045]10%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....
[181]11%        .Npx,.Npy: for images, nbre of pixels in x and y
12% errormsg: error message, ='' by default
13%
14%INPUT
[466]15% FileName: name of the input file
[784]16% FileType: type of file, as determined by the function get_file_info.m
[450]17% ParamIn: movie object or Matlab structure of input parameters
[1197]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
[181]21%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
[1162]22%     .TimeDimName: name of the dimension considered as 'time', selected index value then set by input 'frame_index'
[1197]23%     .TimeVarName: name of the variable considered as 'time'
[1162]24% frame_index: frame number for movies or multidimensional netcdf files with dim >2
[497]25%
26% see also read_image.m,read_civxdata.m,read_civdata.m,
[466]27
[809]28%=======================================================================
[1126]29% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]30%   http://www.legi.grenoble-inp.fr
[1127]31%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[809]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
[1162]46function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,frame_index)
[748]47%% default output and check input
[181]48Field=[];
[1162]49if ~exist('frame_index','var')
50    frame_index=1;
[354]51end
[1162]52if isempty(frame_index)
53    frame_index=1;
[1033]54end
[354]55if ~exist('ParamIn','var')
56    ParamIn=[];
57end
58ParamOut=ParamIn;%default
[181]59errormsg='';
[1189]60if isempty(regexp(FileName,'^http://', 'once'))&& exist(FileName,'file')~=2
[1184]61    errormsg='input file does not exist';
[635]62    return
63end
[334]64A=[];
[575]65InputField={};
66check_colorvar=0;
[526]67if isstruct(ParamIn)
[575]68    if isfield(ParamIn,'FieldName')
69        if ischar(ParamIn.FieldName)
[576]70            InputField={ParamIn.FieldName};
[575]71        else
72            InputField= ParamIn.FieldName;
73        end
[526]74    end
[654]75    check_colorvar=zeros(size(InputField));
[684]76    if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar)
[526]77        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
[654]78        check_colorvar(numel(InputField))=1;
[526]79    end
[521]80end
[575]81
[334]82%% distingush different input file types
[527]83switch FileType
[1196]84    case {'civdata','civdata_compress','civdata_3D'}% format for civ results
[1162]85        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType,frame_index);
[907]86        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
[527]87        ParamOut.CivStage=Field.CivStage;
[958]88    case 'pivdata_fluidimage'
[1134]89        if ~isfield(ParamIn,'VelType')
90            ParamIn.VelType='';
91        end
[958]92        [Field,ParamOut.VelType,errormsg]=read_pivdata_fluidimage(FileName,InputField,ParamIn.VelType);
93        ParamOut.CivStage=Field.CivStage;
[748]94    case 'civx'% old (obsolete) format for civ results
[527]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;
[1095]99    case {'netcdf','mat'}% general netcdf file (not recognized as civ)
[1045]100        ListVarName={};
[675]101        Role={};
102        ProjModeRequest={};
[1041]103        % scan the list InputField
[1045]104        Operator=cell(1,numel(InputField));
[1162]105        %InputVar=cell(1,numel(InputField));
[527]106        for ilist=1:numel(InputField)
[748]107            % look for input variables to read
[527]108            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
[654]109            if isempty(r)%  no operator used
[1045]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)
[681]117                end
[1045]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'}];
[907]126                    end
[654]127                else
[1045]128                     Role=[Role {'vector_x'}];
[654]129                end
[1045]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)}='';           
[667]134                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
[1045]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)
[667]137                else
[1045]138                    ProjModeRequest{numel(ListVarName)}='';
139                    ProjModeRequest{numel(ListVarName)-1}='';
[667]140                end
[493]141            end
[527]142        end
[748]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);
[648]147        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
[1180]148            [Field,~,~,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,frame_index,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]);
[747]149        elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time  in a multidimensional array
[1180]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
[648]154        else
[1180]155            [Field,~,~,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]);
[648]156        end
[747]157        if ~isempty(errormsg)
[534]158            return
159        end
[1162]160       % CheckStructured=1;
[1045]161        %scan all the variables
162        NbCoord=0;
163        if ~isempty(ParamIn.Coord_x)
[1162]164            index_Coord_x=strcmp(ParamIn.Coord_x,Field.ListVarName);
[1045]165            Field.VarAttribute{index_Coord_x}.Role='coord_x';%
166            NbCoord=NbCoord+1;
[675]167        end
[1045]168        if ~isempty(ParamIn.Coord_y)
169            if ischar(ParamIn.Coord_y)
[1162]170                index_Coord_y=strcmp(ParamIn.Coord_y,Field.ListVarName);
[1045]171                Field.VarAttribute{index_Coord_y}.Role='coord_y';%
172                NbCoord=NbCoord+1;
173            else
174                for icoord_y=1:numel(ParamIn.Coord_y)
[1162]175                    index_Coord_y=strcmp(ParamIn.Coord_y{icoord_y},Field.ListVarName);
[1045]176                    Field.VarAttribute{index_Coord_y}.Role='coord_y';%
177                    NbCoord=NbCoord+1;
[1001]178                end
179            end
[1002]180        end
[1045]181        NbDim=1;
182        if ~isempty(ParamIn.Coord_z)
[1162]183            index_Coord_z=strcmp(ParamIn.Coord_z,Field.ListVarName);
[1045]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}))];
[907]202                end
[1137]203                if numel(DimOrder)>=ndims(Field.(ListVarName{ilist}))
[1045]204                    Field.(ListVarName{ilist})=permute(Field.(ListVarName{ilist}),DimOrder);
205                    VarDimName{ilist}=VarDimName{ilist}(DimOrder);
[907]206                end
[1045]207                if numel(Field.VarAttribute)>=index_var
208                VarAttribute{ilist}=Field.VarAttribute{index_var};% read var attributes from input if exist
[684]209                end
[681]210            end
[1045]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};
[907]217                end
[1045]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
[769]227            end
[1045]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];
[1095]234        end 
[527]235    case 'video'
[1162]236        if isa(ParamIn,'VideoReader')
237            A=read(ParamIn,frame_index);
[527]238        else
239            ParamOut=VideoReader(FileName);
[1162]240            A=read(ParamOut,frame_index);
[527]241        end
[1170]242    case 'telopsIR'
243         [A,Header]=readIRCam(FileName,'Frames',frame_index);
[1180]244         A=flip(A);
[1170]245         A=(reshape(A,Header(1).Width,Header(1).Height))';
[527]246    case 'mmreader'
[1162]247        if isa(ParamIn,'mmreader')
248            A=read(ParamIn,frame_index);
[527]249        else
250            ParamOut=mmreader(FileName);
[1162]251            A=read(ParamOut,frame_index);
[527]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'
[1162]259        A=imread(FileName,frame_index);
[527]260    case 'image'
261        A=imread(FileName);
[784]262    case 'rdvision'
[1162]263        [A,FileInfo,timestamps,errormsg]=read_rdvision(FileName,frame_index);
[1007]264    case 'image_DaVis'
265        Input=readimx(FileName);
[1033]266        if numel(Input.Frames)==1
[1162]267            frame_index=1;
[1033]268        end
[1162]269        A=Input.Frames{frame_index}.Components{1}.Planes{1}';
[1033]270        for ilist=1:numel(Input.Frames{1}.Attributes)
271            if strcmp(Input.Frames{1}.Attributes{ilist}.Name,'AcqTimeSeries')
[1045]272                timestamps=str2num(Input.Frames{1}.Attributes{ilist}.Value(1:end-3))/1000000;
273                break
[1033]274            end
275        end
[980]276    case 'cine_phantom'
[1162]277        [A,FileInfo] = read_cine_phantom(FileName,frame_index );
[784]278    otherwise
279        errormsg=[ FileType ': invalid input file type for uvmat'];
[1045]280       
[527]281end
[445]282
[334]283%% case of image
284if ~isempty(A)
[1032]285    if strcmp(FileType,'rdvision')||strcmp(FileType,'image_DaVis')
[784]286        Field.Time=timestamps;
287    end
[452]288    if isstruct(ParamOut)
[580]289        ParamOut.FieldName='image';
[452]290    end
[182]291    Npz=1;%default
[181]292    npxy=size(A);
293    Field.NbDim=2;%default
294    Field.AName='image';
[782]295    Field.ListVarName={'Coord_y','Coord_x','A'}; %
[1024]296    Field.VarAttribute{1}.Unit='pixel';
297    Field.VarAttribute{2}.Unit='pixel';
[1045]298    Field.VarAttribute{1}.Role='coord_y';
299    Field.VarAttribute{2}.Role='coord_x';
[1024]300    Field.VarAttribute{3}.Role='scalar';
[181]301    if ndims(A)==3
[1162]302        if Npz==1%color
[782]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
[452]306            if isstruct(ParamOut)
[580]307                ParamOut.Npx=npxy(2);% display image size on the interface
308                ParamOut.Npy=npxy(1);
[452]309            end
[181]310        else
311            Field.NbDim=3;
312            Field.ListVarName=['AZ' Field.ListVarName];
[782]313            Field.VarDimName={'AZ','Coord_y','Coord_x',{'AZ','Coord_y','Coord_x'}};
[181]314            Field.AZ=[npxy(1)-0.5 0.5];
[782]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
[452]317            if isstruct(ParamOut)
[580]318                ParamOut.Npx=npxy(3);% display image size on the interface
319                ParamOut.Npy=npxy(2);
320            end
[181]321        end
322    else
[782]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
[181]326    end
327    Field.A=A;
328    Field.CoordUnit='pixel'; %used for mouse_motion
329end
330
331
[334]332
Note: See TracBrowser for help on using the repository browser.