source: trunk/src/read_civdata.m @ 1195

Last change on this file since 1195 was 1195, checked in by sommeria, 5 days ago

format of PIV data made more compact/4

File size: 10.9 KB
Line 
1%'read_civdata': reads new civ data from netcdf files
2%------------------------------------------------------------------
3%
4% function [Field,VelTypeOut]=read_civdata(FileName,FieldNames,VelType)
5%
6% OUTPUT:
7% Field: structure representing the selected field, containing
8%            .Txt: (char string) error message if any
9%            .ListGlobalAttribute: list of global attributes containing:
10%                    .NbCoord: number of vector components
11%                    .NbDim: number of dimensions (=2 or 3)
12%                    .Dt: time interval for the corresponding image pair
13%                    .Time: absolute time (average of the initial image pair)
14%                    .CivStage: =0,
15%                       =1, civ1 has been performed only
16%                       =2, fix1 has been performed
17%                       =3, pacth1 has been performed
18%                       =4, civ2 has been performed
19%                       =5, fix2 has been performed
20%                       =6, pacth2 has been performed
21%                     .CoordUnit: 'pixel'
22%             .ListVarName: {'X'  'Y'  'U'  'V'  'F'  'FF'}
23%                   .X, .Y, .Z: set of vector coordinates
24%                    .U,.V,.W: corresponding set of vector components
25%                    .F: warning flags
26%                    .FF: error flag, =0 for good vectors
27%                     .C: scalar associated with velocity (used for vector colors)
28%
29% VelTypeOut: velocity type corresponding to the selected field: ='civ1','interp1','interp2','civ2'....
30%
31% INPUT:
32% FileName: file name (string).
33% FieldNames =cell of field names to get, which can contain the strings:
34% 'U','V','norm(U,V)','curl(U,V)','div(U,V)','strain(U,V)'
35% VelType : character string indicating the types of velocity fields to read ('civ1','civ2'...)
36%            if vel_type=[] or'*', a  priority choice, given by vel_type_out{1,2}, is done depending
37%            if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)
38%
39% FUNCTIONS called:
40% 'varcivx_generator':, sets the names of vaiables to read in the netcdf file
41% 'nc2struct': reads a netcdf file
42
43%=======================================================================
44% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
45%   http://www.legi.grenoble-inp.fr
46%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
47%
48%     This file is part of the toolbox UVMAT.
49%
50%     UVMAT is free software; you can redistribute it and/or modify
51%     it under the terms of the GNU General Public License as published
52%     by the Free Software Foundation; either version 2 of the license,
53%     or (at your option) any later version.
54%
55%     UVMAT is distributed in the hope that it will be useful,
56%     but WITHOUT ANY WARRANTY; without even the implied warranty of
57%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58%     GNU General Public License (see LICENSE.txt) for more details.
59%=======================================================================
60
61function [Field,VelTypeOut,errormsg]=read_civdata(FileName,FieldNames,VelType,frame_index)
62
63%% default input
64if ~exist('frame_index','var')
65    frame_index=1;
66end
67if ~exist('VelType','var')
68    VelType='';
69end
70if isempty(VelType)||strcmp(VelType,'*')
71    VelType='';
72end
73if ~exist('FieldNames','var')
74    FieldNames={}; %default
75end
76Field=[];
77VelTypeOut=VelType;
78errormsg='';
79if ischar(FieldNames), FieldNames={FieldNames}; end
80ProjModeRequest='';
81for ilist=1:length(FieldNames)
82    if ~isempty(FieldNames{ilist})
83        switch FieldNames{ilist}
84            case {'U','V','norm(U,V)'}
85                if ~strcmp(FieldNames{1},'vec(U,V)')% if the scalar is not used as color of vectors
86                ProjModeRequest='interp_lin';
87                end
88            case {'curl(U,V)','div(U,V)','strain(U,V)'}
89                ProjModeRequest='interp_tps';
90        end
91    end
92end
93
94%% reading data
95[Data,~,~,errormsg]=nc2struct(FileName,'ListGlobalAttribute','Conventions','CivStage');% read the global attributes to get Data.CivStage
96if ~isempty(errormsg)
97     errormsg=['read_civdata: ' errormsg];
98    return
99end
100
101% set the list of variables to read and their role
102CheckCompress=strcmp(Data.Conventions,'uvmat/civdata/compress');
103[varlist,role,VelTypeOut]=varcivx_generator(ProjModeRequest,VelType,Data.CivStage,CheckCompress);
104if isempty(varlist)
105    errormsg=['read_civdata: unknow velocity type ' VelType];
106    return
107else
108    if strcmp(Data.Conventions,'uvmat/civdata_3D')
109        [Field,vardetect,~,errormsg]=nc2struct(FileName,'TimeDimName','npz',frame_index,varlist);%read the variables in the netcdf file
110    else
111        [Field,vardetect,~,errormsg]=nc2struct(FileName,varlist);%read the variables in the netcdf file
112    end
113end
114if ~isempty(errormsg)
115     errormsg=['read_civdata: ' errormsg];
116    return
117end
118if vardetect(1)==0
119     errormsg=[ 'requested field not available in ' FileName '/' VelType ': need to run patch'];
120     return
121end
122if strcmp(Data.Conventions,'uvmat/civdata/compress')
123    Field.X=Field.X-0.5+Field.U/2;% shift to the convected position
124    Field.Y=Field.Y-0.5+Field.V/2;
125end
126switch VelTypeOut
127    case {'civ1','filter1'}
128        if isfield(Field,'Patch1_SubDomain')
129            Field.SubDomain=Field.Patch1_SubDomain;
130            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
131        end
132        if isfield(Field,'Civ1_Dt')
133            Field.Dt=Field.Civ1_Dt;
134        end
135        if isfield(Field,'Civ1_Time')
136            Field.Time=Field.Civ1_Time;
137        end
138    case {'civ2','filter2'}
139        if isfield(Field,'Patch2_SubDomain')
140            Field.SubDomain=Field.Patch2_SubDomain;
141            Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'SubDomain'}];
142        end
143        if isfield(Field,'Civ2_Dt')
144        Field.Dt=Field.Civ2_Dt;
145        end
146        if isfield(Field,'Civ2_Time')
147        Field.Time=Field.Civ2_Time;
148        end
149end
150Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'Dt','Time'}];
151ivar_U_tps=[];
152ivar_V_tps=[];
153var_ind=find(vardetect);
154for ivar=1:numel(var_ind)
155    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
156    %Field.VarAttribute{ivar}.Mesh=0.025;%typical mesh for histograms O.025 pixel (used in series)
157    Field.VarAttribute{ivar}.ProjModeRequest=ProjModeRequest;
158    if strcmp(role{var_ind(ivar)},'vector_x')
159        Field.VarAttribute{ivar}.FieldName=FieldNames;
160        ivar_U=ivar;
161    end
162    if strcmp(role{var_ind(ivar)},'vector_x_tps')
163        Field.VarAttribute{ivar}.FieldName=FieldNames;
164        ivar_U_tps=ivar;
165    end
166    if strcmp(role{var_ind(ivar)},'vector_y')
167        Field.VarAttribute{ivar}.FieldName=FieldNames;
168        ivar_V=ivar;
169    end
170    if strcmp(role{var_ind(ivar)},'vector_y_tps')
171        Field.VarAttribute{ivar}.FieldName=FieldNames;
172        ivar_V_tps=ivar;
173    end
174end
175if ~isempty(ivar_U_tps)
176    Field.VarAttribute{ivar_U}.VarIndex_tps=ivar_U_tps;
177end
178if ~isempty(ivar_V_tps)
179    Field.VarAttribute{ivar_V}.VarIndex_tps=ivar_V_tps;
180end
181
182%% update list of global attributes
183Field.ListGlobalAttribute=[Field.ListGlobalAttribute {'NbCoord','NbDim','TimeUnit','CoordUnit'}];
184Field.NbCoord=2;
185Field.NbDim=2;
186Field.TimeUnit='s';
187Field.CoordUnit='pixel';
188
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
191%INPUT:
192% FieldNames =cell of field names to get, which can contain the strings:
193%             'ima_cor': image correlation, vec_c or vec2_C
194%             'vort','div','strain': requires velocity derivatives DUDX...
195%             'error': error estimate (vec_E or vec2_E)
196%             
197% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
198%            if vel_type=[] or'*', a  priority choice is done, civ2 considered better than civ1 )
199
200function [var,role,vel_type_out,errormsg]=varcivx_generator(ProjModeRequest,vel_type,CivStage,CheckCompress)
201
202%% default input values
203if ~exist('vel_type','var'),vel_type='';end
204if iscell(vel_type),vel_type=vel_type{1}; end%transform cell to string if needed
205errormsg='';
206if CivStage>=6
207    CivStage=6;
208end
209
210%% select the priority order for automatic vel_type selection
211if strcmp(ProjModeRequest,'derivatives')&& numel(vel_type)>=3 && strcmp(vel_type(1:3),'civ')
212    vel_type=['filter' vel_type(4:end)];
213end
214if isempty(vel_type)||strcmp(vel_type,'*')
215    vel_type='filter2';% case CivStage >=6
216    switch CivStage
217        case {1,2}% civ1 available but not filter1
218            vel_type='civ1';
219        case 3
220            vel_type='filter1';
221
222        case {4,5}% civ2 available but not filter2
223            if strcmp(ProjModeRequest,'derivatives')% derivatives needed
224                vel_type='filter1';
225            else
226                vel_type='civ2';
227            end
228    end
229end
230
231if CheckCompress
232    switch vel_type
233        case{'civ1','civ2'}
234            varout={'X','Y','Z','U','V','W','C','FF'};
235            varin= {'X','Y','Z','U','V','W','C','FF'};
236            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'};
237        case{'filter1','filter2'}
238            varout={'X','Y','Z','U','V','W','C','FF'};
239            varin={'X','Y','Z','U_smooth','V_smooth','W_smooth','C','FF'};
240            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'};
241            %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
242    end
243else
244    switch vel_type
245        case{'civ1','civ2'}
246            varout={'X','Y','Z','U','V','W','C','FF'};
247            varin= {'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U','Civ1_V','Civ1_W','Civ1_C','Civ1_FF'};
248            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag'};
249        case{'filter1','filter2'}
250            varout={'X','Y','Z','U','V','W','C','FF','Coord_tps','U_tps','V_tps','W_tps','SubRange','NbCentre','NbCentre','NbCentre'};
251            varin={'Civ1_X','Civ1_Y','Civ1_Z','Civ1_U_smooth','Civ1_V_smooth','Civ1_W','Civ1_C','Civ1_FF',...
252                'Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps','Civ1_W_tps','Civ1_SubRange','Civ1_NbCentre','Civ1_NbCentres','Civ1_NbSites'};
253            role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','errorflag','coord_tps','vector_x_tps',...
254                'vector_y_tps','vector_z_tps','ancillary','ancillary','ancillary','ancillary'};
255            %  rmq: NbCentres and NbSites obsolete replaced by NbCentre, kept for consistency with previous data
256    end
257    switch vel_type
258        case {'civ2','filter2'}
259            varin=regexprep(varin,'1','2');
260    end
261end
262var=[varout;varin];
263if ~strcmp(ProjModeRequest,'interp_tps')
264    var=var(:,1:8);%suppress tps if not needed
265end
266vel_type_out=vel_type;
267
268
269
270
271
Note: See TracBrowser for help on using the repository browser.