[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=''; |
---|
[334] | 37 | A=[]; |
---|
[575] | 38 | InputField={}; |
---|
| 39 | check_colorvar=0; |
---|
[526] | 40 | if isstruct(ParamIn) |
---|
[575] | 41 | if isfield(ParamIn,'FieldName') |
---|
| 42 | if ischar(ParamIn.FieldName) |
---|
[576] | 43 | InputField={ParamIn.FieldName}; |
---|
[575] | 44 | else |
---|
| 45 | InputField= ParamIn.FieldName; |
---|
| 46 | end |
---|
[526] | 47 | end |
---|
| 48 | if isfield(ParamIn,'ColorVar') |
---|
| 49 | InputField=[ParamIn.FieldName {ParamIn.ColorVar}]; |
---|
| 50 | check_colorvar=1; |
---|
| 51 | end |
---|
[521] | 52 | end |
---|
[575] | 53 | |
---|
[334] | 54 | %% distingush different input file types |
---|
[527] | 55 | switch FileType |
---|
| 56 | case 'civdata' |
---|
| 57 | [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType); |
---|
| 58 | if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end |
---|
| 59 | ParamOut.CivStage=Field.CivStage; |
---|
| 60 | case 'civx' |
---|
| 61 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
| 62 | [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
| 63 | if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end |
---|
| 64 | ParamOut.CivStage=Field.CivStage; |
---|
| 65 | case 'netcdf' |
---|
| 66 | ListVar={}; |
---|
| 67 | for ilist=1:numel(InputField) |
---|
| 68 | r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
| 69 | if isempty(r) |
---|
| 70 | ListVar=[ListVar InputField(ilist)]; |
---|
| 71 | Role{numel(ListVar)}='scalar'; |
---|
| 72 | % FieldRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
[493] | 73 | else |
---|
[527] | 74 | ListVar=[ListVar {r.UName,r.VName}]; |
---|
| 75 | Role{numel(ListVar)}='vector_y'; |
---|
| 76 | Role{numel(ListVar)-1}='vector_x'; |
---|
| 77 | % TODO; introduce that for unstructured coordinates |
---|
| 78 | % switch r.Operator TODO; introduce that for unstructured coordinates |
---|
| 79 | % case 'norm' |
---|
| 80 | % FieldRequest{numel(ListVar)-1}='interp_lin';%scalar field (requires interpolation for plot) |
---|
| 81 | % FieldRequest{numel(ListVar)}='interp_lin'; |
---|
| 82 | % otherwise |
---|
| 83 | % FieldRequest{numel(ListVar)-1}=''; |
---|
| 84 | % end |
---|
[493] | 85 | end |
---|
[527] | 86 | end |
---|
| 87 | if check_colorvar |
---|
| 88 | Role{numel(ListVar)}='ancillary';% scalar used for color vector (not projected) |
---|
| 89 | end |
---|
[538] | 90 | [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y)' ListVar]); |
---|
[534] | 91 | if isfield(Field,'Txt') |
---|
| 92 | errormsg=Field.Txt; |
---|
| 93 | return |
---|
| 94 | end |
---|
[527] | 95 | for ivar=1:numel(ListVar) |
---|
| 96 | Field.VarAttribute{ivar+2}.Role=Role{ivar}; |
---|
| 97 | % Field.VarAttribute{ivar+2}.FieldRequest=FieldRequest{ivar}; |
---|
| 98 | end |
---|
| 99 | |
---|
| 100 | case 'video' |
---|
| 101 | if strcmp(class(ParamIn),'VideoReader') |
---|
| 102 | A=read(ParamIn,num); |
---|
| 103 | else |
---|
| 104 | ParamOut=VideoReader(FileName); |
---|
| 105 | A=read(ParamOut,num); |
---|
| 106 | end |
---|
| 107 | case 'mmreader' |
---|
| 108 | if strcmp(class(ParamIn),'mmreader') |
---|
| 109 | A=read(ParamIn,num); |
---|
| 110 | else |
---|
| 111 | ParamOut=mmreader(FileName); |
---|
| 112 | A=read(ParamOut,num); |
---|
| 113 | end |
---|
| 114 | case 'vol' |
---|
| 115 | A=imread(FileName); |
---|
| 116 | Npz=size(A,1)/ParamIn.Npy; |
---|
| 117 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
| 118 | A=permute(A,[3 2 1]); |
---|
| 119 | case 'multimage' |
---|
| 120 | warning 'off' |
---|
| 121 | A=imread(FileName,num); |
---|
| 122 | case 'image' |
---|
| 123 | A=imread(FileName); |
---|
| 124 | end |
---|
| 125 | if ~isempty(errormsg) |
---|
| 126 | errormsg=[FileType ' input: ' errormsg]; |
---|
[445] | 127 | return |
---|
[334] | 128 | end |
---|
[445] | 129 | |
---|
[334] | 130 | %% case of image |
---|
| 131 | if ~isempty(A) |
---|
[452] | 132 | if isstruct(ParamOut) |
---|
[181] | 133 | ParamOut.FieldName='image'; |
---|
| 134 | ParamOut.FieldList={'image'}; |
---|
[452] | 135 | end |
---|
[182] | 136 | Npz=1;%default |
---|
[181] | 137 | npxy=size(A); |
---|
[397] | 138 | % Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
| 139 | % Rangy=[npxy(1)-0.5 0.5]; % |
---|
[181] | 140 | Field.NbDim=2;%default |
---|
| 141 | Field.AName='image'; |
---|
| 142 | Field.ListVarName={'AY','AX','A'}; % |
---|
| 143 | if ndims(A)==3 |
---|
| 144 | if Npz==1;%color |
---|
| 145 | Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; % |
---|
| 146 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
| 147 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 148 | if isstruct(ParamOut) |
---|
[182] | 149 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 150 | ParamOut.Npy=npxy(1); |
---|
[452] | 151 | end |
---|
[186] | 152 | Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 153 | else |
---|
| 154 | Field.NbDim=3; |
---|
| 155 | Field.ListVarName=['AZ' Field.ListVarName]; |
---|
| 156 | Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}}; |
---|
| 157 | Field.AZ=[npxy(1)-0.5 0.5]; |
---|
| 158 | Field.AY=[npxy(2)-0.5 0.5]; |
---|
| 159 | Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 160 | if isstruct(ParamOut) |
---|
[182] | 161 | ParamOut.Npx=npxy(3);% display image size on the interface |
---|
| 162 | ParamOut.Npy=npxy(2); |
---|
[452] | 163 | end |
---|
[186] | 164 | Field.VarAttribute{4}.Mesh=1; |
---|
[181] | 165 | end |
---|
| 166 | else |
---|
| 167 | Field.VarDimName={'AY','AX',{'AY','AX'}}; % |
---|
| 168 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
| 169 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[182] | 170 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 171 | ParamOut.Npy=npxy(1); |
---|
[186] | 172 | Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 173 | end |
---|
| 174 | Field.A=A; |
---|
| 175 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
| 176 | end |
---|
| 177 | |
---|
| 178 | |
---|
[334] | 179 | |
---|