source: trunk/src/read_field.m @ 1033

Last change on this file since 1033 was 1033, checked in by sommeria, 6 years ago

miscellaneous updates

File size: 15.2 KB
RevLine 
[497]1%'read_field': read the fields from files in different formats (netcdf files, images, video)
[181]2%--------------------------------------------------------------------------
[450]3%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]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
[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
[445]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'...)
[181]20%     .ColorVar: variable used for vector color
21%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
[693]22%     .TimeDimName: name of the dimension considered as 'time', selected index value then set by input 'num'   
[466]23% num: frame number for movies
[497]24%
25% see also read_image.m,read_civxdata.m,read_civdata.m,
[466]26
[809]27%=======================================================================
[1027]28% Copyright 2008-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]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
[450]45function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[748]46%% default output and check input
[181]47Field=[];
[354]48if ~exist('num','var')
49    num=1;
50end
[1033]51if isempty(num)
52    num=1;
53end
[354]54if ~exist('ParamIn','var')
55    ParamIn=[];
56end
57ParamOut=ParamIn;%default
[181]58errormsg='';
[1022]59if isempty(regexp(FileName,'^http://'))&& ~exist(FileName,'file')
[747]60    errormsg=['input file ' FileName ' does not exist'];
[635]61    return
62end
[334]63A=[];
[575]64InputField={};
65check_colorvar=0;
[526]66if isstruct(ParamIn)
[575]67    if isfield(ParamIn,'FieldName')
68        if ischar(ParamIn.FieldName)
[576]69            InputField={ParamIn.FieldName};
[575]70        else
71            InputField= ParamIn.FieldName;
72        end
[526]73    end
[654]74    check_colorvar=zeros(size(InputField));
[684]75    if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar)
[526]76        InputField=[ParamIn.FieldName {ParamIn.ColorVar}];
[654]77        check_colorvar(numel(InputField))=1;
[526]78    end
[521]79end
[575]80
[334]81%% distingush different input file types
[527]82switch FileType
[748]83    case 'civdata'% new format for civ results
[527]84        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType);
[907]85        if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end
[527]86        ParamOut.CivStage=Field.CivStage;
[958]87    case 'pivdata_fluidimage'
88        [Field,ParamOut.VelType,errormsg]=read_pivdata_fluidimage(FileName,InputField,ParamIn.VelType);
89        ParamOut.CivStage=Field.CivStage;
[748]90    case 'civx'% old (obsolete) format for civ results
[527]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;
[748]95    case 'netcdf'% general netcdf file (not recognized as civ)
[527]96        ListVar={};
[675]97        Role={};
98        ProjModeRequest={};
[684]99        ListInputField={};
100        ListOperator={};
[681]101        checkU=0;
102        checkV=0;
[527]103        for ilist=1:numel(InputField)
[748]104            % look for input variables to read
[527]105            r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
[654]106            if isempty(r)%  no operator used
[681]107                if isempty(find(strcmp(InputField{ilist},ListVar)))
[684]108                    ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list
109                    ListInputField=[ListInputField InputField(ilist)];
110                    ListOperator=[ListOperator {''}];
[681]111                end
[654]112                if check_colorvar(ilist)
[907]113                    if isempty(find(strcmp(InputField{ilist},ListVar)))
[667]114                    Role{numel(ListVar)}='ancillary';% not projected with interpolation
115                    ProjModeRequest{numel(ListVar)}='';
[907]116                    end
[654]117                else
[667]118                    Role{numel(ListVar)}='scalar';
119                    ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot)
[654]120                end
121            else  % an operator 'vec' or 'norm' is used
[667]122                if ~check_colorvar(ilist) && strcmp(r.Operator,'norm')
[675]123                    ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot)
[667]124                else
[675]125                    ProjModeRequestVar='';
[667]126                end
[675]127                ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName
128                ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName
129                if isempty(ind_var_U)
[684]130                    ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed
[675]131                    Role=[Role {'vector_x'}];
132                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
[684]133                    ListInputField=[ListInputField InputField(ilist)];
[681]134                else
135                    checkU=1;
[675]136                end
137                if isempty(ind_var_V)
[684]138                    ListVar=[ListVar {r.VName}];% append the variable in the list if not previously listed
[675]139                    Role=[Role {'vector_y'}];
140                    ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}];
[867]141                    ListInputField=[ListInputField InputField(ilist)];
[890]142                   
[681]143                else
144                    checkV=1;
[675]145                end
[493]146            end
[527]147        end
[748]148        if ~isfield(ParamIn,'Coord_z')
149            ParamIn.Coord_z=[];
150        end
151        NbCoord=~isempty(ParamIn.Coord_x)+~isempty(ParamIn.Coord_y)+~isempty(ParamIn.Coord_z);
[648]152        if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array
[748]153            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
[747]154        elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time  in a multidimensional array
[748]155            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeVarName',ParamIn.TimeVarName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
[907]156            if numel(num)~=1
[1001]157                NbCoord=NbCoord+1;% adds time coordinate, except if a single time has been selected
[907]158            end
[648]159        else
[748]160            [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]);
[648]161        end
[747]162        if ~isempty(errormsg)
[534]163            return
164        end
[1001]165        CheckStructured=1;
166        %scan all the variables beyond the two first NbCoord ones describing the coordinates.
[748]167        for ilist=NbCoord+1:numel(Field.VarDimName)
168            if isequal(Field.VarDimName{1},Field.VarDimName{ilist}) % if a variable has the same dimension as the coordinate, it denotes a field with unstructured coordinates
[675]169                Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates
170                Field.VarAttribute{2}.Role='coord_y';
[748]171                if NbCoord>=3
172                    Field.VarAttribute{3}.Role='coord_z';
173                end
[1001]174                CheckUnstructured=0;
[675]175                break
176            end
177        end
[1001]178        if CheckStructured
179            for ilist=NbCoord+1:numel(Field.VarDimName)
[1002]180                if numel(Field.VarDimName{ilist})==NbCoord
181                    rank(1)=find(strcmp(ParamIn.Coord_x,Field.VarDimName{ilist}));
182                    rank(2)=find(strcmp(ParamIn.Coord_y,Field.VarDimName{ilist}));
183                    if NbCoord==3
184                        rank(3)=find(strcmp(ParamIn.Coord_z,Field.VarDimName{ilist}));
185                    end
186                    rank=rank(end:-1:1);
187                    VarName=Field.ListVarName{ilist};
188                    Field.(VarName)=permute(Field.(VarName),rank);
189                    Field.VarDimName{ilist}=Field.VarDimName{ilist}(rank);% permute the order of dimensions
[1001]190                end
191            end
[1002]192        end
[684]193        NormName='';
194        UName='';
195        VName='';
[896]196        if numel(Field.ListVarName)>NbCoord % if there are variables beyond coord (1 D plots)
[907]197            for ilist=1:numel(ListVar)
198                Field.VarAttribute{ilist+NbCoord}.Role=Role{ilist};
199                Field.VarAttribute{ilist+NbCoord}.ProjModeRequest=ProjModeRequest{ilist};
200                if isfield(ParamIn,'FieldName')
201                    Field.VarAttribute{ilist+NbCoord}.FieldName=ListInputField{ilist};
202                end
203                r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names');
204                if ~isempty(r)&& strcmp(r.Operator,'norm')
205                    NormName='norm';
206                    if ~isempty(find(strcmp(ListVar,'norm')))
207                        NormName='norm_1';
208                    end
209                    Field.ListVarName=[Field.ListVarName {NormName}];
210                    ilistmax=numel(Field.ListVarName);
211                    Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2};
212                    Field.VarAttribute{ilistmax}.Role='scalar';
213                    Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName);
214                    Field.(NormName)=sqrt(Field.(NormName));
215                    UName=r.UName;
216                    VName=r.VName;
217                end
[648]218            end
[907]219           
220            if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables
221                ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName
222                ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName
223                if ~checkU && ~checkV
224                    Field.ListVarName([ind_var_U+2 ind_var_V+2])=[];
225                    Field.VarDimName([ind_var_U+2 ind_var_V+2])=[];
226                    Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[];
227                elseif ~checkU
228                    Field.ListVarName(ind_var_U+2)=[];
229                    Field.VarDimName(ind_var_U+2)=[];
230                    Field.VarAttribute(ind_var_U+2 )=[];
231                elseif ~checkV
232                    Field.ListVarName(ind_var_V+2)=[];
233                    Field.VarDimName(ind_var_V+2)=[];
234                    Field.VarAttribute(ind_var_V+2 )=[];
[684]235                end
[681]236            end
[907]237            % insert coordinates as indices in case of plots vs matrix index
238            if isfield(ParamIn,'CheckCoordIndex') && ParamIn.CheckCoordIndex
239                Field.ListVarName=[Field.ListDimName Field.ListVarName];
240                Field.VarDimName=[Field.ListDimName Field.VarDimName];
241                for idim=1:numel(Field.ListDimName)
242                    CoordName=Field.ListDimName{idim};
243                    Field.(CoordName)=1:Field.DimValue(idim);
244                end
245                Field.VarAttribute=[cell(1,numel(Field.ListDimName)) Field.VarAttribute];
[769]246            end
[1001]247           
[684]248        end
[527]249    case 'video'
250        if strcmp(class(ParamIn),'VideoReader')
251            A=read(ParamIn,num);
252        else
253            ParamOut=VideoReader(FileName);
254            A=read(ParamOut,num);
255        end
256    case 'mmreader'
257        if strcmp(class(ParamIn),'mmreader')
258            A=read(ParamIn,num);
259        else
260            ParamOut=mmreader(FileName);
261            A=read(ParamOut,num);
262        end
263    case 'vol'
264        A=imread(FileName);
265        Npz=size(A,1)/ParamIn.Npy;
266        A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
267        A=permute(A,[3 2 1]);
268    case 'multimage'
[907]269        %  warning 'off'
[527]270        A=imread(FileName,num);
271    case 'image'
272        A=imread(FileName);
[784]273    case 'rdvision'
[785]274        [A,FileInfo,timestamps]=read_rdvision(FileName,num);
[1007]275    case 'image_DaVis'
276        Input=readimx(FileName);
[1033]277        if numel(Input.Frames)==1
278            num=1;
279        end
[1007]280        A=Input.Frames{num}.Components{1}.Planes{1}';
[1033]281        for ilist=1:numel(Input.Frames{1}.Attributes)
282            if strcmp(Input.Frames{1}.Attributes{ilist}.Name,'AcqTimeSeries')
283        timestamps=str2num(Input.Frames{1}.Attributes{ilist}.Value(1:end-3))/1000000;
284        break
285            end
286        end
[980]287    case 'cine_phantom'
288        [A,FileInfo] = read_cine_phantom(FileName,num );
[784]289    otherwise
290        errormsg=[ FileType ': invalid input file type for uvmat'];
[527]291end
[445]292
[334]293%% case of image
294if ~isempty(A)
[1032]295    if strcmp(FileType,'rdvision')||strcmp(FileType,'image_DaVis')
[784]296        Field.Time=timestamps;
297    end
[452]298    if isstruct(ParamOut)
[580]299        ParamOut.FieldName='image';
[452]300    end
[182]301    Npz=1;%default
[181]302    npxy=size(A);
[580]303    %     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
304    %     Rangy=[npxy(1)-0.5 0.5]; %
[181]305    Field.NbDim=2;%default
306    Field.AName='image';
[782]307    Field.ListVarName={'Coord_y','Coord_x','A'}; %
[1024]308    Field.VarAttribute{1}.Unit='pixel';
309    Field.VarAttribute{2}.Unit='pixel';
310    Field.VarAttribute{3}.Role='scalar';
[181]311    if ndims(A)==3
312        if Npz==1;%color
[782]313            Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x','rgb'}}; %
314            Field.Coord_y=[npxy(1)-0.5 0.5];
315            Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[452]316            if isstruct(ParamOut)
[580]317                ParamOut.Npx=npxy(2);% display image size on the interface
318                ParamOut.Npy=npxy(1);
[452]319            end
[871]320%             Field.VarAttribute{3}.Mesh=1;
[181]321        else
322            Field.NbDim=3;
323            Field.ListVarName=['AZ' Field.ListVarName];
[782]324            Field.VarDimName={'AZ','Coord_y','Coord_x',{'AZ','Coord_y','Coord_x'}};
[181]325            Field.AZ=[npxy(1)-0.5 0.5];
[782]326            Field.Coord_y=[npxy(2)-0.5 0.5];
327            Field.Coord_x=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
[452]328            if isstruct(ParamOut)
[580]329                ParamOut.Npx=npxy(3);% display image size on the interface
330                ParamOut.Npy=npxy(2);
331            end
[871]332%             Field.VarAttribute{4}.Mesh=1;
[181]333        end
334    else
[782]335        Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}}; %
336        Field.Coord_y=[npxy(1)-0.5 0.5];
337        Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[880]338%        ParamOut.Npx=npxy(2);% display image size on the interface
339 %       ParamOut.Npy=npxy(1);
[871]340%         Field.VarAttribute{3}.Mesh=1;
[181]341    end
342    Field.A=A;
343    Field.CoordUnit='pixel'; %used for mouse_motion
[784]344       
[181]345end
346
347
[334]348
Note: See TracBrowser for help on using the repository browser.