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