source: trunk/src/read_field.m @ 472

Last change on this file since 472 was 472, checked in by sommeria, 12 years ago

many bugs repaired. series set to work in mode background

File size: 10.0 KB
RevLine 
[379]1
[181]2%'read_field': read input fields in different formats
3%--------------------------------------------------------------------------
[450]4%  function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]5%
6% OUTPUT:
7% Field: matlab structure representing the field
8% ParamOut: structure representing parameters:
9%        .FieldName; field name
10%        .FieldList: menu of possible fields
11%        .VelType
12%        .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2  (fix1)....     
13%        .Npx,.Npy: for images, nbre of pixels in x and y
14% errormsg: error message, ='' by default
15%
16%INPUT
[466]17% FileName: name of the input file
[181]18% FileType: type of file
19%     = netcdf : netcdf file
20%     = image : usual image as recognised by Matlab
21%     = multimage: image series stored in a single file
[441]22%     = mmreader: movie read with mmreader
23%     = video: movie read with VideoReader (recent versions of Matlab)
[181]24%     = vol: images representing scanned volume (images concatened in the y direction)
[450]25% ParamIn: movie object or Matlab structure of input parameters
[445]26%     .FieldName: name (char string) of the input field (for Civx data)
27%     .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...)
[181]28%     .ColorVar: variable used for vector color
29%     .Npx, .Npy: nbre of pixels along x and y (used for .vol input files)
[466]30% num: frame number for movies
31
[450]32function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num)
[181]33Field=[];
[354]34if ~exist('num','var')
35    num=1;
36end
37if ~exist('ParamIn','var')
38    ParamIn=[];
39end
40ParamOut=ParamIn;%default
[181]41errormsg='';
[466]42% if isfield(ParamIn,'VelType')
43% VelType=ParamIn.VelType;
44% end
[334]45A=[];
46%% distingush different input file types
[445]47try
48    switch FileType
49        case {'civx','civdata','netcdf'}  %read the first nc field
50            %         ParamOut.FieldName=ParamIn.FieldName;
51            GUIName='get_field'; %default name of the GUI get_field
52            if isfield(ParamIn,'GUIName')
53                GUIName=ParamIn.GUIName;
54            end
55            CivStage=0;
56            if ~strcmp(ParamIn.FieldName,'get_field...')% if get_field is not requested, look for Civx data
57                FieldList=calc_field;%list of possible fields for Civx data
58                ParamOut.ColorVar='';%default
[472]59                if ischar(ParamIn.FieldName)
60                    FieldName=ParamIn.FieldName;
61                else
62                    FieldName=ParamIn.FieldName{1};
63                end
64                field_index=strcmp(FieldName,FieldList);%look for ParamIn.FieldName in the list of possible fields for Civx data
[445]65                if isempty(find(field_index,1))% ParamIn.FieldName is not in the list, check whether Civx data exist
[450]66                    Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ','CivStage');
[445]67                    % case of new civdata conventions
68                    if isequal(Data.Conventions,'uvmat/civdata')
69                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
70                        ParamOut.ColorVar='ima_cor';
71                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
[450]72                        [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType,Data.CivStage);
[472]73                        if ~isempty(errormsg),errormsg=['read_civdata:' errormsg];return,end
[445]74                        CivStage=Field.CivStage;
75                        ParamOut.CivStage=Field.CivStage;
76                        %case of old civx conventions
77                    elseif ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0)
78                        ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default
79                        ParamOut.ColorVar='ima_cor';
80                        InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}];
[450]81                        [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType);
[472]82                        if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
[445]83                        CivStage=Field.CivStage;
84                        ParamOut.CivStage=Field.CivStage;
85                        % not cvix file, fields will be chosen through the GUI get_field
86                    else
87                        ParamOut.FieldName='get_field...';
88                        hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
89                        if ~isempty(hget_field)
90                            delete(hget_field)%delete  get_field for reinitialisation
91                        end
92                    end
[472]93                else             
94                    InputField=ParamOut.FieldName;
95                    if ischar(InputField)
96                        InputField={InputField};
97                    end
[445]98                    if isfield(ParamIn,'ColorVar')
99                        ParamOut.ColorVar=ParamIn.ColorVar;
100                        InputField=[InputField {ParamOut.ColorVar}];
101                    end
[450]102                    [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType);
[472]103                    if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end
[404]104                    CivStage=Field.CivStage;
[405]105                    ParamOut.CivStage=Field.CivStage;
[334]106                end
[445]107                ParamOut.FieldList=[{'image'};FieldList;{'get_field...'}];
108            end
109            if CivStage==0% read the field names on the interface get_field.
110                hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI
111                if isempty(hget_field)% open the GUI get_field if it is not found
[450]112                    hget_field= get_field(FileName);%open the get_field GUI
[445]113                    set(hget_field,'Name',GUIName)%update the name of get_field (e.g. get_field_1)
[334]114                end
[445]115                hhget_field=guidata(hget_field);
116                %% update  the get_field GUI
[450]117                set(hhget_field.inputfile,'String',FileName)
[445]118                set(hhget_field.list_fig,'Value',1)
119                if exist('num','var')&&~isnan(num)
120                    set(hhget_field.TimeIndexValue,'String',num2str(num))
[334]121                end
[445]122                funct_list=get(hhget_field.ACTION,'UserData');
123                funct_index=get(hhget_field.ACTION,'Value');
124                funct=funct_list{funct_index};%select  the current action in get_field, e;g. PLOT
125                Field=funct(hget_field); %%activate the current action selected in get_field, e;g.read the names of the variables to plot
126                Tabchar={''};%default
127                Tabcell=[];
[450]128                set(hhget_field.inputfile,'String',FileName)
[445]129                if isfield(Field,'ListGlobalAttribute')&& ~isempty(Field.ListGlobalAttribute)
130                    for iline=1:length(Field.ListGlobalAttribute)
131                        Tabcell{iline,1}=Field.ListGlobalAttribute{iline};
132                        if isfield(Field, Field.ListGlobalAttribute{iline})
[472]133                            val=Field.(Field.ListGlobalAttribute{iline});
[445]134                            if ischar(val);
135                                Tabcell{iline,2}=val;
136                            else
137                                Tabcell{iline,2}=num2str(val);
138                            end
[334]139                        end
[181]140                    end
[445]141                    if ~isempty(Tabcell)
142                        Tabchar=cell2tab(Tabcell,'=');
143                        Tabchar=[{''};Tabchar];
144                    end
[181]145                end
[445]146                ParamOut.CivStage=0;
147                ParamOut.VelType=[];
148                if isfield(Field,'TimeIndex')
149                    ParamOut.TimeIndex=Field.TimeIndex;
[334]150                end
[445]151                if isfield(Field,'TimeValue')
152                    ParamOut.TimeValue=Field.TimeValue;
153                end
154                ParamOut.FieldList={'get_field...'};
[181]155            end
[445]156        case {'video','mmreader'}
[450]157            A=read(ParamIn,num);
[445]158        case 'vol'
[450]159            A=imread(FileName);
[445]160            Npz=size(A,1)/ParamIn.Npy;
161            A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz);
162            A=permute(A,[3 2 1]);
163        case 'multimage'
164            warning 'off'
[450]165            A=imread(FileName,num);
[445]166        case 'image'
[450]167            A=imread(FileName);
[445]168    end
169catch ME
[472]170    errormsg=[FileType ' input: ' ME.message];
[445]171    return
[334]172end
[445]173
[334]174%% case of image
175if ~isempty(A)
[452]176    if isstruct(ParamOut)
[181]177    ParamOut.FieldName='image';
178    ParamOut.FieldList={'image'};
[452]179    end
[182]180    Npz=1;%default
[181]181    npxy=size(A);
[397]182%     Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
183%     Rangy=[npxy(1)-0.5 0.5]; %
[181]184    Field.NbDim=2;%default
185    Field.AName='image';
186    Field.ListVarName={'AY','AX','A'}; %
187    if ndims(A)==3
188        if Npz==1;%color
189            Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
190            Field.AY=[npxy(1)-0.5 0.5];
191            Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[452]192            if isstruct(ParamOut)
[182]193            ParamOut.Npx=npxy(2);% display image size on the interface
194            ParamOut.Npy=npxy(1);
[452]195            end
[186]196            Field.VarAttribute{3}.Mesh=1;
[181]197        else
198            Field.NbDim=3;
199            Field.ListVarName=['AZ' Field.ListVarName];
200            Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}};
201            Field.AZ=[npxy(1)-0.5 0.5];
202            Field.AY=[npxy(2)-0.5 0.5];
203            Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers
[452]204            if isstruct(ParamOut)
[182]205            ParamOut.Npx=npxy(3);% display image size on the interface
206            ParamOut.Npy=npxy(2);
[452]207             end
[186]208            Field.VarAttribute{4}.Mesh=1;
[181]209        end
210    else
211        Field.VarDimName={'AY','AX',{'AY','AX'}}; %
212        Field.AY=[npxy(1)-0.5 0.5];
213        Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
[182]214        ParamOut.Npx=npxy(2);% display image size on the interface
215        ParamOut.Npy=npxy(1);
[186]216        Field.VarAttribute{3}.Mesh=1;
[181]217    end
218    Field.A=A;
219    Field.CoordUnit='pixel'; %used for mouse_motion
220end
221
222
[334]223
Note: See TracBrowser for help on using the repository browser.